别着急,我是新手:游戏结束画面中的“按一个键玩”并不总是有效,当它有效时,TNT 箱子掉落的速度更快。我需要重新设置 tnt 盒子,然后像玩新游戏一样再次开始以缓慢的速度掉落。我尝试创建一个 mainLoop() 函数并将我的大部分“松散”代码(尚未在函数中)放入新函数中以在游戏结束时调用,这样我可以更轻松地重置,但我的代码将不再有效,所以我把它拿出来。您可以提供的任何帮助将不胜感激。我不知道我做错了什么。
下面是我的代码......................
#Imports
import pygame, sys
from pygame.locals import *
import random, time
from pygame import mixer`
#Initializing
pygame.init()
#Setting up FPS
FPS = 60
FramePerSec = pygame.time.Clock()
#Creating colors
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
DARKGREEN = (21, 89, 30)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
ORANGE = (255, 204, 51)
GRAY = (185, 185, 185)
BORDERCOLOR = BLUE
BGCOLOR = DARKGREEN
BASICFONT = pygame.font.Font('freesansbold.ttf', 36)
BIGFONT = pygame.font.Font('freesansbold.ttf', 80)
TEXTCOLOR = WHITE
TEXTSHADOWCOLOR = GRAY
#Other Variables for use in the program
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 800
SPEED = 5
#Create a white screen
DISPLAYSURF = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
def showTextScreen(text):
#Adding a new User event
INC_SPEED = pygame.USEREVENT + 1
pygame.time.set_timer(INC_SPEED, 1000)
pygame.display.update()
DISPLAYSURF.fill(BGCOLOR)
# This function displays large text in the
# center of the screen until a key is pressed.
# Draw the text drop shadow
titleSurf, titleRect = makeTextObjs(text, BIGFONT, TEXTSHADOWCOLOR)
titleRect.center = (int(SCREEN_WIDTH / 2), int(SCREEN_HEIGHT / 2))
DISPLAYSURF.blit(titleSurf, titleRect)
# Draw the text
titleSurf, titleRect = makeTextObjs(text, BIGFONT, TEXTCOLOR)
titleRect.center = (int(SCREEN_WIDTH / 2) - 3, int(SCREEN_HEIGHT / 2) - 3)
DISPLAYSURF.blit(titleSurf, titleRect)
# Draw the additional "Press a key to play." text.
pressKeySurf, pressKeyRect = makeTextObjs('Press a key to play.', BASICFONT, TEXTCOLOR)
pressKeyRect.center = (int(SCREEN_WIDTH / 2), int(SCREEN_HEIGHT / 2) + 100)
DISPLAYSURF.blit(pressKeySurf, pressKeyRect)
pygame.display.flip()
while checkForKeyPress() == None:
pygame.display.update()
FramePerSec.tick()
def makeTextObjs(text, font, color):
surf = font.render(text, True, color)
return surf, surf.get_rect()
def checkForKeyPress():
# Grab KEYDOWN events to remove them from the event queue.
checkForQuit()
for event in pygame.event.get([KEYDOWN, KEYUP]):
if event.type == KEYDOWN:
#Adding a new User event
INC_SPEED = pygame.USEREVENT + 1
pygame.time.set_timer(INC_SPEED, 1000)
continue
return event.key
return None
def checkForQuit():
for event in pygame.event.get(QUIT): # get all the QUIT events
terminate() # terminate if any QUIT events are present
for event in pygame.event.get(KEYUP): # get all the KEYUP events
if event.key == K_ESCAPE:
terminate() # terminate if the KEYUP event was for the Esc key
pygame.event.post(event) # put the other KEYUP event objects back
def terminate():
pygame.quit()
sys.exit()
background = pygame.image.load("minecraftbackground.jpg").convert_alpha()
background = pygame.transform.scale(background, (1500,800))
DISPLAYSURF.fill((0,0,0))
DISPLAYSURF.blit(background, (0,0))
pygame.display.update()
pygame.display.set_caption("MacPhee Minecraft")
showTextScreen('MacPhee Minecraft')
class Enemy(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.image.load("TNT.jpg")
self.image=pygame.transform.scale(self.image,(70,70))
self.rect = self.image.get_rect()
self.rect.center = (random.randint(40,SCREEN_WIDTH-40), 0)
def move(self):
self.rect.move_ip(0,SPEED)
if (self.rect.top > 800):
self.rect.top = 0
self.rect.center = (random.randint(10, 1500), 0)
pygame.display.update()
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Steve4.png")
self.image=pygame.transform.scale(self.image,(140,200))
self.rect = self.image.get_rect()
self.rect.center = (260, 480)
def move(self):
pressed_keys = pygame.key.get_pressed()
if self.rect.left > 0:
if pressed_keys[K_LEFT]:
self.rect.move_ip(-55, 0)
if self.rect.right < SCREEN_WIDTH:
if pressed_keys[K_RIGHT]:
self.rect.move_ip(55, 0)
#Setting up Sprites
P1 = Player()
E1 = Enemy()
#Creating Sprites Groups
enemies = pygame.sprite.Group()
enemies.add(E1)
all_sprites = pygame.sprite.Group()
all_sprites.add(P1)
all_sprites.add(E1)
#Adding a new User event
INC_SPEED = pygame.USEREVENT + 1
pygame.time.set_timer(INC_SPEED, 1000)
#Game Loop
while True:
#Cycles through all events occuring
for event in pygame.event.get():
if event.type == INC_SPEED:
SPEED += 1
if event.type == QUIT:
pygame.quit()
sys.exit()
# drawing background on the screen
background = pygame.image.load("minecraftbackground.jpg").convert_alpha()
background = pygame.transform.scale(background, (1500,800))
DISPLAYSURF.fill((0,0,0))
DISPLAYSURF.blit(background, (0,0))
pygame.display.update()
#Moves and Re-draws all Sprites
for entity in all_sprites:
DISPLAYSURF.blit(entity.image, entity.rect)
entity.move()
#To be run if collision occurs between Player and Enemy
if pygame.sprite.spritecollideany(P1, enemies):
showTextScreen('Game Over')
pygame.time.delay(5)
pygame.display.flip()
FramePerSec.tick(FPS)