Python Pygame程序停止响应,没有给出错误
因此,我的程序是按行显示按钮,然后等待用户单击一个按钮,但是会发生的情况是,它将返回主菜单 我不确定如何让程序等待(我尝试了time.sleep()和os.system(“暂停”),但是这两种方法都会等待一定的时间,然后返回主菜单-因此我求助于使用控制台输入,只是为了查看框是否正确显示 但是现在有了控制台输入,点击任何一个按钮都会使程序没有响应(当按钮应该改变颜色时)——我已经包括了一个屏幕截图。 我想知道我做错了什么,我知道这有点乱,我真的很高兴能得到一些回应,如果需要的话,我可以提供更多的信息。 非常感谢 这是显示按钮的代码Python Pygame程序停止响应,没有给出错误,python,pygame,Python,Pygame,因此,我的程序是按行显示按钮,然后等待用户单击一个按钮,但是会发生的情况是,它将返回主菜单 我不确定如何让程序等待(我尝试了time.sleep()和os.system(“暂停”),但是这两种方法都会等待一定的时间,然后返回主菜单-因此我求助于使用控制台输入,只是为了查看框是否正确显示 但是现在有了控制台输入,点击任何一个按钮都会使程序没有响应(当按钮应该改变颜色时)——我已经包括了一个屏幕截图。 我想知道我做错了什么,我知道这有点乱,我真的很高兴能得到一些回应,如果需要的话,我可以提供更多的
screen.fill(white) #clear the screen
xPos = -135;yPos = 200;tasktodo = "null" #set the position of where the first buttons should be placed
for i in tasklist[posInList][2]: #For each task the user has been set
for y in QuestionTypes: #For each item in QuestionTypes ([MPQ,DBQ,RatioQ]...)
if str(y) in str(i): #If the any item in QuestionTypes is in the tasks the user is set:
task = (i.split(",")[0]) #This splits up the various tasks to the form of [DBQ,MPQ,AFQ]
if xPos > display_height: #If the place a button would be placed is out of bounds
xPos = 50 #Reset xPos
yPos += yPos -82 #Lower the position of the button to another row
else:
xPos += 185 #Else, place the button 185 pixels to the right
button(str(task), xPos, yPos, 150, 90, grey, lightGrey, action=str(task)) #Place a button with the name of the task, in position (xPos,yPos), dimensions (150,90),active/inactive colors of grey/light grey action being the task
listOfSetQuestions.append(y) #Append the tasks the user has to do to a list
amountoftasks += 1 #Keep track of the amount of tasks the user has to do.
# After the tasks are all printed, the user must input what task they wish to do
message_to_screen("These are the tasks your teacher has set, pick one:", black, "medium", -200) #send a message to the screen, black color, medium size font and y position of -200
pygame.display.update()
x = input("press ENTER to continue")
这是按钮的代码,因此您可以查看更多上下文
def button(text, x, y, width, height, inactiveColor, activeColor, action = None):
cur = pygame.mouse.get_pos() #Gets the position of the cursor
click = pygame.mouse.get_pressed() #will return a tuple of 3, click[0] is left click, click[1] is middle and click[2] is right.
if x + width > cur[0] > x and y + height > cur[1] > y: #If the mouse is clicked and is between the borders of the button.
pygame.draw.rect(screen, activeColor, (x,y,width,height)) #Change the color of the button to the active color, to show it is clicked
if click[0] == 1 and action != None: #if the mouse is clicked, and there is a purpose to the click (clicked on a button)
if action == "Create":
Create()
if action == "Sign in":
Sign_In()
if action == "Teacher":
pass
if action in QuestionTypes:
print (action)
global tasktodo
tasktodo = action
else:
pygame.draw.rect(screen, inactiveColor, (x,y,width,height))
text_to_button(text,black,x,y,width,height)
当请求
input
时,程序停止并等待用户输入内容。当程序停止时,它无法处理使操作系统相信程序已崩溃的事件。因此,当使用pygame时,您无法获得控制台输入(除非创建进程)
如果您只需要一个延迟,那么可以使用while
-循环
def wait(time):
clock = pygame.time.Clock()
while time > 0:
dt = clock.tick(30) / 1000 # Takes the time between each loop and convert to seconds.
time -= dt
pygame.event.pump() # Let's pygame handle internal actions so the os don't think it has crashed.
如果希望用户决定等待多长时间,可以检查用户事件
def wait_user_response():
clock = pygame.time.Clock()
waiting = True
while waiting:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN: # Or whatever event you're waiting for.
waiting = False
当然还有其他更好的方法,但因为我不知道您的程序如何运行,所以我只能提供这些。您需要检查事件等的
mainloop
。并且它不会让程序返回主菜单。参见图:和示例