Pygame的fill和blit函数不能编辑屏幕。

3

我正在熟悉Pygame,想要写一个简单的游戏,我已经有以下代码:

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()

除了“按下 ‘w’ 键”的部分外,它运行得很好:它应该填充黑色,显示“game_over”图像两秒钟,然后继续工作。但是它会冻结所有内容两秒钟,然后继续工作,就好像以下的代码根本不存在一样。
screen.fill(black)
screen.blit(over, overrect)
pygame.display.flip()

你能告诉我哪里做错了吗?

1个回答

0

当游戏重新开始时,必须将条形图的位置(barrect)、球的位置(ballrect)和速度(speed)设置为初始状态:

创建一个函数来初始化游戏状态:

def InitGame():
    global ballrect, barrect, speed
    speed = [2, 2]
    ballrect = ball.get_rect()
    barrect = bar.get_rect(center=(320,475))  

在启动时调用它一次:

screen = pygame.display.set_mode(size)

ball = pygame.image.load("intro_ball.gif")
bar = pygame.image.load("bar_low.gif")
over = pygame.image.load("game_over.gif")
overrect = over.get_rect(center=(width/2, height/2 ))

InitGame();

当游戏需要重新开始时,请再次调用它。 我建议不要通过 pygame.time.delay() 延迟游戏。计算游戏需要重新开始的时间 starttime。根据时间绘制游戏屏幕或“游戏结束”屏幕:

starttime = 0
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:

                # reset game and init the new start time
                InitGame()
                starttime = pygame.time.get_ticks() + 2000

    if pygame.time.get_ticks() > starttime: 

        keys = pygame.key.get_pressed()
        # [...]

        screen.fill(black)
        screen.blit(ball, ballrect)
        screen.blit(bar, barrect)

    else:

        screen.fill(black)
        screen.blit(over, overrect)

    pygame.display.flip()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接