added (very) basic level handling

This commit is contained in:
kleph 2010-01-16 01:19:07 +01:00
parent a540a6b4fc
commit 10de1c5c71

View file

@ -24,16 +24,22 @@ class Globals:
self.die = False self.die = False
self.moving_objects = pygame.sprite.RenderUpdates() self.moving_objects = pygame.sprite.RenderUpdates()
self.bullets_list = pygame.sprite.RenderUpdates() self.bullets_list = pygame.sprite.RenderUpdates()
self.bulletfactory = BulletFactory() self.bullet_factory = BulletFactory()
self.enemies_list = pygame.sprite.RenderUpdates() self.enemies_list = pygame.sprite.RenderUpdates()
self.enemies_active_list = pygame.sprite.RenderUpdates() self.enemies_active_list = pygame.sprite.RenderUpdates()
self.enemy_factory = EnemyFactory()
self.player = None self.player = None
class Event_Spawn:
class Event: def __init__(self, enemy_type, pos):
def __init__(self):
self.time = 0 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): class Object(pygame.sprite.Sprite):
def __init__(self, frames, pos, speed): def __init__(self, frames, pos, speed):
@ -124,7 +130,7 @@ class Player(Object):
def shoot(self): def shoot(self):
""" create a bullet and add it to the global bullet list""" """ 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: if self.shoot_wavepos == 0:
self.shoot_wavepos = 1 self.shoot_wavepos = 1
if self.shoot_wavepos == 2: if self.shoot_wavepos == 2:
@ -166,6 +172,22 @@ class Player(Object):
else: else:
self.bullet_counter += 1 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): class Enemy(Object):
def __init__(self, frames, type, pos): def __init__(self, frames, type, pos):
Object.__init__(self, frames, pos, (-1, 0)) Object.__init__(self, frames, pos, (-1, 0))
@ -218,13 +240,46 @@ def init():
random.seed() random.seed()
#globals.event_type = {'create_enemeny' : create_enemey, 'vanish_object': vanish_object()} #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(): def create_level1(self):
""" phony level one""" """ phony level one"""
# create events # create events
# dict events string => function # dict events string => function
# for e in keys(globals.events_type): # 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)
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 pass
def wait_keypress(): def wait_keypress():
@ -320,6 +375,9 @@ wait_keypress()
nbframe = 0 nbframe = 0
# initialize level
level = Level()
# create_player # create_player
player = Player(['player1_1.bmp', 'player1_2.bmp'], (50, 50), (0, 0)) player = Player(['player1_1.bmp', 'player1_2.bmp'], (50, 50), (0, 0))
globals.player = player globals.player = player
@ -361,6 +419,9 @@ while not globals.die:
# pygame.display.flip() # pygame.display.flip()
nbframe = nbframe + 1 nbframe = nbframe + 1
# do level things
level.update(nbframe)
globals.clock.tick(60) globals.clock.tick(60)
for event in pygame.event.get(): for event in pygame.event.get():