【发布时间】:2023-03-19 17:38:01
【问题描述】:
尝试创建一个新的游戏实例,以便我可以创建一个在实际游戏发生之前显示的主菜单。
我正在尝试这样当你按下键盘上的 a 时,游戏会开始并且主菜单会消失,但游戏会循环一些代码并使其崩溃;没有反应。
import pygame
import random
import time
pygame.init()
#Setting Variables
screenW = 1020
screenH = 630
x = 125
y = 164
width = 50
height = 50
velocity = 5
wave = 3
GOLD = (255,215,0)
BLACK = (0, 0, 0)
class ZombieChars():
def __init__(self):
self.y = 164
self.vel = 5
self.x_change = random.randrange(1,3)
self.y_change = 1
self.height = random.randrange(35, 60)
self.width = random.randrange(60, 70)
self.color = random.sample(range(250), 4)
self.image = pygame.Surface([self.width, self.height], pygame.HWSURFACE, 32)
self.rect = self.image.get_rect(topleft = (random.randrange(700, 1200), 550))
self.image.fill(self.color)
#pygame.draw.rect(self.image, (self.color), (self.x, self.y, self.width, self.height))
def draw(self):
#print(self.rect.x)
window.blit(self.image, self.rect.topleft)
def update(self):
if self.rect.x >= 220:
self.rect.x -= self.x_change
else:
self.rect.x -= 0
def death(self):
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos
if self.rect.collidepoint(mouse_pos):
self.rect.x = -500
print ("Square Clicked")
print(self.rect.x)
#FPS
clock = pygame.time.Clock()
clock.tick(60)
#Screen
window = pygame.display.set_mode((screenW,screenH))
pygame.display.set_caption(("Zombie Shooter"))
bg = pygame.image.load("bg.jpg")
mainmenu = pygame.image.load("mainmenu.jpg")
zombies = ZombieChars()
my_list = []
for sanjh in range(wave):
my_object = ZombieChars()
my_list.append(my_object)
def text_objects(text, font):
textSurface = font.render(text, True, BLACK)
return textSurface, textSurface.get_rect()
smallText = pygame.font.Font('freesansbold.ttf', 30)
TextSurf, TextRect = text_objects("Welcome to Zombie Shooter Alpha", smallText)
TextRect.center = ((1020 / 2), (50))
TextSurf2, TextRect2 = text_objects("Shoot the zombies before they arrive at your fortress!", smallText)
TextRect2.center = ((1020 / 2 - 80), (100))
TextSurf3, TextRect3 = text_objects("Wave: " + str(wave), smallText)
TextRect3.center = ((1020 / 2), (50))
#Main Loop
run = True
mainMenu = True
keys = pygame.key.get_pressed()
global event
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
mainMenu = False
while mainMenu == True:
window.blit(mainmenu, (0,0))
pygame.display.flip()
if keys[pygame.K_a]:
mainMenu = False
print ("yeah i clicked")
while mainMenu == False:
window.blit(bg, (0,0))
window.blit(TextSurf, TextRect)
window.blit(TextSurf2, TextRect2)
pygame.time.delay(25)
for zombie in my_list:
zombie.draw()
zombie.update()
zombie.death()
pygame.display.flip()
#Drawing
如果有人能找出是什么破坏了我的游戏,那就太棒了。
【问题讨论】: