Я строю игру в крота. Я хочу иметь возможность перезапустить игру, когда вы нажимаете клавишу пробела. В настоящее время я установил значение '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()