正文之前
不好意思忘了配图了!!时间太紧迫了,图书馆就要关门了!!
正文
import pygame,sys
import time
from pygame.locals import *
white=255,255,255
blue = 1,1,200
pygame.init()
screen = pygame.display.set_mode((600,600))
width=0 # define the initilization
pygame.display.set_caption("冒泡排序") # the title
a=[550,500,450,400,350,300,250,200,100,50] # the list
def swap(t1, t2): # define a function of swap
return t2, t1
for x in range(0,10):
width+=3
for s in range(0,9-x): # 冒泡循环
if a[s]>a[s+1]:
a[s],a[s+1]=swap(a[s],a[s+1])
time.sleep(0.5)
for i in range(1,10): # uodate the picture every sorting time
pygame.draw.line(screen,white,(50*(i+1),10),(50*(i+1),a[i]/2),width)
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
pygame.display.flip()
求教大神啊!!我有个问题。那就是上一次draw的图像没法刷新掉,所以以前的图像总是会覆盖掉现在的图像。所以最后就显得很乱!!而且到了最后所有的屏幕充满了我的柱状图!!!
我一直以为是算法错了!还想着传值错误??!!或者是因为我太久没玩python忘了啥特性??结果都不是!!狗日的pygame坑死我!! display这个函数我知道为了节约性能会保留原来渲染出来的!!但是你也别这么坑我 啊!!八点写好,现在都快被赶出图书馆了!!
正文之后
求大神指导!!!
度娘没找到!!暂时还没想到法子!!update换成flip都没用!!
感谢老通帮忙!!!终于整出来了。利用局部作用域不停刷新画面。。虽然开销大的吓人。但是好歹实现了A! !!!
import pygame,sys
import time
from pygame.locals import *
white=255,255,255
blue = 1,1,200
pygame.init()
width = 25
pygame.display.set_caption("bubble sorting")
a = [550,500,450,400,350,300,250,200,100,50]
def swap(t1, t2):
return t2, t1
for x in range(0,10):
for s in range(0,9-x):
if a[s]>a[s+1]:
a[s],a[s+1]=swap(a[s],a[s+1])
time.sleep(0.3)
# pygame.display.get_init()
screen = pygame.display.set_mode((600,600))
for i in range(0,10):
pygame.draw.line(screen,white,(50*(i+1),10),(50*(i+1),a[i]),width)
pygame.display.flip()
最新版本的代码。目前在无法利用pygame刷新屏幕的前提下,无法继续改进了。希望大神可以给我提供给点方向!!!怎么才能才能直接刷新画面而不需要一直刷新屏幕呢?
import pygame,sys
import time
from pygame.locals import *
white=255,255,255
blue = 1,1,200
pygame.init()
width = 25
pygame.display.set_caption("bubble sorting")
a = [550,500,450,400,350,300,250,200,100,50]
def swap(t1, t2):
return t2, t1
for x in range(0,10):
for s in range(0,9-x):
if a[s]>a[s+1]:
a[s],a[s+1]=swap(a[s],a[s+1])
time.sleep(0.3)
# pygame.display.get_init()
screen = pygame.display.set_mode((600,600))
for i in range(0,10):
pygame.draw.line(screen,white,(50*(i+1),10),(50*(i+1),a[i]),width)
pygame.display.flip()
最后再在最外层补上一段代码就可以了。这样最后就会停留在排序好厚的画面中!!
while(True):
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen = pygame.display.set_mode((600,600))
for i in range(0,10):
pygame.draw.line(screen,white,(50*(i+1),10),(50*(i+1),a[i]),width)
pygame.display.update()
最新稳定高性能版本:
------------1
width = 20
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("bubble sorting")
a = [400,150,200,490,390,180,301,511,540,90]
def swap(t1, t2):
return t2, t1
for x in range(0,10):
for s in range(0,9-x):
if a[s]>a[s+1]:
a[s],a[s+1]=swap(a[s],a[s+1])
time.sleep(0.05)
screen.fill((0,200,0))
for i in range(0,10):
pygame.draw.line(screen,white,(50*(i+1),10),(50*(i+1),a[i]),width)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill((0,200,0))
for i in range(0,10):
pygame.draw.line(screen,(255,0,0),(50*(i+1),10),(50*(i+1),a[i]),width)
pygame.display.update()