【问题标题】:Python - Game is not responding?Python - 游戏没有响应?
【发布时间】: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

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

【问题讨论】:

    标签: python pygame


    【解决方案1】:

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

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

    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
    

    【讨论】:

    • 嘿,我自己修复了它,但后来遇到了其他一些错误,比如 blitting 表面等,但你的完全修复了一切!谢谢你。你说的比我老师说的要清楚得多。
    • @TerraceMason 哈哈,allgud,我的老师也一样,我发现自己学习更容易
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多