该程序在运行几秒钟后仍会以某种方式崩溃。有人能帮我吗?
这是一个用于可视化排序算法的程序。对不起,如果我做错了很多事情,但是我刚开始使用pygame,但我仍然不确定是否一切正常。
谢谢你的帮助!
import random
import time
import pygame
pygame.init()
display_width=1200
display_height=800
gamedisplay=pygame.display.set_mode((display_width,display_height))
clock=pygame.time.Clock()
correct=[]
shuffled=[]
for i in range(100):
correct.append(i+1)
shuffled.append(i+1)
random.shuffle(shuffled)
block_width=display_width/(len(shuffled))
def bar(block_width,shuffled):
for i in shuffled:
colour=(i,i,255)
pygame.draw.rect(gamedisplay, colour,
[shuffled.index(i)+shuffled.index(i)*block_width,750,block_width,-i-i*2.5])
def inserting(shuffled):
a=0
for i in range(len(shuffled)):
x=a
while shuffled[x]>shuffled[x+1] or x+1==len(shuffled):
change=shuffled[x]
shuffled.remove(shuffled[x])
shuffled.insert(x+1,change)
if a+1!=len(shuffled)-1:
a=a+1
else:
a=0
return shuffled
def Loop(block_width,shuffled,correct):
FPS=10
while shuffled!=correct:
shuffled=inserting(shuffled)
bar(block_width,shuffled)
pygame.display.update()
time.sleep(1/FPS)
bar(block_width,shuffled)
pygame.display.update()
print(shuffled)
Loop(block_width,shuffled,correct)
python大神给出的解决方案
您可能需要使用事件循环(for event in pygame.event.get():
)或在每个帧中调用pygame.event.pump()
,否则操作系统会认为程序已锁定。我建议以这种方式重组Loop
函数:
def Loop(block_width,shuffled,correct):
clock = pygame.time.Clock() # A clock to limit the frame rate.
FPS=10
while True:
for event in pygame.event.get():
# Quit if the user closes the window.
if event.type == pygame.QUIT:
return
# Fill the background with a color each frame.
gamedisplay.fill((30, 30, 30))
# If not sorted, keep sorting.
if shuffled != correct:
shuffled = inserting(shuffled)
bar(block_width,shuffled)
pygame.display.update()
clock.tick(FPS)
Loop(block_width,shuffled,correct)
pygame.quit()
Python sqlite3数据库已锁定 - python我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
子条件的python条件覆盖 - python我试图找到一个python代码覆盖率工具,该工具可以衡量语句中是否包含子表达式:例如,我想看看下面的示例是否涵盖了condition1 / condition2 / condtion3?if condition1 or condition2 or condition3: x = true_value python大神给出的解决方案 对此的唯一合理答案是:当前…
USB设备发行 - python我目前正在使用PyUSB。由于我不熟悉USB,所以我不知道如何执行以下操作。我已经从Python PyUSB成功连接到我的USB设备硬件。在代码中,我需要重置USB设备硬件。通过向硬件发送命令来完成。现在,在硬件重置后,我想从Python PyUSB释放当前的USB设备。然后,我想在重置后将其重新连接到USB设备硬件。请让我知道,如何释放USB设备连接和接口…
Python-熊猫描述了抛出错误:无法散列的类型“ dict” - python更新:我正在使用“ Socrata开源API”中的一些示例代码。我在代码中注意到以下注释:# First 2000 results, returned as JSON from API / converted to Python # list of dictionaries by sodapy. 我不熟悉JSON。我已经下载了一个数据集,并创建了一个包含大量…