我正在做一个涉及旋转链条部分的小游戏。我是pygame的新手,但这是开始。
#!/usr/bin/python
import pygame
def draw(square):
(x,y) = square
pygame.draw.rect(screen, black, (100+x*20,100+y*20,20,20), 1)
def rotate(chain, index, direction):
(pivotx, pivoty) = chain[index]
if (direction == 1):
newchain = chain[:index]+[(y-pivoty+pivotx, (x-pivotx)+pivoty) for (x,y) in chain[index:]]
else:
newchain = chain[:index]+[(y-pivoty+pivotx, -(x-pivotx)+pivoty) for (x,y) in chain[index:]]
if (set(chain) & set(newchain[index+1:]) == set()):
return newchain
else:
print "Collision!"
return chain
pygame.init()
size = [600, 600]
screen = pygame.display.set_mode(size)
white = (255,255,255)
black = (0,0,0)
n = 20
chain = [(i,0) for i in xrange(n)]
screen.fill(white)
for square in chain:
draw(square)
pygame.display.flip()
raw_input("Press Enter to continue...")
newchain = rotate(chain, 5, 1)
print chain
print newchain
screen.fill(white)
for square in newchain:
draw(square)
pygame.display.flip()
raw_input("Press Enter to continue...")
是否可以使旋转作为动画顺利显示,而不是仅仅跳转到pygame中的正确位置?
答案 0 :(得分:1)
您应该创建一个不会将状态更改为最终状态的函数,但会执行一小部分移动,如果未完成动画,则会调用自身超时。
例如(那是伪pygame)
f(object):
object.position.x = 10
不会使对象动画平滑,但
f(object):
if object.position.x >= 10:
return
object.position.x += 1
setTimer(10, f(object))
确实