在游戏循环、Python2和Pygame中实现主菜单
目前我的游戏有问题,游戏运行良好。然而,当添加我的主菜单时,它不起作用,它会加载它,但当你按play时,什么都不会发生(它不会进入游戏,它只是停留在那里)。我已附上我的简单主菜单([1]:)的图像。我的代码如下,谢谢:在游戏循环、Python2和Pygame中实现主菜单,python,pygame,game-loop,Python,Pygame,Game Loop,目前我的游戏有问题,游戏运行良好。然而,当添加我的主菜单时,它不起作用,它会加载它,但当你按play时,什么都不会发生(它不会进入游戏,它只是停留在那里)。我已附上我的简单主菜单([1]:)的图像。我的代码如下,谢谢: import pygame, sys, Funk from tileC import Tile from object_classes import * from interaction import interaction from A_Star import A_Star i
import pygame, sys, Funk
from tileC import Tile
from object_classes import *
from interaction import interaction
from A_Star import A_Star
import time
pygame.init()
pygame.font.init()
pygame.mixer.init()
pygame.mixer.music.load('audio/zombie_theme.ogg')
pygame.mixer.music.play(-1)
screen = pygame.display.set_mode((704, 448)) # 32, 32
Tile.pre_init(screen)
clock = pygame.time.Clock()
FPS = 30
total_frames = 0
dungeon = pygame.image.load('images/dungeon.jpg')
survivor = Survivor(32 * 2, 32 * 4)
intro = True
# colours
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
orange = (255,127,0)
grey = (50,50,50)
bright_red = (255,0,0)
bright_green = (0,255,0)
bright_orange = (255,215,0)
def text_objects(text, font):
textSurface = font.render(text, True, (white))
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('Creepster-Regular.ttf',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((704/2),(448/2))
gameDisplay.blit(TextSurf, TextRect,)
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, (ac), (x, y, w, h))
if click[0] == 1 and action != None:
if action == "start":
return
if action == "end":
pygame.quit()
sys.exit()
else:
pygame.draw.rect(screen, (ic), (x, y, w, h))
smallText = pygame.font.SysFont("monospace.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
screen.blit(textSurf, textRect)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(grey)
largeText = pygame.font.SysFont('Creepster-Regular.ttf',45)
TextSurf, TextRect = text_objects("2D Tile Based Zombie Game", largeText)
TextRect.center = ((704/2),(448/2))
screen.blit(TextSurf, TextRect)
button("Play",100,300,100,50,green,bright_green,"start")
button("Controls",300,300,100,50,orange,bright_orange, "how to play")
button("Quit",500,300,100,50,red,bright_red, "end")
pygame.display.update()
clock.tick(15)
#break
game_intro()
while True:
screen.blit(dungeon, (0,0) )
Zombie.spawn(total_frames, FPS)
Zombie.update(screen, survivor)
survivor.movement()
Bullet.super_massive_jumbo_loop(screen)
A_Star(screen, survivor, total_frames, FPS)
interaction(screen, survivor)
survivor.draw(screen)
Funk.text_to_screen(screen, 'Health: {0}'.format(survivor.health), 0,0)
Funk.text_to_screen(screen, 'Score: {0}'.format(survivor.score), 0,15)
Funk.text_to_screen(screen, 'FPS {0}'.format(FPS), 0,30)
pygame.display.flip()
clock.tick(FPS)
total_frames += 1
if survivor.health <= 0:
time.sleep(1)
screen.blit(pygame.image.load('images/dead.jpg'), (0,0))
pygame.display.update()
screen.fill(black)
Funk.text_to_screen(screen, "You scored {0} points!".format(survivor.score),704/2, 448/2, size = 50)
break
time.sleep(4)
导入pygame、sys、Funk
从tileC导入磁贴
从对象类导入*
从交互导入交互
从一颗星星导入一颗星星
导入时间
pygame.init()
pygame.font.init()
pygame.mixer.init()
pygame.mixer.music.load('audio/zombie\u theme.ogg')
pygame.mixer.music.play(-1)
screen=pygame.display.set_模式((70448))#32,32
Tile.pre_init(屏幕)
clock=pygame.time.clock()
FPS=30
总帧数=0
dungeon=pygame.image.load('images/dungeon.jpg')
幸存者=幸存者(32*2,32*4)
简介=正确
#颜色
黑色=(0,0,0)
白色=(255255)
红色=(200,0,0)
绿色=(0200,0)
橙色=(255127,0)
灰色=(50,50,50)
鲜红色=(255,0,0)
亮绿色=(0255,0)
亮橙色=(255215,0)
def text_对象(文本、字体):
textSurface=font.render(文本,真,(白色))
返回textSurface,textSurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font('crowster-Regular.ttf',115)
TextSurf,TextRect=text\u对象(text,largeText)
TextRect.center=((704/2)、(448/2))
blit(TextSurf,TextRect,)
def按钮(消息、x、y、w、h、ic、ac、操作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(屏幕,(ac),(x,y,w,h))
如果单击[0]==1并执行操作!=无:
如果操作==“开始”:
返回
如果操作==“结束”:
pygame.quit()
sys.exit()
其他:
pygame.draw.rect(屏幕,(ic),(x,y,w,h))
smallText=pygame.font.SysFont(“monospace.ttf”,20)
textSurf,textRect=text\u对象(msg,smallText)
textRect.center=((x+(w/2)),(y+(h/2)))
screen.blit(textSurf,textRect)
def game_intro():
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
屏幕填充(灰色)
largeText=pygame.font.SysFont('crowster-Regular.ttf',45)
TextSurf,TextRect=text_对象(“基于2D瓷砖的僵尸游戏”,largeText)
TextRect.center=((704/2)、(448/2))
screen.blit(TextSurf,TextRect)
按钮(“播放”,100300100,50,绿色,亮绿色,“开始”)
按钮(“控件”,300100,50,橙色,亮橙色,“如何播放”)
按钮(“退出”,500300100,50,红色,亮红色,“结束”)
pygame.display.update()
时钟滴答(15)
#中断
游戏介绍()
尽管如此:
屏幕。blit(地牢,(0,0))
僵尸。繁殖(总帧数,FPS)
僵尸。更新(屏幕,幸存者)
幸存者运动()
子弹。超级巨大的环形(屏幕)
A_星(屏幕、幸存者、总帧数、FPS)
交互(屏幕,幸存者)
幸存者。绘制(屏幕)
Funk.text-to-screen(screen'Health:{0})。格式(survivor.Health),0,0)
Funk.text到屏幕(屏幕,'Score:{0})。格式(survivor.Score),0,15)
文本到屏幕(屏幕'FPS{0}'。格式(FPS),0,30)
pygame.display.flip()
时钟滴答声(FPS)
总帧数+=1
如果survivor.health看起来您正在跟踪
当您单击“播放”时,按钮没有任何作用,它只是返回
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, (ac), (x, y, w, h))
if click[0] == 1 and action != None:
if action == "start":
return ### Fix here
if action == "end":
pygame.quit()
sys.exit()
不过,您确实应该为您的操作定义一个方法
def start_game():
# TODO: Implement
print("Starting game")
def how_to_play():
# TODO: Implement
print("How to")
def quit_game():
pygame.quit()
sys.exit()
在按钮方法(来自教程)中,您只需要调用操作
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
然后可以将方法附加到按钮而不是字符串
button("Play",100,300,100,50,green,bright_green,start_game)
button("Controls",300,300,100,50,orange,bright_orange, how_to_play)
button("Quit",500,300,100,50,red,bright_red, quit_game)
如果操作==“开始”
您好,问题仍然存在,我尝试将我写“return”的地方改为“intro=False”,希望它能退出主菜单并继续游戏循环,是的,我确实遵循了教程。您有两个不同的intro
变量。尝试删除game\u intro
中的一个,我得到了相同的结果:(我认为问题在于,在教程中,玩家使用了一个函数而不是游戏循环,因此他可以调用该函数退出主菜单并运行游戏