Reputation: 21353
I am making a little game involving rotating parts of chains. I am new to pygame but here is the start.
#!/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...")
Is it possible to make the rotation appear smoothly as an animation rather than just jump to the right place in pygame?
Upvotes: 1
Views: 496
Reputation: 6881
You should create a function which doesn't change the state to the final, but does a little part of the movement and, if the animation is not done, calls itself with a timeout.
For example (that's pseudo-pygame)
f(object):
object.position.x = 10
doesn't make object animate smoothly, but
f(object):
if object.position.x >= 10:
return
object.position.x += 1
setTimer(10, f(object))
does.
Upvotes: 1