Pygame重设游戏

问题描述 投票:0回答:1

我需要一些帮助来重置自己制作的游戏。我已经完成了主循环,并且碰撞检测正常了。我正在尝试立即重新启动游戏,一个只是重新设置比分并重新开始的游戏-我不希望它在重新启动游戏之前得到任何用户输入。

MoveAsteroids()只是在屏幕上移动小行星,玩家必须避免。它也是每次躲避小行星时分数增加1的功能。

def game_loop():
    global score
    while not game_over:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    spaceship.change = -5
                elif event.key == pygame.K_DOWN:
                    spaceship.change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    spaceship.change = 0

        spaceship.y += spaceship.change
        if spaceship.y > window_height - spaceship.height:     # Creating borders on the window
            spaceship.y = window_height - spaceship.height
        elif spaceship.y < 0:
            spaceship.y = 0

        window.blit(bg_img, (0, 0))
        MoveAsteroids()
        CollisionDetection()
        Score_display("Score: " + str(score * 100), white)


        pygame.display.update()

def CollisionDetection():
    global score
    spaceship_rect = pygame.Rect(spaceship.x, spaceship.y, spaceship.width, spaceship.height)
    for x in range(1, 5):
        rect = pygame.Rect(asteroids[x].x, asteroids[x].y, asteroids[x].width, asteroids[x].height)
        if spaceship_rect.colliderect(rect):
            pass 

# The part I need help with is this line of code just above^. .colliderect() returns true when a collision happens. 

pygame reset
1个回答
0
投票

如果我答对了,您只想重置游戏。刚做

def CollisionDetection():
global score
spaceship_rect = pygame.Rect(spaceship.x, spaceship.y, spaceship.width, spaceship.height)
for x in range(1, 5):
    rect = pygame.Rect(asteroids[x].x, asteroids[x].y, asteroids[x].width, asteroids[x].height)
    if spaceship_rect.colliderect(rect):
        score = 0
        // here you reset your spaceship.x and y to the normal state

您也可以查看sprites。它使碰撞检测更加容易,并且对于具有分组的大型游戏非常有用。

© www.soinside.com 2019 - 2024. All rights reserved.