How to move an image when clicked on it in PYGAME? - python

Basically, when I click on an image I want the image to move to a new different location. When I click again, it should move again.
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((1420, 750))
pygame.display.set_caption("Soccer Game")
icon = pygame.image.load('soccer-ball-variant.png')
pygame.display.set_icon(icon)
ball = pygame.image.load('soccer2.png')
ballrect = ball.get_rect()
X = random.randrange(0, 1100)
Y = random.randrange(0, 600)
def player():
screen.blit(ball, (X, Y))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
if ball.get_rect().collidepoint(x, y):
X = random.randrange(0, 1100)
Y = random.randrange(0, 600)
player()
screen.fill((255, 255, 255))
player()
pygame.display.update()
The problem is that this program only works when i click on the left-up corner of the screen, and not on the ball. I am a biginner in the pygame module, but i think the problem is this if statement:
if ball.get_rect().collidepoint(x, y):
X = random.randrange(0, 1100)
Y = random.randrange(0, 600)
player()

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. A Surface is blit at a position on the screen. The position of the rectangle can be specified by a keyword argument. For example, the top leftof the rectangle can be specified with the keyword argument topleft. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect for a full list of the keyword arguments).
if ball.get_rect().collidepoint(x, y):
if ball.get_rect(topleft = (X, Y)).collidepoint(x, y):
However, I recommend removing the X and Y variables, but using ballrect instead:
ballrect = ball.get_rect()
ballrect.x = random.randrange(0, 1100)
ballrect.y = random.randrange(0, 600)
def player():
screen.blit(ball, ballrect)
if ballrect.collidepoint(event.pos):
ballrect.x = random.randrange(0, 1100)
ballrect.y = random.randrange(0, 600)
Complete example:
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((1420, 750))
pygame.display.set_caption("Soccer Game")
icon = pygame.image.load('soccer-ball-variant.png')
pygame.display.set_icon(icon)
ball = pygame.image.load('soccer2.png')
ballrect = ball.get_rect()
ballrect.x = random.randrange(0, 1100)
ballrect.y = random.randrange(0, 600)
def player():
screen.blit(ball, ballrect)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
if ballrect.collidepoint(event.pos):
ballrect.x = random.randrange(0, 1100)
ballrect.y = random.randrange(0, 600)
screen.fill((255, 255, 255))
player()
pygame.display.update()

Related

If Statement stops working after sometime and font not rendering

I am creating a game with pygame & python 3.10.0 using VSCode everything is working fine but this if statement stops working after sometime and the font doesn't draw but I can't pinpoint the problem for that , for the if statement it usually runs till 10 or 11 score but it can be quicker like 2:
if player.rect.colliderect(food):
pygame.sprite.Sprite.kill(food)
food.rect.x = random.randrange(20, 1700)
food.rect.y = random.randrange(20, 860)
all_sprites_list.add(food)
score += 1
print(score)
whole code:
import pygame
import sys
import time
import random
import ctypes
from ctypes import wintypes
from pygame import sprite
from pygame.draw import rect
from pygame.event import pump, wait
from pygame import font
pygame.font.init()
myappid = 'elementalstudios.snake.Alpha 0_1' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
#------ Initialize Variables------#
# Player
player_width = 20
player_height = 20
# Food
food_width = 10
food_height = 10
# Colours
seafoam_gr = (159, 226, 191)
black = (0, 0, 0)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (0, 255, 0)
white = (255, 255, 255)
# Score
score = 0
#------ Score Font Initialize ------#
font = pygame.font.Font(None, 50)
text = font.render("Score:", False, white, None)
#------Sprites Class------#
class Sprite(pygame.sprite.Sprite):
def __init__(self, color, height, width):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(seafoam_gr)
self.image.set_colorkey(black)
pygame.draw.rect(self.image,
color,
pygame.Rect(0, 0, width, height))
self.rect = self.image.get_rect()
def moveRight(self, pixels):
self.rect.x += pixels
def moveLeft(self, pixels):
self.rect.x -= pixels
def moveUp(self, speed):
self.rect.y -= speed * speed / 5
def moveDown(self, speed):
self.rect.y += speed * speed / 5
#------ Font Class ------#
def create_font(t,s = 24, colour_font = white, bold = False, italic = False):
font = pygame.font.Font("prstart.ttf", s, bold, italic)
text = font.render(t, True, colour_font)
return text
#------ Initialize Pygame and Window------#
pygame.init()
icon = pygame.image.load('Icon.ico')
pygame.display.set_icon(icon)
gameDisplay = pygame.display.set_mode((1920,1080), pygame.FULLSCREEN)
#rect = pygame.Rect( * gameDisplay.get_rect().center, 0, 0).inflate(100, 100)
pygame.display.set_caption("Blocky")
gameDisplay.fill(black)
clock = pygame.time.Clock()
running = True
#------Initialize Sprites------#
all_sprites_list = pygame.sprite.Group()
player = Sprite(seafoam_gr, player_height, player_width)
food = Sprite(red, food_height, food_width)
player.rect.x = 960
player.rect.y = 540
food.rect.x = 20 #random.randrange(20, 1800)
food.rect.y = 30 #random.randrange(20, 1050)
#------Add Sprites to sprite list------#
all_sprites_list.add(player)
all_sprites_list.add(food)
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
#------Eating------#
if player.rect.colliderect(food):
pygame.sprite.Sprite.kill(food)
food.rect.x = random.randrange(20, 1700)
food.rect.y = random.randrange(20, 860)
all_sprites_list.add(food)
score += 1
print(score)
#------ Score Draw ------#
text_rect = text.get_rect()
text_rect.center = (100, 150)
gameDisplay.blit(text, text_rect)
#------Key Movement------#
keys = pygame.key.get_pressed()
# Move Left
if keys[pygame.K_a]:
player.moveLeft(3)
if keys[pygame.K_LCTRL]:
player.moveLeft(5)
if keys[pygame.K_LEFT]:
player.moveLeft(3)
if keys[pygame.K_LCTRL]:
player.moveLeft(5)
# Move Right
if keys[pygame.K_d]:
player.moveRight(5)
if keys[pygame.K_LCTRL]:
player.moveRight(5)
if keys[pygame.K_RIGHT]:
player.moveRight(3)
if keys[pygame.K_LCTRL]:
player.moveRight(5)
# Move Down
if keys[pygame.K_s]:
player.moveDown(3)
if keys[pygame.K_LCTRL]:
player.moveDown(5)
if keys[pygame.K_DOWN]:
player.moveDown(3)
if keys[pygame.K_LCTRL]:
player.moveDown(5)
# Move Up
if keys[pygame.K_w]:
player.moveUp(3)
if keys[pygame.K_LCTRL]:
player.moveUp(5)
if keys[pygame.K_UP]:
player.moveUp(3)
if keys[pygame.K_LCTRL]:
player.moveUp(5)
all_sprites_list.update()
gameDisplay.fill(black)
all_sprites_list.draw(gameDisplay)
pygame.display.flip()
clock.tick(60)
pygame.quit()
quit()
It is not necessary to kill the food object. It is sufficient to change the position of the food. pygame.sprite.Sprite.kill just removes the Sprite object from all Gorups. So there is no point in calling kill and then adding the object back to the Group.
The text does not show up, because you draw it before you clear the display. pygame.Surface.fill fills the Surface with a solid color. Everything that was previously drawn will be cleared. blit the text after fill.
You will have to render the text Surface again if the score has changed.
I recommend simplifying the code that moves the player. See How can I make a sprite move when key is held down.
class Sprite(pygame.sprite.Sprite):
def __init__(self, color, x, y, height, width):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.image.set_colorkey(black)
self.rect = self.image.get_rect(topleft = (x, y))
self.x, self.y = self.rect.x, self.rect.y
def move(self, move_x, move_y, speed_up):
if move_x or move_y:
direction = pygame.math.Vector2(move_x, move_y)
direction.scale_to_length(5 if speed_up else 3)
self.x += direction.x
self.y += direction.y
self.rect.x, self.rect.y = round(self.x), round(self.y)
player = Sprite(seafoam_gr, 400, 300, player_height, player_width)
food = Sprite(red, 20, 30, food_height, food_width)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if player.rect.colliderect(food):
food.rect.x = random.randrange(20, 780)
food.rect.y = random.randrange(20, 580)
score += 1
# render text
text = font.render("Score: " + str(score), False, white, None)
keys = pygame.key.get_pressed()
move_x = ((keys[pygame.K_d] or keys[pygame.K_RIGHT]) - (keys[pygame.K_a] or keys[pygame.K_LEFT]))
move_y = ((keys[pygame.K_s] or keys[pygame.K_DOWN]) - (keys[pygame.K_w] or keys[pygame.K_UP]))
player.move(move_x, move_y, keys[pygame.K_LCTRL])
all_sprites_list.update()
gameDisplay.fill(black)
# draw text
gameDisplay.blit(text, text.get_rect(center = (100, 150)))
all_sprites_list.draw(gameDisplay)
pygame.display.flip()
clock.tick(60)
pygame.quit()
quit()

Why is my pygame program laggy even with a great pc and other code works fine

Code to run the simple game.
All it does is move a circle just to test the fps
But when you run it, it takes awhile for it to start and than stutters
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
playerX = 250 # Player X
playerY = 250 # Player Y
playerColor = (10, 10, 100)
class Player:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
def show(self):
pygame.draw.circle(screen, self.color, (self.x, self.y), 10)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
playerX += 5
player = Player(playerX, playerY, playerColor)
screen.fill((60, 30, 10))
player.show()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
It is a matter of Indentation. You need to update the position of the player and draw the scene in the application loop rather than the event loop.
Create the instance of the Player before the application loop:
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
class Player:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
def show(self):
pygame.draw.circle(screen, self.color, (self.x, self.y), 10)
playerX = 250 # Player X
playerY = 250 # Player Y
playerColor = (10, 10, 100)
player = Player(playerX, playerY, playerColor)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#<--| INDENTATION
player.x += 5
screen.fill((60, 30, 10))
player.show()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()

How can I make this demo drawing game update drawing the circles faster with Pygame?

I am trying to make my drawing game draw a circle where I drag my mouse, but the circles won't update often enough to create a smooth line. How can I fix this?
import pygame
from random import randint
width=800
height=600
pygame.init() #As necessary as import, initalizes pygame
global gameDisplay
gameDisplay = pygame.display.set_mode((width,height))#Makes window
pygame.display.set_caption('Demo')#Titles window
clock = pygame.time.Clock()#Keeps time for pygame
gameDisplay.fill((0,0,255))
class Draw:
def __init__(self):
self.color = (255, 0, 0)
def update(self, x, y):
self.x = x
self.y = y
pygame.draw.circle(gameDisplay, self.color, (self.x, self.y), (5))
end = False
down = False
Line = Draw()
while not end:
x, y = pygame.mouse.get_pos()
#drawShape()
#pygame.draw.rect(gameDisplay, (0,255,0), (10, 10, 4, 4))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
down = True
if event.type == pygame.MOUSEBUTTONUP:
down = False
if down:
Line.update(x, y)
if event.type == pygame.QUIT:
end = True
lastx, lasty = pygame.mouse.get_pos()
pygame.display.update()
clock.tick(60)
pygame.quit()
This is what my problem looks like
I recommend to draw a line from the previous mouse position to the current mouse position. Additionally draw a dot at the start and the end of the line. That causes a round beginning and end.
Track the previous position of the mouse (lastx, lasty) and draw the line in the main application loop rather than the event loop:
e.g.:
import pygame
width, height = 800, 600
pygame.init() #As necessary as import, initalizes pygame
gameDisplay = pygame.display.set_mode((width,height)) #Makes window
pygame.display.set_caption('Demo') #Titles window
clock = pygame.time.Clock() #Keeps time for pygame
gameDisplay.fill((0,0,255))
class Draw:
def __init__(self):
self.color = (255, 0, 0)
def update(self, from_x, from_y, to_x, to_y):
pygame.draw.circle(gameDisplay, self.color, (from_x, from_y), 5)
pygame.draw.line(gameDisplay, self.color, (from_x, from_y), (to_x, to_y), 10)
pygame.draw.circle(gameDisplay, self.color, (to_x, to_y), 5)
end = False
down = False
line = Draw()
while not end:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
lastx, lasty = event.pos
down = True
if event.type == pygame.MOUSEBUTTONUP:
down = False
if event.type == pygame.QUIT:
end = True
x, y = pygame.mouse.get_pos()
if down:
line.update(lastx, lasty, x, y)
lastx, lasty = x, y
pygame.display.update()
clock.tick(60)
pygame.quit()

Detecting the point of collision with Pygame

I am making a game with Pygame. For this game I need to be able to detect not only that two rectangles have collided, but also the point of collision between them. I looked at the documentation but couldn't seem to find any answers.
Is something like this possible?
You can use Rect.clip:
crops a rectangle inside another
clip(Rect) -> Rect
Returns a new rectangle that is cropped to be completely inside the argument Rect. If the two rectangles do not overlap to begin with, a Rect with 0 size is returned.
Here's an example:
import pygame
import random
class Stuff(pygame.sprite.Sprite):
def __init__(self, pos, color, *args):
super().__init__(*args)
self.image = pygame.Surface((30, 30))
self.image.fill(color)
self.rect = self.image.get_rect(center=pos)
self.pos = pygame.Vector2(pos)
def update(self):
self.rect.center = self.pos
def main():
pygame.init()
screen = pygame.display.set_mode((500, 500))
screen_rect = screen.get_rect()
font = pygame.font.SysFont(None, 26)
clock = pygame.time.Clock()
sprites = pygame.sprite.Group()
blocks = pygame.sprite.Group()
movement = {
pygame.K_UP: ( 0, -1),
pygame.K_DOWN: ( 0, 1),
pygame.K_LEFT: (-1, 0),
pygame.K_RIGHT: ( 1, 0)
}
for _ in range(15):
x, y = random.randint(0, 500), random.randint(0, 500)
color = random.choice(['green', 'yellow'])
Stuff((x, y), pygame.Color(color), sprites, blocks)
player = Stuff(screen_rect.center, pygame.Color('dodgerblue'))
sprites.add(player)
dt = 0
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
pressed = pygame.key.get_pressed()
move = pygame.Vector2()
for dir in (movement[key] for key in movement if pressed[key]):
move += dir
if move.length() > 0: move.normalize_ip()
player.pos += move * dt/5
sprites.update()
screen.fill(pygame.Color('black'))
sprites.draw(screen)
for block in pygame.sprite.spritecollide(player, blocks, False):
clip = player.rect.clip(block.rect)
pygame.draw.rect(screen, pygame.Color('red'), clip)
hits = [edge for edge in ['bottom', 'top', 'left', 'right'] if getattr(clip, edge) == getattr(player.rect, edge)]
text = font.render(f'Collision at {", ".join(hits)}', True, pygame.Color('white'))
screen.blit(text, (20, 20))
pygame.display.flip()
dt = clock.tick(60)
if __name__ == '__main__':
main()

Making image move straight with pygame

I have to move the rectangular object straight through the pygame window. I have tried some code with pygame. The code is
import pygame
from itertools import cycle
pygame.init()
screen = pygame.display.set_mode((300, 300))
s_r = screen.get_rect()
player = pygame.Rect((100, 100, 50, 50))
timer = pygame.time.Clock()
movement = "straight"
x = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise
if movement == 'straight':
x += 50
screen.fill(pygame.color.Color('Black'))
pygame.draw.rect(screen, pygame.color.Color('Grey'), player)
pygame.display.flip()
timer.tick(25)
Here the image didnt moves. What I need is that the image must be moved in a straight way.
x is adding, but that does not affect player, which actually affects the drawing of the rectangle.
import pygame
from itertools import cycle
pygame.init()
screen = pygame.display.set_mode((300, 300))
s_r = screen.get_rect()
timer = pygame.time.Clock()
movement = "straight"
x = 0
player = pygame.Rect((x, 100, 50, 50))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise
if movement == 'straight':
x += 10
player = pygame.Rect((x, 100, 50, 50))
if x >= 300:
x = 0
screen.fill(pygame.color.Color('Black'))
pygame.draw.rect(screen, pygame.color.Color('Grey'), player)
pygame.display.flip()
timer.tick(25)
You need to adjust the player rectangle each time you change x. From http://www.pygame.org/docs/ref/rect.html, you can see that the first two arguments are "left" and "top". So, if you want to the rectangle to move from left to right, you'll want something like this:
player = pygame.Rect((100 + x, 100, 50, 50))
pygame.draw.rect(screen, pygame.color.Color('Grey'), player)
import pygame
BLACK = pygame.color.Color('Black')
GREY = pygame.color.Color('Grey')
pygame.init()
screen = pygame.display.set_mode((300, 300))
screen_rect = screen.get_rect()
timer = pygame.time.Clock()
movement = "straight"
player = pygame.Rect(0, 100, 50, 50) # four aguments in place of tuple (,,,)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if movement == 'straight':
player.x += 10
if player.x >= 300: # check only when `player.x` was changed
player.x = 0
screen.fill(BLACK)
pygame.draw.rect(screen, GREY, player)
pygame.display.flip()
timer.tick(25)
BTW:
don't use raise to exit program.
use readable variable - not s_r but screen_rect
you don't need x - you have player.x
you can create rectangle only once
remove repeated empty lines when you add code to question

Categories

Resources