PyGame主菜单

时间:2014-01-06 21:42:38

标签: python pygame

所以,我有一个学校项目,它是创造一个游戏。我创造了游戏,但我们也需要一个主菜单。我对PyGame很新,并且不了解如何做到这一点。我对编程的某些方面也很新,比如类。在我的程序中,我有我的主文件名“Computer Science Game.py”,我希望我的主菜单在另一个文件中。然后我想要我的主菜单,当我点击播放加载“Computer Science Game.py”文件。我不知道该怎么做。另外PyGame制作Main Menus并不是很好,我花了几个小时研究,但无济于事,有人可以给我一个开始,我可以扩展吗?谢谢 附:这是我第一次使用StackOverflow,请原谅任何错误:)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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()