PyGame主菜单

2024-04-19 14:01:55 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我有一个学校项目,它是创造一个游戏。我已经创建了游戏,但我们还需要一个主菜单。我对PyGame还不太熟悉,也不知道该怎么做。我对编程的某些方面也相当陌生,比如类。在我的程序中,我有我的主文件名“Computer Science Game.py”,我希望主菜单在另一个文件中。然后我想要我的主菜单,当我点击play加载“Computer Science Game.py”文件时。我不知道怎么做。另外,PyGame不太适合制作我花了几个小时研究的主菜单,但是没有用,有人能给我一个开始吗,我可以扩展一下吗?谢谢 P、 这是我第一次使用StackOverflow,所以请原谅任何错误:)


Tags: 文件项目py程序game游戏play文件名
3条回答

我不打算完全编码出如何为您编写菜单,因为这会破坏类练习的目的,但我可以为您指出正确的方向。

如果您有两个选项菜单,我将如何编写主菜单如下:quit and start game(伪代码):

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循环,该循环显示一个基本菜单,并在它进入游戏之前显示它。这也许不是最有效的方法,但对我来说,这似乎是最简单的方法。

顺便说一句,你现在可能希望远离复杂的动画和鼠标事件,直到你找到了基本菜单。使用老式的好盒子和箭头键输入。

下面是一些我用来处理菜单之类事情的便捷功能:

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

我不打算完全编码出如何为您编写菜单,因为这会破坏类练习的目的,但我可以为您指出正确的方向。

如果您有两个选项菜单,我将如何编写主菜单如下:quit and start game(伪代码):

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循环,该循环显示一个基本菜单,并在它进入游戏之前显示它。这可能不是最有效的方法,但对我来说,这似乎是最简单的方法。

顺便说一句,你现在可能希望远离复杂的动画和鼠标事件,直到你找到了基本菜单。使用老式的好盒子和箭头键输入。

相关问题 更多 >