所以,我有一個學校項目,它是創建一個遊戲。我創建了遊戲,但我們還需要一個主菜單。我對PyGame相當陌生,不知道如何去做。我對編程的某些方面也很新穎,例如類。在我的程序中,我有我的主要文件名「計算機科學Game.py」,我想我的主菜單在另一個文件。然後我想要我的主菜單,當我點擊播放加載「計算機科學Game.py」文件。我不知道如何做到這一點。另外PyGame並不是很適合製作我花了數小時研究的主菜單,但無濟於事,有人能給我一個開始,我可以擴展嗎?謝謝 P.S.這是我第一次使用StackOverflow,所以請原諒任何錯誤:)PyGame主菜單
回答
我不會完全編寫出如何爲您編寫菜單,因爲這會挫敗課堂鍛鍊的目的,但我可以指出你在正確的方向。
我將如何去寫主菜單將是以下假設你有兩個選擇菜單:退出並開始遊戲(僞代碼):
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
粘在箭頭鍵上。
非常感謝。 :) –
不要聽起來像一個乞丐,但請隨時留下一個upvote /複選標記。 ;)也讓我知道,如果你需要特定代碼的幫助,我會很樂意提供幫助。 –
好東西哥們! :D –
下面是一些方便的功能,我用的東西,如菜單:
# `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()
我自己沒有測試過。我會在一秒鐘內完成 – p99will
- 1. Pygame類菜單
- 2. Pygame菜單點擊功能
- 3. 實現遊戲循環中的主菜單,Python2和Pygame
- 4. 使用pygame創建piTFT的菜單主屏幕
- 5. 如何爲pygame創建tkinter菜單欄
- 6. Pygame中的遊戲的菜單按鈕
- 7. 在Pygame中製作的遊戲菜單
- 8. PyGame MOUSEBUTTONDOWN事件的菜單問題
- 9. 在pygame中組合效果到菜單
- 10. Pygame運行主
- 11. 主菜單影響子菜單
- 12. 子菜單重疊IE主菜單11
- 13. 將子菜單與主菜單對齊
- 14. 主菜單影響我的子菜單
- 15. 遊戲主菜單
- 16. 主菜單上的
- 17. MVVM主菜單欄
- 18. 主菜單佈局
- 19. 的主題激活自動設置菜單位置「主菜單」
- 20. 禁用Wordpress主菜單下拉菜單(子菜單)
- 21. 2菜單 - 主菜單,二級菜單和麪包屑
- 22. NET主菜單幫助
- 23. 對主菜單的貢獻
- 24. 主菜單級別深度
- 25. 無法創建主菜單
- 26. Delphi主菜單變色
- 27. 製作動畫主菜單
- 28. 菜單主動與jQuery
- 29. 如何使菜單主動
- 30. ICS中的Android主菜單
好了,所以我在谷歌搜索「pygame的菜單」,並且第一個鏈接把我帶到[此頁(HTTP:/ /www.pygame.org/tags/menu)在pygame網站上。 [該清單上的第一個鏈接](http://www.pygame.org/project-menu_key-2278-.html)將帶您進入一個頁面,您可以通過使用pygame實現的菜單下載示例應用程序的源代碼。這有幫助嗎? –
請看,因爲我是Python的新手,我已經嘗試過了,並且感到困惑:/ –
您是否嘗試下載源代碼並查看它(這包括閱讀註釋)?你在這裏要求的是有人爲pygame菜單編寫示例代碼,這是不必要的,因爲示例代碼已經可用。 –