我仍在Pygame中从事游戏。我已经开始玩游戏了,我只想做一个主菜单功能,这样当按下按钮时,游戏就开始了。
当我添加到主菜单中并编辑了游戏循环时,我无法移动玩家精灵,并且当我按下“ X out”按钮时游戏无法关闭(它没有响应(这没有反应)不会发生)。
这本来没有发生。
而且没有回溯。
这是我的代码:
import pygame
import time
import itertools
import os
pygame.init()
SCREENWIDTH = 1000
SCREENHEIGHT = 650
screen = pygame.display.set_mode([SCREENWIDTH, SCREENHEIGHT])
screen.fill((255, 123, 67))
screen.blit(pygame.image.load("Backgrounds/mainmenu.png"), (0, 0))
background = screen.copy()
clock = pygame.time.Clock()
mi_level = False
class Player(pygame.sprite.Sprite):
sprite = pygame.image.load("Sprites/lee.png")
def __init__(self, *groups):
super().__init__(*groups)
self.image = Player.sprite
self.rect = self.image.get_rect(topleft=(445, 550))
self.pos = pygame.Vector2(self.rect.center)
self.lives = 50
self.score = 0
self.hitbox = self.rect.inflate(-20, -30)
def update(self):
self.pos = self.rect.center
key = pygame.key.get_pressed()
dist = 3
if key[pygame.K_DOWN]:
self.rect.y += dist
self.hitbox.y += dist
elif key[pygame.K_UP]:
self.rect.y -= dist
self.hitbox.y -= dist
if key[pygame.K_RIGHT]:
self.rect.x += dist
self.hitbox.x += dist
elif key[pygame.K_LEFT]:
self.rect.x -= dist
self.hitbox.x -= dist
if self.rect.right > SCREENWIDTH:
self.rect.right = SCREENWIDTH
if self.rect.left < 0:
self.rect.left = 0
if self.rect.bottom > SCREENHEIGHT:
self.rect.bottom = SCREENHEIGHT
if self.rect.top < 50:
self.rect.top = 50
self.hitbox.center = self.pos
if self.lives <= 0:
screen.blit(pygame.image.load("gameover.png"), (0,0))
pygame.display.flip()
time.sleep(5)
os._exit(0)
pygame.quit()
stageon = False
sprites = pygame.sprite.Group()
player = Player(sprites)
def main():
running = True
mi_level = False
screen.blit(pygame.image.load("Backgrounds/mainmenu.png"), (0, 0))
pygame.display.update()
while True:
for events in pygame.event.get():
if events.type == pygame.QUIT or running == False:
time.sleep(1)
running = False
pygame.quit()
return
pygame.draw.rect(screen, (255, 204, 204), (690, 110, 700, 135), 0)
if pygame.mouse.get_pressed()[0] and pygame.Rect(690, 110, 880, 165).collidepoint(pygame.mouse.get_pos()):
screen.fill((255, 123, 67))
screen.blit(pygame.image.load("Backgrounds/mi_level.png"), (0, 50))
pygame.display.flip
background = screen.copy()
mi_level = True
pygame.display.update()
while mi_level:
sprites.update()
screen.blit(background, (0, 0))
sprites.draw(screen)
pygame.display.update()
clock.tick(100)
if mi_level == False:
return
if events.type == pygame.QUIT or running == False:
time.sleep(1)
running = False
pygame.quit()
return
if __name__ == '__main__':
main()
我想知道这次我犯了什么业余错误...
任何帮助将不胜感激。谢谢:)
答案 0 :(得分:1)
已经存在一个游戏循环。不需要“嵌套”游戏循环:
while mi_level:
一个简单的if
语句,用于评估游戏是否开始就足够了:
if mi_level:
# []...]
请注意,在内部循环的代码中,由于pygame.event.get()
缺失,因此未对事件进行评估。这导致pygame.QUIT
事件不再得到处理,并且窗口没有按预期关闭并做出反应。
查看对主循环的更改:
def main():
running = True
mi_level = False
screen.blit(pygame.image.load("Backgrounds/mainmenu.png"), (0, 0))
pygame.display.update()
while True:
for events in pygame.event.get():
if events.type == pygame.QUIT or running == False:
time.sleep(1)
running = False
pygame.quit()
return
pygame.draw.rect(screen, (255, 204, 204), (690, 110, 700, 135), 0)
if pygame.mouse.get_pressed()[0] and pygame.Rect(690, 110, 880, 165).collidepoint(pygame.mouse.get_pos()):
screen.fill((255, 123, 67))
screen.blit(pygame.image.load("Backgrounds/mi_level.png"), (0, 50))
pygame.display.flip()
background = screen.copy()
mi_level = True
if mi_level:
sprites.update()
screen.blit(background, (0, 0))
sprites.draw(screen)
pygame.display.update()
clock.tick(100)
pygame.display.update()