Игра Сброс Pygame - PullRequest
       7

Игра Сброс Pygame

1 голос
/ 29 октября 2019

Мне нужна помощь с перезагрузкой игры, которую я сделал. У меня работает основной цикл и работает обнаружение столкновений. Я пытаюсь получить мгновенный перезапуск игры, тот, который просто сбрасывает счет и начинает снова - я не хочу, чтобы у него был какой-либо ввод пользователя, прежде чем он снова перезапустит игру.

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. 

1 Ответ

1 голос
/ 30 октября 2019

Если я правильно понял, вы просто хотите перезагрузить игру. Просто сделайте

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

, вы также можете взглянуть на спрайтов . Это облегчает обнаружение столкновений и хорошо для больших игр с его группами.

...