繁体   English   中英

Python - 游戏没有响应?

[英]Python - Game is not responding?

尝试创建一个新的游戏实例,以便我可以创建一个在实际游戏发生之前显示的主菜单。

我正在尝试这样做,以便当您按键盘上的 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

如果有人能确定是什么破坏了我的游戏,那就太棒了。

您在游戏循环内有一个游戏循环内的游戏循环。 不不不。 你有while mainMenu == True然后在那个循环里面你有一个while mainMenu == False所以当它是 mainmenu 时它正在循环通过大循环,然后当它为假时,它循环通过小循环并且永远不会出去。

你可以有多个游戏循环,但不能相互

while mainMenu == True:
    window.blit(mainmenu, (0,0))
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False   #if x is pressed dont run game
            mainMenu = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        mainMenu = False  #if a is pressed run game
        print ("yeah i clicked")

while run:
    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()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM