combined bullet and bomb classes. added comments
This commit is contained in:
@@ -64,6 +64,8 @@ def read_buttons():
|
||||
#global variables. Sue me.
|
||||
game_score = 0
|
||||
|
||||
# A somewhat flexible BUZZ function. The buzzer has a fixed frequency, but by pulsing it repeatadly in short pulses,
|
||||
# some variety of sounds can be acheived
|
||||
def buzz_ms(time, repeat=1):
|
||||
while True:
|
||||
buzz.value(1)
|
||||
@@ -74,6 +76,7 @@ def buzz_ms(time, repeat=1):
|
||||
break
|
||||
sleep_ms(time)
|
||||
|
||||
# This is python, we might as well be classy. An Object class for bullets, to neatly handle the many bullets on screen
|
||||
class Bullet:
|
||||
pool = [] #keeps all bullets in one place
|
||||
def __init__(self):
|
||||
@@ -82,50 +85,32 @@ class Bullet:
|
||||
self.velocity = 0
|
||||
Bullet.pool.append(self)
|
||||
|
||||
def pick_from_pool():
|
||||
def launch(x, y, velocity):
|
||||
# Step 1: find an unused bullet instance in the bullet pool.
|
||||
# Unused bullets have their velocity set to 0, since its not a useful velocity
|
||||
for bullet in Bullet.pool:
|
||||
if bullet.velocity == 0:
|
||||
return bullet
|
||||
return None
|
||||
|
||||
def launch(x, y, velocity):
|
||||
bullet = Bullet.pick_from_pool()
|
||||
if bullet == None:
|
||||
return #no available bullets. Pls wait
|
||||
break # found one, use the current value of bullet
|
||||
bullet.x = x
|
||||
bullet.y = y
|
||||
bullet.velocity = velocity
|
||||
|
||||
# returns all active bullets shot by the player (going up, so negative y-speed)
|
||||
def get_all_player_bullets():
|
||||
return [bullet for bullet in Bullet.pool if bullet.velocity < 0]
|
||||
|
||||
class Bomb:
|
||||
pool = [] #keeps all bombs in one place
|
||||
def __init__(self):
|
||||
self.x = 0
|
||||
self.y = 0
|
||||
self.velocity = 0
|
||||
Bomb.pool.append(self)
|
||||
# returns all active bullets shot by the aliens (going down, so positive y-speed)
|
||||
def get_all_alien_bullets():
|
||||
return [bullet for bullet in Bullet.pool if bullet.velocity > 0]
|
||||
|
||||
def pick_from_pool():
|
||||
for bomb in Bomb.pool:
|
||||
if bomb.velocity == 0:
|
||||
return bomb
|
||||
return None
|
||||
|
||||
def launch(x, y, velocity):
|
||||
bomb = Bomb.pick_from_pool()
|
||||
if bomb == None:
|
||||
return #no available bullets. Pls wait
|
||||
bomb.x = x
|
||||
bomb.y = y
|
||||
bomb.velocity = velocity
|
||||
#pre-allocate 60 bullets statically. No new objects will be allocated at runtime. We ain't savages, we respect our RAM
|
||||
[Bullet() for i in range(0,60)]
|
||||
|
||||
#pre-allocate 8 bullets statically. No new objects will be allocated at runtime. We ain't savages, we respect our RAM
|
||||
[Bullet() for i in range(0,8)]
|
||||
# same for bombs but 32
|
||||
[Bomb() for i in range(0, 32)]
|
||||
|
||||
class Alien:
|
||||
pool = [] #keeps all aliens in one place
|
||||
digitCounter = 5
|
||||
digitCounter = 5 #for the "score" type alien, to know which ship is which digit of the players score
|
||||
|
||||
def __init__(self):
|
||||
self.x = 0
|
||||
@@ -142,13 +127,17 @@ class Alien:
|
||||
SCORE = 4 #looks like the score
|
||||
|
||||
def spawn(x, y, variant):
|
||||
alien = Alien.pick_from_pool()
|
||||
if alien == None:
|
||||
return #too many aliens! skip this one
|
||||
# Step 1: Find an "empty" instance in the pool
|
||||
for alien in Alien.pool:
|
||||
if alien.variant == 0:
|
||||
break # found one, use the current value of 'alien'
|
||||
|
||||
# Step 2: Update with data for the new Alien
|
||||
alien.x = x
|
||||
alien.y = y
|
||||
alien.variant = variant
|
||||
|
||||
# Step 3: load the graphics for this specific alien
|
||||
# alien.spritedata[0:9] = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00' #empty template
|
||||
|
||||
if variant == Alien.BIGBOX:
|
||||
@@ -171,12 +160,14 @@ class Alien:
|
||||
alien.sprite.text(str(alien.digit), 0, 0, 1)
|
||||
alien.point_value = 0
|
||||
|
||||
# Some aliens have their extra logic that needs to be updated every frame
|
||||
def update(self):
|
||||
# INVADER aliens have a 1/300 chance of dropping a bomb every frame!
|
||||
if self.variant == Alien.INVADER:
|
||||
# INVADER aliens have a 1/300 chance of dropping a bomb!
|
||||
if randrange(300) == 1:
|
||||
Bomb.launch(self.x+4, self.y+6, 1)
|
||||
|
||||
Bullet.launch(self.x+4, self.y+6, 1)
|
||||
|
||||
# SCORE aliens update their values to always represent the players score
|
||||
if self.variant == Alien.SCORE:
|
||||
global game_score
|
||||
self.sprite.fill(0)
|
||||
@@ -184,23 +175,20 @@ class Alien:
|
||||
self.sprite.text(str(digit), 0, 0, 1)
|
||||
self.point_value = digit*10 + 5
|
||||
|
||||
# when shot, call this. Returns score to be awarded to the player
|
||||
# when an alien is shot, call this. Returns score to be awarded to the player
|
||||
def shot(self):
|
||||
# variant 0 represents an inactive alien, so seting it essentially removes an alien
|
||||
self.variant = 0
|
||||
return self.point_value
|
||||
|
||||
def pick_from_pool():
|
||||
for alien in Alien.pool:
|
||||
if alien.variant == 0:
|
||||
return alien
|
||||
return None
|
||||
|
||||
# helper function to get a list of all active aliens. Useful to doing a for loop on all aliens
|
||||
def get_all_active():
|
||||
return [alien for alien in Alien.pool if alien.variant is not 0]
|
||||
|
||||
#pre-allocate 60 aliens
|
||||
[Alien() for i in range(0,60)]
|
||||
|
||||
# Setup the waves of aliens
|
||||
alien_wave = -1
|
||||
alien_waves = [Alien.UFO, Alien.SCORE, Alien.INVADER, Alien.UFO, Alien.INVADER, Alien.SCORE, Alien.INVADER, Alien.INVADER, Alien.SCORE, Alien.UFO, Alien.INVADER, Alien.INVADER, Alien.SCORE]
|
||||
|
||||
@@ -245,7 +233,7 @@ oled.show()
|
||||
while read_buttons() == 0:
|
||||
pass
|
||||
|
||||
# spawn the first 2 waves
|
||||
# spawn the first 2 waves
|
||||
spawn_alien_wave(12)
|
||||
spawn_alien_wave(0)
|
||||
|
||||
@@ -304,7 +292,7 @@ while game_is_not_over_yet:
|
||||
oled.hline(tank_x-2, 61, 5, 1)
|
||||
oled.fill_rect(tank_x-3, 62, 7, 2, 1)
|
||||
|
||||
#process aliens
|
||||
# process aliens
|
||||
alien_step_timer += 1
|
||||
if alien_step_timer >= alien_step_delay:
|
||||
alien_step_timer = 0
|
||||
@@ -347,29 +335,25 @@ while game_is_not_over_yet:
|
||||
game_is_not_over_yet = False
|
||||
|
||||
# Process all bullets (move and hit-detect)
|
||||
for bullet in Bullet.pool:
|
||||
if bullet.velocity is not 0:
|
||||
#move and check for collisions
|
||||
bullet.y += bullet.velocity
|
||||
if bullet.y < 0:
|
||||
for bullet in Bullet.get_all_player_bullets():
|
||||
# move and check for collisions
|
||||
bullet.y += bullet.velocity
|
||||
if bullet.y < 0:
|
||||
bullet.velocity = 0
|
||||
continue
|
||||
|
||||
# Hit detection bullet to alien
|
||||
for alien in Alien.get_all_active():
|
||||
if alien.sprite.pixel(bullet.x-alien.x, bullet.y-alien.y) == 1:
|
||||
game_score += alien.shot()
|
||||
bullet.velocity = 0
|
||||
buzz_ms(10)
|
||||
continue
|
||||
|
||||
# Hit detection bullet to alien
|
||||
for alien in Alien.get_all_active():
|
||||
if alien.sprite.pixel(bullet.x-alien.x, bullet.y-alien.y) == 1:
|
||||
game_score += alien.shot()
|
||||
bullet.velocity = 0
|
||||
buzz_ms(10)
|
||||
continue
|
||||
|
||||
oled.pixel(bullet.x, bullet.y, 1)
|
||||
|
||||
oled.pixel(bullet.x, bullet.y, 1)
|
||||
|
||||
# process all bombs dropped by aliens
|
||||
for bomb in Bomb.pool:
|
||||
if bomb.velocity == 0:
|
||||
continue
|
||||
|
||||
for bomb in Bullet.get_all_alien_bullets():
|
||||
bomb.y += bomb.velocity
|
||||
if(bomb.y > 64):
|
||||
bomb.velocity = 0
|
||||
|
||||
Reference in New Issue
Block a user