added "smooth" movement

added function {start,stop}_move and separate detection of key press and
release
This commit is contained in:
kleph 2009-08-15 03:06:18 +02:00
parent 9acc2876cd
commit 8360689edb

View file

@ -55,7 +55,6 @@ class Object(pygame.sprite.Sprite):
def move(self, dx, dy): def move(self, dx, dy):
""" move the object by adding deltas passed """ """ move the object by adding deltas passed """
self.speed = (dx, dy) self.speed = (dx, dy)
globals.moving_objects.add(self)
def update(self): def update(self):
self.rect.move_ip(self.speed) self.rect.move_ip(self.speed)
@ -94,6 +93,20 @@ class Player(Object):
bullet = globals.bulletfactory.create(0, self.rect.midright) bullet = globals.bulletfactory.create(0, self.rect.midright)
globals.bullets_list.add(bullet) globals.bullets_list.add(bullet)
def start_move(self, vector):
""" initiate moving sequence """
globals.moving_objects.add(self)
self.speed = (self.speed[0] + vector[0], self.speed[1] + vector[1])
if not globals.moving_objects.has(self):
print "error should contain self after start_move"
def stop_move(self):
""" stop moving sequence """
globals.moving_objects.remove(self)
self.speed = (0, 0)
if globals.moving_objects.has(self):
print "error should NOT contain self after stop_move"
def init_gfx(): def init_gfx():
"""graphics initialisation""" """graphics initialisation"""
@ -135,21 +148,31 @@ def wait_keypress():
def process_key(event, player): def process_key(event, player):
""" process keyboard input """ """ process keyboard input """
if event.type == KEYDOWN:
if event.key == K_ESCAPE: if event.key == K_ESCAPE:
globals.die = True globals.die = True
elif event.key == K_n:
player.move(0, 10)
pass
elif event.key == K_UP: elif event.key == K_UP:
player.move(0, -10) player.start_move((0, -1))
elif event.key == K_DOWN: elif event.key == K_DOWN:
player.move(0, 10) player.start_move((0, 1))
elif event.key == K_LEFT: elif event.key == K_LEFT:
player.move(-10, 0) player.start_move((-1, 0))
elif event.key == K_RIGHT: elif event.key == K_RIGHT:
player.move(10, 0) player.start_move((1, 0))
elif event.key == K_x: elif event.key == K_x:
player.shoot() player.start_shoot()
if event.type == KEYUP:
if event.key == K_UP:
player.stop_move()
elif event.key == K_DOWN:
player.stop_move()
elif event.key == K_LEFT:
player.stop_move()
elif event.key == K_RIGHT:
player.stop_move()
elif event.key == K_x:
player.stop_shoot()
## ##
# Main # Main
@ -179,12 +202,9 @@ while not globals.die:
# draw player # draw player
#globals.screen.blit(player.image, player.pos) #globals.screen.blit(player.image, player.pos)
#globals.moving_objects.clear(globals.screen, globals.background) globals.moving_objects.clear(globals.screen, globals.background)
#globals.bullets_list.clear(globals.screen, globals.background) globals.bullets_list.clear(globals.screen, globals.background)
for event in pygame.event.get():
if event.type == KEYDOWN:
process_key(event, player)
# erase moving objects # erase moving objects
globals.moving_objects.update() globals.moving_objects.update()
@ -200,12 +220,13 @@ while not globals.die:
nbframe = nbframe + 1 nbframe = nbframe + 1
globals.clock.tick(60) globals.clock.tick(60)
globals.moving_objects.clear(globals.screen, globals.background)
globals.bullets_list.clear(globals.screen, globals.background)
for event in pygame.event.get():
if event.type == KEYDOWN or event.type == KEYUP:
process_key(event, player)
# reinitializing moving objects list # reinitializing moving objects list
globals.moving_objects = pygame.sprite.RenderUpdates() #globals.moving_objects = pygame.sprite.RenderUpdates()
print "frames : %d" % nbframe print "frames : %d" % nbframe