def update(self, frame): super().update() if self.dead: if stage.collide(self.x + 3, self.y + 1, self.x + 13, self.y + 15, hero.x + 3, hero.y + 1, hero.x + 13, hero.y + 15): self.move(-16, -16) return sprite.set_frame(15, (frame // 4) * 4) bottom_tile = layer1.tile((self.x + 8) // 16, (self.y + 16) // 16) forward_tile = layer1.tile((self.x + 8 + 8 * self.dx) // 16, (self.y + 8) // 16) if bottom_tile != 0 or forward_tile == 0: self.dx = -self.dx self.move(self.x + self.dx, self.y) if stage.collide(self.x + 3, self.y + 1, self.x + 13, self.y + 15, bolt.x + 8, bolt.y + 8): self.kill() bolt.kill() if stage.collide(self.x + 3, self.y + 1, self.x + 13, self.y + 15, hero.x + 3, hero.y + 1, hero.x + 13, hero.y + 15): hero.kill()
def update(self): super().update() if self.y >= 128: return if self.boom: if self.boom == 1: sound.play(boom_sound) self.set_frame(12 + self.boom, 0) self.boom += 1 if self.boom > 4: self.boom = 0 ship.dead = True self.move(self.x, 128) return self.move(self.x, self.y + 8) self.set_frame(6, 0 if self.y % 16 else 4) if stage.collide(self.x + 4, self.y + 4, self.x + 12, self.y + 12, ship.x + 4, ship.y + 4, ship.x + 12, ship.y + 12): self.boom = 1
def game_scene(): # this function sets the background #buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] #get sound ready pew_sound = open("pew.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) boom_sound = open("boom.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # an image bank for CircuitPython image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp") def show_alien(): # I know this is a function that is using variables outside of itself: # But this code is going to be used in 2 places # make an alien show up on screen in the x-axis for alien_number in range(len(aliens)): if aliens[alien_number].x < 0: aliens[alien_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) # aliens[alien_number].move(50, 50) #print("here") break #sets the background to image 0 in the bank background = stage.Grid(image_bank_1, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) # a list of sprites that will be updated every frame sprites = [] # parameters (image_bank_1, image # in bank, x, y) ship = stage.Sprite(image_bank_1, 4, int(constants.SCREEN_X / 2), int(constants.SCREEN_Y - constants.SPRITE_SIZE)) sprites.append(ship) # insert at top of sprite list # create lasers for when we shoot lasers = [] for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS): a_single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers.append(a_single_laser) # create aliens aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_alien = stage.Sprite(image_bank_1, 7, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) aliens.append(a_single_alien) # current number of aliens that should be moving down screen, start with just 1 alien_count = 1 show_alien() # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, constants.FPS) # set the background layer game.layers = sprites + lasers + aliens + [background] # render the background # most likely you will only render background once per scene game.render_block() while True: # get user inputs keys = ugame.buttons.get_pressed() # print(keys) if keys & ugame.K_X != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if keys & ugame.K_UP != 0: if ship.y < 0: ship.move(ship.x, 0) else: ship.move(ship.x, ship.y - constants.SHIP_SPEED) pass if keys & ugame.K_DOWN != 0: if ship.y > constants.SCREEN_Y - constants.SCREEN_GRID_Y: ship.move(ship.x, constants.SCREEN_Y - constants.SPRITE_SIZE) else: ship.move(ship.x, ship.y + constants.SHIP_SPEED) pass if keys & ugame.K_LEFT != 0: if ship.x < 0: ship.move(0, ship.y) else: ship.move(ship.x - constants.SHIP_SPEED, ship.y) pass if keys & ugame.K_RIGHT != 0: if ship.x > constants.SCREEN_X - constants.SCREEN_GRID_X: ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y) else: ship.move(ship.x + constants.SHIP_SPEED, ship.y) pass # update game logic # play sound if A is pressed if a_button == constants.button_state["button_just_pressed"]: sound.play(pew_sound) # if A Button (fire) is pressed if a_button == constants.button_state["button_just_pressed"]: # fire a laser, if we have enough power (meaning we have used up all the lasers) for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(ship.x, ship.y) sound.stop() sound.play(pew_sound) break # each frame move the lasers, that have been fired, up for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # each frame move the aliens down the screen for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: aliens[alien_number].move( aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide(lasers[laser_number].x + 6, lasers[laser_number].y + 2, lasers[laser_number].x + 11, lasers[laser_number].y + 12, aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15): aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boom_sound) show_alien() show_alien() alien_count + alien_count + 1 # redraw sprite list game.render_sprites(sprites + lasers + aliens) game.tick() # wait until refresh rate finishes
def game_scene(): image_bank_1 = stage.Bank.from_bmp16("ball.bmp") sprites = [] image_bank_2 = stage.Bank.from_bmp16("space_aliens.bmp") sprites = [] attack = [] for attack_number in range(constants.TOTAL_ATTACKS): a_single_attack = stage.Sprite(image_bank_1, 2, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) attack.append(a_single_attack) enemy = [] for enemy_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_enemy = stage.Sprite(image_bank_2, 9, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) enemy.append(a_single_enemy) def show_enemy(): for enemy_number in range(len(enemy)): if enemy[enemy_number].x < 20: enemy[enemy_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break enemy_count = 10 show_enemy() score = 0 text = [] score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) text.append(score_text) # this sets the background a_button = constants.button_state["button_up"] pew_sound = open("pew.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) boom_sound = open("boom.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) coin_sound = open("coin.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) pixels = neopixel.NeoPixel(board.NEOPIXEL, 5, auto_write=False) for pixel_number in range(0, 5): pixels[pixel_number] = (0, 10, 0) pixels.show() # this is the background background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) # create a sprite # parameters (image_bank_1, image # in bank, x, y) ball_one = stage.Sprite(image_bank_1, 3, 64, 56) sprites.append(ball_one) game = stage.Stage(ugame.display, constants.FPS) game.layers = sprites + attack + enemy + text + [background] game.render_block() while True: # get user inputs keys = ugame.buttons.get_pressed() # print(keys) if keys & ugame.K_X != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if keys & ugame.K_UP != 0: if ball_one.y < 0: ball_one.move(ball_one.x, 0) else: ball_one.move(ball_one.x, ball_one.y - constants.SPRITE_MOVEMENT_SPEED) pass if keys & ugame.K_DOWN != 0: if ball_one.y > constants.SCREEN_Y - constants.SCREEN_GRID_Y: ball_one.move(ball_one.x, constants.SCREEN_Y - constants.SPRITE_SIZE) else: ball_one.move(ball_one.x, ball_one.y + constants.SPRITE_MOVEMENT_SPEED) pass if keys & ugame.K_LEFT != 0: if ball_one.x < 0: ball_one.move(0, ball_one.y) else: ball_one.move(ball_one.x - constants.SPRITE_MOVEMENT_SPEED, ball_one.y) pass if keys & ugame.K_RIGHT != 0: if ball_one.x > constants.SCREEN_X - constants.SCREEN_GRID_X: ball_one.move(constants.SCREEN_X - constants.SPRITE_SIZE, ball_one.y) else: ball_one.move(ball_one.x + constants.SPRITE_MOVEMENT_SPEED, ball_one.y) pass # update game logic if a_button == constants.button_state["button_just_pressed"]: for attack_number in range(len(attack)): if attack[attack_number].x < 0: attack[attack_number].move(ball_one.x, ball_one.y) sound.play(pew_sound) break for attack_number in range(len(attack)): if attack[attack_number].x > 0: attack[attack_number].move( attack[attack_number].x, attack[attack_number].y - constants.ATTACK_SPEED) if attack[attack_number].y < constants.OFF_TOP_SCREEN: attack[attack_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) for enemy_number in range(len(enemy)): if enemy[enemy_number].x > 0: enemy[enemy_number].move( enemy[enemy_number].x, enemy[enemy_number].y + constants.ALIEN_SPEED) if enemy[enemy_number].y > constants.SCREEN_Y: enemy[enemy_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_enemy() for attack_number in range(len(attack)): if attack[attack_number].x > 0: for enemy_number in range(len(enemy)): if enemy[enemy_number].x > 0: if stage.collide(enemy[enemy_number].x + 1, enemy[enemy_number].y, enemy[enemy_number].x + 15, enemy[enemy_number].y + 15, attack[attack_number].x + 6, attack[attack_number].y + 2, attack[attack_number].x + 11, attack[attack_number].y + 12): # when you hit an alien enemy[enemy_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) attack[attack_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) game.render_block() sound.stop() sound.play(boom_sound) show_enemy() show_enemy() enemy_count = enemy_count + 1 for enemy_number in range(len(enemy)): if enemy[enemy_number].x > 0: # https://circuitpython-stage.readthedocs.io/en/latest/#stage.collide # and https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other if stage.collide(enemy[enemy_number].x + 1, enemy[enemy_number].y, enemy[enemy_number].x + 15, enemy[enemy_number].y + 15, ball_one.x, ball_one.y, ball_one.x + 15, ball_one.y + 15): # alien hit the ship sound.stop() sound.play(coin_sound) for pixel_number in range(0, 5): pixels[pixel_number] = (25, 0, 25) pixels.show() # Wait for 1 seconds time.sleep(1.0) # need to release the NeoPixels pixels.deinit() sound.stop() game_over_scene(score) # redraw sprite list game.render_sprites(sprites + attack + enemy) game.tick() # wait until refresh rate finishes
def game_scene(): # this function is the main game scene score = 0 score_text = stage.Text(width=29, height=14) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) def show_alien(): for alien_number in range(len(aliens)): if aliens[alien_number].x < 0: aliens[alien_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break # image bank image_bank_background = stage.Bank.from_bmp16( "space_aliens_background.bmp") image_bank_sprites = stage.Bank.from_bmp16("space_aliens.bmp") # buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # get sound ready pew_sound = open("pew.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) boom_sound = open("boom.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) crash_sound = open("crash.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # set background to img 0 and 10x8 tiles of size 16x16 background = stage.Grid(image_bank_background, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) # sprite that will be updated every frame ship = stage.Sprite(image_bank_sprites, 5, 75, constants.SCREEN_Y - (2 * constants.SPRITE_SIZE)) aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_alien = stage.Sprite(image_bank_sprites, 9, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) aliens.append(a_single_alien) show_alien() lasers = [] for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS): a_single_laser = stage.Sprite(image_bank_sprites, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers.append(a_single_laser) # creates stage and sets it to 60fps game = stage.Stage(ugame.display, constants.FPS) # sets the layers of all sprites, in order game.layers = [score_text] + lasers + [ship] + aliens + [background] # renders all sprites, only once game.render_block() # forever game loop while True: # get user input keys = ugame.buttons.get_pressed() # A button to fire if keys & ugame.K_O != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if keys & ugame.K_X != 0: pass if keys & ugame.K_START != 0: pass if keys & ugame.K_SELECT != 0: pass if keys & ugame.K_RIGHT != 0: if ship.x <= constants.SCREEN_X - constants.SPRITE_SIZE: ship.move(ship.x + constants.SPRITE_MOVEMENT_SPEED, ship.y) else: ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y) if keys & ugame.K_LEFT != 0: if ship.x >= 0: ship.move(ship.x - constants.SPRITE_MOVEMENT_SPEED, ship.y) else: ship.move(0, ship.y) if keys & ugame.K_UP != 0: pass if keys & ugame.K_DOWN != 0: pass # update game logic if a_button == constants.button_state["button_just_pressed"]: for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(ship.x, ship.y) sound.play(pew_sound) break for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: aliens[alien_number].move( aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() score -= 1 if score < 0: score = 0 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide(lasers[laser_number].x + 6, lasers[laser_number].y + 2, lasers[laser_number].x + 11, lasers[laser_number].y + 12, aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15): aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boom_sound) show_alien() show_alien() score = score + 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide(aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15, ship.x, ship.y, ship.x + 15, ship.y + 15): # alien hit the ship sound.stop() sound.play(crash_sound) time.sleep(3.0) game_over_scene(score) # redraw Sprite game.render_sprites(aliens + lasers + [ship]) game.tick()
# ugame.K_LEFT will be true if the left button is pressed elif keys & ugame.K_LEFT: dx = MOVE_SPEED # ugame.K_UP will be true if the up button is pressed if keys & ugame.K_UP: dy = MOVE_SPEED # ugame.K_DOWN will be true if the down button is pressed elif keys & ugame.K_DOWN: dy = -MOVE_SPEED # Check if Blinka hits an enemy (causes Game Over) for sprite in enemy_sprites: collision = stage.collide(ax0=blinka.x, ay0=blinka.y, ax1=blinka.x + 16, ay1=blinka.y + 16, bx0=sprite.x + 4 + dx, # Enemy is narrow, move left edge 4 pixels towards middle by0=sprite.y, bx1=sprite.x + dx + 12, # Enemy is narrow, move right edge 4 pixels towards middle by1=sprite.y + 16) # If there is an enemy collision, the game is over if collision: game_state = 'lose' # Display the message text.text("Game Over!") # Re-render block to get text to show game.render_block() # Check if Blinka reached the goal (causes Win)
def game_scene(): # this function is the game scene score = 0 text = [] all_the_bricks = [] both_paddles = [] ball_sprite = [] # an image bank for CircuitPython image_bank_2 = stage.Bank.from_bmp16("break_out.bmp") background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) ball = stage.Sprite(image_bank_2, 1, 74, 56) sprites.insert(0, ball) # insert at the top of sprite list paddle1 = stage.Sprite(image_bank_2, 2, 74, 100) sprites.insert(0, paddle1) # insert at the top of sprite list paddle2 = stage.Sprite(image_bank_2, 2, 90, 100) sprites.insert(0, paddle2) # insert at the top of sprite list brick1 = stage.Sprite(image_bank_2, 5, 90, 40) all_the_bricks.insert(0, brick1) # insert at the top of sprite list brick2 = stage.Sprite(image_bank_2, 6, 74, 40) all_the_bricks.insert(0, brick2) # insert at the top of sprite list brick3 = stage.Sprite(image_bank_2, 5, 58, 40) all_the_bricks.insert(0, brick3) # insert at the top of sprite list brick4 = stage.Sprite(image_bank_2, 6, 42, 40) all_the_bricks.insert(0, brick4) # insert at the top of sprite list brick5 = stage.Sprite(image_bank_2, 5, 26, 40) all_the_bricks.insert(0, brick5) # insert at the top of sprite list brick6 = stage.Sprite(image_bank_2, 6, 10, 40) all_the_bricks.insert(0, brick6) # insert at the top of sprite list brick7 = stage.Sprite(image_bank_2, 5, -6, 40) all_the_bricks.insert(0, brick7) # insert at the top of sprite list brick8 = stage.Sprite(image_bank_2, 6, 106, 40) all_the_bricks.insert(0, brick8) # insert at the top of sprite list brick9 = stage.Sprite(image_bank_2, 5, 122, 40) all_the_bricks.insert(0, brick9) # insert at the top of sprite list brick10 = stage.Sprite(image_bank_2, 6, 138, 40) all_the_bricks.insert(0, brick10) # insert at the top of sprite list # create a stage for the background to show up # setting the frame rate to 60fps game = stage.Stage(ugame.display, 60) # setting the layers to show them in order game.layers = text + sprites + all_the_bricks + [background] # rendering the background and the locations of the sprites game.render_block() # repeat forever game loop while True: #move the ball ball.move(ball.x + constants.BALL_SPEED, ball.y + constants.BALL_SPEED) #check if touching top or bottom of the screen if not 0 < ball.x < 150: constants.BALL_SPEED = -constants.BALL_SPEED if not 0 < ball.y < 118: constants.BALL_SPEED = -constants.BALL_SPEED #check if touching a paddle1 for ball in sprites: if ball.x > 0: pass # Each frame check if the ball is touching any of the bricks for brick_number in range(len(all_the_bricks)): if all_the_bricks[brick_number].x > 0: # the first 4 numbers are the coordinates of A box # the second 4 numbers are the alien, it is more of a box so I just made it slightly smaller if stage.collide(ball.x, ball.y, ball.x + 16, ball.y + 16, all_the_bricks[brick_number].x, all_the_bricks[brick_number].y, all_the_bricks[brick_number].x + 16, all_the_bricks[brick_number].y + 16): # you hit a brick all_the_bricks[brick_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # bounce ball # redraw sprite list game.render_sprites(sprites) game.tick() # wait until refresh rate finishes # get user input keys = ugame.buttons.get_pressed() #print(keys) # update game logic # move ship right if keys & ugame.K_RIGHT != 0: if paddle1.x > constants.SCREEN_X - constants.SPRITE_SIZE: paddle1.move(constants.SCREEN_X - constants.SPRITE_SIZE, paddle1.y) else: paddle1.move(paddle1.x + 1, paddle1.y) # move ship right if keys & ugame.K_LEFT != 0: if paddle1.x < 0: paddle1.move(0, paddle1.y) else: paddle1.move(paddle1.x - 1, paddle1.y) if keys & ugame.K_RIGHT != 0: if paddle2.x > constants.SCREEN_X - constants.SPRITE_SIZE: paddle2.move(constants.SCREEN_X - constants.SPRITE_SIZE, paddle2.y) else: paddle2.move(paddle2.x + 1, paddle2.y) # move ship right if keys & ugame.K_LEFT != 0: if paddle2.x < 0: paddle2.move(0, paddle2.y) else: paddle2.move(paddle2.x - 1, paddle2.y) # redraw sprite list game.render_sprites(sprites) game.tick() # wait until refresh rate finishes
def game_scene(): # this function is the game scene # game score score = 0 # variables increaser = 0 chicken_speed = 1 egg_speed = 1 bomb_speed = 1 # buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # get sound ready coin_sound = open("coin.wav", 'rb') boom_sound = open("boom.wav", 'rb') big_boom_sound = open("big_boom.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # Function to make eggs reappear at the top of the screen def show_egg(): for egg_number in range(len(eggs)): if eggs[egg_number].x < 0: # meaning it is off the screen, eggs[egg_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break # Function to make bombs reappear at the top of the screen def show_bomb(): for bomb_number in range(len(bombs)): if bombs[bomb_number].x < 0: # meaning it is off the screen bombs[bomb_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN - random.randint(0, 50)) break image_bank_2 = stage.Bank.from_bmp16("egg_collector_image_bank_test.bmp") # sets the background to image 0 in the bank background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) # add text at top of screen for score score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # list to create plants at the bottom of the screen plants = [] # procedurally generating grass for grass_number in range(0, 10): a_single_grass = stage.Sprite(image_bank_2, 5, constants.GRASS_POINT + increaser, 128 - 16) plants.append(a_single_grass) increaser += 16 # list to hold all trunk sprites trunk = [] trunkL = stage.Sprite(image_bank_2, 6, constants.SPRITE_SIZE * 4, 112) trunk.append(trunkL) trunkR = stage.Sprite(image_bank_2, 7, constants.SPRITE_SIZE * 5, 112) trunk.append(trunkR) trunkM = stage.Sprite(image_bank_2, 8, 72, 96) trunk.append(trunkM) trunkM2 = stage.Sprite(image_bank_2, 8, 72, 80) trunk.append(trunkM2) trunk_branchL = stage.Sprite(image_bank_2, 9, constants.SPRITE_SIZE * 4, 64) trunk.append(trunk_branchL) trunk_branchR = stage.Sprite(image_bank_2, 10, constants.SPRITE_SIZE * 5, 64) trunk.append(trunk_branchR) # list to hold all leaf/foliage sprites foliage = [] foliageLBB = stage.Sprite(image_bank_2, 12, 60, 59) foliage.append(foliageLBB) foliageRBB = stage.Sprite(image_bank_2, 11, 84, 59) foliage.append(foliageRBB) foliageLMB = stage.Sprite(image_bank_2, 13, constants.SPRITE_SIZE * 4, 50) foliage.append(foliageLMB) foliageRMB = stage.Sprite(image_bank_2, 13, constants.SPRITE_SIZE * 5, 50) foliage.append(foliageRMB) foliageLLB = stage.Sprite(image_bank_2, 12, constants.SPRITE_SIZE * 3, 50) foliage.append(foliageLLB) foliageRRB = stage.Sprite(image_bank_2, 11, constants.SPRITE_SIZE * 6, 50) foliage.append(foliageRRB) foliageLMM = stage.Sprite(image_bank_2, 13, constants.SPRITE_SIZE * 4, 40) foliage.append(foliageLMM) foliageRMM = stage.Sprite(image_bank_2, 13, constants.SPRITE_SIZE * 5, 40) foliage.append(foliageRMM) foliageLLM = stage.Sprite(image_bank_2, 13, constants.SPRITE_SIZE * 3, 40) foliage.append(foliageLLM) foliageRRM = stage.Sprite(image_bank_2, 13, constants.SPRITE_SIZE * 6, 40) foliage.append(foliageRRM) foliageLLLM = stage.Sprite(image_bank_2, 12, 38, 40) foliage.append(foliageLLLM) foliageRRRM = stage.Sprite(image_bank_2, 11, 104, 40) foliage.append(foliageRRRM) foliageLLLT = stage.Sprite(image_bank_2, 15, 38, 26) foliage.append(foliageLLLT) foliageRRRT = stage.Sprite(image_bank_2, 14, 104, 26) foliage.append(foliageRRRT) foliageLLMT = stage.Sprite(image_bank_2, 13, 54, 26) foliage.append(foliageLLMT) foliageRRMT = stage.Sprite(image_bank_2, 13, 88, 26) foliage.append(foliageRRMT) foliageLMMT = stage.Sprite(image_bank_2, 13, 70, 26) foliage.append(foliageLMMT) foliageRMMT = stage.Sprite(image_bank_2, 13, 72, 26) foliage.append(foliageRMMT) foliageLLTT = stage.Sprite(image_bank_2, 15, 50, 14) foliage.append(foliageLLTT) foliageRRTT = stage.Sprite(image_bank_2, 14, 91, 14) foliage.append(foliageRRTT) foliageLMTT = stage.Sprite(image_bank_2, 13, 65, 14) foliage.append(foliageLMTT) foliageRMTT = stage.Sprite(image_bank_2, 13, 75, 14) foliage.append(foliageRMTT) foliage_deco_1 = stage.Sprite(image_bank_2, 12, 60, 20) foliage.insert(0, foliage_deco_1) foliage_deco_2 = stage.Sprite(image_bank_2, 11, 80, 30) foliage.insert(1, foliage_deco_2) # list to hold chicken sprites chickens = [] # create right chicken sprite chickenR = stage.Sprite(image_bank_2, 1, 80, 128 - constants.SPRITE_SIZE) chickens.insert(0, chickenR) # insert at top of sprite list # create left chicken sprite chickenL = stage.Sprite(image_bank_2, 2, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) chickens.append(chickenL) eggs = [] # generates eggs to fall from the top of the screen for egg_number in range(constants.TOTAL_NUMBER_OF_EGGS): a_single_egg = stage.Sprite(image_bank_2, 3, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) eggs.append(a_single_egg) egg_count = 1 show_egg() # generates bombs to fall from the top of the screen bombs = [] for bomb_number in range(constants.TOTAL_NUMBER_OF_BOMBS): a_single_bomb = stage.Sprite(image_bank_2, 4, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) bombs.append(a_single_bomb) bomb_count = 1 show_bomb() # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, 60) # sets which layers appear above the others game.layers = (chickens + eggs + bombs + plants + trunk + foliage + [score_text] + [background]) # render the background game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() # print(keys) # sets button states if keys & ugame.K_X != 0: # A button if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] # if right D-Pad is pressed if keys & ugame.K_RIGHT != 0: # if chicken moves off right screen, move it back if chickenR.x > constants.SCREEN_X - constants.SPRITE_SIZE: chickenR.x = 0 # else move chicken right else: # if chickenL is onscreen and right d-pad is pressed # replace chickenL with chickenR if chickenL.x > 0: chickenR.move(chickenL.x, chickenL.y) chickenL.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # once chicken is faced in direction of pressed d-pad # move chicken that way chickenR.move(chickenR.x + chicken_speed, chickenR.y) else: # if chickenL isnt onscreen and right d-pad is # pressed move chickenR chickenR.move(chickenR.x + chicken_speed, chickenR.y) # if left D-Pad is pressed if keys & ugame.K_LEFT != 0: # if chicken moves off left screen, move it back if chickenL.x < 0 and chickenL.y != constants.OFF_SCREEN_Y: chickenL.x = constants.SCREEN_X # else move chicken left else: # if chickenR is onscreen and left d-pad is pressed replace # chickenL with chickenL if chickenR.x > 0: chickenL.move(chickenR.x, chickenR.y) chickenR.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # once chicken is faced in direction of pressed d-pad # move chicken that way chickenL.move(chickenL.x - chicken_speed, chickenL.y) else: # if chickenR isnt onscreen and left d-pad is # pressed move chickenL chickenL.move(chickenL.x - chicken_speed, chickenL.y) # if A Button (speed) is pressed if a_button == constants.button_state["button_still_pressed"]: chicken_speed += 1 # increase speed at which chicken moves if chicken_speed > 3: chicken_speed = 3 # if A Button (speed) is not pressed if a_button == constants.button_state["button_up"]: chicken_speed = 2 # Egg and bomb speed increaser if score > 20 and egg_speed == 1: egg_speed += 1 bomb_speed += 1 elif score > 40 and egg_speed == 2: egg_speed += 1 bomb_speed += 1 elif score > 60 and egg_speed == 3: egg_speed += 1 bomb_speed += 1 elif score > 80 and egg_speed == 4: egg_speed += 1 bomb_speed += 1 # check if egg falls off the screen for egg_number in range(len(eggs)): if eggs[egg_number].x > 0: # meaning it is on the screen eggs[egg_number].move(eggs[egg_number].x, eggs[egg_number].y + egg_speed) if eggs[egg_number].y > constants.SCREEN_Y: eggs[egg_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) score = score - 2 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # this will freeze the screen for a split second, # but we have no option game.render_block() # play sound effect sound.stop() sound.play(boom_sound) show_egg() if score < 0: game_over_scene(score) # each frame check if any of the eggs are touching ChickenR for egg_number in range(len(eggs)): if eggs[egg_number].x > 0 and chickenR.x > 0: # https://circuitpython-stage.readthedocs.io/en/latest/# # stage.collide # and https://stackoverflow.com/questions/306316/determine # -if-two-rectangles-overlap-each-other if stage.collide(eggs[egg_number].x + 1, eggs[egg_number].y, eggs[egg_number].x + 15, eggs[egg_number].y + 15, chickenR.x, chickenR.y, chickenR.x + 15, chickenR.y + 15): eggs[egg_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # this will freeze the screen for a split second, # but we have no option game.render_block() # play sound effect sound.stop() sound.play(coin_sound) show_egg() show_egg() egg_count = egg_count + 1 # each frame check if any of the eggs are touching ChickenL if eggs[egg_number].x > 0 and chickenL.x > 0: # check if any of the eggs are touching ChickenR if stage.collide(eggs[egg_number].x + 1, eggs[egg_number].y, eggs[egg_number].x + 15, eggs[egg_number].y + 15, chickenL.x, chickenL.y, chickenL.x + 15, chickenL.y + 15): eggs[egg_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # this will freeze the screen for a split second, but we # have no option game.render_block() # play sound effect sound.stop() sound.play(coin_sound) show_egg() show_egg() egg_count = egg_count + 1 # check if bomb falls off the screen for bomb_number in range(len(bombs)): if bombs[bomb_number].x > 0: # meaning it is on the screen bombs[bomb_number].move(bombs[bomb_number].x, bombs[bomb_number].y + bomb_speed) if bombs[bomb_number].y > constants.SCREEN_Y: bombs[bomb_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # this will freeze the screen for a split second, # but we have no option game.render_block() # play sound effect sound.stop() sound.play(boom_sound) show_bomb() show_bomb() bomb_count = bomb_count + 1 # each frame check if any of the bombs are touching the chickenR for bomb_number in range(len(bombs)): if bombs[bomb_number].x > 0 and chickenR.x > 0: # https://circuitpython-stage.readthedocs.io/en/latest/# # stage.collide # and https://stackoverflow.com/questions/306316/determine # -if-two-rectangles-overlap-each-other if stage.collide(bombs[bomb_number].x + 1, bombs[bomb_number].y, bombs[bomb_number].x + 15, bombs[bomb_number].y + 15, chickenR.x, chickenR.y, chickenR.x + 15, chickenR.y + 15): bombs[bomb_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) game.render_block() # play sound effect sound.stop() sound.play(big_boom_sound) time.sleep(5.0) game_over_scene(score) # each frame check if any of the bombs are touching the chickenL if bombs[bomb_number].x > 0 and chickenL.x > 0: # https://circuitpython-stage.readthedocs.io/en/latest/# # stage.collide # and https://stackoverflow.com/questions/306316/determine # -if-two-rectangles-overlap-each-other if stage.collide(bombs[bomb_number].x + 1, bombs[bomb_number].y, bombs[bomb_number].x + 15, bombs[bomb_number].y + 15, chickenL.x, chickenL.y, chickenL.x + 15, chickenL.y + 15): bombs[bomb_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) game.render_block() # play sound effect sound.stop() sound.play(big_boom_sound) time.sleep(5.0) game_over_scene(score) # redraw sprite list game.render_sprites(eggs + bombs + chickens) game.tick()
def game_scene(): # this function is the game scene ALIEN_SPEED = 1 image_bank_2 = stage.Bank.from_bmp16("sprites.bmp") # game score score = 0 # tank direction tank_direction = None # These functions set and reset the start coordinates of frogs def reset_left_frog(): # Sets and resets the start coordinates of frogs starting on the left for left_frog_number in range(len(left_frogs)): if left_frogs[left_frog_number].x < 0: left_frogs[left_frog_number].move( random.randint(-100, 0 - constants.SPRITE_SIZE), random.randint(0, constants.SCREEN_Y)) break def reset_top_frog(): # Sets and resets the start coordinates of frogs starting on the top for top_frog_number in range(len(top_frogs)): if top_frogs[top_frog_number].y < 0: top_frogs[top_frog_number].move( random.randint(0, constants.SCREEN_X), random.randint(-100, 0 - constants.SPRITE_SIZE)) break def reset_right_frog(): # Sets and resets the start coordinates of frogs starting on the right for right_frog_number in range(len(right_frogs)): if right_frogs[right_frog_number].x < 0: right_frogs[right_frog_number].move( random.randint(constants.SCREEN_X, 228), random.randint(0, constants.SCREEN_Y)) break def reset_bottom_frog(): # Sets and resets the start coordinates of frogs starting on the bottom for down_frog_number in range(len(bottom_frogs)): if bottom_frogs[down_frog_number].y < 0: bottom_frogs[down_frog_number].move( random.randint(0, constants.SCREEN_X), random.randint(160 + constants.SPRITE_SIZE, 260)) break # Creating frogs # Frogs staring from the left left_frogs = [] for left_frog_number in range(constants.TOTAL_NUMBER_OF_ALIENS): single_left_frog = stage.Sprite(image_bank_2, 3, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) left_frogs.append(single_left_frog) reset_left_frog() # Frogs staring from the top top_frogs = [] for top_frog_number in range(constants.TOTAL_NUMBER_OF_ALIENS): single_up_frog = stage.Sprite(image_bank_2, 3, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) top_frogs.append(single_up_frog) reset_top_frog() # Frogs starting from the right right_frogs = [] for right_frog_number in range(constants.TOTAL_NUMBER_OF_ALIENS): single_right_frog = stage.Sprite(image_bank_2, 3, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) right_frogs.append(single_right_frog) reset_right_frog() # Frogs staring from the bottom bottom_frogs = [] for down_frog_number in range(constants.TOTAL_NUMBER_OF_ALIENS): single_down_frog = stage.Sprite(image_bank_2, 3, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) bottom_frogs.append(single_down_frog) reset_bottom_frog() start_time = time.time() # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, 60) # set the layers, items show up in order # render the background and inital location of sprite list # most likely you will only render background once per scene game.render_block() # buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # get sound ready pew_sound = open( "pew2.wav", 'rb') # to change the wav volume: https://audioalter.com/volume/ boom_sound = open("boom.wav", 'rb') crash_sound = open("crash.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # an image bank for CircuitPython # a list of sprites that will be updated every frame sprites = [] # create lasers for when we shoot lasers = [] lasers_direction = [] for laser_number in range(constants.TOTAL_ATTACKS): a_single_laser = stage.Sprite(image_bank_2, 2, constants.OFF_SCREEN_X, constants.OFF_SCREEN_X) lasers_direction.append("None") lasers.append(a_single_laser) # set up the NeoPixels to match the # of lasers fired pixels = neopixel.NeoPixel(board.NEOPIXEL, 5, auto_write=False) for pixel_number in range(0, 5): pixels[pixel_number] = (0, 10, 0) pixels.show() # create frogs frogs = [] for frog_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_frog = stage.Sprite(image_bank_2, 3, constants.OFF_SCREEN_X, constants.OFF_SCREEN_X) frogs.append(a_single_frog) # current number of frogs that should be moving down screen, start with just 1 frog_counter = 1 # add text at top of screen for score score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) tank = stage.Sprite(image_bank_2, 4, 75, 60) sprites.append(tank) # insert at the top of sprite list # sets the background to image 0 in the bank background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(0, 0) background.tile(x_location, y_location, tile_picked) # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, 60) # set the layers, items show up in order game.layers = left_frogs + right_frogs + top_frogs \ + bottom_frogs + sprites + lasers + [background] # render the background and inital location of sprite list # most likely you will only render background once per scene game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() #print(keys) if keys & ugame.K_X != 0: # A button if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] # update game logic # if right D-Pad is pressed if keys & ugame.K_RIGHT != 0: # if tank moves off right screen, move it back if tank.x > constants.SCREEN_X - constants.SPRITE_SIZE: tank.x = constants.SCREEN_X - constants.SPRITE_SIZE # else move tank right else: tank.move(tank.x + constants.BALL_SPEED, tank.y) tank.set_frame(frame=None, rotation=1) tank_direction = "right" # if left D-Pad is pressed if keys & ugame.K_LEFT != 0: # if tank moves off left screen, move it back if tank.x < 0: tank.x = 0 # else move tank left else: tank.move(tank.x - constants.BALL_SPEED, tank.y) tank.set_frame(frame=None, rotation=3) tank_direction = "left" if keys & ugame.K_UP != 0: # if tank moves off up screen, move it back if tank.y > constants.SCREEN_Y - constants.SPRITE_SIZE: tank.y = constants.SCREEN_Y - constants.SPRITE_SIZE # else move tank up else: tank.move(tank.x, tank.y - 1) tank.set_frame(frame=None, rotation=0) tank_direction = "up" # if left D-Pad is pressed if keys & ugame.K_DOWN != 0: # if tank moves off down screen, move it back if tank.y < 0: tank.y = 0 # else move tank down else: tank.move(tank.x, tank.y + 1) tank.set_frame(frame=None, rotation=2) tank_direction = "down" # if A Button (fire) is pressed if a_button == constants.button_state["button_just_pressed"]: # fire a laser, if we have enough power (meaning we have not used up all the lasers) for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(tank.x, tank.y) lasers_direction[laser_number] = tank_direction sound.stop() sound.play(pew_sound) break # each frame move the lasers, that have been fired, up # first make all the neopixels yellow, then make them green if it is moving up lasers_moving_counter = -1 for pixel_number in range(0, 5): pixels[pixel_number] = (0, 10, 0) for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: laser_move_direction = None if lasers_direction[laser_number] == "down": lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y + constants.ATTACK_SPEED) elif lasers_direction[laser_number] == "up": lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y - constants.ATTACK_SPEED) elif lasers_direction[laser_number] == "left": lasers[laser_number].move( lasers[laser_number].x - constants.ATTACK_SPEED, lasers[laser_number].y) elif lasers_direction[laser_number] == "right": lasers[laser_number].move( lasers[laser_number].x + constants.ATTACK_SPEED, lasers[laser_number].y) lasers_moving_counter = lasers_moving_counter + 1 pixels[lasers_moving_counter] = ( 10, 10 - (2 * lasers_moving_counter + 2), 0) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) if lasers[laser_number].y > 128: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) if lasers[laser_number].x > 160: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) if lasers[laser_number].y < 1: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) if lasers_moving_counter == 4: for pixel_number in range(0, 5): pixels[pixel_number] = (10, 0, 0) pixels.show() # each frame move the frogs down the screen for left_frog_number in range(len(left_frogs)): if left_frogs[left_frog_number].x < constants.OFF_RIGHT_SCREEN: left_frogs[left_frog_number].move( left_frogs[left_frog_number].x + ALIEN_SPEED, left_frogs[left_frog_number].y) if left_frogs[left_frog_number].x > constants.SCREEN_X: left_frogs[left_frog_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) reset_left_frog() # Scroll asteroids from top of screen for top_frog_number in range(len(top_frogs)): if top_frogs[top_frog_number].y < constants.OFF_BOTTOM_SCREEN: top_frogs[top_frog_number].move( top_frogs[top_frog_number].x, top_frogs[top_frog_number].y + ALIEN_SPEED) if top_frogs[top_frog_number].y > constants.SCREEN_Y: top_frogs[top_frog_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) reset_top_frog() # Scroll asteroids from right of screen left for right_frog_number in range(len(right_frogs)): if right_frogs[right_frog_number].x > constants.OFF_LEFT_SCREEN: right_frogs[right_frog_number].move( right_frogs[right_frog_number].x - ALIEN_SPEED, right_frogs[right_frog_number].y) if right_frogs[right_frog_number].x < 0 - constants.SPRITE_SIZE: right_frogs[right_frog_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) reset_right_frog() # Scroll asteroids from bottom of screen for down_frog_number in range(len(bottom_frogs)): if bottom_frogs[down_frog_number].y > constants.OFF_TOP_SCREEN: bottom_frogs[down_frog_number].move( bottom_frogs[down_frog_number].x, bottom_frogs[down_frog_number].y - ALIEN_SPEED) if bottom_frogs[down_frog_number].y < 0 - constants.SPRITE_SIZE: bottom_frogs[down_frog_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) reset_bottom_frog() # each frame check if any of the lasers are touching any of the frogs for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for frog_number in range(len(left_frogs)): if left_frogs[frog_number].x > 0: if stage.collide(left_frogs[frog_number].x + 1, left_frogs[frog_number].y + 1, left_frogs[frog_number].x + 15, left_frogs[frog_number].y + 15, lasers[laser_number].x + 3, lasers[laser_number].y + 3, lasers[laser_number].x + 13, lasers[laser_number].y + 13): left_frogs[frog_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() # sound.play(impact_sound) score = score + 1 reset_left_frog() frog_counter = frog_counter + 1 # This detects if any lasers hit asteroids heading down for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for frog_number in range(len(top_frogs)): if top_frogs[frog_number].x > 0: if stage.collide(top_frogs[frog_number].x + 1, top_frogs[frog_number].y + 1, top_frogs[frog_number].x + 15, top_frogs[frog_number].y + 15, lasers[laser_number].x + 3, lasers[laser_number].y + 3, lasers[laser_number].x + 13, lasers[laser_number].y + 13): top_frogs[frog_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() # sound.play(impact_sound) score = score + 1 if score % 10 == 0: ALIEN_SPEED = ALIEN_SPEED + 0.5 reset_top_frog() frog_counter = frog_counter + 1 # This detects if any lasers hit asteroids heading left for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for frog_number in range(len(right_frogs)): if right_frogs[frog_number].x > 0: if stage.collide(right_frogs[frog_number].x + 1, right_frogs[frog_number].y + 1, right_frogs[frog_number].x + 15, right_frogs[frog_number].y + 15, lasers[laser_number].x + 3, lasers[laser_number].y + 3, lasers[laser_number].x + 13, lasers[laser_number].y + 13): right_frogs[frog_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() # sound.play(impact_sound) score = score + 1 reset_right_frog() frog_counter = frog_counter + 1 # This detects if any lasers hit asteroids heading up for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for frog_number in range(len(bottom_frogs)): if bottom_frogs[frog_number].x > 0: if stage.collide(bottom_frogs[frog_number].x + 1, bottom_frogs[frog_number].y + 1, bottom_frogs[frog_number].x + 15, bottom_frogs[frog_number].y + 15, lasers[laser_number].x + 3, lasers[laser_number].y + 3, lasers[laser_number].x + 13, lasers[laser_number].y + 13): bottom_frogs[frog_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() # sound.play(impact_sound) score = score + 1 reset_bottom_frog() frog_counter = frog_counter + 1 # This detects a collision between the tank and asteroids going right for frog_number in range(len(left_frogs)): if left_frogs[frog_number].x > 0: if stage.collide(left_frogs[frog_number].x + 1, left_frogs[frog_number].y + 1, left_frogs[frog_number].x + 15, left_frogs[frog_number].y + 15, tank.x + 3, tank.y + 3, tank.x + 12, tank.y + 12): sound.stop() sound.play(crash_sound) time.sleep(4.0) sound.stop() game_over_scene(score) # This detects a collision between the tank and asteroids going down for frog_number in range(len(top_frogs)): if top_frogs[frog_number].x > 0: if stage.collide(top_frogs[frog_number].x + 1, top_frogs[frog_number].y + 1, top_frogs[frog_number].x + 15, top_frogs[frog_number].y + 15, tank.x + 3, tank.y + 3, tank.x + 12, tank.y + 12): sound.stop() sound.play(crash_sound) time.sleep(4.0) sound.stop() game_over_scene(score) # This detects a collision between the tank and asteroids going left for frog_number in range(len(right_frogs)): if right_frogs[frog_number].x > 0: if stage.collide(right_frogs[frog_number].x + 1, right_frogs[frog_number].y + 1, right_frogs[frog_number].x + 15, right_frogs[frog_number].y + 15, tank.x + 3, tank.y + 3, tank.x + 12, tank.y + 12): sound.stop() sound.play(crash_sound) time.sleep(4.0) sound.stop() game_over_scene(score) # This detects a collision between the tank and asteroids going up for frog_number in range(len(bottom_frogs)): if bottom_frogs[frog_number].x > 0: if stage.collide(bottom_frogs[frog_number].x + 1, bottom_frogs[frog_number].y + 1, bottom_frogs[frog_number].x + 15, bottom_frogs[frog_number].y + 15, tank.x + 3, tank.y + 3, tank.x + 12, tank.y + 12): sound.stop() sound.play(crash_sound) time.sleep(4.0) sound.stop() game_over_scene(score) game.render_sprites(left_frogs + right_frogs + top_frogs + bottom_frogs + sprites + lasers) game.tick()
def lvl_2(score): vilheleme_list = [] water_sprites = [] ice_sprites = [] key_list = [] door_list = [] finish_list = [] wall_sprites = [] # get sound ready key_collect = open("key_collect.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) level = 2 # buttons that keep state information a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] up_button = constants.button_state["button_up"] down_button = constants.button_state["button_up"] left_button = constants.button_state["button_up"] right_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] image_bank_1 = stage.Bank.from_bmp16("iib_sprites.bmp") # create vilheleme vilheleme = stage.Sprite(image_bank_1, 2, 16, 48) vilheleme_list.append(vilheleme) # insert at the top of sprite list # create ice for ice_number in range(constants.TOTAL_NUMBER_OF_ICE_2): a_single_ice = stage.Sprite(image_bank_1, 7, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) ice_sprites.append(a_single_ice) # create walls for wall_number in range(constants.TOTAL_NUMBER_OF_WALLS_2): a_single_wall = stage.Sprite(image_bank_1, 14, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) wall_sprites.append(a_single_wall) # create water for water_number in range(constants.TOTAL_NUMBER_OF_WATER_2): a_single_water = stage.Sprite(image_bank_1, 4, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) water_sprites.append(a_single_water) # create key key = stage.Sprite(image_bank_1, 11, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) key_list.append(key) # insert at the top of sprite list # create door door = stage.Sprite(image_bank_1, 12, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) door_list.append(door) # insert at the top of sprite list # create finish finish = stage.Sprite(image_bank_1, 13, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) finish_list.append(finish) # insert at the top of sprite list # create background background = stage.Grid(image_bank_1, constants.SCREEN_X, constants.SCREEN_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_X): background.tile(x_location, y_location, 3) counter = 0 if wall_sprites[counter].x < 0: wall_sprites[counter].move(0, 32) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(0, 48) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(0, 64) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(0, 80) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(0, 96) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(16, 32) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(16, 96) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(32, 32) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(32, 96) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(48, 32) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(48, 96) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(64, 32) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(64, 96) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(80, 32) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(80, 96) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(96, 32) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(96, 96) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(112, 32) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(112, 96) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(128, 32) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(128, 96) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(144, 32) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(144, 48) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(144, 64) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(144, 80) counter += 1 if wall_sprites[counter].x < 0: wall_sprites[counter].move(144, 96) counter += 1 counter = 0 if ice_sprites[counter].x < 0: ice_sprites[counter].move(16, 48) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(32, 48) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(48, 48) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(64, 48) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(80, 48) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(96, 48) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(112, 48) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(128, 48) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(16, 64) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(32, 64) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(48, 64) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(64, 64) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(80, 64) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(96, 64) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(112, 64) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(128, 64) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(16, 80) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(32, 80) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(48, 80) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(64, 80) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(80, 80) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(96, 80) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(112, 80) counter += 1 if ice_sprites[counter].x < 0: ice_sprites[counter].move(128, 80) counter += 1 counter = 0 if key_list[counter].x < 0: key_list[counter].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) counter += 1 counter = 0 if door_list[counter].x < 0: door_list[counter].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) counter += 1 counter = 0 if finish_list[counter].x < 0: finish_list[counter].move(128, 80) counter += 1 # V If game lags, change this V game = stage.Stage(ugame.display, constants.FPS) counter_r = 0 while True: # get user input keys = ugame.buttons.get_pressed() if keys & ugame.K_UP != 0: # up button possible positions if up_button == constants.button_state["button_up"]: up_button = constants.button_state["button_just_pressed"] elif up_button == constants.button_state["button_just_pressed"]: up_button = constants.button_state["button_still_pressed"] else: if up_button == constants.button_state["button_still_pressed"]: up_button = constants.button_state["button_released"] else: up_button = constants.button_state["button_up"] if keys & ugame.K_DOWN != 0: # down button possible positions if down_button == constants.button_state["button_up"]: down_button = constants.button_state["button_just_pressed"] elif down_button == constants.button_state["button_just_pressed"]: down_button = constants.button_state["button_still_pressed"] else: if down_button == constants.button_state["button_still_pressed"]: down_button = constants.button_state["button_released"] else: down_button = constants.button_state["button_up"] if keys & ugame.K_LEFT != 0: # left button possible positions if left_button == constants.button_state["button_up"]: left_button = constants.button_state["button_just_pressed"] elif left_button == constants.button_state["button_just_pressed"]: left_button = constants.button_state["button_still_pressed"] else: if left_button == constants.button_state["button_still_pressed"]: left_button = constants.button_state["button_released"] else: left_button = constants.button_state["button_up"] if keys & ugame.K_RIGHT != 0: # right button possible positions if right_button == constants.button_state["button_up"]: right_button = constants.button_state["button_just_pressed"] elif right_button == constants.button_state["button_just_pressed"]: right_button = constants.button_state["button_still_pressed"] else: if right_button == constants.button_state["button_still_pressed"]: right_button = constants.button_state["button_released"] else: right_button = constants.button_state["button_up"] # add text at top of screen for score score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("{0}".format(score)) # add text at top of screen for level level_text = stage.Text(width=20, height=15, font=None, palette=constants.LEVEL_PALETTE, buffer=None) level_text.clear() level_text.cursor(12, 0) level_text.move(1, 1) level_text.text("Level {0}".format(level)) # V add layers here V game.layers = vilheleme_list + wall_sprites + key_list + door_list + finish_list + water_sprites + ice_sprites + [score_text] + [level_text] + [background] game.render_block() # keys if keys & ugame.K_RIGHT != 0: # right if right_button == constants.button_state["button_just_pressed"]: vilheleme.move(vilheleme.x + constants.SPRITE_SIZE, vilheleme.y) for ice_number in range(len(ice_sprites)): if vilheleme.x > 0: if stage.collide(ice_sprites[ice_number].x, ice_sprites[ice_number].y, ice_sprites[ice_number].x + 15, ice_sprites[ice_number].y + 15, vilheleme_list[0].x, vilheleme_list[0].y, vilheleme_list[0].x + 15, vilheleme_list[0].y + 15): if water_sprites[counter_r].x < 0: water_sprites[counter_r].move(vilheleme.x - constants.SPRITE_SIZE, vilheleme.y) counter_r += 1 score += 100 for wall_number in range(len(wall_sprites)): if vilheleme.x > 0: if stage.collide(wall_sprites[wall_number].x, wall_sprites[wall_number].y, wall_sprites[wall_number].x + 15, wall_sprites[wall_number].y + 15, vilheleme_list[0].x, vilheleme_list[0].y, vilheleme_list[0].x + 15, vilheleme_list[0].y + 15): vilheleme.move(vilheleme.x - constants.SPRITE_SIZE, vilheleme.y) game.render_sprites(vilheleme_list + wall_sprites + key_list + door_list + finish_list + water_sprites + ice_sprites) if keys & ugame.K_LEFT != 0: # left if left_button == constants.button_state["button_just_pressed"]: if vilheleme.x > 16: vilheleme.move(vilheleme.x - constants.SPRITE_SIZE, vilheleme.y) for ice_number in range(len(ice_sprites)): if vilheleme.x > 0: if stage.collide(ice_sprites[ice_number].x, ice_sprites[ice_number].y, ice_sprites[ice_number].x + 15, ice_sprites[ice_number].y + 15, vilheleme_list[0].x, vilheleme_list[0].y, vilheleme_list[0].x + 15, vilheleme_list[0].y + 15): if water_sprites[counter_r].x < 0: water_sprites[counter_r].move(vilheleme.x + constants.SPRITE_SIZE, vilheleme.y) counter_r += 1 score += 100 game.render_sprites(vilheleme_list + wall_sprites + key_list + door_list + finish_list + water_sprites + ice_sprites) game.tick() if keys & ugame.K_UP: # up if up_button == constants.button_state["button_just_pressed"]: vilheleme.move(vilheleme.x, vilheleme.y - constants.SPRITE_SIZE) for ice_number in range(len(ice_sprites)): if vilheleme.x > 0: if stage.collide(ice_sprites[ice_number].x, ice_sprites[ice_number].y, ice_sprites[ice_number].x + 15, ice_sprites[ice_number].y + 15, vilheleme_list[0].x, vilheleme_list[0].y, vilheleme_list[0].x + 15, vilheleme_list[0].y + 15): if water_sprites[counter_r].x < 0: water_sprites[counter_r].move(vilheleme.x, vilheleme.y + constants.SPRITE_SIZE) counter_r += 1 score += 100 for wall_number in range(len(wall_sprites)): if vilheleme.x > 0: if stage.collide(wall_sprites[wall_number].x, wall_sprites[wall_number].y, wall_sprites[wall_number].x + 15, wall_sprites[wall_number].y + 15, vilheleme_list[0].x, vilheleme_list[0].y, vilheleme_list[0].x + 15, vilheleme_list[0].y + 15): vilheleme.move(vilheleme.x, vilheleme.y + constants.SPRITE_SIZE) game.render_sprites(vilheleme_list + wall_sprites + key_list + door_list + finish_list + water_sprites + ice_sprites) game.tick() if keys & ugame.K_DOWN: # down if down_button == constants.button_state["button_just_pressed"]: vilheleme.move(vilheleme.x, vilheleme.y + constants.SPRITE_SIZE) for ice_number in range(len(ice_sprites)): if vilheleme.x > 0: if stage.collide(ice_sprites[ice_number].x, ice_sprites[ice_number].y, ice_sprites[ice_number].x + 15, ice_sprites[ice_number].y + 15, vilheleme_list[0].x, vilheleme_list[0].y, vilheleme_list[0].x + 15, vilheleme_list[0].y + 15): if water_sprites[counter_r].x < 0: water_sprites[counter_r].move(vilheleme.x, vilheleme.y - constants.SPRITE_SIZE) counter_r += 1 score += 100 for wall_number in range(len(wall_sprites)): if vilheleme.x > 0: if stage.collide(wall_sprites[wall_number].x, wall_sprites[wall_number].y, wall_sprites[wall_number].x + 15, wall_sprites[wall_number].y + 15, vilheleme_list[0].x, vilheleme_list[0].y, vilheleme_list[0].x + 15, vilheleme_list[0].y + 15): vilheleme.move(vilheleme.x, vilheleme.y - constants.SPRITE_SIZE) if stage.collide(key_list[0].x, key_list[0].y, key_list[0].x + 15, key_list[0].y + 15, vilheleme_list[0].x, vilheleme_list[0].y, vilheleme_list[0].x + 15, vilheleme_list[0].y + 15): key.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) door.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) if stage.collide(finish_list[0].x, finish_list[0].y, finish_list[0].x + 15, finish_list[0].y + 15, vilheleme_list[0].x, vilheleme_list[0].y, vilheleme_list[0].x + 15, vilheleme_list[0].y + 15): score += 1000 counter_r = 0 vilheleme_list = None water_sprites = None ice_sprites = None key_list = None door_list = None finish_list = None wall_sprites = None sound.play(key_collect) time.sleep(3) game_over(score) game.render_sprites(vilheleme_list + wall_sprites + key_list + door_list + finish_list + water_sprites + ice_sprites) game.tick() for water_number in range(len(water_sprites)): if stage.collide(water_sprites[water_number].x, water_sprites[water_number].y, water_sprites[water_number].x + 15, water_sprites[water_number].y + 15, vilheleme_list[0].x, vilheleme_list[0].y, vilheleme_list[0].x + 15, vilheleme_list[0].y + 15): game.render_sprites(vilheleme_list + wall_sprites + key_list + door_list + finish_list + water_sprites + ice_sprites) game.tick() final_score = score splash = open("splash_sound.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) sound.play(splash) time.sleep(2) game_over(final_score) return final_score
def game_scene(): # buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # get sound ready pew_sound = open( "blip.wav", 'rb') # to change the wav volume: https://audioalter.com/volume/ boom_sound = open("squish.wav", 'rb') crash_sound = open('boing3.wav', 'rb') health_sound = open('bloop_x.wav', 'rb') sound = ugame.audio sound.stop() sound.mute(False) # get image bank ready image_bank_1 = stage.Bank.from_bmp16("SPRITES.bmp") sprites = [] # create rocks for when we shoot rocks = [] rock_direction = [] for rock_number in range(constants.TOTAL_NUMBER_OF_ROCKS): a_single_rock = stage.Sprite(image_bank_1, 15, constants.OFF_SCREEN_X, constants.OFF_SCREEN_X) rocks.append(a_single_rock) rock_direction.append("None") # create the particle cloud for when snakob moves cloud = [] a_single_cloud = stage.Sprite(image_bank_1, 11, constants.OFF_SCREEN_X, constants.OFF_SCREEN_X) cloud.append(a_single_cloud) def show_clouds(): # create the particle cloud for when snakob moves a_single_cloud.move(snakob_bank.x + constants.SNAKOB_SPEED, snakob_bank.y) a_single_cloud.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # This creates snakob snakob = [] snakob_bank = stage.Sprite(image_bank_1, 13, int(constants.SCREEN_X / 2), constants.SCREEN_Y - constants.SPRITE_SIZE) snakob.append(snakob_bank) # # This creates the swords for snakobs left and right swords = [] swords_bank = stage.Sprite( image_bank_1, 8, int((constants.SCREEN_X / 2) + 14), ((constants.SCREEN_Y - 4) - constants.SPRITE_SIZE)) swords.append(swords_bank) swords1_bank = stage.Sprite( image_bank_1, 9, int(constants.OFF_SCREEN_X), (constants.OFF_SCREEN_X - constants.SPRITE_SIZE)) swords.append(swords1_bank) # This creates the sparks for snakobs sword sparks = [] spark_bank = stage.Sprite(image_bank_1, 10, int(constants.OFF_SCREEN_X), (constants.OFF_SCREEN_X - constants.SPRITE_SIZE)) sparks.append(spark_bank) # Gets the score ready score = 0 score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # get the health bar ready health_hearts = 5 health_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) health_text.clear() health_text.cursor(0, 2) health_text.move(1, 2) health_text.text("Health: {0}".format(health_hearts)) # create the apple sprite apples = [] apple_bank = stage.Sprite(image_bank_1, 12, int(constants.OFF_SCREEN_X), (constants.OFF_SCREEN_X - constants.SPRITE_SIZE)) apples.append(apple_bank) timer = 0 def health(): # makes the apples apear on screen apple_bank.move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_Y - constants.SPRITE_SIZE)) # sets the background to image 0 in the bank and randomly generates the backround background = stage.Grid(image_bank_1, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): boolean = random.randint(0, 1) if boolean == 0: tile_picked = 0 if boolean == 1: tile_picked = 14 background.tile(x_location, y_location, tile_picked) # sets the snake counts to these snake_count = 1 snake_counter = 1 def reset_left_snake(): # Sets and resets the start coordinates of snakes starting on the left for left_snake_number in range(len(left_snakes)): if left_snakes[left_snake_number].x < 0: left_snakes[left_snake_number].move( random.randint(-100, 0 - constants.SPRITE_SIZE), random.randint(0, constants.SCREEN_Y)) break def reset_top_snake(): # Sets and resets the start coordinates of snakes starting on the top for top_snake_number in range(len(top_snakes)): if top_snakes[top_snake_number].y < 0: top_snakes[top_snake_number].move( random.randint(0, constants.SCREEN_X), random.randint(-100, 0 - constants.SPRITE_SIZE)) break def reset_right_snake(): # Sets and resets the start coordinates of snakes starting on the right for right_snake_number in range(len(right_snakes)): if right_snakes[right_snake_number].x < 0: right_snakes[right_snake_number].move( random.randint(constants.SCREEN_X, 228), random.randint(0, constants.SCREEN_Y)) break def reset_bottom_snake(): # Sets and resets the start coordinates of snakes starting on the bottom for down_snake_number in range(len(bottom_snakes)): if bottom_snakes[down_snake_number].y < 0: bottom_snakes[down_snake_number].move( random.randint(0, constants.SCREEN_X), random.randint(160 + constants.SPRITE_SIZE, 260)) break # Gets the left snakes ready left_snakes = [] for left_snake_number in range(constants.SNAKE_CREATION_TOTAL): single_left_snake = stage.Sprite(image_bank_1, 4, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) left_snakes.append(single_left_snake) reset_left_snake() # Gets the top snakes ready top_snakes = [] for top_snake_number in range(constants.SNAKE_CREATION_TOTAL): single_up_snake = stage.Sprite(image_bank_1, 6, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) top_snakes.append(single_up_snake) reset_top_snake() # Gets the right snakes ready right_snakes = [] for right_snake_number in range(constants.SNAKE_CREATION_TOTAL): single_right_snake = stage.Sprite(image_bank_1, 7, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) right_snakes.append(single_right_snake) reset_right_snake() # Gets the bottem snakes ready bottom_snakes = [] for down_snake_number in range(constants.SNAKE_CREATION_TOTAL): single_down_snake = stage.Sprite(image_bank_1, 5, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) bottom_snakes.append(single_down_snake) reset_bottom_snake() start_time = time.time() # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, 60) # set the layers, items show up in order game.layers = apples + cloud + snakob + top_snakes + bottom_snakes + left_snakes + right_snakes + swords + sprites + rocks + [ health_text ] + [score_text] + [background] # render the background and inital location of sprite list # most likely you will only render background once per scene game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() #print(keys # sets rock number to zero rock_number = 0 # chcecks if keys & ugame.K_X != 0: A button if keys & ugame.K_X != 0: # A button if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if keys & ugame.K_O != 0: # B button if b_button == constants.button_state["button_up"]: b_button = constants.button_state["button_just_pressed"] elif b_button == constants.button_state["button_just_pressed"]: b_button = constants.button_state["button_still_pressed"] else: if b_button == constants.button_state["button_still_pressed"]: b_button = constants.button_state["button_released"] else: b_button = constants.button_state["button_up"] # IF one of the d pad is being pressed, move the prticles with snakob if keys & ugame.K_RIGHT != 0 or keys & ugame.K_LEFT != 0 or keys & ugame.K_DOWN != 0 or keys & ugame.K_UP != 0: a_single_cloud.move((snakob_bank.x - 3) + constants.SNAKOB_SPEED, (snakob_bank.y + 2)) # takes off cloud particles if none of the d pad is being pressed if keys & ugame.K_RIGHT == 0 and keys & ugame.K_LEFT == 0 and keys & ugame.K_DOWN == 0 and keys & ugame.K_UP == 0: a_single_cloud.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # if right D-Pad is pressed if keys & ugame.K_RIGHT != 0: # if snakob moves off right screen, move it back if snakob_bank.x > constants.SCREEN_X - constants.SPRITE_SIZE: snakob_bank.x = constants.SCREEN_X - constants.SPRITE_SIZE if swords_bank.x > constants.SCREEN_X - constants.SPRITE_SIZE: swords_bank.x = constants.SCREEN_X - constants.SPRITE_SIZE # else move snakob right and sword else: snakob_bank.move(snakob_bank.x + constants.SNAKOB_SPEED, snakob_bank.y) swords_bank.move((snakob_bank.x + 14) + constants.SNAKOB_SPEED, (snakob_bank.y) - 4) swords1_bank.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) snakob_direction = "right" # if left D-Pad is pressed if keys & ugame.K_LEFT != 0: # if snakob moves off left screen, move it back if snakob_bank.x < 0: snakob_bank.x = 0 if swords_bank.x < 16: swords_bank.x = 16 # else move snakob left and the sword else: snakob_bank.move(snakob_bank.x - constants.SNAKOB_SPEED, snakob_bank.y) swords1_bank.move( (snakob_bank.x - 14) - constants.SNAKOB_SPEED, (snakob_bank.y - 4)) swords_bank.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) snakob_direction = "left" # if left up-Pad is pressed if keys & ugame.K_UP != 0: # if snakob moves off up screen, move it back if snakob_bank.y < 0: snakob_bank.y = 0 else: snakob_bank.move(snakob_bank.x, snakob_bank.y - constants.SNAKOB_SPEED) swords_bank.move((snakob_bank.x + 14), snakob_bank.y - 4) swords1_bank.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) snakob_direction = "up" # if left D-Pad is pressed if keys & ugame.K_DOWN != 0: # if snakob moves off down screen, move it back if snakob_bank.y > 116: snakob_bank.y = 116 # else move snakob down and sword else: snakob_bank.move(snakob_bank.x, snakob_bank.y + constants.SNAKOB_SPEED) swords_bank.move((snakob_bank.x + 14), snakob_bank.y - 4) swords1_bank.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) snakob_direction = "down" # if the a button is pressed, move rocks across the screen if a_button == constants.button_state["button_just_pressed"]: for rock_number in range(len(rocks)): if rock_number == 3: break elif rocks[rock_number].x < 0: rocks[rock_number].move(snakob_bank.x, snakob_bank.y) rock_direction[rock_number] = snakob_direction sound.stop() sound.play(pew_sound) if b_button == constants.button_state["button_just_pressed"]: if swords1_bank.x < 0: spark_bank.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) elif swords_bank.x < 0: spark_bank.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # makes the rocks move across the screen acoring to snakobs orientation for rock_number in range(len(rocks)): if rocks[rock_number].x > 0: laser_direction = None if rock_direction[rock_number] == "down": rocks[rock_number].move( rocks[rock_number].x, rocks[rock_number].y + constants.ROCK_SPEED) elif rock_direction[rock_number] == "up": rocks[rock_number].move( rocks[rock_number].x, rocks[rock_number].y - constants.ROCK_SPEED) elif rock_direction[rock_number] == "left": rocks[rock_number].move( rocks[rock_number].x - constants.ROCK_SPEED, rocks[rock_number].y) elif rock_direction[rock_number] == "right": rocks[rock_number].move( rocks[rock_number].x + constants.ROCK_SPEED, rocks[rock_number].y) if rocks[rock_number].y < constants.OFF_TOP_SCREEN: rocks[rock_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) if rocks[rock_number].y > 128: rocks[rock_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) if rocks[rock_number].x > 160: rocks[rock_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) if rocks[rock_number].x < 1: rocks[rock_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # this is the clock to make health packs apeer for counter in range(1, 61): if counter == 60: timer = timer + 1 if timer == 500: health() timer = 0 else: continue # Scroll snakes from left of screen for left_snake_number in range(len(left_snakes)): if left_snakes[left_snake_number].x < constants.OFF_RIGHT_SCREEN: left_snakes[left_snake_number].move( left_snakes[left_snake_number].x + constants.SNAKE_SPEED, left_snakes[left_snake_number].y) if left_snakes[left_snake_number].x > constants.SCREEN_X: left_snakes[left_snake_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) reset_left_snake() # Scroll snakes from top of screen for top_snake_number in range(len(top_snakes)): if top_snakes[top_snake_number].y < constants.OFF_BOTTOM_SCREEN: top_snakes[top_snake_number].move( top_snakes[top_snake_number].x, top_snakes[top_snake_number].y + constants.SNAKE_SPEED) if top_snakes[top_snake_number].y > constants.SCREEN_Y: top_snakes[top_snake_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) reset_top_snake() # Scroll snakes from right of screen left for right_snake_number in range(len(right_snakes)): if right_snakes[right_snake_number].x > constants.OFF_LEFT_SCREEN: right_snakes[right_snake_number].move( right_snakes[right_snake_number].x - constants.SNAKE_SPEED, right_snakes[right_snake_number].y) if right_snakes[ right_snake_number].x < 0 - constants.SPRITE_SIZE: right_snakes[right_snake_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) reset_right_snake() # Scroll snakes from bottom of screen for down_snake_number in range(len(bottom_snakes)): if bottom_snakes[down_snake_number].y > constants.OFF_TOP_SCREEN: bottom_snakes[down_snake_number].move( bottom_snakes[down_snake_number].x, bottom_snakes[down_snake_number].y - constants.SNAKE_SPEED) if bottom_snakes[ down_snake_number].y < 0 - constants.SPRITE_SIZE: bottom_snakes[down_snake_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) reset_bottom_snake() # This detects if any rocks hit snakes heading right for rock_number in range(len(rocks)): if rocks[rock_number].x > 0: for left_snake_number in range(len(left_snakes)): if left_snakes[left_snake_number].x > 0: if stage.collide(left_snakes[left_snake_number].x + 1, left_snakes[left_snake_number].y + 1, left_snakes[left_snake_number].x + 15, left_snakes[left_snake_number].y + 15, rocks[rock_number].x + 3, rocks[rock_number].y + 3, rocks[rock_number].x + 13, rocks[rock_number].y + 13): left_snakes[left_snake_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) rocks[rock_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boom_sound) score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # this will freeze the screen for a split second, but we have no option game.render_block() left_snake_number = left_snake_number + 1 # This detects if any rocks hit snakes heading down for rock_number in range(len(rocks)): if rocks[rock_number].x > 0: for top_snake_number in range(len(top_snakes)): if top_snakes[top_snake_number].x > 0: if stage.collide(top_snakes[top_snake_number].x + 1, top_snakes[top_snake_number].y + 1, top_snakes[top_snake_number].x + 15, top_snakes[top_snake_number].y + 15, rocks[rock_number].x + 3, rocks[rock_number].y + 3, rocks[rock_number].x + 13, rocks[rock_number].y + 13): top_snakes[top_snake_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) rocks[rock_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boom_sound) score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # this will freeze the screen for a split second, but we have no option game.render_block() top_snake_number = top_snake_number + 1 # This detects if any rocks hit snakes heading left for rock_number in range(len(rocks)): if rocks[rock_number].x > 0: for right_snake_number in range(len(right_snakes)): if right_snakes[right_snake_number].x > 0: if stage.collide( right_snakes[right_snake_number].x + 1, right_snakes[right_snake_number].y + 1, right_snakes[right_snake_number].x + 15, right_snakes[right_snake_number].y + 15, rocks[rock_number].x + 3, rocks[rock_number].y + 3, rocks[rock_number].x + 13, rocks[rock_number].y + 13): right_snakes[right_snake_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) rocks[rock_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boom_sound) score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # this will freeze the screen for a split second, but we have no option game.render_block() right_snake_number = right_snake_number + 1 # This detects if any rocks hit snakes heading up for rock_number in range(len(rocks)): if rocks[rock_number].x > 0: for bottom_snake_number in range(len(bottom_snakes)): if bottom_snakes[bottom_snake_number].x > 0: if stage.collide( bottom_snakes[bottom_snake_number].x + 1, bottom_snakes[bottom_snake_number].y + 1, bottom_snakes[bottom_snake_number].x + 15, bottom_snakes[bottom_snake_number].y + 15, rocks[rock_number].x + 3, rocks[rock_number].y + 3, rocks[rock_number].x + 13, rocks[rock_number].y + 13): bottom_snakes[bottom_snake_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) rocks[rock_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boom_sound) score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # this will freeze the screen for a split second, but we have no option game.render_block() bottom_snake_number = bottom_snake_number + 1 # this detects if snakob has collided with an apple for ammo_number in range(len(apples)): if apples[ammo_number].x > 0: if snakob_bank.x > 0: if stage.collide(apples[ammo_number].x + 6, apples[ammo_number].y + 3, apples[ammo_number].x + 10, apples[ammo_number].y + 13, snakob_bank.x + 1, snakob_bank.y + 1, snakob_bank.x + 14, snakob_bank.y + 14): apples[ammo_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) health_hearts = health_hearts + 1 sound.stop() sound.play(health_sound) if health_hearts == 6: health_hearts = 5 health_text.clear() health_text.cursor(0, 2) health_text.move(1, 2) health_text.text("Health: {0}".format(health_hearts)) # detects collision in snakes and snakob for left_snake_number in range(len(left_snakes)): if left_snakes[left_snake_number].x > 0: if stage.collide(left_snakes[left_snake_number].x + 1, left_snakes[left_snake_number].y + 1, left_snakes[left_snake_number].x + 15, left_snakes[left_snake_number].y + 15, snakob_bank.x + 3, snakob_bank.y + 3, snakob_bank.x + 12, snakob_bank.y + 12): left_snakes[left_snake_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(crash_sound) health_hearts = health_hearts - 1 health_text.clear() health_text.cursor(0, 2) health_text.move(1, 2) health_text.text("Health: {0}".format(health_hearts)) # detects collision in snakes and snakob for bottom_snake_number in range(len(bottom_snakes)): if bottom_snakes[bottom_snake_number].x > 0: if stage.collide(bottom_snakes[bottom_snake_number].x + 1, bottom_snakes[bottom_snake_number].y + 1, bottom_snakes[bottom_snake_number].x + 15, bottom_snakes[bottom_snake_number].y + 15, snakob_bank.x + 3, snakob_bank.y + 3, snakob_bank.x + 12, snakob_bank.y + 12): bottom_snakes[bottom_snake_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(crash_sound) health_hearts = health_hearts - 1 health_text.clear() health_text.cursor(0, 2) health_text.move(1, 2) health_text.text("Health: {0}".format(health_hearts)) # This detects a collision between snakob and snakes going right for right_snake_number in range(len(right_snakes)): if right_snakes[right_snake_number].x > 0: if stage.collide(right_snakes[right_snake_number].x + 1, right_snakes[right_snake_number].y + 1, right_snakes[right_snake_number].x + 15, right_snakes[right_snake_number].y + 15, snakob_bank.x + 3, snakob_bank.y + 3, snakob_bank.x + 12, snakob_bank.y + 12): right_snakes[right_snake_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(crash_sound) health_hearts = health_hearts - 1 health_text.clear() health_text.cursor(0, 2) health_text.move(1, 2) health_text.text("Health: {0}".format(health_hearts)) # This detects a collision between snakob and snakes going up for top_snake_number in range(len(top_snakes)): if top_snakes[top_snake_number].x > 0: if stage.collide(top_snakes[top_snake_number].x + 1, top_snakes[top_snake_number].y + 1, top_snakes[top_snake_number].x + 15, top_snakes[top_snake_number].y + 15, snakob_bank.x + 3, snakob_bank.y + 3, snakob_bank.x + 12, snakob_bank.y + 12): top_snakes[top_snake_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(crash_sound) health_hearts = health_hearts - 1 health_text.clear() health_text.cursor(0, 2) health_text.move(1, 2) health_text.text("Health: {0}".format(health_hearts)) # if the hearts are equal to zero, the game is over if health_hearts == 0: game_over_scene(score) # redraw sprite list game.render_sprites(snakob + apples + cloud + sprites + sparks + swords + rocks + bottom_snakes + top_snakes + left_snakes + right_snakes) game.tick() # wait until refresh rate finishes
def game_scene(): # this function is a scene def show_alien(): # this function uses variables outside of itself # But this code will be used in 2 places # Make alien show up on screen in x-axis for alien_number in range(len(aliens)): if aliens[alien_number].x < 0: aliens[alien_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break # an image bank for CircuitPython image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp") # a list of sprites that will be updated every frame sprites = [] # create lasers for when we shoot lasers = [] for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS): a_single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers.append(a_single_laser) # sets the background to image 0 in the bank background = stage.Grid(image_bank_1, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) # buttons that you want to state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # get sound ready pew_sound = open("pew.wav", 'rb') boom_sound = open("boom.wav", 'rb') coin_sound = open("coin.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # create a sprite # parameters (image_bank, image # in bank, x, y) # alien = stage.Sprite(image_bank_1, 9, 64, 56) # sprites.append(alien) ship = stage.Sprite( image_bank_1, 5, int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2), int(constants.SCREEN_Y - constants.SPRITE_SIZE + constants.SPRITE_SIZE / 2)) sprites.insert(0, ship) # insert at the top of sprite list # create aliens aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_alien = stage.Sprite(image_bank_1, 9, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) aliens.append(a_single_alien) # aliens will move down screen, start with one alien_count = 1 show_alien() # add text at top of screen for score global score scoretext = [] score_text = stage.Text(width=29, height=14, font=None, palette=constants.S_P, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) scoretext.append(score_text) # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, constants.FPS) # set the layers, items show up in order game.layers = sprites + lasers + aliens + scoretext + [background] # render the background and inital location of sprite list # most likely you will only render background once per scene game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() # print(keys) if keys & ugame.K_X != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] # update game logic if keys & ugame.K_RIGHT != 0: if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE: ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y) else: ship.move(ship.x + constants.SPRITE_MOVEMENT_SPEED, ship.y) if keys & ugame.K_LEFT != 0: if ship.x < 0: ship.move(0, ship.y) else: ship.move(ship.x - constants.SPRITE_MOVEMENT_SPEED, ship.y) # play sound if A is pressed if a_button == constants.button_state["button_just_pressed"]: # fire a laser if possible(not all used up) for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(ship.x, ship.y) sound.stop() sound.play(pew_sound) break # each frame move the lasers, that have been fired, up for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # each frame move the aliens down 1 unit every frame for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: aliens[alien_number].move( aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() # each frame check if any lasers are touching aliens for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide(lasers[laser_number].x + 6, lasers[laser_number].y + 2, lasers[laser_number].x + 11, lasers[laser_number].y + 12, aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15): # you hit an alien aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # add 1 to the score score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # this will freeze screen for split second game.render_block() # play sound effect sound.stop() sound.play(coin_sound) show_alien() show_alien() alien_count = alien_count + 1 # each frame checl if alien is touching ship for alien_number in aliens: if aliens[alien_number].x > 0: if stage.collide(aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15, ship.x, ship.y, ship.x + 15, ship.y + 15): # alien hit the ship sound.stop() sound.play(boom_sound) time.sleep(4.0) game_over_scene() # redraw sprite list game.render_sprites(sprites + lasers + aliens) game.tick() # wait until refresh rate finishes
def game_scene(): # this function is the game scene # game score score = 0 def show_alien(): # I know this is a function that is using variables outside of itself! # BUT this code is going to be used in 2 places :) # make an alien show up on screen in the x-axis for alien_number in range(len(aliens)): if aliens[ alien_number].x < 0: # meaning it is off the screen, so available to move on the screen aliens[alien_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break # buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # an image bank for CircuitPython image_bank_1 = stage.Bank.from_bmp16("sprite.bmp") image_bank_2 = stage.Bank.from_bmp16("sprite.bmp") # a list of sprites that will be updated every frame background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) background.tile(0, 0, 1) background.tile(1, 0, 2) background.tile(2, 0, 1) background.tile(3, 0, 2) background.tile(4, 0, 1) background.tile(5, 0, 2) background.tile(6, 0, 1) background.tile(7, 0, 2) background.tile(8, 0, 1) background.tile(9, 0, 2) background.tile(0, 1, 1) background.tile(1, 1, 2) background.tile(2, 1, 1) background.tile(3, 1, 2) background.tile(4, 1, 1) background.tile(5, 1, 2) background.tile(6, 1, 1) background.tile(7, 1, 2) background.tile(8, 1, 1) background.tile(9, 1, 2) background.tile(0, 2, 1) background.tile(1, 2, 2) background.tile(2, 2, 1) background.tile(3, 2, 2) background.tile(4, 2, 1) background.tile(5, 2, 2) background.tile(6, 2, 1) background.tile(7, 2, 2) background.tile(8, 2, 1) background.tile(9, 2, 2) background.tile(0, 3, 13) background.tile(1, 3, 13) background.tile(2, 3, 13) background.tile(3, 3, 13) background.tile(4, 3, 13) background.tile(5, 3, 13) background.tile(6, 3, 13) background.tile(7, 3, 13) background.tile(8, 3, 13) background.tile(9, 3, 13) background.tile(0, 4, 13) background.tile(1, 4, 13) background.tile(2, 4, 13) background.tile(3, 4, 13) background.tile(4, 4, 13) background.tile(5, 4, 13) background.tile(6, 4, 13) background.tile(7, 4, 13) background.tile(8, 4, 13) background.tile(9, 4, 13) background.tile(0, 5, 13) background.tile(1, 5, 13) background.tile(2, 5, 13) background.tile(3, 5, 13) background.tile(4, 5, 13) background.tile(5, 5, 13) background.tile(6, 5, 13) background.tile(7, 5, 13) background.tile(8, 5, 13) background.tile(9, 5, 13) background.tile(0, 6, 10) background.tile(1, 6, 11) background.tile(2, 6, 10) background.tile(3, 6, 11) background.tile(4, 6, 10) background.tile(5, 6, 11) background.tile(6, 6, 10) background.tile(7, 6, 11) background.tile(8, 6, 10) background.tile(9, 6, 11) background.tile(0, 7, 12) background.tile(1, 7, 12) background.tile(2, 7, 12) background.tile(3, 7, 12) background.tile(4, 7, 12) background.tile(5, 7, 12) background.tile(6, 7, 12) background.tile(7, 7, 12) background.tile(8, 7, 12) background.tile(9, 7, 12) sprites = [] # create lasers for when we shoot # create aliens aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_PLATFORMS): a_single_alien = stage.Sprite(image_bank_1, 6, constants.OFF_SCREEN_X, constants.OFF_SCREEN_X) aliens.append(a_single_alien) # current number of aliens that should be moving down screen, start with just 1 alien_count = 1 show_alien() # add text at top of screen for score score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) ship = stage.Sprite(image_bank_1, 3, int(constants.SCREEN_X / 2), constants.SCREEN_Y - constants.SPRITE_SIZE) sprites.append(ship) # insert at the top of sprite list # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, 60) # set the layers, items show up in order game.layers = sprites + aliens + [score_text] + [background] # render the background and inital location of sprite list # most likely you will only render background once per scene game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() #print(keys) if keys & ugame.K_X != 0: # A button if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] # update game logic # if right D-Pad is pressed if keys & ugame.K_RIGHT != 0: # if ship moves off right screen, move it back if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE: ship.x = constants.SCREEN_X - constants.SPRITE_SIZE # else move ship right else: ship.move(ship.x + constants.SHIP_SPEED, ship.y) # if left D-Pad is pressed if keys & ugame.K_LEFT != 0: # if ship moves off left screen, move it back if ship.x < 0: ship.x = 0 # else move ship left else: ship.move(ship.x - constants.SHIP_SPEED, ship.y) # each frame move the aliens down the screen for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: # meaning it is on the screen aliens[alien_number].move( aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() # make it randomly show up at top again # each frame check if any of the aliens are touching the ship for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: # https://circuitpython-stage.readthedocs.io/en/latest/#stage.collide # and https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other if stage.collide(aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15, ship.x, ship.y, ship.x + 15, ship.y + 15): # Wait for 1 seconds time.sleep(4.0) game_over_scene(score) # redraw sprite list game.render_sprites(sprites + aliens) game.tick() # wait until refresh rate finishes
def game_scene(plane_number): # this function is the game scene global score global volume global number_of_missiles a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] image_bank_3 = stage.Bank.from_bmp16("avoid_or_shoot.bmp") background = stage.Grid(image_bank_3, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for y_location in range(constants.SCREEN_GRID_Y): for x_location in range(constants.SCREEN_GRID_X): if y_location == 1: tile_picked = random.randint(1, 3) else: tile_picked = random.randint(1, 1) background.tile(x_location, y_location, tile_picked) birds_sound = open("bird.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) bomb_sound = open("boom.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) shot_sound = open("pew.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) coin_sound = open("coin.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) def show_flying(): enemy_picked = random.randint(0, 1) if enemy_picked == 0: if bird.y < 0: bird.move( 200, random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_Y - constants.SPRITE_SIZE)) else: if enemy.y < 0: enemy.move( 200, random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_Y - constants.SPRITE_SIZE)) def show_missile(): if loaded_missile.y < 0: loaded_missile.move( random.randint(200, 500), random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_Y - constants.SPRITE_SIZE)) text = [] score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score:{0}".format(score)) text.append(score_text) missile_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) missile_text.clear() missile_text.cursor(0, 0) missile_text.move(1, 10) missile_text.text("Missile:{0}".format(number_of_missiles)) text.append(missile_text) sprites = [] plane = stage.Sprite(image_bank_3, int(plane_number), 2, int(constants.SCREEN_Y / 2)) sprites.append(plane) missiles = [] for missile_number in range(constants.TOTAL_NUMBER_OF_MISSILES): missile = stage.Sprite(image_bank_3, 12, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) missiles.append(missile) birds = [] bird = stage.Sprite(image_bank_3, 5, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) birds.append(bird) enemies = [] enemy = stage.Sprite(image_bank_3, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) enemies.append(enemy) loaded_missiles = [] loaded_missile = stage.Sprite(image_bank_3, 11, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) loaded_missiles.append(loaded_missile) show_flying() show_missile() game = stage.Stage(ugame.display, constants.FPS) # set the background layer game.layers = (text + sprites + enemies + birds + missiles + loaded_missiles + [background]) # render the background # most likely you will only render background once per scene game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() if keys & ugame.K_X != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] # shot the missile if a_button == constants.button_state["button_just_pressed"]: if number_of_missiles > 0: for missile_number in range(len(missiles)): if missiles[missile_number].y < 0: missiles[missile_number].move(plane.x, plane.y) number_of_missiles = number_of_missiles - 1 missile_text.clear() missile_text.cursor(0, 0) missile_text.move(1, 10) missile_text.text( "Missile:{0}".format(number_of_missiles)) game.render_block() if volume % 2 == 0: sound.stop() sound.play(shot_sound) break # missiles move to right for missile_number in range(len(missiles)): if missiles[missile_number].y > 0: missiles[missile_number].move( missiles[missile_number].x + constants.MISSLE_SPEED, missiles[missile_number].y) if missiles[missile_number].x > constants.SCREEN_X: missiles[missile_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # move user's plane with d-pad if keys & ugame.K_UP != 0: if plane.y < 0: plane.move(plane.x, 0) else: plane.move(plane.x, plane.y - 2) pass if keys & ugame.K_DOWN != 0: if plane.y > constants.SCREEN_Y - constants.SPRITE_SIZE: plane.move(plane.x, constants.SCREEN_Y - constants.SPRITE_SIZE) else: plane.move(plane.x, plane.y + 2) pass # flying move left if bird.y > 0: # level if score <= 10: bird.move(bird.x - constants.BIRD_LEVEL_1, bird.y) elif score > 10 and score <= 30: bird.move(bird.x - constants.BIRD_LEVEL_2, bird.y) else: bird.move(bird.x - constants.BIRD_LEVEL_3, bird.y) if bird.x < constants.OFF_SCREEN_X: bird.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_flying() score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score:{0}".format(score)) game.render_block() elif enemy.y > 0: # level if score <= 10: enemy.move(enemy.x - constants.ENEMY_LEVEL_1, enemy.y) elif score > 10 and score <= 30: enemy.move(enemy.x - constants.ENEMY_LEVEL_2, enemy.y) else: enemy.move(enemy.x - constants.ENEMY_LEVEL_3, enemy.y) if enemy.x < constants.OFF_SCREEN_X: enemy.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_flying() if score > 1: score -= 2 elif score == 1: score -= 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score:{0}".format(score)) game.render_block() # loaded missile move left if loaded_missile.y > 0: loaded_missile.move(loaded_missile.x - 1, loaded_missile.y) if loaded_missile.x < constants.OFF_SCREEN_X: loaded_missile.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_missile() # if the missile hit the flying thing for missile_number in range(len(missiles)): if missiles[missile_number].x > 0: if bird.x > 0: if stage.collide(missiles[missile_number].x, missiles[missile_number].y, missiles[missile_number].x + 16, missiles[missile_number].y + 16, bird.x, bird.y, bird.x + 16, bird.y + 16): # you hit an bird bird.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) missiles[missile_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_flying() if score > 2: score -= 3 elif score == 2: score -= 2 elif score == 1: score -= 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score:{0}".format(score)) game.render_block() if volume % 2 == 0: sound.stop() sound.play(birds_sound) elif enemy.x > 0: if stage.collide(missiles[missile_number].x + 7, missiles[missile_number].y + 7, missiles[missile_number].x + 8, missiles[missile_number].y + 8, enemy.x, enemy.y, enemy.x + 16, enemy.y + 16): # missile hit an plane enemy.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) missiles[missile_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_flying() score += 2 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score:{0}".format(score)) game.render_block() if volume % 2 == 0: sound.stop() sound.play(bomb_sound) # user hit the flying thing if bird.x > 0: if stage.collide(bird.x, bird.y, bird.x + 10, bird.y + 10, plane.x, plane.y, plane.x + 10, plane.y + 10): if volume % 2 == 0: sound.stop() sound.play(bomb_sound) time.sleep(2.0) else: time.sleep(1.0) cause = 0 game_over_scene(score, cause) elif enemy.x > 0: if stage.collide(enemy.x, enemy.y, enemy.x + 12, enemy.y + 12, plane.x, plane.y, plane.x + 12, plane.y + 12): if volume % 2 == 0: sound.stop() sound.play(bomb_sound) time.sleep(4.0) else: time.sleep(1.0) cause = 1 game_over_scene(score, cause) # loaded missile hit the plane if loaded_missile.x > 0: if stage.collide(loaded_missile.x + 1, loaded_missile.y, loaded_missile.x + 15, loaded_missile.y + 15, plane.x, plane.y, plane.x + 15, plane.y + 15): number_of_missiles += 3 missile_text.clear() missile_text.cursor(0, 0) missile_text.move(1, 10) missile_text.text("Missile:{0}".format(number_of_missiles)) game.render_block() if volume % 2 == 0: sound.stop() sound.play(coin_sound) loaded_missile.move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_missile() # redraw sprite list game.render_sprites(sprites + birds + enemies + missiles + loaded_missiles) game.tick()
def game_scene(): # this function is the game scene on the PyBadge def show_alien(): # make an alien show up on screen on the x-axis for alien_number in range(len(aliens)): if aliens[alien_number].x < 0: aliens[alien_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break # an image bank for CircuitPython image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp") # a list of sprites sprites = [] # Buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # Get laser shooting sound ready pew_sound = open("pew.wav", "rb") sound = ugame.audio sound.stop() sound.mute(False) # Get sound ready boom_sound = open("boom.wav", "rb") sound = ugame.audio sound.stop() sound.mute(False) # sets the background to image 0 in the bank background = stage.Grid(image_bank_1, 10, 8) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_X): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) # create lasers lasers = [] for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS): a_single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers.append(a_single_laser) # create lasers aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_alien = stage.Sprite(image_bank_1, 9, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) aliens.append(a_single_alien) alien_count = 1 show_alien() text = [] score_text = stage.Text(width=29, height=14, font=None, palette=constants.NEW_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(alien_count)) text.append(score_text) # create sprite alien = stage.Sprite(image_bank_1, 9, 64, 56) sprites.append(alien) ship = stage.Sprite(image_bank_1, 5, 75, 56) sprites.insert(0, ship) # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, 60) # set the background layer game.layers = sprites + text + lasers + aliens + [background] # render the background # most likely you will only render background once per scene game.render_block() while True: # get user input keys = ugame.buttons.get_pressed() # print(keys) if keys & ugame.K_X: # print("A") pass if keys & ugame.K_O: # print("B") pass if keys & ugame.K_START: # print("K_START") pass if keys & ugame.K_SELECT: # print("K_SELECT") pass if keys & ugame.K_RIGHT: ship.move(ship.x + 1, ship.y) pass if keys & ugame.K_LEFT: ship.move(ship.x - 1, ship.y) pass if keys & ugame.K_UP: ship.move(ship.x, ship.y - 1) pass if keys & ugame.K_DOWN: ship.move(ship.x, ship.y + 1) pass for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: aliens[alien_number].move( aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() # A button to fire if keys & ugame.K_X != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] # Play sound if A is pressed if a_button == constants.button_state["button_just_pressed"]: for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(ship.x, ship.y) sound.stop() sound.play(pew_sound) break for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for alien_number in range(len(lasers)): if aliens[alien_number].x > 0: if stage.collide(lasers[laser_number].x + 6, lasers[laser_number].y + 2, lasers[laser_number].x + 11, lasers[laser_number].y + 12, aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15): aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boom_sound) show_alien() show_alien() alien_count += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(alien_count)) game.render_block() # update game logic # redraw sprite list game.render_sprites(sprites + lasers + aliens) game.tick()
def game_scene(): # this function is the main game scene # import sound damage_sound = open("damage.wav", 'rb') boop_sound = open("boop.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # score score = 0 score_text = stage.Text(width=29, height=14) score_text.clear() score_text.cursor(0,0) score_text.move(1,1) score_text.text("Score: {0}".format(score)) # imports image bank .bmp file image_bank = stage.Bank.from_bmp16("space_aliens_background.bmp") # sets the background to image 0 in the image Bank # and the size (10x8 tiles of size 16x16) background = stage.Grid(image_bank, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) # sets randomized starry background for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(0, 3) background.tile(x_location, y_location, tile_picked) # sets the lanes for x_location in range(5, 9): for y_location in range(constants.SCREEN_GRID_Y): background.tile(x_location, y_location, 4) # 4th element = straight ship # places target pads background.tile(5, constants.SCREEN_GRID_Y - 1, 8) # 8th element = chill squid background.tile(6, constants.SCREEN_GRID_Y - 1, 8) background.tile(7, constants.SCREEN_GRID_Y - 1, 8) background.tile(8, constants.SCREEN_GRID_Y - 1, 8) def show_note(): # this function takes a note from off the screen and moves it on screen, # picking 1 of 4 different lanes for note_number in range(len(notes)): if notes[note_number].x < 0: lane_picked = random.randint(5, 8) notes[note_number].move(lane_picked * constants.SPRITE_SIZE, constants.OFF_TOP_SCREEN) break # create a list of notes notes = [] for note_number in range(constants.TOTAL_NUMBER_OF_NOTES): # (bmp file, element, x, y) a_single_note = stage.Sprite(image_bank, 9, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) notes.append(a_single_note) # summon one note to start off show_note() # create the pads and place them off screen pad_1 = stage.Sprite(image_bank, 7, # 7 = surprised squid constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) pads.append(pad_1) pad_2 = stage.Sprite(image_bank, 7, # 7 = surprised squid constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) pad_3 = stage.Sprite(image_bank, 7, # 7 = surprised squid constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) pad_4 = stage.Sprite(image_bank, 7, # 7 = surprised squid constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, constants.FPS) # set the layers, items show up in order game.layers = [score_text] + [pad_1] + [pad_2] + [pad_3] + [pad_4] + notes + [background] # render the background and initial location of sprite list game.render_block() # buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] left_button = constants.button_state["button_up"] right_button = constants.button_state["button_up"] up_button = constants.button_state["button_up"] down_button = constants.button_state["button_up"] # repeat forever, game loop # repeatedly checks for player inputs, collisions, moves notes while True: # get user input keys = ugame.buttons.get_pressed() # left button for 1st pad if keys & ugame.K_LEFT != 0: if left_button == constants.button_state["button_up"]: left_button = constants.button_state["button_just_pressed"] elif left_button == constants.button_state["button_just_pressed"]: left_button = constants.button_state["button_still_pressed"] else: if left_button == constants.button_state["button_still_pressed"]: left_button = constants.button_state["button_released"] else: left_button = constants.button_state["button_up"] # Right button for 2nd pad if keys & ugame.K_RIGHT != 0: if right_button == constants.button_state["button_up"]: right_button = constants.button_state["button_just_pressed"] elif right_button == constants.button_state["button_just_pressed"]: right_button = constants.button_state["button_still_pressed"] else: if right_button == constants.button_state["button_still_pressed"]: right_button = constants.button_state["button_released"] else: right_button = constants.button_state["button_up"] # up button for 3rd pad if keys & ugame.K_UP != 0: if up_button == constants.button_state["button_up"]: up_button = constants.button_state["button_just_pressed"] elif up_button == constants.button_state["button_just_pressed"]: up_button = constants.button_state["button_still_pressed"] else: if up_button == constants.button_state["button_still_pressed"]: up_button = constants.button_state["button_released"] else: up_button = constants.button_state["button_up"] # A button for 4th pad if keys & ugame.K_DOWN != 0: if down_button == constants.button_state["button_up"]: down_button = constants.button_state["button_just_pressed"] elif down_button == constants.button_state["button_just_pressed"]: down_button = constants.button_state["button_still_pressed"] else: if down_button == constants.button_state["button_still_pressed"]: down_button = constants.button_state["button_released"] else: down_button = constants.button_state["button_up"] # not used if keys & ugame.K_START != 0: pass if keys & ugame.K_SELECT != 0: pass if keys & ugame.K_X != 0: pass if keys & ugame.K_O != 0: pass # takes input and does in-game action # pad 1 if left_button == constants.button_state["button_still_pressed"]: # activation if pad_1.x < 0: pad_1.move(5 * constants.SPRITE_SIZE, 7 * constants.SPRITE_SIZE) # deactivation elif left_button == constants.button_state["button_released"]: pad_1.move(constants.OFF_SCREEN_X,constants.OFF_SCREEN_Y) # pad 2 if right_button == constants.button_state["button_still_pressed"]: # activation if pad_2.x < 0: pad_2.move(6 * constants.SPRITE_SIZE, 7 * constants.SPRITE_SIZE) # deactivation elif right_button == constants.button_state["button_released"]: pad_2.move(constants.OFF_SCREEN_X,constants.OFF_SCREEN_Y) # pad 3 if up_button == constants.button_state["button_still_pressed"]: # activation if pad_3.x < 0: pad_3.move(7 * constants.SPRITE_SIZE, 7 * constants.SPRITE_SIZE) # deactivation elif up_button == constants.button_state["button_released"]: pad_3.move(constants.OFF_SCREEN_X,constants.OFF_SCREEN_Y) # pad 4 if down_button == constants.button_state["button_still_pressed"]: # activation if pad_4.x < 0: pad_4.move(8 * constants.SPRITE_SIZE, 7 * constants.SPRITE_SIZE) # deactivation elif down_button == constants.button_state["button_released"]: pad_4.move(constants.OFF_SCREEN_X,constants.OFF_SCREEN_Y) # downward note movement, only for on-screen notes for note_number in range(len(notes)): if notes[note_number].x > 0: notes[note_number].move(notes[note_number].x, notes[note_number].y + constants.STARTING_NOTE_SPEED) # if note goes below screen: if notes[note_number].y > constants.SCREEN_Y: notes[note_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # the game ends when the player misses a note sound.play(damage_sound) time.sleep(1.0) game_over_scene(score) # collision checks # pad 1 if pad_1.x > 0: # the pad is on-screen for note_number in range(len(notes)): if notes[note_number].x == 5 * constants.SPRITE_SIZE: # if any aliens are in lane 1 if stage.collide(pad_1.x, pad_1.y, pad_1.x, pad_1.y, notes[note_number].x, notes[note_number].y, notes[note_number].x + constants.SPRITE_SIZE, notes[note_number].y + constants.SPRITE_SIZE): # you hit a note notes[note_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boop_sound) show_note() show_note() score = score + 1 score_text.clear() score_text.cursor(0,0) score_text.move(1,1) score_text.text("Score: {0}".format(score)) # pad 2 if pad_2.x > 0: # the pad is on-screen for note_number in range(len(notes)): if notes[note_number].x == 6 * constants.SPRITE_SIZE: # if any aliens are in lane 1 if stage.collide(pad_2.x, pad_2.y, pad_2.x, pad_2.y, notes[note_number].x, notes[note_number].y, notes[note_number].x + constants.SPRITE_SIZE, notes[note_number].y + constants.SPRITE_SIZE): # you hit a note notes[note_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boop_sound) show_note() show_note() score = score + 1 score_text.clear() score_text.cursor(0,0) score_text.move(1,1) score_text.text("Score: {0}".format(score)) # pad 3 if pad_3.x > 0: # the pad is on-screen for note_number in range(len(notes)): if notes[note_number].x == 7 * constants.SPRITE_SIZE: # if any aliens are in lane 1 if stage.collide(pad_3.x, pad_3.y, pad_3.x, pad_3.y, notes[note_number].x, notes[note_number].y, notes[note_number].x + constants.SPRITE_SIZE, notes[note_number].y + constants.SPRITE_SIZE): # you hit a note notes[note_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boop_sound) show_note() show_note() score = score + 1 score_text.clear() score_text.cursor(0,0) score_text.move(1,1) score_text.text("Score: {0}".format(score)) # pad 4 if pad_4.x > 0: # the pad is on-screen for note_number in range(len(notes)): if notes[note_number].x == 8 * constants.SPRITE_SIZE: # if any aliens are in lane 1 if stage.collide(pad_4.x, pad_4.y, pad_4.x, pad_4.y, notes[note_number].x, notes[note_number].y, notes[note_number].x + constants.SPRITE_SIZE, notes[note_number].y + constants.SPRITE_SIZE): # you hit a note notes[note_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boop_sound) show_note() show_note() score = score + 1 score_text.clear() score_text.cursor(0,0) score_text.move(1,1) score_text.text("Score: {0}".format(score)) # redraw ONLY sprites game.render_sprites([pad_1] + [pad_2] + [pad_3] + [pad_4] + notes) # pads will be added later game.tick()
def game_scene(): # This function shows a sprite and makes a sound score = 0 a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] def show_alien(): # make an alien show up on screen in the x-axis for alien_number in range(len(aliens)): if aliens[alien_number].x < 0: aliens[alien_number].move(random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break # an image bank for circuitpython image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp") # a list of sprites that will be updated every frame sprites = [] # create lasers for when you shoot lasers = [] for lasers_number in range(constants.TOTAL_NUMBER_OF_LASERS): a_single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers.append(a_single_laser) # create aliens aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_alien = stage.Sprite(image_bank_1, 9, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) aliens.append(a_single_alien) # current number of aliens that should be moving down screen, # start with just 1 alien_count = 1 show_alien() # Add text at top of the screen score_text = stage.Text(width=29, height=29, font=None, palette=constants.NEW_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # get sound ready pew_sound = open("pew.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) boom_sound = open("boom.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) sound.play(boom_sound) # sprites in the scene # parameters (image_bank, image # in bank, x, y) ship = stage.Sprite(image_bank_1, 5, int(constants.SCREEN_X / 2), int(constants.SCREEN_Y - constants.SPRITE_SIZE)) sprites.append(ship) # insert at the top of the sprite list # sets the background to image 0 in the bank # backgrounds do not have magents as a transparent color background = stage.Grid(image_bank_1, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) # create a stage for the background to show up on # and set the frame rate to 60 game = stage.Stage(ugame.display, constants.FPS) # set the layers, items show up in order game.layers = [score_text] + sprites + lasers + aliens + [background] # render the background and initial location of sprite list # most likely you will only render background once per scene game.render_block() # repeat forever, or you turn it off while True: # get user input keys = ugame.buttons.get_pressed() if keys & ugame.K_X != 0: # a button (fire) if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if keys & ugame.K_X: # print("A") pass if keys & ugame.K_O: # print("B") pass if keys & ugame.K_START: # print("K_START") pass if keys & ugame.K_SELECT: # print("K_SELECT") pass # update_game_logic # move ship to the right and the left if keys & ugame.K_RIGHT != 0: if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE: ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y) else: ship.move(ship.x + 1, ship.y) if keys & ugame.K_LEFT != 0: if ship.x < 0: ship.move(0, ship.y) else: ship.move(ship.x - 1, ship.y) pass if a_button == constants.button_state["button_just_pressed"]: sound.play(pew_sound) if a_button == constants.button_state["button_just_pressed"]: # firea laser for lasers_number in range(len(lasers)): if lasers[lasers_number].x < 0: lasers[lasers_number].move(ship.x, ship.y) sound.stop() sound.play(pew_sound) break # each frame move the lasers, that have been fired for lasers_number in range(len(lasers)): if lasers[lasers_number].x > 0: lasers[lasers_number].move(lasers[lasers_number].x, lasers[lasers_number].y - constants.LASER_SPEED) if lasers[lasers_number].y < constants.OFF_TOP_SCREEN: lasers[lasers_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: aliens[alien_number].move(aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() # make it randomly show up at top again # Each frame check if any lasers are touching any of the aliens for lasers_number in range(len(lasers)): if lasers[lasers_number].x > 0: for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: # the first 4 numbers are the coordinates of A box # since the laser is thin, it made it thinner # and slightly smaller # the second 4 numbers are the alien, it is more of # a box so I just made it slightly smaller # if stage.collide(lasers[lasers_number].x + 6, lasers[lasers_number].y + 2, lasers[lasers_number].x + 11, lasers[lasers_number].y + 12, aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15): # you hit an alien aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[lasers_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # add 1 to the score score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) game.render_block() # play sound effect sound.stop() sound.play(boom_sound) show_alien() show_alien() alien_count = alien_count + 1 # redraw sprite list game.render_sprites(sprites + lasers + aliens) game.tick() # wait until refresh rate finishes
def game_scene(diff_mul): # this function is the game scene # background image bank ready background_bank = stage.Bank.from_bmp16("background.bmp") image_bank_0 = stage.Bank.from_bmp16("meteor.bmp") image_bank_1 = stage.Bank.from_bmp16("ship-and-lasers.bmp") background = stage.Grid(background_bank, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(0, 15) background.tile(x_location, y_location, tile_picked) a_button = constants.button_state["button_up"] shoot_sound = open("pew.wav", 'rb') boom_sound = open("boom.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # Buttons that you want to keep state information on a_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] sprites = [] ship = stage.Sprite(image_bank_1, 0, 80, 64) sprites.insert(0, ship) # insert at the top of sprite list score = 0 scoretext = [] score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.cursor(0, 0) score_text.move(1, 118) score_text.text("Points: {0}".format(score)) scoretext.append(score_text) lives = 3 livestext = [] lives_text = stage.Text(width=29, height=14, font=None, palette=constants.LIVES_PALETTE, buffer=None) lives_text.cursor(0, 0) lives_text.move(1, 1) lives_text.text("Lives: {0}".format(lives)) livestext.append(lives_text) asteroids = [] for asteroids_number in range(constants.TOTAL_ASTEROIDS * diff_mul): single_asteroid = stage.Sprite(image_bank_0, 0, constants.OFF_TOP_SCREEN, constants.OFF_TOP_SCREEN) asteroids.append(single_asteroid) enemy_1 = [] for enemy_number_1 in range(constants.TOTAL_ENEMY_1 * diff_mul): single_1 = stage.Sprite(image_bank_0, 1, constants.OFF_TOP_SCREEN, constants.OFF_TOP_SCREEN) enemy_1.append(single_1) enemy_2 = [] for enemy_number_2 in range(constants.TOTAL_ENEMY_2 * diff_mul): single_2 = stage.Sprite(image_bank_0, 2, constants.OFF_TOP_SCREEN, constants.OFF_TOP_SCREEN) enemy_2.append(single_2) lasers = [] for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS): single_laser = stage.Sprite(image_bank_1, 8, constants.OFF_TOP_SCREEN, constants.OFF_TOP_SCREEN) lasers.append(single_laser) enemy_count = 1 show_enemy(asteroids) show_enemy_2(enemy_1) show_enemy_3(enemy_2) death_mul = 1 # set frame rate to 60fps game = stage.Stage(ugame.display, 60) # set layers, items show up in order game.layers = sprites + enemy_1 + enemy_2 + asteroids + lasers + scoretext + livestext + [ background ] # render background and sprite list game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() if keys & ugame.K_X != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if a_button == constants.button_state["button_just_pressed"]: for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(ship.x, ship.y) sound.stop() sound.play(shoot_sound) break for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: if ship.rotation == 0: lasers[laser_number].set_frame(rotation=0) lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) if lasers[laser_number].y < constants.OFF_SCREEN_Y: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) elif ship.rotation == 1: lasers[laser_number].set_frame(rotation=1) lasers[laser_number].move( lasers[laser_number].x + constants.LASER_SPEED, lasers[laser_number].y) if lasers[laser_number].x > 160: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) elif ship.rotation == 2: lasers[laser_number].set_frame(rotation=0) lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y + constants.LASER_SPEED) if lasers[laser_number].y > 128: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) elif ship.rotation == 3: lasers[laser_number].set_frame(rotation=1) lasers[laser_number].move( lasers[laser_number].x - constants.LASER_SPEED, lasers[laser_number].y) if lasers[laser_number].x < 5: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # Move ship right if keys & ugame.K_RIGHT: state_of_button = 2 if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE: ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y) else: ship.move( ship.x + constants.SHIP_MOVEMENT_SPEED * death_mul * diff_mul, ship.y) ship.set_frame(rotation=1) pass # Move ship left if keys & ugame.K_LEFT: state_of_button = 4 if ship.x < 5: ship.move(5, ship.y) else: ship.move( ship.x - constants.SHIP_MOVEMENT_SPEED * death_mul * diff_mul, ship.y) ship.set_frame(rotation=3) pass # Move ship up if keys & ugame.K_UP: state_of_button = 1 if ship.y < 0: ship.move(ship.x, 0) else: ship.move( ship.x, ship.y - constants.SHIP_MOVEMENT_SPEED * death_mul * diff_mul) ship.set_frame(rotation=0) pass # Move ship down if keys & ugame.K_DOWN: state_of_button = 3 if ship.y > constants.SCREEN_Y - constants.SPRITE_SIZE: ship.move( ship.x, constants.SCREEN_Y - constants.SPRITE_SIZE * death_mul * diff_mul) else: ship.move( ship.x, ship.y + constants.SHIP_MOVEMENT_SPEED * death_mul * diff_mul) ship.set_frame(rotation=2) pass # update game logic for asteroid_number in range(len(asteroids)): if asteroids[asteroid_number].x > 0: asteroids[asteroid_number].move( asteroids[asteroid_number].x, asteroids[asteroid_number].y + constants.ENEMY_SPEED * diff_mul * death_mul) if asteroids[asteroid_number].y > constants.SCREEN_Y: asteroids[asteroid_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_enemy(asteroids) for enemy_number_1 in range(len(enemy_1)): if enemy_1[enemy_number_1].y > 0: enemy_1[enemy_number_1].move( enemy_1[enemy_number_1].x + constants.ENEMY_SPEED * death_mul * diff_mul, enemy_1[enemy_number_1].y) if enemy_1[enemy_number_1].x > constants.SCREEN_X: enemy_1[enemy_number_1].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_enemy_2(enemy_1) for enemy_number_2 in range(len(enemy_2)): if enemy_2[enemy_number_2].y > 0: enemy_2[enemy_number_2].move( enemy_2[enemy_number_2].x + constants.ENEMY_SPEED * death_mul * diff_mul, enemy_2[enemy_number_2].y) if enemy_2[enemy_number_2].x > constants.SCREEN_X: enemy_2[enemy_number_2].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_enemy_3(enemy_2) for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for enemy_number_1 in range(len(enemy_1)): if enemy_1[enemy_number_1].x > 0: if stage.collide(lasers[laser_number].x, lasers[laser_number].y, lasers[laser_number].x + 16, lasers[laser_number].y + 16, enemy_1[enemy_number_1].x, enemy_1[enemy_number_1].y, enemy_1[enemy_number_1].x + 16, enemy_1[enemy_number_1].y + 16): enemy_1[enemy_number_1].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) score += 10 * diff_mul score_text.clear() score_text.cursor(0, 0) score_text.move(1, 118) score_text.text("Points: {0}".format(score)) sound.stop() sound.play(boom_sound) show_enemy_2(enemy_1) show_enemy_2(enemy_1) death_mul += (1 / 30) for enemy_number_2 in range(len(enemy_2)): if enemy_2[enemy_number_2].x > 0: if stage.collide(lasers[laser_number].x, lasers[laser_number].y, lasers[laser_number].x + 16, lasers[laser_number].y + 16, enemy_2[enemy_number_2].x, enemy_2[enemy_number_2].y, enemy_2[enemy_number_2].x + 16, enemy_2[enemy_number_2].y + 16): enemy_2[enemy_number_2].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) score += 10 * diff_mul score_text.clear() score_text.cursor(0, 0) score_text.move(1, 118) score_text.text("Points: {0}".format(score)) sound.stop() sound.play(boom_sound) show_enemy_3(enemy_2) show_enemy_3(enemy_2) death_mul += (1 / 30) for asteroid_number in range(len(asteroids)): if asteroids[asteroid_number].x > 0: if stage.collide(lasers[laser_number].x, lasers[laser_number].y, lasers[laser_number].x + 16, lasers[laser_number].y + 16, asteroids[asteroid_number].x, asteroids[asteroid_number].y, asteroids[asteroid_number].x + 16, asteroids[asteroid_number].y + 16): asteroids[asteroid_number].move( constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) score += 5 * diff_mul score_text.clear() score_text.cursor(0, 0) score_text.move(1, 118) score_text.text("Points: {0}".format(score)) sound.stop() sound.play(boom_sound) show_enemy(asteroids) show_enemy(asteroids) death_mul += (1 / 30) for enemy_number_1 in range(len(enemy_1)): if enemy_1[enemy_number_1].x > 0: if stage.collide(enemy_1[enemy_number_1].x, enemy_1[enemy_number_1].y, enemy_1[enemy_number_1].x + 16, enemy_1[enemy_number_1].y + 16, ship.x, ship.y, ship.x + 16, ship.y + 16): lives -= 1 ship.move(-100, -100) sound.stop() sound.play(boom_sound) time.sleep(1) if lives == 0: game_over_scene(score) else: lives_text.clear() lives_text.cursor(0, 0) lives_text.move(1, 1) lives_text.text("Lives: {0}".format(lives)) ship.move(random.randint(16, 146), random.randint(16, 106)) for enemy_number_2 in range(len(enemy_2)): if enemy_2[enemy_number_2].x > 0: if stage.collide(enemy_2[enemy_number_2].x, enemy_2[enemy_number_2].y, enemy_2[enemy_number_2].x + 16, enemy_2[enemy_number_2].y + 16, ship.x, ship.y, ship.x + 16, ship.y + 16): lives -= 1 ship.move(-100, -100) sound.stop() sound.play(boom_sound) time.sleep(1) if lives == 0: game_over_scene(score) else: lives_text.clear() lives_text.cursor(0, 0) lives_text.move(1, 1) lives_text.text("Lives: {0}".format(lives)) ship.move(random.randint(16, 146), random.randint(16, 106)) for asteroid_number in range(len(asteroids)): if asteroids[asteroid_number].x > 0: if stage.collide(asteroids[asteroid_number].x, asteroids[asteroid_number].y, asteroids[asteroid_number].x + 16, asteroids[asteroid_number].y + 16, ship.x, ship.y, ship.x + 16, ship.y + 16): lives -= 1 ship.move(-100, -100) sound.stop() sound.play(boom_sound) time.sleep(1) if lives == 0: game_over_scene(score) else: lives_text.clear() lives_text.cursor(0, 0) lives_text.move(1, 1) lives_text.text("Lives: {0}".format(lives)) ship.move(random.randint(16, 146), random.randint(16, 106)) # redraw sprite list game.render_sprites(sprites + asteroids + enemy_1 + enemy_2 + lasers) game.tick()
def game_scene(): # comment def show_alien(): for alien_number in range(len(aliens)): if aliens[alien_number].y < 0: aliens[alien_number].move(random.randint(constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), 0) break # a list of sprites that will be updated every frame sprites = [] lasers = [] aliens = [] score = 0 # buttons that keep state information a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # get sound ready pew_sound = open("pew.wav", 'rb') boom_sound = open("boom.wav", 'rb') coin = open("coin.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # an image bank for CircuitPython image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp") # sets the background to image 0 in the bank background = stage.Grid(image_bank_1, constants.SCREEN_X, constants.SCREEN_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) # create a sprite # parameters (image_bank, image # in bank, x, y) ship = stage.Sprite(image_bank_1, 5, int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2), int(constants.SCREEN_Y - constants.SPRITE_SIZE + constants.SPRITE_SIZE / 2)) sprites.append(ship) # insert at the top of sprite list # create aliens for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_alien = stage.Sprite(image_bank_1, 7, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) aliens.append(a_single_alien) alien_count = 2 show_alien() # text at top of screen for score score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # create lasers for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS): a_single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers.append(a_single_laser) # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, constants.FPS) # set the layers, items show up in order game.layers = sprites + aliens + lasers + [score_text] + [background] # render the background and inital location of sprite list # most likely you will only render background once per scene game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() # (keys) if keys & ugame.K_X != 0: # a button (fire) if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if keys & ugame.K_O: # b pass if keys & ugame.K_START: # start pass if keys & ugame.K_SELECT: # select pass if keys & ugame.K_RIGHT != 0: # right if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE: ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y) else: ship.move(ship.x + 1, ship.y) pass if keys & ugame.K_LEFT != 0: # left if ship.x < 0: ship.move(0, ship.y) else: ship.move(ship.x - 1, ship.y) pass if keys & ugame.K_UP: # up if ship.y < 0 + constants.SPRITE_SIZE: ship.move(ship.x, 0 + constants.SPRITE_SIZE) else: ship.move(ship.x, ship.y - 1) pass if keys & ugame.K_DOWN: # down if ship.y > constants.SCREEN_Y - constants.SPRITE_SIZE: ship.move(ship.x, constants.SCREEN_Y - constants.SPRITE_SIZE) else: ship.move(ship.x, ship.y + 1) pass # update game logic # Play sound if a button is pressed # if A button is pressed if a_button == constants.button_state["button_just_pressed"]: for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(ship.x, ship.y) sound.stop() sound.play(pew_sound) break # each frame move the lasers for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: lasers[laser_number].move(lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # each frame move the aliens for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: aliens[alien_number].move(aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.OFF_BOTTOM_SCREEN: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() # Each frame check if lasers are touching aliens. for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide(lasers[laser_number].x + 6, lasers[laser_number].y + 2, lasers[laser_number].x + 11, lasers[laser_number].y + 12, aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15): aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # add to score score = score + constants.SCORE_ADDER score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) game.render_block() # play sound sound.stop() sound.play(boom_sound) show_alien() show_alien() alien_count = alien_count + 1 # if aliens are touching the ship for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide(aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15, ship.x, ship.y, ship.x + 15, ship.y + 15): sound.stop() sound.play(coin) time.sleep(2) sound.stop() game_over_scene(score_text) # game.render_block() # redraw sprite list game.render_sprites(sprites + lasers + aliens) game.tick() # wait until refresh rate finishes
def game_scene(): # this function is a scene sprites = [] ball_direction_x = 1 ball_direction_y = 1 # sets the paddle background = stage.Grid(image_bank_1, 10, 8) # parameters (image_bank, image # in bank, x, y) paddle = stage.Sprite(image_bank_1, 5, 140, 56) sprites.insert(0, paddle) # parameters (image_bank, image # in bank, x, y) paddle2 = stage.Sprite(image_bank_1, 5, 5, 56) sprites.insert(0, paddle2) # parameters (image_bank, image # in bank, x, y) ball = stage.Sprite(image_bank_1, 6, 50, 56) sprites.insert(0, ball) # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, 60) # set the layers, items show up in order game.layers = sprites + [background] # render the background and inital location of sprite list # most likely you will only render background once per scene game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() if keys & ugame.K_X: pass if keys & ugame.K_O: pass if keys & ugame.K_START: pass if keys & ugame.K_SELECT: pass if keys & ugame.K_UP: if paddle.y > 0: paddle.move(paddle.x, paddle.y - 1) pass if keys & ugame.K_DOWN: if paddle.y < constants.SCREEN_Y - constants.SPRITE_SIZE: paddle.move(paddle.x, paddle.y + 1) pass # update game logic if stage.collide(paddle.x + 7, paddle.y, paddle.x + 9, paddle.y + 16, ball.x + 4, ball.y + 4, ball.x + 8, ball.y + 8): ball_direction_x = -2 ball_direction_y = -2 sound.stop() sound.play(pew_sound) else: # ball.move(ball.x + constants.SPRITE_MOVEMENT_SPEED, ball.y) pass # update game logic if stage.collide(paddle2.x + 7, paddle2.y, paddle2.x + 9, paddle2.y + 16, ball.x + 4, ball.y + 4, ball.x + 8, ball.y + 8): ball_direction_x = 2 ball_direction_y = 2 sound.stop() sound.play(pew_sound) else: # ball.move(ball.x + constants.SPRITE_MOVEMENT_SPEED, ball.y) pass if ball.y > constants.SCREEN_Y - constants.SPRITE_SIZE: ball_direction_x = 1 ball_direction_y = -1 if ball.y < 0: ball_direction_x = -1 ball_direction_y = 1 if ball.y > 0: paddle2.move(paddle2.x, ball.y) ball.move(ball.x + ball_direction_x, ball.y + ball_direction_y) if ball.x > 140: game_over() # redraw sprite list game.render_sprites(sprites) game.tick() # wait until refresh rate finishes
def game_scene(): # this function is the game scene # game score score = 0 def show_alien(): # I know this is a function that is using variables outside of itself! # BUT this code is going to be used in 2 places :) # make an alien show up on screen in the x-axis for alien_number in range(len(aliens)): if aliens[ alien_number].x < 0: # meaning it is off the screen, so available to move on the screen aliens[alien_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break # buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # get sound ready pew_sound = open( "pew2.wav", 'rb') # to change the wav volume: https://audioalter.com/volume/ boom_sound = open("boom.wav", 'rb') crash_sound = open("crash.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # an image bank for CircuitPython image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp") image_bank_2 = stage.Bank.from_bmp16("sprites.bmp") # a list of sprites that will be updated every frame sprites = [] # create lasers for when we shoot lasers = [] for laser_number in range(constants.TOTAL_ATTACKS): a_single_laser = stage.Sprite(image_bank_2, 1, constants.OFF_SCREEN_X, constants.OFF_SCREEN_X) lasers.append(a_single_laser) # set up the NeoPixels to match the # of lasers fired pixels = neopixel.NeoPixel(board.NEOPIXEL, 5, auto_write=False) for pixel_number in range(0, 5): pixels[pixel_number] = (0, 10, 0) pixels.show() # create aliens aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_alien = stage.Sprite(image_bank_2, 3, constants.OFF_SCREEN_X, constants.OFF_SCREEN_X) aliens.append(a_single_alien) # current number of aliens that should be moving down screen, start with just 1 alien_count = 1 show_alien() # add text at top of screen for score score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) ship = stage.Sprite(image_bank_2, 4, int(constants.SCREEN_X / 2), constants.SCREEN_Y - constants.SPRITE_SIZE) sprites.append(ship) # insert at the top of sprite list # sets the background to image 0 in the bank background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(0, 0) background.tile(x_location, y_location, tile_picked) # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, 60) # set the layers, items show up in order game.layers = sprites + lasers + aliens + [score_text] + [background] # render the background and inital location of sprite list # most likely you will only render background once per scene game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() #print(keys) if keys & ugame.K_X != 0: # A button if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] # update game logic # if right D-Pad is pressed if keys & ugame.K_RIGHT != 0: # if ship moves off right screen, move it back if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE: ship.x = constants.SCREEN_X - constants.SPRITE_SIZE # else move ship right else: ship.move(ship.x + constants.BALL_SPEED, ship.y) ship.set_frame(frame=None, rotation=1) ship_direction = "right" # if left D-Pad is pressed if keys & ugame.K_LEFT != 0: # if ship moves off left screen, move it back if ship.x < 0: ship.x = 0 # else move ship left else: ship.move(ship.x - constants.BALL_SPEED, ship.y) ship.set_frame(frame=None, rotation=3) ship_direction = "left" if keys & ugame.K_UP != 0: # if ship moves off up screen, move it back if ship.y > constants.SCREEN_Y - constants.SPRITE_SIZE: ship.y = constants.SCREEN_Y - constants.SPRITE_SIZE # else move ship up else: ship.move(ship.x, ship.y - 1) ship.set_frame(frame=None, rotation=0) ship_direction = "up" # if left D-Pad is pressed if keys & ugame.K_DOWN != 0: # if ship moves off down screen, move it back if ship.y < 0: ship.y = 0 # else move ship down else: ship.move(ship.x, ship.y + 1) ship.set_frame(frame=None, rotation=2) ship_direction = "down" # if A Button (fire) is pressed if a_button == constants.button_state["button_just_pressed"]: # fire a laser, if we have enough power (meaning we have not used up all the lasers) for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(ship.x, ship.y) sound.stop() sound.play(pew_sound) break # each frame move the lasers, that have been fired, up # first make all the neopixels yellow, then make them green if it is moving up lasers_moving_counter = -1 for pixel_number in range(0, 5): pixels[pixel_number] = (0, 10, 0) for laser_number in range(len(lasers)): if lasers[laser_number].x > 0 and ship_direction == "up": lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y - constants.ATTACK_SPEED) lasers_moving_counter = lasers_moving_counter + 1 pixels[lasers_moving_counter] = ( 10, 10 - (2 * lasers_moving_counter + 2), 0) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) if lasers_moving_counter == 4: for pixel_number in range(0, 5): pixels[pixel_number] = (10, 0, 0) pixels.show() for laser_number in range(len(lasers)): if lasers[laser_number].x > 0 and ship_direction == "down": lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y - constants.ATTACK_SPEED) lasers_moving_counter = lasers_moving_counter + 1 pixels[lasers_moving_counter] = ( 10, 10 - (2 * lasers_moving_counter + 2), 0) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) if lasers_moving_counter == 4: for pixel_number in range(0, 5): pixels[pixel_number] = (10, 0, 0) # each frame move the aliens down the screen for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: # meaning it is on the screen aliens[alien_number].move( aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() # make it randomly show up at top again # each frame check if any of the lasers are touching any of the aliens for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: # https://circuitpython-stage.readthedocs.io/en/latest/#stage.collide # and https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other # the first 4 numbers are the coordinates of A box # since the laser is thin, it made it thinner and slightly smaller # # the second 4 numbers are the alien, it is more of a box so I just made it slightly smaller # # if you slow down the FPS, then you can see the interaction more easily to alter these numbers if stage.collide(lasers[laser_number].x + 6, lasers[laser_number].y + 2, lasers[laser_number].x + 11, lasers[laser_number].y + 12, aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15): # you hit an alien aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # add 1 to the score score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # this will freeze the screen for a split second, but we have no option game.render_block() # play sound effect sound.stop() sound.play(boom_sound) show_alien() show_alien() alien_count = alien_count + 1 # each frame check if any of the aliens are touching the ship for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: # https://circuitpython-stage.readthedocs.io/en/latest/#stage.collide # and https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other if stage.collide(aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15, ship.x, ship.y, ship.x + 15, ship.y + 15): # alien hit the ship sound.stop() sound.play(crash_sound) for pixel_number in range(0, 5): pixels[pixel_number] = (25, 0, 25) pixels.show() # Wait for 1 seconds time.sleep(4.0) # need to release the NeoPixels pixels.deinit() sound.stop() game_over_scene(score) # redraw sprite list game.render_sprites(sprites + lasers + aliens) game.tick() # wait until refresh rate finishes
def game_scene(): # this function is the main game game_scene # define sound boom_sound = open("boom.wav", 'rb') crash_sound = open("crash.wav", 'rb') # for score score = 0 score_text = stage.Text(width=29, height=14) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) def show_alien(): # this function takes an alien from off the screen and moves it on screen for alien_number in range(len(aliens)): if aliens[alien_number].x < 0: aliens[alien_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break # image banks for CircuitPython image_bank_background = stage.Bank.from_bmp16( "space_aliens_background.bmp") image_bank_sprites = stage.Bank.from_bmp16("space_aliens.bmp") # buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # get sound ready pew_sound = open("pew.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # sets the background to image 0 in the image Bank # and the size (10x8 tiles of size 16x16) background = stage.Grid(image_bank_background, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) ship = stage.Sprite(image_bank_sprites, 5, 75, constants.SCREEN_Y - (2 * constants.SPRITE_SIZE)) # create a list of aliens aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_alien = stage.Sprite(image_bank_sprites, 9, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) aliens.append(a_single_alien) # place 1 alien on the screen show_alien() # create list of lasers for when we shoot lasers = [] for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS): a_single_laser = stage.Sprite(image_bank_sprites, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers.append(a_single_laser) # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, 60) # set the layers, items show up in order game.layers = [score_text] + lasers + [ship] + aliens + [background] # render the background and initial location of sprite list # most likely you will only render background once per scene game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() # A button to fire if keys & ugame.K_O != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] # B button if keys & ugame.K_X != 0: pass if keys & ugame.K_START != 0: pass if keys & ugame.K_SELECT != 0: pass if keys & ugame.K_RIGHT != 0: if ship.x <= constants.SCREEN_X - constants.SPRITE_SIZE: ship.move((ship.x + constants.SPRITE_MOVEMENT_SPEED), ship.y) else: ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y) if keys & ugame.K_LEFT != 0: if ship.x >= 0: ship.move(ship.x - constants.SPRITE_MOVEMENT_SPEED, ship.y) else: ship.move(0, ship.y) if keys & ugame.K_UP != 0: pass if keys & ugame.K_DOWN != 0: pass # update game logic # play sound if A was just button_just_pressed if a_button == constants.button_state["button_just_pressed"]: # fire a laser, if we have enough power (have not used up all the lasers) for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(ship.x, ship.y) sound.play(pew_sound) break # each frame moves the lasers that have been fired up for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # each frame move the aliens down, that are on the screen for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: aliens[alien_number].move( aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() score -= 1 if score < 0: score = 0 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # each frame check if any of the lasers are touching any of the aliens for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide( lasers[laser_number].x + 6, lasers[laser_number].y + 2, lasers[laser_number].x + 11, lasers[laser_number].y + 12, aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15, ): # you hit an alien aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boom_sound) show_alien() show_alien() score = score + 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # each frame, check if any aliens are touching the space ship for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide(aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15, ship.x, ship.y, ship.x + 15, ship.y + 15): # alien hit the ship sound.stop() sound.play(crash_sound) time.sleep(3.0) game_over_scene(score) # redraw Sprites game.render_sprites(lasers + [ship] + aliens) game.tick()
def game_scene(): # image bank image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp") # setting button state a_button = constants.button_state["button_up"] # setting sound pew_sound = open("pew.wav", 'rb') boom_sound = open("boom.wav", 'rb') crash_sound = open("pew2.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # set background background = stage.Grid(image_bank_1, constants.SCREEN_X, constants.SCREEN_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1,3) background.tile(x_location, y_location, tile_picked) # sprite bank sprites = [] # load ship sprite ship = stage.Sprite(image_bank_1, 4, int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2), int(constants.SCREEN_Y - constants.SPRITE_SIZE + constants.SPRITE_SIZE / 2)) sprites.append(ship) # load lasers lasers = [] for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS): single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers.append(single_laser) # load aliens aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): single_alien = stage.Sprite(image_bank_1, 9, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) aliens.append(single_alien) # number of aliens moving down alien_count = 1 show_alien(aliens) # add score text global score scoretext = [] score_text = stage.Text(width=29, height=14, font=None, palette=constants.PLT, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) scoretext.append(score_text) # set game configurations game = stage.Stage(ugame.display, constants.FPS) game.layers = scoretext + sprites + lasers + aliens + [background] game.render_block() while True: keys = ugame.buttons.get_pressed() # print (keys) if keys & ugame.K_X != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if keys & ugame.K_RIGHT != 0: if ship.x > constants.SCREEN_X - constants.SPRITE_SIZE: ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y) else: ship.move(ship.x + 3, ship.y) if keys & ugame.K_LEFT != 0: if ship.x < 0: ship.move(0, ship.y) else: ship.move(ship.x - 3, ship.y) if a_button == constants.button_state["button_just_pressed"]: for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number] .move(ship.x, ship.y) sound.stop() sound.play(pew_sound) break for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: lasers[laser_number].move(lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) lasers[laser_number].move(lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) if lasers[laser_number].y < constants.OFF_SCREEN_Y: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: aliens[alien_number].move(aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien(aliens) for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide(lasers[laser_number].x, lasers[laser_number].y, lasers[laser_number].x + 16, lasers[laser_number].y + 16, aliens[alien_number].x, aliens[alien_number].y, aliens[alien_number].x + 16, aliens[alien_number].y + 16): aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) sound.stop() sound.play(boom_sound) show_alien(aliens) show_alien(aliens) if stage.collide(aliens[alien_number].x, aliens[alien_number].y, aliens[alien_number].x + 16, aliens[alien_number].y + 16 ship.x, ship.y, ship.x + 15, ship.y + 15): sound.stop() sound.play(crash_sound) time.sleep(4.0) sounds.stop() game_over_scene() game.render_sprites(sprites + lasers + aliens) game.tick() # wait until refresh rate finishes
def game_scene(): # this function is the game scene score = 0 text = [] score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) text.append(score_text) def show_tomato(): # make an tomato show up on screen on the x-axis for tomato_number in range(len(tomatos)): if tomatos[tomato_number].x < 0: tomatos[tomato_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break def show_pie(): for pie_number in range(len(pies)): if pies[pie_number].x < 0: pies[pie_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break def show_balloon(): for balloon_number in range(len(balloons)): if balloons[balloon_number].x < 0: balloons[balloon_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break # an image bank for CircuitPython image_bank_2 = stage.Bank.from_bmp16("sprites.bmp") splat_sound = open("splat.wav", "rb") sound = ugame.audio sound.stop() sound.mute(False) tomatos = [] pies = [] balloons = [] # drops tomatos for tomato_number in range(constants.TOTAL_NUMBER_OF_TOMATOS): a_single_tomato = stage.Sprite(image_bank_2, 3, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) tomatos.append(a_single_tomato) show_tomato() # drops pie for pie_number in range(constants.TOTAL_NUMBER_OF_PIES): a_single_pie = stage.Sprite(image_bank_2, 4, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) pies.append(a_single_pie) show_pie() # drops balloon for balloon_number in range(constants.TOTAL_NUMBER_OF_BALLOONS): a_single_balloon = stage.Sprite(image_bank_2, 5, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) balloons.append(a_single_balloon) show_balloon() # sets the background to image 0 in the bank background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) clown = stage.Sprite(image_bank_2, 2, 74, 56) sprites.insert(0, clown) # insert at the top of sprite list # create a stage for the background to show up # setting the frame rate to 60fps game = stage.Stage(ugame.display, 60) # setting the layers to show them in order game.layers = text + sprites + pies + tomatos + balloons + [background] # rendering the background and the locations of the sprites game.render_block() # repeat forever game loop while True: # get user input keys = ugame.buttons.get_pressed() if keys & ugame.K_RIGHT != 0: if clown.x > constants.SCREEN_X - constants.SPRITE_SIZE: clown.move(constants.SCREEN_X - constants.SPRITE_SIZE, clown.y) else: clown.move(clown.x + constants.CLOWN_SPEED, clown.y) if keys & ugame.K_LEFT != 0: if clown.x < 0: clown.move(0, clown.y) else: clown.move(clown.x - constants.CLOWN_SPEED, clown.y) if keys & ugame.K_UP != 0: if clown.y < 0: clown.move(clown.x, 0) else: clown.move(clown.x, clown.y - constants.CLOWN_SPEED) if keys & ugame.K_DOWN != 0: if clown.y > constants.SCREEN_Y - constants.SPRITE_SIZE: clown.move(clown.x, constants.SCREEN_Y - constants.SPRITE_SIZE) else: clown.move(clown.x, clown.y + constants.CLOWN_SPEED) # resets tomato for tomato_number in range(len(tomatos)): if tomatos[tomato_number].x > 0: tomatos[tomato_number].move( tomatos[tomato_number].x, tomatos[tomato_number].y + constants.TOMATO_SPEED) if tomatos[tomato_number].y > constants.SCREEN_Y: tomatos[tomato_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) game.render_block() show_tomato() # resets pie for pie_number in range(len(pies)): if pies[pie_number].x > 0: pies[pie_number].move(pies[pie_number].x, pies[pie_number].y + constants.PIE_SPEED) if pies[pie_number].y > constants.SCREEN_Y: pies[pie_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_pie() # resets pie for balloon_number in range(len(balloons)): if balloons[balloon_number].x > 0: balloons[balloon_number].move( balloons[balloon_number].x, balloons[balloon_number].y + constants.BALLOON_SPEED) if balloons[balloon_number].y > constants.SCREEN_Y: balloons[balloon_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_balloon() # collision with tomato for tomato_number in range(len(tomatos)): if tomatos[tomato_number].x > 0: if stage.collide(tomatos[tomato_number].x + 1, tomatos[tomato_number].y, tomatos[tomato_number].x + 15, tomatos[tomato_number].y + 15, clown.x, clown.y, clown.x + 15, clown.y + 15): sound.stop() sound.play(splat_sound) time.sleep(2.0) sound.stop() sprites.remove(clown) game_over_scene(score) # collision with pie for pie_number in range(len(pies)): if pies[pie_number].x > 0: if stage.collide(pies[pie_number].x + 1, pies[pie_number].y, pies[pie_number].x + 15, pies[pie_number].y + 15, clown.x, clown.y, clown.x + 15, clown.y + 15): sound.stop() sound.play(splat_sound) time.sleep(2.0) sound.stop() sprites.remove(clown) game_over_scene(score) # collision with balloon for balloon_number in range(len(balloons)): if balloons[balloon_number].x > 0: if stage.collide(balloons[balloon_number].x + 1, balloons[balloon_number].y, balloons[balloon_number].x + 15, balloons[balloon_number].y + 15, clown.x, clown.y, clown.x + 15, clown.y + 15): sound.stop() sound.play(splat_sound) time.sleep(2.0) sound.stop() sprites.remove(clown) game_over_scene(score) # update game logic # redraw sprite list game.render_sprites(sprites + pies + tomatos + balloons) game.tick() # wait until refresh rate finishes
def game_scene(): # this function is the main game game_scene def show_alien(): # this function takes an alien from off screen and moves it on screen for alien_number in range(len(aliens)): if aliens[alien_number].x < 0: aliens[alien_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break neopixels = neopixel.NeoPixel(board.NEOPIXEL, constants.NEOPIXEL_COUNT, auto_write=False) # for score score = 0 score_text = stage.Text(width=29, height=14) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # image banks for CircuitPython image_bank_bankground = stage.Bank.from_bmp16( "space_aliens_background.bmp") image_bank_sprites = stage.Bank.from_bmp16("space_aliens.bmp") # buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # get sound ready pew_sound = open("pew.wav", 'rb') boom_sound = open("boom.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # set the background to image 0 in the image Bank # and the size (10x8 tiles of the size 16x16) background = stage.Grid(image_bank_bankground, constants.SCREEN_X, constants.SCREEN_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) # a sprite that will be updated every frame ship = stage.Sprite(image_bank_sprites, 5, 75, constants.SCREEN_Y - (2 * constants.SPRITE_SIZE)) alien = stage.Sprite( image_bank_sprites, 9, int(constants.SCREEN_X / 2 - constants.SPRITE_SIZE / 2), 16) # create list of aliens aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_alien = stage.Sprite(image_bank_sprites, 9, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) aliens.append(a_single_alien) #place 1 alien on the screen show_alien() # create list of lasers for when we shoot lasers = [] for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS): a_single_laser = stage.Sprite(image_bank_sprites, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers.append(a_single_laser) # create a stage for the background to show up on # and set the frame rate for 60fps game = stage.Stage(ugame.display, constants.FPS) # set layers of all sprites, items show up in order game.layers = [score_text] + lasers + [ship] + aliens + [background] # render all sprites # most likely you will only render the background once per game scene game.render_block() # repeat forever, game loop while True: # get user input keys = ugame.buttons.get_pressed() if keys & ugame.K_O != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] # B button if keys & ugame.K_X != 0: pass if keys & ugame.K_START: pass if keys & ugame.K_SELECT: print("Select") if keys & ugame.K_RIGHT != 0: if ship.x < (constants.SCREEN_X - constants.SPRITE_SIZE): ship.move((ship.x + constants.SPRITE_MOVEMENT_SPEED), ship.y) else: ship.move((constants.SCREEN_X - constants.SPRITE_SIZE), ship.y) if keys & ugame.K_LEFT != 0: if ship.x > 0: ship.move((ship.x - constants.SPRITE_MOVEMENT_SPEED), ship.y) else: ship.move(0, ship.y) if keys & ugame.K_UP: pass if keys & ugame.K_DOWN: pass #set neopixels to green by default neopixels[0] = (0, 10, 0) neopixels[1] = (0, 10, 0) neopixels[2] = (0, 10, 0) neopixels[3] = (0, 10, 0) neopixels[4] = (0, 10, 0) # update game logic # play sound if A was just button_just_pressed if a_button == constants.button_state["button_just_pressed"]: # fire a laser, if we have enough power (have not used up all the # lasers) for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(ship.x, ship.y) sound.play(pew_sound) break # each frame move the lasers, that have been fired up and set neopixels # to yellow when laser is on screen. for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: lasers[laser_number].move( lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) neopixels[laser_number] = (10, 10, 0) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) #Set neopixel to red if all 5 lasers are on screen. if (neopixels[0] == (10, 10, 0) and neopixels[1] == (10, 10, 0) and neopixels[2] == (10, 10, 0) and neopixels[3] == (10, 10, 0) and neopixels[4] == (10, 10, 0)): neopixels[0] = (10, 0, 0) neopixels[1] = (10, 0, 0) neopixels[2] = (10, 0, 0) neopixels[3] = (10, 0, 0) neopixels[4] = (10, 0, 0) #each frame move the aliens down, that are on the screen for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: aliens[alien_number].move( aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() score -= 1 if score < 0: score = 0 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # each frame check if any of the lasers are touching any of the aliens for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide(lasers[laser_number].x + 6, lasers[laser_number].y + 2, lasers[laser_number].x + 11, lasers[laser_number].y + 12, aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15): # you hit an alien aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(boom_sound) show_alien() show_alien() score = score + 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) neopixels.show() # redraw sprites game.render_sprites(aliens + lasers + [ship]) game.tick()
def Game_scene(): global score text = [] image_bank_1 = stage.Bank.from_bmp16("ball.bmp") sprites = [] image_bank_2 = stage.Bank.from_bmp16("space_aliens.bmp") sprites = [] attack = [] for attack_number in range(constants.TOTAL_ATTACKS): a_single_attack = stage.Sprite(image_bank_1, 2, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) attack.append(a_single_attack) enemy = [] for enemy_number in range(constants.TOTAL_ENEMIES): a_single_enemy = stage.Sprite(image_bank_1, 3, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) enemy.append(a_single_enemy) def Show_enemy(): for enemy_number in range(len(enemy)): if enemy[enemy_number].x < 20: enemy[enemy_number].move( random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break enemy_count = 10 Show_enemy() score_text = stage.Text(width=29, height=14, font=None, palette=constants.NEW_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) text.append(score_text) # this sets the background a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] pew_sound = open("pew.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) boom_sound = open("boom.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # this is the background background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) # create a sprite # parameters (image_bank_1, image # in bank, x, y) ball_one = stage.Sprite(image_bank_1, 3, 64, 56) sprites.append(ball_one) game = stage.Stage(ugame.display, constants.FPS) game.layers = sprites + text + attack + enemy + [background] game.render_block() while True: # get user inputs keys = ugame.buttons.get_pressed() # print(keys) if keys & ugame.K_X != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if keys & ugame.K_O != 0: if b_button == constants.button_state["button_up"]: b_button = constants.button_state["button_just_pressed"] elif b_button == constants.button_state["button_just_pressed"]: b_button = constants.button_state["button_still_pressed"] else: if b_button == constants.button_state["button_still_pressed"]: b_button = constants.button_state["button_released"] else: b_button = constants.button_state["button_up"] if keys & ugame.K_UP != 0: if ball_one.y < 0: ball_one.move(ball_one.x, 0) else: ball_one.move(ball_one.x, ball_one.y - constants.SPRITE_MOVEMENT_SPEED) pass if keys & ugame.K_DOWN != 0: if ball_one.y > constants.SCREEN_Y - constants.SCREEN_GRID_Y: ball_one.move(ball_one.x, constants.SCREEN_Y - constants.SPRITE_SIZE) else: ball_one.move(ball_one.x, ball_one.y + constants.SPRITE_MOVEMENT_SPEED) pass if keys & ugame.K_LEFT != 0: if ball_one.x < 0: ball_one.move(0, ball_one.y) else: ball_one.move(ball_one.x - constants.SPRITE_MOVEMENT_SPEED, ball_one.y) pass if keys & ugame.K_RIGHT != 0: if ball_one.x > constants.SCREEN_X - constants.SCREEN_GRID_X: ball_one.move(constants.SCREEN_X - constants.SPRITE_SIZE, ball_one.y) else: ball_one.move(ball_one.x + constants.SPRITE_MOVEMENT_SPEED, ball_one.y) pass # update game logic if a_button == constants.button_state["button_just_pressed"]: for attack_number in range(len(attack)): if attack[attack_number].x < 0: attack[attack_number].move(ball_one.x, ball_one.y) # sound.play(pew_sound) break if b_button == constants.button_state["button_still_pressed"]: for attack_number in range(len(attack)): if attack[attack_number].x < 0: attack[attack_number].move(ball_one.x, ball_one.y) # sound.play(pew_sound) break for attack_number in range(len(attack)): if attack[attack_number].x > 0: attack[attack_number].move( attack[attack_number].x, attack[attack_number].y - constants.ATTACK_SPEED) if attack[attack_number].y < constants.OFF_TOP_SCREEN: attack[attack_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) for enemy_number in range(len(enemy)): if enemy[enemy_number].x > 0: enemy[enemy_number].move( enemy[enemy_number].x, enemy[enemy_number].y + constants.ENEMY_SPEED) if enemy[enemy_number].y > constants.SCREEN_Y: enemy[enemy_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) Show_enemy() for attack_number in range(len(attack)): if attack[attack_number].x > 0: for enemy_number in range(len(enemy)): if enemy[enemy_number].x > 0: if stage.collide(attack[attack_number].x + 6, attack[attack_number].y + 2, attack[attack_number].x + 11, attack[attack_number].y + 12, enemy[enemy_number].x + 1, enemy[enemy_number].y, enemy[enemy_number].x + 15, enemy[enemy_number].y + 15): enemy[enemy_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) score += 10 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) game.render_block() sound.stop() sound.play(boom_sound) Show_enemy() Show_enemy() enemy_count = enemy_count + 1 # redraw sprtie list game.render_sprites(sprites + attack + enemy) game.tick() # wait until refresh rate finishes
def game_scene(): # Main game scene # FUNCTION DEFINITION def show_ghost(): numbers_x = list(range(character.x - 3 * constants.SPRITE_SIZE, character.x, character.x + 3 * constants.SPRITE_SIZE)) numbers_y = list(range(character.y - 3 * constants.SPRITE_SIZE, character.y, character.y + 3 * constants.SPRITE_SIZE)) random_x = random.choice([element for element in range(-1 * constants.SPRITE_SIZE, constants.SCREEN_X + constants.SPRITE_SIZE) if element != numbers_x]) random_y = random.choice([element for element in range(-1 * constants.SPRITE_SIZE, constants.SCREEN_Y + constants.SPRITE_SIZE) if element != numbers_y]) for ghost_number in range(len(ghosts)): ghosts[ghost_number].move(random_x, random_y) break # IMAGE BANKS game_image_bank = stage.Bank.from_bmp16("image_bank_1.bmp") # BACKGROUND # Sets background to the 0th image in the image bank, 10x8 grid game_background = stage.Grid(game_image_bank, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) # Sets the floor as the 1st image in the image bank, the walls are # still going to be the 0th image for x_location in range(1, constants.SCREEN_GRID_X - 1): for y_location in range(1, constants.SCREEN_GRID_Y - 1): tile_picked = 1 game_background.tile(x_location, y_location, tile_picked) # SOUND # Sound library # Shooting sound sword_swoosh_sound = open("sword_swoosh.wav", 'rb') # Sword hitting ghosts ghost_hit_sound = open("ghost_hit.wav", 'rb') # Sound setup sound = ugame.audio # Stop all sound sound.stop() # Unmute sound.mute(False) # BUTTON STATES # Buttons with state information a_button = constants.button_state["button_up"] # SPRITES CREATION # Character sprite being displayed character = stage.Sprite(game_image_bank, 3, 75, 66) # Creates sword swoops sword_hits = [] sword_direction = [] for sword_number in range(constants.TOTAL_NUMBER_OF_SWORD_HITS): a_single_hit = stage.Sprite(game_image_bank, 5, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sword_hits.append(a_single_hit) # Sets bullet direction sword_direction.append("") direction = "Up" # Creates ghosts ghosts = [] for ghost_number in range(constants.TOTAL_NUMBER_OF_GHOSTS): a_single_ghost = stage.Sprite(game_image_bank, 6, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) ghosts.append(a_single_ghost) show_ghost() # STAGE AND RENDER # Creates a stage for the background # Sets frame rate to 60fps game = stage.Stage(ugame.display, constants.FPS) # Sets sprite layers and show up in order game.layers = (sword_hits + [character] + ghosts + [game_background]) # Renders all sprites, only once game.render_block() # GAME LOOP while True: # USER MOVEMENT + SHOOTING keys = ugame.buttons.get_pressed() # Button states to use sword if keys & ugame.K_O != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if keys & ugame.K_RIGHT: # Move right with constraints of the right border if character.x <= constants.SCREEN_X - 2 * constants.SPRITE_SIZE: character.move(character.x + constants.SPRITE_MOVEMENT_SPEED, character.y) else: character.move(constants.SCREEN_X - 2 * constants.SPRITE_SIZE, character.y) character.set_frame(2, 0) direction = "Right" if keys & ugame.K_LEFT: # Move left with constraints of the left border if character.x >= constants.SCREEN_X - 9 * constants.SPRITE_SIZE: character.move(character.x - constants.SPRITE_MOVEMENT_SPEED, character.y) else: character.move(constants.SPRITE_SIZE, character.y) character.set_frame(2, 4) direction = "Left" if keys & ugame.K_UP: # Moves down with constraints of the ceiling if character.y >= constants.SPRITE_SIZE: character.move(character.x, character.y - constants.SPRITE_MOVEMENT_SPEED) else: character.move(character.x, constants.SPRITE_SIZE) character.set_frame(3, 0) direction = "Up" if keys & ugame.K_DOWN: # Moves down with constraints of the ground if character.y <= constants.SCREEN_Y - 2 * constants.SPRITE_SIZE: character.move(character.x, character.y + constants.SPRITE_MOVEMENT_SPEED) else: character.move(character.x, constants.SCREEN_Y - 2 * constants.SPRITE_SIZE) character.set_frame(4, 0) direction = "Down" # Hit with sound if a_button == constants.button_state["button_just_pressed"]: for sword_number in range(len(sword_hits)): if sword_hits[sword_number].x < 0: sword_hits[sword_number].move(character.x, character.y) sword_direction[sword_number] = direction sound.play(sword_swoosh_sound) break # SWORD SWOOSH MOVEMENT # When sword hits get used, check the direction for movement. for sword_number in range(len(sword_hits)): if sword_hits[sword_number].x > -1 * constants.SPRITE_SIZE: if sword_direction[sword_number] == "Up": sword_hits[sword_number].move(sword_hits[sword_number].x, sword_hits[sword_number].y - constants.SWORD_SPEED) sword_hits[sword_number].set_frame(5, 0) if sword_direction[sword_number] == "Down": sword_hits[sword_number].move(sword_hits[sword_number].x, sword_hits[sword_number].y + constants.SWORD_SPEED) sword_hits[sword_number].set_frame(5, 2) if sword_direction[sword_number] == "Left": sword_hits[sword_number].move(sword_hits[sword_number].x - constants.SWORD_SPEED, sword_hits[sword_number].y) sword_hits[sword_number].set_frame(5, 3) if sword_direction[sword_number] == "Right": sword_hits[sword_number].move(sword_hits[sword_number].x + constants.SWORD_SPEED, sword_hits[sword_number].y) sword_hits[sword_number].set_frame(5, 1) # Move back sword hits to "staging" # if they are too far from the character # Right if sword_hits[sword_number].x > (character.x + 2 * constants.SPRITE_SIZE): sword_hits[sword_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # Right if sword_hits[sword_number].x < (character.x - 2 * constants.SPRITE_SIZE): sword_hits[sword_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # Down if sword_hits[sword_number].y > (character.y + 2 * constants.SPRITE_SIZE): sword_hits[sword_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # Up if sword_hits[sword_number].y < (character.y - 2 * constants.SPRITE_SIZE): sword_hits[sword_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) ghost_speed = 0.25 # GHOST MOVEMENT # Ghost's movement towards character for ghost_number in range(len(ghosts)): if ghosts[ghost_number].x > -1 * constants.SPRITE_SIZE: # Right of character (horizontal) if ghosts[ghost_number].x > character.x: ghosts[ghost_number].move(ghosts[ghost_number].x - ghost_speed, ghosts[ghost_number].y) ghosts[ghost_number].set_frame(6, 0) # Left of character (horizontal) if ghosts[ghost_number].x < character.x: ghosts[ghost_number].move(ghosts[ghost_number].x + ghost_speed, ghosts[ghost_number].y) ghosts[ghost_number].set_frame(6, 4) # Under character (vertical) if ghosts[ghost_number].y > character.y: ghosts[ghost_number].move(ghosts[ghost_number].x, ghosts[ghost_number].y - ghost_speed) # Over character (vertical) if ghosts[ghost_number].y < character.y: ghosts[ghost_number].move(ghosts[ghost_number].x, ghosts[ghost_number].y + ghost_speed) # Stay if the same if ghosts[ghost_number].x == character.x: ghosts[ghost_number].move(ghosts[ghost_number].x, ghosts[ghost_number].y) # Stay if the same if ghosts[ghost_number].y == character.y: ghosts[ghost_number].move(ghosts[ghost_number].x, ghosts[ghost_number].y) # HIT COLLISION # Sword hitting ghosts for sword_number in range(len(sword_hits)): if sword_hits[sword_number].x > 0: for ghost_number in range(len(ghosts)): if ghosts[ghost_number].x > 0: if stage.collide(sword_hits[sword_number].x + 1, sword_hits[sword_number].y, sword_hits[sword_number].x + 15, sword_hits[sword_number].y + 9, ghosts[ghost_number].x + 3, ghosts[ghost_number].y, ghosts[ghost_number].x + 13, ghosts[ghost_number].y + 13): ghosts[ghost_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sword_hits[sword_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) sound.stop() sound.play(ghost_hit_sound) show_ghost() # RENDER AND REDRAW # Renders and redraws the sprites that move game.render_sprites(ghosts + sword_hits + [character]) # Waits until refresh rate finishes game.tick()
def game_scene(): # this function sets the background global score # buttons that you want to keep state information on a_button = constants.button_state["button_up"] b_button = constants.button_state["button_up"] start_button = constants.button_state["button_up"] select_button = constants.button_state["button_up"] # get sound ready pew_sound = open("pew.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # get sound ready boom_sound = open("boom.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # get sound ready crash_sound = open("crash.wav", 'rb') sound = ugame.audio sound.stop() sound.mute(False) # an image bank for CircuitPython image_bank_1 = stage.Bank.from_bmp16("space_aliens.bmp") def show_alien(): # I know this is a function that is using variables outside of itself: # But this code is going to be used in 2 places # make an alien show up on screen in the x-axis for alien_number in range(len(aliens)): if aliens[alien_number].x < 0: aliens[alien_number].move(random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN) break # sets the background to image 0 in the bank background = stage.Grid(image_bank_1, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) for x_location in range(constants.SCREEN_GRID_X): for y_location in range(constants.SCREEN_GRID_Y): tile_picked = random.randint(1, 3) background.tile(x_location, y_location, tile_picked) # a list of sprites that will be updated every frame sprites = [] # add text at top of screen for score text = [] score_text = stage.Text(width=29, height=14, font=None, palette=constants.SCORE_PALETTE, buffer=None) score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: 0".format(score)) text.append(score_text) # parameters (image_bank_1, image # in bank, x, y) ship = stage.Sprite(image_bank_1, 5, int(constants.SCREEN_X / 2), int(constants.SCREEN_Y - constants.SPRITE_SIZE)) sprites.append(ship) # insert at top of sprite list # create lasers for when we shoot lasers = [] for laser_number in range(constants.TOTAL_NUMBER_OF_LASERS): a_single_laser = stage.Sprite(image_bank_1, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers.append(a_single_laser) # create aliens aliens = [] for alien_number in range(constants.TOTAL_NUMBER_OF_ALIENS): a_single_alien = stage.Sprite(image_bank_1, 9, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) aliens.append(a_single_alien) # current number of aliens that should be moving down screen, # start with just 1 alien_count = 0 show_alien() # create a stage for the background to show up on # and set the frame rate to 60fps game = stage.Stage(ugame.display, constants.FPS) # set the background layer game.layers = sprites + text + lasers + aliens + [background] # render the background # most likely you will only render background once per scene game.render_block() while True: # get user inputs keys = ugame.buttons.get_pressed() # print(keys) if keys & ugame.K_X != 0: if a_button == constants.button_state["button_up"]: a_button = constants.button_state["button_just_pressed"] elif a_button == constants.button_state["button_just_pressed"]: a_button = constants.button_state["button_still_pressed"] else: if a_button == constants.button_state["button_still_pressed"]: a_button = constants.button_state["button_released"] else: a_button = constants.button_state["button_up"] if keys & ugame.K_UP != 0: if ship.y < 0: ship.move(ship.x, 0) else: ship.move(ship.x, ship.y - 1) pass if keys & ugame.K_DOWN != 0: if ship.y > constants.SCREEN_Y - constants.SCREEN_GRID_Y: ship.move(ship.x, constants.SCREEN_Y - constants.SPRITE_SIZE) else: ship.move(ship.x, ship.y + 1) pass if keys & ugame.K_LEFT != 0: if ship.x < 0: ship.move(0, ship.y) else: ship.move(ship.x - 1, ship.y) pass if keys & ugame.K_RIGHT != 0: if ship.x > constants.SCREEN_X - constants.SCREEN_GRID_X: ship.move(constants.SCREEN_X - constants.SPRITE_SIZE, ship.y) else: ship.move(ship.x + 1, ship.y) pass # update game logic # play sound if A is pressed if a_button == constants.button_state["button_just_pressed"]: sound.play(pew_sound) # if A Button (fire) is pressed if a_button == constants.button_state["button_just_pressed"]: # fire a laser, if we have enough power # (meaning we have used up all the lasers) for laser_number in range(len(lasers)): if lasers[laser_number].x < 0: lasers[laser_number].move(ship.x, ship.y) sound.stop() sound.play(pew_sound) break # each frame move the lasers, that have been fired, up for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: lasers[laser_number].move(lasers[laser_number].x, lasers[laser_number].y - constants.LASER_SPEED) if lasers[laser_number].y < constants.OFF_TOP_SCREEN: lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # each frame move the aliens down the screen for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: aliens[alien_number].move(aliens[alien_number].x, aliens[alien_number].y + constants.ALIEN_SPEED) if aliens[alien_number].y > constants.SCREEN_Y: aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) show_alien() if score > 1: score = score - 2 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) game.render_block() for laser_number in range(len(lasers)): if lasers[laser_number].x > 0: for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: # the first 4 numbers are the coordinates of a box # since the laser os thin, it made it thinner # and slightly smaller # the second 4 numbers are the alien it is more of box # so I just made it slightly smaller # if you slow down the FPS, then you can see # the interaction more easily to alter these numbers if stage.collide(lasers[laser_number].x, lasers[laser_number].y, lasers[laser_number].x + 16, lasers[laser_number].y + 16, aliens[alien_number].x, aliens[alien_number].y, aliens[alien_number].x + 16, aliens[alien_number].y + 16): # you hit an alien aliens[alien_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) lasers[laser_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y) # add 1 to the score score += 1 score_text.clear() score_text.cursor(0, 0) score_text.move(1, 1) score_text.text("Score: {0}".format(score)) # this will freeze the screen for a split second, # but we have no option game.render_block() # play sound effect sound.stop() sound.play(boom_sound) show_alien() show_alien() alien_count = alien_count + 1 for alien_number in range(len(aliens)): if aliens[alien_number].x > 0: if stage.collide(aliens[alien_number].x + 1, aliens[alien_number].y, aliens[alien_number].x + 15, aliens[alien_number].y + 15, ship.x, ship.y, ship.x + 15, ship.y + 15): sound.stop() sound.play(crash_sound) time.sleep(4.0) sound.stop() game_over_scene(score) # redraw sprite list game.render_sprites(sprites + lasers + aliens) game.tick() # wait until refresh rate finishes