From 10de1c5c71b0a5a96eb0136381ebf55b851d72cc Mon Sep 17 00:00:00 2001 From: kleph Date: Sat, 16 Jan 2010 01:19:07 +0100 Subject: [PATCH] added (very) basic level handling --- pyshoot.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 12 deletions(-) diff --git a/pyshoot.py b/pyshoot.py index 50af1de..981440a 100755 --- a/pyshoot.py +++ b/pyshoot.py @@ -24,16 +24,22 @@ class Globals: self.die = False self.moving_objects = pygame.sprite.RenderUpdates() self.bullets_list = pygame.sprite.RenderUpdates() - self.bulletfactory = BulletFactory() + self.bullet_factory = BulletFactory() self.enemies_list = pygame.sprite.RenderUpdates() self.enemies_active_list = pygame.sprite.RenderUpdates() + self.enemy_factory = EnemyFactory() self.player = None - -class Event: - def __init__(self): +class Event_Spawn: + def __init__(self, enemy_type, pos): self.time = 0 - self.type = 0 + self.enemy_type = 0 + self.pos = pos + + def activate(self): + e = globals.enemy_factory.create(self.enemy_type, self.pos) + globals.enemies_list.add(e) + globals.enemies_active_list.add(e) class Object(pygame.sprite.Sprite): def __init__(self, frames, pos, speed): @@ -124,7 +130,7 @@ class Player(Object): def shoot(self): """ create a bullet and add it to the global bullet list""" - bullet = globals.bulletfactory.create(self.bullet_type, self.rect.midright, self.shoot_wavepos) + bullet = globals.bullet_factory.create(self.bullet_type, self.rect.midright, self.shoot_wavepos) if self.shoot_wavepos == 0: self.shoot_wavepos = 1 if self.shoot_wavepos == 2: @@ -166,6 +172,22 @@ class Player(Object): else: self.bullet_counter += 1 + +class EnemyFactory: + """ Enemy Factory - Creates Enemy """ + + def __init__(self): + pass + + def create(self, type, pos): + if type == 0: + return self.create_type0(pos) + + def create_type0(self, pos): + e = Enemy(['enemy1_1.bmp', 'enemy1_2.bmp', 'enemy1_3.bmp', 'enemy1_4.bmp', 'enemy1_5.bmp', 'enemy1_6.bmp'], 0, pos) + return e + + class Enemy(Object): def __init__(self, frames, type, pos): Object.__init__(self, frames, pos, (-1, 0)) @@ -218,14 +240,47 @@ def init(): random.seed() #globals.event_type = {'create_enemeny' : create_enemey, 'vanish_object': vanish_object()} +class Level(): + """ Level holder """ + def __init__(self): + self.event_list = [] + self.past_event_list = [] + self.next_event = 0 + self.create_level1() -def create_level(): - """ phony level one""" + def create_level1(self): + """ phony level one""" + + # create events + # dict events string => function + # for e in keys(globals.events_type): + self.event_list.insert(0, (50, Event_Spawn(0, (500,200)) ) ) + self.event_list.insert(0, (150, Event_Spawn(0, (500,100)) ) ) + self.event_list.insert(0, (150, Event_Spawn(0, (500,200)) ) ) + self.event_list.insert(0, (250, Event_Spawn(0, (500,100)) ) ) + self.event_list.insert(0, (250, Event_Spawn(0, (500,200)) ) ) + self.event_list.insert(0, (250, Event_Spawn(0, (500,300)) ) ) + self.next_event, event = self.event_list[-1] + print "next : " + str(self.next_event) - # create events - # dict events string => function - # for e in keys(globals.events_type): - pass + def update(self, nbframe): + # print "(next, frame) : (" + str(self.next_event) + ", " + str(self.next_event) + ")" + while nbframe == self.next_event: + event_tuple = self.event_list.pop() + frame, event = event_tuple + event.activate() + self.past_event_list.append(event) + #self.event_list.remove(event_tuple) + if len(self.event_list) > 0: + self.next_event, event = self.event_list[-1] + else: + # no more event + self.end() + self.next_event = 0 + + def end(self): + """ end of level """ + pass def wait_keypress(): """ wait for a keypress and then return """ @@ -320,6 +375,9 @@ wait_keypress() nbframe = 0 +# initialize level +level = Level() + # create_player player = Player(['player1_1.bmp', 'player1_2.bmp'], (50, 50), (0, 0)) globals.player = player @@ -361,6 +419,9 @@ while not globals.die: # pygame.display.flip() nbframe = nbframe + 1 + # do level things + level.update(nbframe) + globals.clock.tick(60) for event in pygame.event.get():