2016-03-20 80 views
0

我現在有一個問題與我的遊戲,遊戲工作正常。然而,當添加我的主菜單時,它不起作用,它會加載它,但是當你按下播放時什麼也沒有發生(它不會去遊戲它只是停留在那裏)。我重視我的簡單的主菜單的圖像([1]:http://i.stack.imgur.com/YKY9q.jpg)。我的代碼是感謝下面:實現遊戲循環中的主菜單,Python2和Pygame

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) 
+0

你還沒有做了'如果行動==「開始」' –

回答

0

看起來你是以下this tutorial ....

您的按鈕不當您點擊播放時做任何事,它只是返回。

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() 

而在button方法(從教程)中,您只需要調用該操作。

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) 
+0

嗨,問題依然存在,我試過了,希望它改變我在那兒寫過「迴歸」到「前奏=假」什麼將退出主菜單,並繼續遊戲循環 –

+0

,是的,我的確在遵循那個教程 –

+0

你有兩個不同的'intro'變量。嘗試刪除'game_intro'裏面的一個 –