combined bullet and bomb classes. added comments

This commit is contained in:
Marek Baczynski
2026-11-27 23:47:07 +01:00
parent b952fd79bd
commit b825b7fd12
+36 -52
View File
@@ -64,6 +64,8 @@ def read_buttons():
#global variables. Sue me. #global variables. Sue me.
game_score = 0 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): def buzz_ms(time, repeat=1):
while True: while True:
buzz.value(1) buzz.value(1)
@@ -74,6 +76,7 @@ def buzz_ms(time, repeat=1):
break break
sleep_ms(time) 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: class Bullet:
pool = [] #keeps all bullets in one place pool = [] #keeps all bullets in one place
def __init__(self): def __init__(self):
@@ -82,50 +85,32 @@ class Bullet:
self.velocity = 0 self.velocity = 0
Bullet.pool.append(self) 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: for bullet in Bullet.pool:
if bullet.velocity == 0: if bullet.velocity == 0:
return bullet break # found one, use the current value of bullet
return None
def launch(x, y, velocity):
bullet = Bullet.pick_from_pool()
if bullet == None:
return #no available bullets. Pls wait
bullet.x = x bullet.x = x
bullet.y = y bullet.y = y
bullet.velocity = velocity bullet.velocity = velocity
class Bomb: # returns all active bullets shot by the player (going up, so negative y-speed)
pool = [] #keeps all bombs in one place def get_all_player_bullets():
def __init__(self): return [bullet for bullet in Bullet.pool if bullet.velocity < 0]
self.x = 0
self.y = 0
self.velocity = 0
Bomb.pool.append(self)
def pick_from_pool(): # returns all active bullets shot by the aliens (going down, so positive y-speed)
for bomb in Bomb.pool: def get_all_alien_bullets():
if bomb.velocity == 0: return [bullet for bullet in Bullet.pool if bullet.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 8 bullets statically. No new objects will be allocated at runtime. We ain't savages, we respect our RAM #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,8)] [Bullet() for i in range(0,60)]
# same for bombs but 32
[Bomb() for i in range(0, 32)]
class Alien: class Alien:
pool = [] #keeps all aliens in one place 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): def __init__(self):
self.x = 0 self.x = 0
@@ -142,13 +127,17 @@ class Alien:
SCORE = 4 #looks like the score SCORE = 4 #looks like the score
def spawn(x, y, variant): def spawn(x, y, variant):
alien = Alien.pick_from_pool() # Step 1: Find an "empty" instance in the pool
if alien == None: for alien in Alien.pool:
return #too many aliens! skip this one 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.x = x
alien.y = y alien.y = y
alien.variant = variant 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 # alien.spritedata[0:9] = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00' #empty template
if variant == Alien.BIGBOX: if variant == Alien.BIGBOX:
@@ -171,12 +160,14 @@ class Alien:
alien.sprite.text(str(alien.digit), 0, 0, 1) alien.sprite.text(str(alien.digit), 0, 0, 1)
alien.point_value = 0 alien.point_value = 0
# Some aliens have their extra logic that needs to be updated every frame
def update(self): def update(self):
# INVADER aliens have a 1/300 chance of dropping a bomb every frame!
if self.variant == Alien.INVADER: if self.variant == Alien.INVADER:
# INVADER aliens have a 1/300 chance of dropping a bomb!
if randrange(300) == 1: 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: if self.variant == Alien.SCORE:
global game_score global game_score
self.sprite.fill(0) self.sprite.fill(0)
@@ -184,23 +175,20 @@ class Alien:
self.sprite.text(str(digit), 0, 0, 1) self.sprite.text(str(digit), 0, 0, 1)
self.point_value = digit*10 + 5 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): def shot(self):
# variant 0 represents an inactive alien, so seting it essentially removes an alien
self.variant = 0 self.variant = 0
return self.point_value return self.point_value
def pick_from_pool(): # helper function to get a list of all active aliens. Useful to doing a for loop on all aliens
for alien in Alien.pool:
if alien.variant == 0:
return alien
return None
def get_all_active(): def get_all_active():
return [alien for alien in Alien.pool if alien.variant is not 0] return [alien for alien in Alien.pool if alien.variant is not 0]
#pre-allocate 60 aliens #pre-allocate 60 aliens
[Alien() for i in range(0,60)] [Alien() for i in range(0,60)]
# Setup the waves of aliens
alien_wave = -1 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] 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]
@@ -304,7 +292,7 @@ while game_is_not_over_yet:
oled.hline(tank_x-2, 61, 5, 1) oled.hline(tank_x-2, 61, 5, 1)
oled.fill_rect(tank_x-3, 62, 7, 2, 1) oled.fill_rect(tank_x-3, 62, 7, 2, 1)
#process aliens # process aliens
alien_step_timer += 1 alien_step_timer += 1
if alien_step_timer >= alien_step_delay: if alien_step_timer >= alien_step_delay:
alien_step_timer = 0 alien_step_timer = 0
@@ -347,9 +335,8 @@ while game_is_not_over_yet:
game_is_not_over_yet = False game_is_not_over_yet = False
# Process all bullets (move and hit-detect) # Process all bullets (move and hit-detect)
for bullet in Bullet.pool: for bullet in Bullet.get_all_player_bullets():
if bullet.velocity is not 0: # move and check for collisions
#move and check for collisions
bullet.y += bullet.velocity bullet.y += bullet.velocity
if bullet.y < 0: if bullet.y < 0:
bullet.velocity = 0 bullet.velocity = 0
@@ -366,10 +353,7 @@ while game_is_not_over_yet:
oled.pixel(bullet.x, bullet.y, 1) oled.pixel(bullet.x, bullet.y, 1)
# process all bombs dropped by aliens # process all bombs dropped by aliens
for bomb in Bomb.pool: for bomb in Bullet.get_all_alien_bullets():
if bomb.velocity == 0:
continue
bomb.y += bomb.velocity bomb.y += bomb.velocity
if(bomb.y > 64): if(bomb.y > 64):
bomb.velocity = 0 bomb.velocity = 0