[英]Pygame: Can't move player when main menu function added
I'm still working on a game in Pygame.我仍在 Pygame 中开发游戏。 I've got the gameplay to work, I just want to make a main menu function so when a button is pressed, the game starts.
我有游戏玩法,我只想制作一个主菜单功能,这样当按下按钮时,游戏就开始了。
When I added in the main menu, and edited the game loop, I was unable to move the player sprite, and the game wouldn't close when I press the "X out" button (it goes irresponsive(this didn't happen before)).当我在主菜单中添加并编辑游戏循环时,我无法移动玩家精灵,并且当我按下“X out”按钮时游戏也不会关闭(它没有响应(这之前没有发生过) ))。
This didn't happen originally.这原本没有发生。
And there's no traceback.而且没有回溯。
Here's my code:这是我的代码:
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()
I wonder what amateur mistake I've made this time...我想知道这次我犯了什么业余错误......
Any help would be appreciated.任何帮助,将不胜感激。 Thanks :)
谢谢 :)
There is already a game loop.已经有一个游戏循环。 There is not any need of "nested" game loops:
不需要“嵌套”游戏循环:
while mi_level:
A simple if
statement, which evaluates if the game is started is sufficient:一个简单的
if
语句,它评估游戏是否开始就足够了:
if mi_level:
# []...]
Note, in your code in the inner loops, the events haven't been evaluated, because pygame.event.get()
is missing.请注意,在您的内部循环代码中,尚未评估事件,因为缺少
pygame.event.get()
。 This causes that the pygame.QUIT
event wasn't handled any more and the window didn't close and react as intended.这会导致
pygame.QUIT
事件不再被处理,并且窗口没有按预期关闭和反应。
See the changes to the main loop:查看主循环的变化:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.