added enemy class
This commit is contained in:
parent
9477f8aa39
commit
4be9f64baa
1 changed files with 27 additions and 6 deletions
33
pyshoot.py
33
pyshoot.py
|
@ -25,6 +25,9 @@ class Globals:
|
|||
self.moving_objects = pygame.sprite.RenderUpdates()
|
||||
self.bullets_list = pygame.sprite.RenderUpdates()
|
||||
self.bulletfactory = BulletFactory()
|
||||
self.enemies_list = pygame.sprite.RenderUpdates()
|
||||
self.player = None
|
||||
|
||||
|
||||
class Event:
|
||||
def __init__(self):
|
||||
|
@ -158,9 +161,26 @@ class Player(Object):
|
|||
if self.bullet_counter == self.bullet_threshold:
|
||||
self.shoot()
|
||||
self.bullet_counter = 0
|
||||
else :
|
||||
else:
|
||||
self.bullet_counter += 1
|
||||
|
||||
class Enemy(Object):
|
||||
def __init__(self, frames, type, pos):
|
||||
Object.__init__(self, frames, pos, (-1, 0))
|
||||
self.counter = 0
|
||||
self.threshold = 10
|
||||
|
||||
def update(self):
|
||||
if self.counter == self.threshold:
|
||||
self.rect.move_ip(self.speed)
|
||||
self.count = 0
|
||||
else:
|
||||
self.counter += 1
|
||||
|
||||
def aim_player(self):
|
||||
""" compute speed vector to aim at current player position """
|
||||
|
||||
|
||||
def init_gfx():
|
||||
"""graphics initialisation"""
|
||||
|
||||
|
@ -181,11 +201,6 @@ def init():
|
|||
#globals.event_type = {'create_enemeny' : create_enemey, 'vanish_object': vanish_object()}
|
||||
|
||||
|
||||
|
||||
def create_enemy(type):
|
||||
""" create an enemy """
|
||||
pass
|
||||
|
||||
def create_level():
|
||||
""" phony level one"""
|
||||
|
||||
|
@ -217,6 +232,8 @@ def process_key(event, player):
|
|||
player.start_move((1, 0))
|
||||
elif event.key == K_x:
|
||||
player.start_shoot()
|
||||
elif event.key == K_e:
|
||||
globals.enemies_list.add(Enemy(['enemy1_1.bmp', 'enemy1_2.bmp'], 0, (500, 200)))
|
||||
elif event.key == K_SPACE:
|
||||
player.change_bullet_type()
|
||||
|
||||
|
@ -277,6 +294,7 @@ nbframe = 0
|
|||
|
||||
# create_player
|
||||
player = Player(['player1_1.bmp', 'player1_2.bmp'], (50, 50), (0, 0))
|
||||
globals.player = player
|
||||
|
||||
# background
|
||||
globals.background = pygame.image.load(os.path.join('data', 'background.bmp')).convert()
|
||||
|
@ -290,10 +308,12 @@ while not globals.die:
|
|||
# erase previously moved objects
|
||||
globals.moving_objects.clear(globals.screen, globals.background)
|
||||
globals.bullets_list.clear(globals.screen, globals.background)
|
||||
globals.enemies_list.clear(globals.screen, globals.background)
|
||||
|
||||
# update game logic
|
||||
globals.moving_objects.update()
|
||||
globals.bullets_list.update()
|
||||
globals.enemies_list.update()
|
||||
|
||||
# add player (if it's not already in the group). ugly, but ok for now.
|
||||
globals.moving_objects.add(player)
|
||||
|
@ -301,6 +321,7 @@ while not globals.die:
|
|||
# drawing
|
||||
rectlist = globals.moving_objects.draw(globals.screen)
|
||||
rectlist.extend(globals.bullets_list.draw(globals.screen))
|
||||
rectlist.extend(globals.enemies_list.draw(globals.screen))
|
||||
|
||||
pygame.display.update(rectlist)
|
||||
|
||||
|
|
Loading…
Reference in a new issue