Python忽略while循环与True条件

时间:2016-06-26 16:05:46

标签: python-2.7 while-loop

我正在为一个简单的游戏编写一个菜单,所以我想用一个while循环来让用户点击它来选择他想要的选项。虽然我的程序在此循环之前正常工作,但在到达以下循环的行时它不会继续:

pygame.mouse.set_visible(True)        # This is the last line processed.
while True:                           # This line ist not processed.
    (curx,cury)= pygame.mouse.get_pos()
    screen.blit(cursor,(curx-17,cury-21))
    pygame.display.flip()
    # rating values of the cursor position and pressed mouse button below:
    (b1,b2,b3) = pygame.mouse.get_pressed()    #getting states of mouse buttons
    if (b1 == True or b2 == True or b3 == True): # "if one mouse button is pressed"
        (cx,cy) = pygame.mouse.get_pos()
        if (px <= curx <= px+spx and py <= cury <= py+spy):
            return (screen,0)
        elif (ox <= curx <= ox+sox and oy <= cury <= oy+soy):
            return (screen,1)
        elif (cx <= curx <= cx+scx and cy <= cury <= cy+scy):
            return (screen,2)
        else:
            return (screen,3)
    time.sleep(0.05)

我已经检查了错误的缩进等内容。

BTW我的解释器(python.exe,Python 2.7.11)在到达

的这一行后总是没有响应
while True:

因为在删除的答案中提出了问题:

我在每个上面显示的行之间有一个打印(&#34;&#34;)以找到有问题的行。 正如我在上面写了4行:解释器(以及调试器和错误报告)已经崩溃而没有任何进一步的响应。

此功能的整个代码是:     # 主菜单 def smenu(screen,res,menuimg,cursor):     #preset:     x,y = res     click = False

print("preset") # TEST PART

# Fontimports, needed because of non-standard font in use
menu = pygame.image.load("menu.png")
playGame = pygame.image.load("play.png")
options = pygame.image.load("options.png")
crdts = pygame.image.load("credits.png")

print("Fontimport") # TEST PART


#SIZETRANSFORMATIONS
# setting of sizes
smx,smy = int((y/7)*2.889),int(y/7)
spx,spy = int((y/11)*6.5),int(y/11)
sox,soy = int((y/11)*5.056),int(y/11)
scx,scy = int((y/11)*5.056),int(y/11)

print("setting of sizes") # TEST PART

# setting real size of text 'n' stuff

menu = pygame.transform.scale(menu,(smx,smy))
playGame = pygame.transform.scale(playGame,(spx,spy))
options = pygame.transform.scale(options, (sox,soy))
crdts = pygame.transform.scale(crdts, (scx,scy))
cursor = pygame.transform.scale(cursor,(41,33))

print("actual size transformation") # TEST PART 

#DISPLAY OF MENU
# fixing positions
mx, my = int((x/2)-((y/7)/2)*2.889),10 # position: 1. centered (x) 2. moved to the left for half of the text's length 3. positioned to the top(y), 10 pixels from edge
px, py = int((x/2)-((y/11)/2)*6.5),int(y/7+10+y/10) # position: x like above, y: upper edge -"menu"'s height, -10, - height/10
ox, oy = int((x/2)-((y/11)/2)*5.056),int(y/7+10+2*y/10+y/11)
cx, cy = int((x/2)-((y/11)/2)*5.056),int(y/7+10+3*y/10+2*y/11)

print("fixing positions") # TEST PART

# set to display
#screen.fill(0,0,0)
screen.blit(menuimg,(0,0))
screen.blit(menu,(mx,my)) 
screen.blit(playGame,(px,py)) 
screen.blit(options,(ox,oy))
screen.blit(crdts,(cx,cy))
pygame.display.flip()

print("set to display") # TEST PART

# request for input (choice of menu options)
pygame.mouse.set_visible(True)
print("mouse visible")  # TEST PART last processed line
while (True):
    print("While-loop") # TEST PART
    curx,cury = pygame.mouse.get_pos()
    screen.blit(cursor,(curx-17,cury-21))
    pygame.display.flip()
    # decision value below
    (b1,b2,b3) = pygame.mouse.get_pressed() # getting mouse button's state
    if (b1 == True or b2 == True or b3 == True): # condition true if a buton is pressed
        (cx,cy) = pygame.mouse.get_pos()
        if (px <= curx <= px+spx and py <= cury <= py+spy):
            return (screen,0)
        elif (ox <= curx <= ox+sox and oy <= cury <= oy+soy):
            return (screen,1)
        elif (cx <= curx <= cx+scx and cy <= cury <= cy+scy):
            return (screen,2)
        else:
            return (screen,3)
    time.sleep(0.05)
print("directly skipped")

2 个答案:

答案 0 :(得分:0)

我认为问题出现在您的代码的以下行中:

if (b1 == True or b2 == True or b3 == True):

这种情况永远不会成为现实,所以你在没有函数返回任何东西的情况下陷入了while循环。

答案 1 :(得分:0)

会评论但不能因为低代表。

问题可能是,您正在向函数外部的while循环返回一个值。

虽然您的解释器/ IDE没有为您提供正确的错误消息,但有点奇怪。