为了做你正在尝试做一个开始,你将不得不随着“while True:”循环而改变。这里是一个代码,将做你正在做的事情的一个例子:
import pygame, sys, pygame.locals#1
pygame.init()#2
window=pygame.display.set_mode((500, 400), 0, 32)#3
pygame.display.set_caption("Paint")#4
BLACK = (0, 0, 0)#5
WHITE = (255, 255, 255)#6
RED = (255, 0, 0)#7
GREEN = (0, 255, 0)#8
BLUE = (0, 0, 255)#9
pentagon=pygame.Surface((250, 265))#10
pentagon.fill((0, 0, 0))#11
pygame.draw.polygon(pentagon, BLUE, ((146, 0), (250, 100), (230, 265), (44, 250), (0,110)))#12
pentagon.set_colorkey((0, 0, 0))#13
triangle=pygame.Surface((150, 200))#14
triangle.fill((0, 0, 0))#15
pygame.draw.polygon(triangle, RED, ((70, 0), (150, 200), (0, 50)))#16
triangle.set_colorkey((0, 0, 0))#17
line=pygame.Surface((60, 8))#18
line.fill(BLACK)#19
circle=pygame.Surface((30, 30))#20
circle.fill((0, 0, 0))#21
pygame.draw.circle(circle, GREEN , (15, 15), 15, 0)#22
circle.set_colorkey((0, 0, 0))#23
rects={'pentagon': pentagon.get_rect(), 'triangle': triangle.get_rect(), 'line': line.get_rect(), 'circle': circle.get_rect()}#24
rects['line'].centery=60#25
rects['line'].left=60#26
rects['circle'].centerx=150#27
rects['circle'].centery=150#28
while True:#29
for event in pygame.event.get():#30
if event.type==pygame.locals.QUIT:#31
pygame.quit()#32
sys.exit()#33
for rect in rects:#34
rects[rect].right+=1#35
if rects[rect].right>500:#36
if rect=='line':#37
rects['line'].centery=60#38
rects['line'].left=60#39
elif rect=='circle':#40
rects['circle'].centerx=150#41
rects['circle'].centery=150#42
else:#43
rects[rect].topleft=(0, 0)#44
window.fill(WHITE)#45
window.blit(pentagon, rects['pentagon'])#46
window.blit(triangle, rects['triangle'])#47
window.blit(line, rects['line'])#48
window.blit(circle, rects['circle'])#49
pygame.time.Clock().tick(40)#50
pygame.display.update()#51
我会尽我所能,以尽可能解释这个代码。
1-9行,你已经知道了。
第10行从您可能不知道的事情开始。表面物体是一种矩形图片,可以附加到任何其他表面,或者使用pygame.draw绘制。相信与否,窗户实际上是一个表面。
第10行创建一个表面,第11行用黑色填充,第12行在其上绘制五边形。
第13行基本上使所有黑色透明的像素。
14-17行,你现在应该明白了。
第18行为该线条创建了一个新的曲面对象,但不是在其上绘制线条,而是用黑色填充并单独放置。这是因为,如果你看看你的旧程序中的行,你可以看到它实际上只是一个矩形,没有别的。
20-23行应该理解。
24行制作矩形字典。矩形显示表面对象的位置,并且为了将它们绘制到任何东西上都是必需的。
25-28行更改线条和圆的位置。我这样做的原因是因为我认为你可能会喜欢它,因为你的程序中没有左上角的圆和线。
29-33行你应该明白。
第34行启动了一个for循环,它将遍历所有rects。
35行将每个矩形的右侧向右移动一个像素。
第36行检查右边的右边是否已经到达窗口的右边缘。
第37行检查是否碰到一边的线是线,如果是,则第38-39行将其移动到您拥有它的位置。
第40行检查是否碰到一边的是圆形,如果是,则第41-42行将其移动到您拥有它的位置。
第43-44行将矩形放在左上角,其他两个形状开始。
你应该知道第45行。
第46-49行使用了surface.blit()函数。这个函数的第一个参数是一个曲面,第二个参数是一个曲面。
50行不允许超过40帧通过秒。
51行更新屏幕。
我真的希望这会有所帮助,如果您投了票或接受了我的回答,考虑我写了多长时间,我将不胜感激。如果您有任何疑问,请询问。谢谢!
也许这可能有所帮助:http://stackoverflow.com/questions/8252860/pygame-move-object-position-from-one-point-to-another-at-constant-speed/8253040#8253040 – sinan