Difficulty balance and fix spawn bug

Reworked difficulty progression. Fixed bug where aliens could spawn in a fast loop overrunning the player. After these fixes current highscore is 2355pts
This commit is contained in:
Marek Baczynski
2026-11-30 01:50:38 +01:00
parent fde19150cd
commit 11e475c9b1
+70 -48
View File
@@ -192,7 +192,7 @@ class Alien:
alien_wave = -1 alien_wave = -1
alien_waves = [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.INVADER, Alien.UFO, Alien.INVADER, Alien.SCORE, Alien.INVADER, Alien.INVADER, Alien.SCORE, Alien.UFO, Alien.INVADER, Alien.INVADER, Alien.SCORE]
def spawn_alien_wave(y=0, alien_type=None): def spawn_alien_wave(x=0, y=0, alien_type=None):
global alien_wave global alien_wave
global alien_waves global alien_waves
if alien_type is None: if alien_type is None:
@@ -201,28 +201,30 @@ def spawn_alien_wave(y=0, alien_type=None):
if alien_type in [Alien.UFO, Alien.INVADER]: if alien_type in [Alien.UFO, Alien.INVADER]:
for i in range(0, 8): for i in range(0, 8):
Alien.spawn(i*13, y, alien_type) Alien.spawn(i*13 +x, y, alien_type)
if alien_type == Alien.SCORE: if alien_type == Alien.SCORE:
for i in range(0, 6): for i in range(0, 6):
Alien.spawn(i*9 + 23, y, Alien.SCORE) Alien.spawn(i*9 +23 +x, y, Alien.SCORE)
Alien.spawn(0, y, Alien.INVADER) Alien.spawn(0 +x, y, Alien.INVADER)
Alien.spawn(11, y, Alien.INVADER) Alien.spawn(11 +x, y, Alien.INVADER)
Alien.spawn(80, y, Alien.INVADER) Alien.spawn(80 +x, y, Alien.INVADER)
Alien.spawn(91, y, Alien.INVADER) Alien.spawn(91 +x, y, Alien.INVADER)
# game variables # game variables
tank_x = 63 tank_x = 63 # position of players tank
alien_step_delay = 8 alien_step_delay = 6 # every this many frames, aliens take a step
alien_step_timer = 0 alien_step_timer = 0 # counts the frames for aliens
alien_step_direction = 1 alien_step_direction = 1 # direction (left or right) for aliens
alien_armada_offset = 0 alien_armada_offset = 0 # x-position of aliens, tracked so that new rows spawn in-line with exisitng ones
game_progress = 0 alien_descent_rate = 4 # how far the aliens move down, whenever they move down
shoot_left_was_released = True shoot_left_was_released = True
shoot_right_was_released = True shoot_right_was_released = True
game_is_not_over_yet = True game_is_not_over_yet = True
ammo = 6 ammo = 6
timer = 0 timer = 0
difficulty = 0
debug_value = 0
# Draw START screen # Draw START screen
oled.fill(0) oled.fill(0)
@@ -237,12 +239,13 @@ while read_buttons() == 0:
pass pass
# spawn the first 2 waves # spawn the first 2 waves
spawn_alien_wave(0, Alien.SCORE) spawn_alien_wave(0, 0, Alien.SCORE)
spawn_alien_wave(12, Alien.UFO) spawn_alien_wave(0, 12, Alien.UFO)
# main loop # main loop
while game_is_not_over_yet: while game_is_not_over_yet:
# useful game timer
timer += 1 timer += 1
# Button logic # Button logic
@@ -272,74 +275,90 @@ while game_is_not_over_yet:
else: else:
shoot_right_was_released = True shoot_right_was_released = True
# Check if all aliens dead, if so, advance the game
if len(Alien.get_all_active()) == 0:
spawn_alien_wave()
alien_step_direction = 1
buzz_ms(2,4)
game_progress = (game_progress//2+2)*2 #don't ask
# slow gun recharge: # slow gun recharge:
if timer%20 == 0 and ammo < 6: if timer%20 == 0 and ammo < 6:
ammo += 1 ammo += 1
buzz_ms(1,2) buzz_ms(1,2)
cp_off() cp_off()
cp_on((ammo + 8)%12) # don't ask cp_on((ammo + 8)%12) # fancy math to select which LED represents ammo count
# Draw new frame # time-based difficulty increment
if timer%800 == 0:
difficulty += 1
# at first, the aliens accelerate in x-direction
if alien_step_delay > 3:
alien_step_delay -= 1
# then, they start moving DOWN faster as well!
elif alien_descent_rate < 8:
alien_descent_rate += 1
# Draw new frame, start from blank
oled.fill(0); oled.fill(0);
# draw the tank # Draw the tank
oled.pixel(tank_x-1, 60, 1) oled.pixel(tank_x-1, 60, 1)
oled.pixel(tank_x+1, 60, 1) oled.pixel(tank_x+1, 60, 1)
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 stepping. They take a step every couple of frames
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
# check for direction change, each direction coded separatly
switching_direction = False switching_direction = False
if alien_step_direction > 0:
for alien in Alien.get_all_active(): for alien in Alien.get_all_active():
# switch direction if an alien hits a side wall if alien.x > 118:
if alien.x + alien_step_direction> 119 or alien.x + alien_step_direction < 0:
switching_direction = True switching_direction = True
break break
if alien_armada_offset > 40:
alien_armada_offset = 28
switching_direction = True
# BUT, shooting the aliens on the edge can only postpone the advancing to the next line by the width of 1 alien if alien_step_direction < 0:
if alien_armada_offset + alien_step_direction > 41 or alien_armada_offset + alien_step_direction < -13: for alien in Alien.get_all_active():
alien_armada_offset -= alien_step_direction * 13 if alien.x < 1:
switching_direction = True
break
if alien_armada_offset < -12:
alien_armada_offset = 0
switching_direction = True switching_direction = True
if switching_direction: if switching_direction:
# aliens turn around and go down by one line # Aliens turn around and go down by one line
alien_step_direction *= -1 alien_step_direction *= -1
for alien in Alien.get_all_active(): for alien in Alien.get_all_active():
alien.y += 6 alien.y += alien_descent_rate
if(alien.y > 58): #aliens reached planet Bell - GAME OVER! if(alien.y > 58): #aliens reached planet Bell - GAME OVER!
game_is_not_over_yet = False game_is_not_over_yet = False
buzz_ms(2,4) buzz_ms(2,4)
# check for other game progression effects
game_progress += 1
# every other line, we spawn new aliens
if game_progress % 2 == 0:
spawn_alien_wave()
# complex math to gradually increase game speed
if (12-game_progress)**2 > alien_step_delay and alien_step_delay > 3:
alien_step_delay -= 1
else: else:
alien_armada_offset += alien_step_direction alien_armada_offset += alien_step_direction
for alien in Alien.get_all_active(): for alien in Alien.get_all_active():
alien.x += alien_step_direction alien.x += alien_step_direction
# If all aliens were shot down, and we have an empty screen:
if len(Alien.get_all_active())==0:
# Switch direction, reset offset, spawn a new row!
alien_step_direction *= 1
alien_armada_offset = 0 if alien_step_direction >0 else 28
spawn_alien_wave(alien_armada_offset, 0)
# Otherwise, if there are aliens on screen, check if there is room for a new row:
else:
highest_alien = min([alien.y for alien in Alien.get_all_active() if alien.y < 23])
# the alien.y<23 filter is a patch for the odd case where player manages to shoot top level or aliens without clearing the entire screen
if highest_alien >= 12:
spawn_alien_wave(0 if alien_step_direction > 0 else 28, highest_alien-12)
# Every frame, update all aliens that have extra animation/logic
for alien in Alien.get_all_active(): for alien in Alien.get_all_active():
alien.update() alien.update()
oled.blit(alien.sprite, alien.x, alien.y, 0) oled.blit(alien.sprite, alien.x, alien.y, 0)
# check for collision between aliens and player tank # Check for collision between aliens and player tank
# only four points on the tank are checked - the corners # only four points on the tank are checked - the corners
for alien in Alien.get_all_active(): for alien in Alien.get_all_active():
if any([alien.sprite.pixel(tank_x-3-alien.x, 60-alien.y), alien.sprite.pixel(tank_x+3-alien.x, 60-alien.y), alien.sprite.pixel(tank_x-3-alien.x, 62-alien.y), alien.sprite.pixel(tank_x+3-alien.x, 62-alien.y)]): if any([alien.sprite.pixel(tank_x-3-alien.x, 60-alien.y), alien.sprite.pixel(tank_x+3-alien.x, 60-alien.y), alien.sprite.pixel(tank_x-3-alien.x, 62-alien.y), alien.sprite.pixel(tank_x+3-alien.x, 62-alien.y)]):
@@ -364,7 +383,7 @@ while game_is_not_over_yet:
# draw the bullet (just 1 pixel) # draw the bullet (just 1 pixel)
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 Bullet.get_all_alien_bullets(): for bomb in Bullet.get_all_alien_bullets():
bomb.y += bomb.velocity bomb.y += bomb.velocity
if(bomb.y > 64): if(bomb.y > 64):
@@ -375,11 +394,13 @@ while game_is_not_over_yet:
if bomb.y>58 and bomb.x > tank_x-4 and bomb.x < tank_x + 3: if bomb.y>58 and bomb.x > tank_x-4 and bomb.x < tank_x + 3:
game_is_not_over_yet = False game_is_not_over_yet = False
# draw the bomb (2x3 rectangle) # Draw the bomb (2x3 rectangle)
oled.fill_rect(bomb.x, bomb.y, 2, 3, 1) oled.fill_rect(bomb.x, bomb.y, 2, 3, 1)
#debug text # Debug text
#oled.text(str(alien_armada_offset), 2, 52, 1) # oled.text(str(difficulty) + " " + str(alien_step_delay) + " " + str(alien_descent_rate), 2, 52, 1)
# send updated image to display
oled.show() oled.show()
# outside of the loop, this is the GAME OVER SCREEN # outside of the loop, this is the GAME OVER SCREEN
@@ -395,3 +416,4 @@ oled.text("GAME OVER", 25, 17, 1)
oled.text(str(game_score) + "pts", 25, 32, 1) oled.text(str(game_score) + "pts", 25, 32, 1)
oled.show() oled.show()