2014-01-06 99 views
-2

所以,我有一個學校項目,它是創建一個遊戲。我創建了遊戲,但我們還需要一個主菜單。我對PyGame相當陌生,不知道如何去做。我對編程的某些方面也很新穎,例如類。在我的程序中,我有我的主要文件名「計算機科學Game.py」,我想我的主菜單在另一個文件。然後我想要我的主菜單,當我點擊播放加載「計算機科學Game.py」文件。我不知道如何做到這一點。另外PyGame並不是很適合製作我花了數小時研究的主菜單,但無濟於事,有人能給我一個開始,我可以擴展嗎?謝謝 P.S.這是我第一次使用StackOverflow,所以請原諒任何錯誤:)PyGame主菜單

+0

好了,所以我在谷歌搜索「pygame的菜單」,並且第一個鏈接把我帶到[此頁(HTTP:/ /www.pygame.org/tags/menu)在pygame網站上。 [該清單上的第一個鏈接](http://www.pygame.org/project-menu_key-2278-.html)將帶您進入一個頁面,您可以通過使用pygame實現的菜單下載示例應用程序的源代碼。這有幫助嗎? –

+0

請看,因爲我是Python的新手,我已經嘗試過了,並且感到困惑:/ –

+0

您是否嘗試下載源代碼並查看它(這包括閱讀註釋)?你在這裏要求的是有人爲pygame菜單編寫示例代碼,這是不必要的,因爲示例代碼已經可用。 –

回答

3

我不會完全編寫出如何爲您編寫菜單,因爲這會挫敗課堂鍛鍊的目的,但我可以指出你在正確的方向。

我將如何去寫主菜單將是以下假設你有兩個選擇菜單:退出並開始遊戲(僞代碼):

initialize all menu variables 

draw menu pix and selections 

while a selection has not been made: 
    look for key inputs (arrow keys/enter/esc) 
     if down arrow pressed: 
      move selection box to next option and note that the selection is on the next object down 
     if up arrow pressed: 
      move selection box to previous option and note that the selection is on the previous object up 
     if escape pressed: 
      quit the game 
     if enter pressed: 
      process which option in the menu was highlighted (quit of quit was selected, etc) and tell the while loop to stop 

    update the display 

set up the game variables and such 

main while loop 
    run the game and get an A+ 

請讓我知道如果這個回答您的題。背後的基本思想是添加另一個while循環,顯示基本菜單並在移動到遊戲之前顯示它。這可能不是最有效的方式,但對我來說,這似乎是最簡單的做事方式。

順便說一下,您可能希望遠離諸如複雜動畫​​之類的事情並使用鼠標事件,直到您找到基本菜單爲止。用老式方框和rect.move粘在箭頭鍵上。

+0

非常感謝。 :) –

+1

不要聽起來像一個乞丐,但請隨時留下一個upvote /複選標記。 ;)也讓我知道,如果你需要特定代碼的幫助,我會很樂意提供幫助。 –

+0

好東西哥們! :D –

0

下面是一些方便的功能,我用的東西,如菜單:

# `pos` is the `x,y` from `event.pos` 
# x, y is the x/y co-ords from the x/y where you render a button 
# x1, y1 is the width/height for the button. 
# This function will return true if the button is clicked on. 

def button_check(pos, x, y, x1, y1): 
    return pos[0] >= x and pos[0] < x + x1 and pos[1] >= y and pos[1] < y + y1 

# This function will create a nice button with text in it. 
# `sufrace` is like the default 'DISPLAYSURF', `color` is the color of the box 
# `text_color` is the color of the text in the box 
# `x/y` are the co-ords of the button. `width/height` are the dimensions of button 
# `text` is the text for the label. 

def make_button(surface,color,text_color,x,y,width,height,text): 
    pygame.draw.rect(surface, (0,0,0),(x-1,y-1,width+2,height+2),1) #makes outline around the box 
    pygame.draw.rect(surface, color,(x,y,width,height))#mkes the box 

    myfont = pygame.font.SysFont('Arial Black', 15) #creates the font, size 15 (you can change this) 
    label = myfont.render(text, 1, text_color) #creates the label 
    surface.blit(label, (x+2, y)) #renders the label 



#example of use: 
menu_items = ['Play','Load','Volume','High Scores','Exit'] 
while True: 
    for i in range(len(menu_items)-1):#goes through each item 
     make_button(DISPLAYSURF,SILVER,BLACK,10,10+(20*i),120,menu_items[i]) #puts the item into the make_button, `+20*i` will make each item 15px down from the last. 

    for event in pygame.event.get(): 
     if event.type == 5: 
      if event.button == 1: 
       for i in range(len(menu_items)-1):#check every button 

        if button_check(event.pos,10,10+(20*i),120): 
         if i == 0: 
          play() 
         elif i == 1: 
          load() 
         elif i == 4: 
          pygame.quit() 
          sys.exit() 
+0

我自己沒有測試過。我會在一秒鐘內完成 – p99will