处理Python屏幕

2024-06-28 14:35:09 发布

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

我已经做了一个程序,可以让你选择一个字符,但我停留在使用屏幕处理。你知道吗

据我所知,使用屏幕会重写整个屏幕,但我想保留一些元素,比如你选择的角色,并确保你可以使用它。你知道吗

所以我想做的是,在你做了4个选择之后,你必须点击空框进入下一个屏幕,只显示你所做的选择。你知道吗

代码:

def mousePressed():
    global choices,playerchoices
    print(choices)

if mouseButton == LEFT:

    if len(playerchoices) < 4 and screen1:
        for character in choices:
            if mouseX >= character['rectangle'][0] and mouseX <= character['rectangle'][2] + character['rectangle'][0]  and mouseY >= character['rectangle'][1] and mouseY <= character['rectangle'][3] + character['rectangle'][1] :
                (playerchoices.append(character))
                fill(100)  
                rect(character['rectangle'][0], character['rectangle'][1], character['rectangle'][2], character['rectangle'][3])


        for i in range(len(playerchoices)):
            fill(135)
            rect(420,90+(50 * i),150,50)
            fill(190)
            text(playerchoices[i]['name'] ,420,90+(50 * i),150,50)


    elif screen2:
        if 490< mouseX < 400 and 50 < mouseY < 50:
            Complete()

完整代码:https://github.com/jagmeet44/lesson-code

我希望有人能帮我,我试着用名字来声明屏幕,但那只显示控制台。你知道吗


Tags: and代码inrectforlenif屏幕
1条回答
网友
1楼 · 发布于 2024-06-28 14:35:09

您必须定义要绘制的不同屏幕,并使用一个变量来确定需要绘制的屏幕。为不同的屏幕创建一个函数。保持不变的元素(如菜单)可以在单独的函数中定义,并在另一个屏幕上绘制。你知道吗

下面我创建了一个基本设置,其中变量gameState包含要绘制的屏幕。我还使用了更灵活的设置来绘制选项。通过这种方式,您可以使用mouseX和mouseY计算项目必须绘制的位置以及单击的位置。你知道吗

在仍然选择的情况下,可以使用不同的选项来显示选项。我通过显示所选的选项并从选项列表中删除这些选项来实现它。单击选定的字符,将其返回到选项。你知道吗

choices =["Spiderman","Hulk","BlackPanther","DrStrange","Thor","IronMan","CptnAmerica"]
playerchoices = []
gameState = 0 # gamestate hold the current fase of the game / current screen to be drawn
# 0 -> select team
# 1 -> team selected, review choices

def draw():
    background(220)
    fill(0)
    if gameState == 0:
        drawChoices()
    elif gameState == 1:
        reviewChoices()

def drawChoices():
    global choices, playerchoices
    # draw all options
    for i in range(len(choices)):        
        text(choices[i],50,20*i+20) # write names at x=50, and y dependent on list index
    # draw chosen
    for i in range(len(playerchoices)):
        text(playerchoices[i],400,20*i+20)  # write names at x=400, and y dependent on list index

def reviewChoices():
    global playerchoices
    for i in range(len(playerchoices)):
        text(playerchoices[i],50,20*i+20)  # write names at x=50, and y dependent on list index

def mousePressed():
    global choices,playerchoices, gameState
    if gameState == 0: # if selecting characters
        if mouseButton == LEFT:
            if mouseX >= 50 and mouseX < 350: # mouseX is on the list of names
                listIndex = (mouseY-20) // 20 #determine listindex based on mouseY
                if listIndex < len(choices): # if not clicked below the list
                    playerchoices.append(choices[listIndex]) # add character to selectedlist
                    choices.remove(listIndex) # remove character from optionlist
                if len(playerchoices) == 4: # if 4 characters chosen, move to next gamestate / screen
                    gameState = 1
            elif mouseX >= 400 and mouseX < 750: # mouseX is on the list of chosen-names
                listIndex = (mouseY-20) // 20 #determine listindex based on mouseY
                if listIndex < len(playerchoices): # if not clicked below the list
                    choices.append(playerchoices[listIndex]) # add character to optionlist
                    playerchoices.remove(listIndex) # remove character from selectedlist

注意:我没有用Python进行处理,所以我没有测试过这段代码。但如果不行的话我相信你会明白的。你知道吗

相关问题 更多 >