I'm getting familiar with Pygame
and I want to write a simple game, I already have following code:
import sys, pygame
pygame.init()
size = width, height = 640, 480
speed = [2, 2]
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("intro_ball.gif")
ballrect = ball.get_rect()
bar = pygame.image.load("bar_low.gif")
barrect = bar.get_rect(center=(320,475))
over = pygame.image.load("game_over.gif")
overrect = over.get_rect(center=(width/2, height/2 ))
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
screen.fill(black)
screen.blit(over, overrect)
pygame.display.flip()
pygame.time.delay(2000)
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] and barrect.right < width:
barrect = barrect.move(4, 0)
screen.blit(bar, barrect)
pygame.display.update()
if keys[pygame.K_LEFT] and barrect.left > 0:
barrect = barrect.move(-4, 0)
screen.blit(bar, barrect)
pygame.display.update()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or (ballrect.bottom > height - bar.get_height() and ballrect.left < barrect.right and ballrect.right > barrect.left):
speed[1] = -speed[1]
if ballrect.bottom > height:
#irrelevant
screen.fill(black)
screen.blit(ball, ballrect)
screen.blit(bar, barrect)
pygame.display.flip()
It works fine apart the "if 'w
' is pressed" part: what it should do is to fill black, show the "game_over
" image for two seconds and continue to work. Instead of this it freezes everything for two seconds and continues to work, like the following lines never existed.
screen.fill(black)
screen.blit(over, overrect)
pygame.display.flip()
Could you tell what do I do wrong?