gpt4 book ai didi

python - 我想在 pygame 中进行可视化的冒泡排序,排序有效,但可视化不起作用

转载 作者:行者123 更新时间:2023-12-01 07:14:36 25 4
gpt4 key购买 nike

就像标题所说,我想用 python 和 pygame 创建一个可视化的冒泡排序。该排序工作完美,但当它可视化时,它永远不会得到正确的输出。

代码如下:

import pygame
import sys
import time

pygame.init()

Vector_len = 0
x = 0
numbers_to_order = []
IsRunning = True

Vector_len = eval(input("How many numbers do you want to sort? "))

for i in range(Vector_len):
numbers_to_order.append(0)

for i in range(len(numbers_to_order)):
numbers_to_order[i] = eval(input("Insert number at index "+str(i+1)+": "))

print("Inserted array: "+str(numbers_to_order))

screen = pygame.display.set_mode((1000,500))

while IsRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
IsRunning = False

for i in range(len(numbers_to_order)):

for j in range(len(numbers_to_order)):
try:
if numbers_to_order[j] > numbers_to_order[j+1]:
x = numbers_to_order[j]
numbers_to_order[j] = numbers_to_order[j+1]
numbers_to_order[j+1] = x
except(IndexError):
pass
pygame.draw.rect(screen,(255,255,255),(j*(1000/len(numbers_to_order)),500-(numbers_to_order[j])*50,(1000/len(numbers_to_order)),(numbers_to_order[j])*50))
print((j*(1000/len(numbers_to_order)), (numbers_to_order[j])*50))
pygame.display.flip()
time.sleep(0.05)

print("Sorted array: "+str(numbers_to_order))
pygame.quit()
sys.exit()

运行时,不是显示正确排序的条,而是以错误的顺序显示它们,但排序是正确的。有什么帮助吗?

最佳答案

主要问题是您必须清除每一帧中的显示并在每一帧中绘制整个“列表”。请注意,如果在 位置绘制了代表较大数字的条形,则代表较小数字的条形将不会“清除”该位置。

screen.fill(0)
for k, n in enumerate(numbers_to_order):
pygame.draw.rect(screen,(255,255,255),(k*(1000/len(numbers_to_order)),500-(numbers_to_order[k])*50,(1000/len(numbers_to_order)),(n)*50))

我建议不要在游戏循环中使用绘制场景的嵌套循环。使用主应用程序循环并在循环中递增控制变量 ij:

i = 0
j = 0

while IsRunning:

# [...]

if j < len(numbers_to_order)-1:
j += 1
elif i < len(numbers_to_order)-1:
i += 1
j = 0

此外,我建议使用pygame.time.Clock()

参见示例:

# start control variables
i = 0
j = 0
IsRunning = True

# main loop
clock = pygame.time.Clock()
while IsRunning:

# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
IsRunning = False
if event.type == pygame.MOUSEBUTTONDOWN:
IsRunning = False

# clear screen
screen.fill(0)

# draw the entire range
for k, n in enumerate(numbers_to_order):
pygame.draw.rect(screen,(255,255,255),(k*(1000/len(numbers_to_order)),500-(numbers_to_order[k])*50,(1000/len(numbers_to_order)),(n)*50))

# update the display
pygame.display.flip()
clock.tick(10)

# sort (1 step)
print((j*(1000/len(numbers_to_order)), (numbers_to_order[j])*50))
try:
if numbers_to_order[j] > numbers_to_order[j+1]:
x = numbers_to_order[j]
numbers_to_order[j] = numbers_to_order[j+1]
numbers_to_order[j+1] = x
except(IndexError):
pass

# increment control variables
if j < len(numbers_to_order)-1:
j += 1
elif i < len(numbers_to_order)-1:
i += 1
j = 0

关于python - 我想在 pygame 中进行可视化的冒泡排序,排序有效,但可视化不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58039583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com