Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在不关闭程序的情况下重新启动游戏_Python_Pygame - Fatal编程技术网

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

Python 如何在不关闭程序的情况下重新启动游戏,python,pygame,Python,Pygame,我正在制作一个鼹鼠射击游戏。我想在你按下空格键时重新启动游戏。出于测试目的,我目前将其设置为'a',但这似乎也不起作用。我需要帮助才能在不退出程序的情况下重新启动游戏。 我已经在我似乎面临问题的区域添加了//。我还需要一种方法来重置我的分数和计数器 import pygame import random import time from threading import Timer pygame.font.init() win_width = 1000 win_height = 710 F

我正在制作一个鼹鼠射击游戏。我想在你按下空格键时重新启动游戏。出于测试目的,我目前将其设置为
'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()

只要在需要时中断run循环,在该循环之后设置变量的默认值,然后再次调用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()

您在本节中的缩进不正确:

    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///
如果该事件是USEREVENT,则在if中测试event.KEYDOWN的if,因此它永远不会匹配。它必须是这样的:

        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可以调用它或其他什么。

它仍然不起作用,但是我会按照你的建议将我的主代码放在函数中,而不是以递归方式调用它。我能够将代码放在函数中,甚至设置了重新启动按钮感谢你再次开始比赛。