Pygame detecting collisions between pictures
I am quite new to programming, and recently I decided to start Pygame.
Based on a tutorial I found, I created this game. The aim is to watch the
ball bounce around and keep it away from the box at the bottom by clicking
at the right time. I know the code is a bit messy, but the big problem is
although the code runs, the game doesn't quit as it should when the ball
and the box touch.
import sys, pygame
pygame.init()
size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("ball.bmp")
target = pygame.image.load("square_target.bmp")
ballrect = ball.get_rect()
clock = pygame.time.Clock()
target_place = [160, 200]
fps = 60
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
if pygame.mouse.get_pressed()[0]:
speed[0] = -speed[0]
if ballrect <= target_place[0] + 50 or ballrect <= target_place[1] + 50:
pygame.event.post(pygame.QUIT)
screen.fill(black)
screen.blit(ball, ballrect)
screen.blit(target, target_place)
pygame.display.flip()
fps += 0.01
print (fps)
clock.tick(fps)
Any help?
No comments:
Post a Comment