fix bug in movement handling when only one of the directions was stopped

This commit is contained in:
kleph 2009-08-17 00:30:20 +02:00
parent 8360689edb
commit b1b585515c

View file

@ -7,7 +7,7 @@
import pygame import pygame
from pygame.locals import * from pygame.locals import *
import os import os
import random
# constants # constants
@ -97,20 +97,24 @@ class Player(Object):
""" initiate moving sequence """ """ initiate moving sequence """
globals.moving_objects.add(self) globals.moving_objects.add(self)
self.speed = (self.speed[0] + vector[0], self.speed[1] + vector[1]) 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): def stop_move(self, vector):
""" stop moving sequence """ """ stop moving sequence """
self.speed = (self.speed[0] - vector[0], self.speed[1] - vector[1])
if self.speed == (0, 0):
globals.moving_objects.remove(self) globals.moving_objects.remove(self)
self.speed = (0, 0)
if globals.moving_objects.has(self): def start_shoot(self):
print "error should NOT contain self after stop_move" self.shoot()
def stop_shoot(self):
pass
def init_gfx(): def init_gfx():
"""graphics initialisation""" """graphics initialisation"""
globals.screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y)) globals.screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y), HWSURFACE|DOUBLEBUF)
globals.screen.fill(0) globals.screen.fill(0)
pygame.display.flip() pygame.display.flip()
@ -123,9 +127,11 @@ def init():
pygame.init() pygame.init()
init_gfx() init_gfx()
globals.clock = pygame.time.Clock() globals.clock = pygame.time.Clock()
random.seed()
#globals.event_type = {'create_enemeny' : create_enemey, 'vanish_object': vanish_object()} #globals.event_type = {'create_enemeny' : create_enemey, 'vanish_object': vanish_object()}
def create_enemy(type): def create_enemy(type):
""" create an enemy """ """ create an enemy """
pass pass
@ -163,17 +169,48 @@ def process_key(event, player):
player.start_shoot() player.start_shoot()
if event.type == KEYUP: if event.type == KEYUP:
if event.key == K_UP: if event.key == K_UP:
player.stop_move() player.stop_move((0, -1))
elif event.key == K_DOWN: elif event.key == K_DOWN:
player.stop_move() player.stop_move((0, 1))
elif event.key == K_LEFT: elif event.key == K_LEFT:
player.stop_move() player.stop_move((-1, 0))
elif event.key == K_RIGHT: elif event.key == K_RIGHT:
player.stop_move() player.stop_move((1,0))
elif event.key == K_x: elif event.key == K_x:
player.stop_shoot() player.stop_shoot()
def transition(type):
""" transition selector """
if type == 0:
transition_disintegrate()
elif type == 1:
transition_fade_to_black()
elif type == 2:
transition_fade_to_white()
def transition_disintegrate():
""" put black pixels at randoom location"""
for i in range(0, 200000):
x = random.randint(0, SCREEN_X)
y = random.randint(0, SCREEN_Y)
globals.screen.set_at((x, y), (0,0,0, 255))
if i % 1000 == 0 :
pygame.display.update()
globals.screen.fill((0, 0, 0))
pygame.display.update()
def transition_fade_to_black():
""" darken every pixels on screen """
array = pygame.PixelArray(globals.screen)
pass
def transition_fade_to_white():
""" lighten every pixels on screen """
pass
## ##
# Main # Main
## ##
@ -228,6 +265,8 @@ while not globals.die:
# reinitializing moving objects list # reinitializing moving objects list
#globals.moving_objects = pygame.sprite.RenderUpdates() #globals.moving_objects = pygame.sprite.RenderUpdates()
# transition(0)
print "frames : %d" % nbframe print "frames : %d" % nbframe