如何在不关闭程序的情况下重启游戏?

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

我正在制作一款鼹鼠射击游戏。我希望当你按下空格键时能够重新开始游戏。我目前将它设置为 'a' 用于测试目的,但这似乎也不起作用。我需要帮助,在不退出程序的情况下重启我的游戏。我在游戏中添加了 /// 到我看来面临问题的地方。我也需要一种方法来重置我的分数和计数器。

import pygame
import random
import time
from threading import Timer

pygame.font.init()

win_width = 1000
win_height = 710

FPS = 90

screen = pygame.display.set_mode((win_width, win_height))

pygame.display.set_caption("Mole Shooter")

white = (255, 255, 255)
red = (255, 0, 0)

counter, text = 30, 'Time Left: 30'.rjust(3)

font = pygame.font.Font('freesansbold.ttf', 32)
score = pygame.font.Font('freesansbold.ttf', 32)
score_text = 'Score: 0'.rjust(3)

run = True
clock = pygame.time.Clock()
background = pygame.transform.scale(pygame.image.load('back_land.png'), (win_width, win_height))

aim = pygame.image.load("aim.png")
mole = pygame.image.load("mole.png")

moles = []
score_check = 0


def mole_spawn_easy():
    molex = random.randint(50, 950)
    moley = random.randint(450, 682)
    moles.append((molex, moley))


pygame.time.set_timer(pygame.USEREVENT, 1000)


# pygame.time.set_timer(pygame.USEREVENT, 1000)
# mask = pygame.mask.from_surface(mole.png)


def paused():
    largeText = pygame.font.Font("freesansbold.ttf", 115)
    # TextSurf, TextRect = text_objects("Your Score: " + score_check, largeText)
    # TextRect.center = ((display_width/2),(display_height/2))
    # screen.blit(TextSurf, TextRect)
    final_score = ('Your Score: ' + str(score_check)).rjust(3)

    screen.blit(score.render(final_score, True, (0, 0, 0)), (((win_width / 2) - 100), (win_height / 2)))
    while True:
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        # gameDisplay.fill(white)

        # button("Continue",150,450,100,50,green,bright_green,unpause)
        # button("Quit",550,450,100,50,red,bright_red,quitgame)

        pygame.display.update()
        clock.tick(15)


def main():
    global FPS
    global screen
    global counter
    global text
    global font
    global score
    global score_text
    global run
    global background
    global aim
    global mole
    global moles
    global score_check
    global clock

    while run:
        ax, ay = pygame.mouse.get_pos()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.USEREVENT:
                counter -= 1
                text = ("Time Left: " + str(counter)).rjust(3)
                if counter > 0:
                    mole_spawn_easy()
                else:
                    # print("game over")
                    paused()
                    ///if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_a:
                            main()
                        counter = 30///

            if event.type == pygame.MOUSEBUTTONDOWN:

                mx = mole.get_width()
                my = mole.get_height()
                for i in moles:
                    if ax in range(i[0], i[0] + int(mx)) and ay in range(i[1], i[1] + int(my)):
                        # print("hit")
                        score_check += 1
                        score_text = ("Score: " + str(score_check)).rjust(3)

        screen.blit(background, [0, 0])

        for pos in moles:
            screen.blit(mole, pos)
            # print(pos)
            if len(moles) >= 2:
                del (moles[0])

        screen.blit(aim, ((ax - 32), (ay - 32)))

        screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
        screen.blit(score.render(score_text, True, (0, 0, 0)), (800, 48))
        clock.tick(FPS)

        pygame.display.flip()
main()
python pygame
1个回答
0
投票

你在这部分的缩进是不正确的。

    if event.type == pygame.USEREVENT:
        counter -= 1
        text = ("Time Left: " + str(counter)).rjust(3)
        if counter > 0:
            mole_spawn_easy()
        else:
            # print("game over")
            paused()
            ///if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    main()
                counter = 30///

你把if测试的event.KEYDOWN放在了if里面,测试事件是否是USEREVENT,所以它永远不会匹配。需要像这样。

        if event.type == pygame.USEREVENT:
            counter -= 1
            text = ("Time Left: " + str(counter)).rjust(3)
            if counter > 0:
                mole_spawn_easy()
            else:
                # print("game over")
                paused()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                main()
            counter = 30

但是,我应该指出一个不同的问题 我看到你的代码结构。为了重启游戏,你是在调用main(),但你是从main()里面做的,导致它是递归的。你最好用一种非递归的方式来做这件事。也许把运行循环移到main调用的新方法中,然后让上面的方法从该方法中退出,然后main可以调用它或其他东西。


-1
投票

只要随时中断运行循环,在循环后设置变量的默认值,然后再调用main。

if event.type == pygame.USEREVENT:
    counter -= 1
    text = ("Time Left: " + str(counter)).rjust(3)
    if counter > 0:
        mole_spawn_easy()
    else:
        # print("game over")
        paused()
            if event.type == pygame.KEYDOWN:
                break            

运行循环之后。

# Prev code ....
    screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
    screen.blit(score.render(score_text, True, (0, 0, 0)), (800, 48))
    clock.tick(FPS)

    pygame.display.flip()

reset_vars() // You have to implement this method or reset your variables here
main()
© www.soinside.com 2019 - 2024. All rights reserved.