diff --git a/how_to_play.jpg b/how_to_play.jpg index c2ada81..36d25b7 100644 Binary files a/how_to_play.jpg and b/how_to_play.jpg differ diff --git a/invader_types.jpg b/invader_types.jpg index 76d4a6d..7dc4f7b 100644 Binary files a/invader_types.jpg and b/invader_types.jpg differ diff --git a/main.py b/main.py index 844e7b3..827ab40 100644 --- a/main.py +++ b/main.py @@ -111,6 +111,8 @@ class Bullet: class Alien: pool = [] #keeps all aliens in one place digitCounter = 5 #for the "score" type alien, to know which ship is which digit of the players score + synchro_centre = 0 #x-coordinate of synchronous bombing centre point + synchro_timer = 0 #keeps time since last bomb drop, to synchronise all bombers def __init__(self): self.x = 0 @@ -121,11 +123,12 @@ class Alien: Alien.pool.append(self) # Alien types - BIGBOX = 1 #max legal size of alien - UFO = 2 #a simple easy to hit block - INVADER = 3 #a classic SPACE INVADERS shape - SCORE = 4 #looks like the score - + UFO = 1 #a simple easy to hit block + INVADER = 2 #a classic SPACE INVADERS shape + SCORE = 3 #looks like the score + SHIELD = 4 #has a shield + SYNCHRO = 6 #synchro-bomber + def spawn(x, y, variant): # Step 1: Find an "empty" instance in the pool for alien in Alien.pool: @@ -138,11 +141,6 @@ class Alien: 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: - alien.sprite.fill_rect(0, 0, 9, 8, 1) - if variant == Alien.UFO: alien.spritedata[0:9] = b'\x20\x70\x70\x7C\x74\x7C\x70\x70\x20' alien.point_value = 5 @@ -159,6 +157,20 @@ class Alien: alien.sprite.fill(0) alien.sprite.text(str(alien.digit), 0, 0, 1) alien.point_value = 0 + + if variant == Alien.SHIELD: + # sprite data is in the UPDATE function for this alien + alien.shield = 35 + alien.point_value = 20 + + if variant == Alien.SYNCHRO: + alien.spritedata[0:9] = b'\x05\x62\x7A\xEE\xFE\xEE\x7A\x62\x05' + alien.point_value = 30 + alien.dropped_already = True + # When a new line of Synchro Bombers spawns, the timer is set to nearly-ready-to-drop + if Alien.synchro_timer < 350: + Alien.synchro_timer = 350 + # Some aliens have their extra logic that needs to be updated every frame def update(self): @@ -174,9 +186,29 @@ class Alien: digit = game_score // 10**self.digit % 10 self.sprite.text(str(digit), 0, 0, 1) self.point_value = digit*10 + 5 + + if self.variant == Alien.SHIELD: + # Check if the shield is charged, to draw the appropriate sprite + if self.shield >= 35: + self.spritedata[0:9] = b'\x80\x8E\xAD\xBD\xAF\xBD\xAD\x8E\x80' + else: + self.spritedata[0:9] = b'\x00\x0E\x2D\x3D\x2F\x3D\x2D\x0E\x00' + self.shield += 1 + + if self.variant == Alien.SYNCHRO: + # check if there is something to synchro with + if self.x > Alien.synchro_centre - Alien.synchro_timer and self.x < Alien.synchro_centre + Alien.synchro_timer and self.dropped_already == False: + self.dropped_already = True + Bullet.launch(self.x+4, self.y+6, 1) + # when an alien is shot, call this. Returns score to be awarded to the player def shot(self): + # If the shot alien has a shield, remove the shield and do nothing else + if self.variant == Alien.SHIELD and self.shield >= 35: + self.shield = 0 + return 0 + # variant 0 represents an inactive alien, so seting it essentially removes an alien self.variant = 0 return self.point_value @@ -184,22 +216,30 @@ class Alien: # 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] + + # get all aliens of a given variant + def get_all_of_variant(variant): + return [alien for alien in Alien.pool if alien.variant == variant] -#pre-allocate 60 aliens +# Pre-allocate 60 aliens [Alien() for i in range(0,60)] -# Setup the waves of aliens +# Spawning Aliens 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] - def spawn_alien_wave(x=0, y=0, alien_type=None): global alien_wave - global alien_waves + allowed_aliens = [Alien.UFO, Alien.INVADER, Alien.SHIELD, Alien.SYNCHRO] if alien_type is None: alien_wave += 1 - alien_type = alien_waves[alien_wave % len(alien_waves)] + # Every third wave is a SCORE wave + if alien_wave % 3 == 0: + alien_type = Alien.SCORE + # Remaining waves are at random, but based on current difficulty. Harder aliens come later + else: + # randomize a numberfrom 1 to difficulty+1 and spawn the most "expensive" alien that number affords + alien_type = max([a for a in allowed_aliens if a <= randrange(difficulty)+1]) - if alien_type in [Alien.UFO, Alien.INVADER]: + if alien_type in [Alien.UFO, Alien.INVADER, Alien.SHIELD, Alien.SYNCHRO]: for i in range(0, 8): Alien.spawn(i*13 +x, y, alien_type) @@ -223,7 +263,7 @@ shoot_right_was_released = True game_is_not_over_yet = True ammo = 6 timer = 0 -difficulty = 0 +difficulty = 1 debug_value = 0 # Draw START screen @@ -242,7 +282,6 @@ while read_buttons() == 0: spawn_alien_wave(0, 0, Alien.SCORE) spawn_alien_wave(0, 12, Alien.UFO) - # main loop while game_is_not_over_yet: # useful game timer @@ -284,13 +323,13 @@ while game_is_not_over_yet: cp_on((ammo + 8)%12) # fancy math to select which LED represents ammo count # time-based difficulty increment - if timer%800 == 0: + if timer%700 == 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: + # then, they start moving DOWN faster as well, every third difficulty increase + elif difficulty %3 == 0 and alien_descent_rate < 8: alien_descent_rate += 1 # Draw new frame, start from blank @@ -342,6 +381,9 @@ while game_is_not_over_yet: # If all aliens were shot down, and we have an empty screen: if len(Alien.get_all_active())==0: + # reward the player for clearing the screen with some points and a beep + game_score += 50 + buzz_ms(10,3); # Switch direction, reset offset, spawn a new row! alien_step_direction *= 1 alien_armada_offset = 0 if alien_step_direction >0 else 28 @@ -358,6 +400,14 @@ while game_is_not_over_yet: alien.update() oled.blit(alien.sprite, alien.x, alien.y, 0) + # Synchro-bomber aliens have common logic that also updates once per frame + Alien.synchro_timer += 3 + if Alien.synchro_timer > 400 and randrange(50) == 1: + Alien.synchro_timer = 0 + Alien.synchro_centre = randrange(80) + 24 #randomize a mid-point for the synchro-bombing + for alien in Alien.get_all_of_variant(Alien.SYNCHRO): + alien.dropped_already = False + # Check for collision between aliens and player tank # only four points on the tank are checked - the corners for alien in Alien.get_all_active(): @@ -398,7 +448,7 @@ while game_is_not_over_yet: oled.fill_rect(bomb.x, bomb.y, 2, 3, 1) # Debug text - # oled.text(str(difficulty) + " " + str(alien_step_delay) + " " + str(alien_descent_rate), 2, 52, 1) + #oled.text(str(difficulty), 2, 52, 1) # send updated image to display oled.show() @@ -416,4 +466,3 @@ oled.text("GAME OVER", 25, 17, 1) oled.text(str(game_score) + "pts", 25, 32, 1) oled.show() -