简体   繁体   English

Pygame填充和blit不会编辑屏幕

[英]Pygame fill and blit does not edit screen

I'm getting familiar with Pygame and I want to write a simple game, I already have following code: 我熟悉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()

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. 它工作正常分开,“如果‘ w ’按下”的一部分:它应该做的是,以填补黑色,显示“ game_over ”的形象两秒钟,然后继续工作。 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? 你能说出我做错了什么吗?

The positions of the bar ( barrect ), the the position of the ball ( ballrect ) and the speed ( speed ) have to be set to the initial state, when the game is restarted: 重新开始游戏时,必须将杆的位置( barrect ),球的位置( ballrect )和速度( speed )设置为初始状态:

Create an function which initializes the game states: 创建一个初始化游戏状态的函数:

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

Call it once at startup: 在启动时调用一次:

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

Call it again when the game has to be restarted. 必须重新启动游戏时再次调用它。 I recommend not to delay the game by pygame.time.delay() . 我建议不要通过pygame.time.delay()延迟游戏。 Calculate the time when the game has to be restarted starttime . 算算时间,当比赛必须重新启动starttime Draw either the game screen or the "game over" screen dependent on the time: 根据时间绘制游戏屏幕或“游戏结束”屏幕:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM