def draw(self, location, screen, can_see=True): """Draws the player onto the screen.""" loc = coda.Vector2(location[0], location[1]) num_cards = len(self.hand) num_offsets = num_cards - 1 length = num_cards * 160 + num_offsets * HAND_OFFSET start_loc = coda.Vector2(loc.x - length / 2 + card.WIDTH / 2, loc.y) jump = coda.Vector2(card.WIDTH + HAND_OFFSET, 0) self.health_text.text = "Health: " + str(self.health.value()) self.health_text.location = (start_loc.x - card.WIDTH, start_loc.y + 30) self.attack_text.text = "Attack: " + str(self.attack.value()) self.attack_text.location = (start_loc.x - card.WIDTH, start_loc.y) self.shield_text.text = "Shield: " + str(self.shield.value()) self.shield_text.location = (start_loc.x - card.WIDTH, start_loc.y - 30) for cards in self.hand: cards.location = start_loc cards.draw(screen, can_see) start_loc += jump self.health_text.draw(screen) self.attack_text.draw(screen) self.shield_text.draw(screen)
class Data: """Modifiable data""" tilesheet = coda.SpriteSheet("tileset.png", (32, 32)) player_sheet = coda.SpriteSheet("player_sheet.png", (42, 48)) tilemap = [] floors = [] walls = [] player_start_position = coda.Vector2(0, 0) boss_start_position = coda.Vector2(0, 0) player = coda.Object(tilesheet.image_at(0)) boss = coda.Object(BOSS_IMAGE) player_health = 100 boss_health = 300 player_dir = coda.dir.UP timer1 = coda.CountdownTimer(0.1) timer3 = coda.CountdownTimer(0.1) numberOfBullets = 0 bullets = [] bullet_owner = [] rotation_speed = 180 state = 0 last_state = 2 player_text = coda.TextObject(coda.color.BLACK, 24, "Player: ") player_hitbox = coda.Object(PROJECTILE_IMAGE) index = 0 boss_logic = coda.StateMachine() player_logic = coda.StateMachine()
def draw(screen): """Draws the state to the given screen.""" MY.background.draw(screen) MY.player1.draw(screen) MY.player2.draw(screen) rect = MY.player1.get_transformed_rect() health_bar(screen, MY.player1_hp, PLAYER_MAX_HP, coda.Vector2(rect.width, 10), rect.topleft) rect = MY.player2.get_transformed_rect() health_bar(screen, MY.player2_hp, PLAYER_MAX_HP, coda.Vector2(rect.width, 10), rect.topleft) for i in range(len(MY.bullets)): if MY.bullets[i].active: MY.bullets[i].draw(screen) for i in range(len(MY.asteroids)): if MY.asteroids[i].active: MY.asteroids[i].draw(screen)
class Data: """place changable state variables here.""" player1 = coda.Object(IMAGE_PLAYER1) player1_hp = 1 player2 = coda.Object(IMAGE_PLAYER2) player2_hp = 1 bullets = [] asteroids = [] bullet_owner = [] maxFrameTime = 0.05 window = coda.Vector2(10, 10) background = coda.Object(IMAGE_BACKGROUND)
def draw(screen): """Draws the state to the given screen.""" MY.background.draw(screen) MY.player1.draw(screen) MY.player2.draw(screen) rect = MY.player1.sprite.surface().get_rect() rect.center = MY.player1.location health_bar(screen, MY.player1_hp, PLAYER_MAX_HP, coda.Vector2(rect.width, 10), rect.topleft) rect = MY.player2.sprite.surface().get_rect() rect.center = MY.player2.location health_bar(screen, MY.player2_hp, PLAYER_MAX_HP, coda.Vector2(rect.width, 10), rect.topleft) for i in range(len(MY.bullets)): if MY.bullets[i].active: MY.bullets[i].draw(screen) for i in range(len(MY.asteroids)): if MY.asteroids[i].active: MY.asteroids[i].draw(screen)
def load_level(level_name_as_string): """Cleans up resources and loads a specified level. Can be used to reload the same level.""" cleanup() MY.tilemap = coda.utilities.read_file(level_name_as_string + ".txt") obj = MY.player for row in range(len(MY.tilemap)): for column in range(len(MY.tilemap[row])): tile_value = int(MY.tilemap[row][column]) obj = coda.Object(MY.tilesheet.image_at(tile_value)) obj.location = coda.Vector2(column * TILE_SIZE + 16, row * TILE_SIZE + 16) if tile_value == GRASS: MY.floors.append(obj) else: MY.walls.append(obj)
def update(delta_time): """Update method for shooter state.""" for event in coda.event.listing(): if coda.event.quit_game(event): coda.stop() elif coda.event.key_down(event, " "): SOUND_LASER[coda.utilities.rand(0, len(SOUND_LASER) - 1)].play() fire_bullet(1) elif coda.event.key_down(event, coda.pygame.K_RETURN): SOUND_LASER[coda.utilities.rand(0, len(SOUND_LASER) - 1)].play() fire_bullet(2) #Process rotation movement for player 1 if coda.event.key_held_down("a"): MY.player1.add_rotation(SHIP_ROTATE_SPEED * delta_time) elif coda.event.key_held_down("d"): MY.player1.add_rotation(-SHIP_ROTATE_SPEED * delta_time) #Process forward and backward movement of player 1 if coda.event.key_held_down("w"): MY.player1.add_velocity(MY.player1.rotation, SHIP_ACCEL, SHIP_MAX_SPEED) elif coda.event.key_held_down("s"): MY.player1.add_velocity(MY.player1.rotation, -SHIP_ACCEL, SHIP_MAX_SPEED) #Process rotation movement for player 2 if coda.event.key_held_down(coda.pygame.K_LEFT): MY.player2.add_rotation(SHIP_ROTATE_SPEED * delta_time) elif coda.event.key_held_down(coda.pygame.K_RIGHT): MY.player2.add_rotation(-SHIP_ROTATE_SPEED * delta_time) #Process forward and backward movement of player 2 if coda.event.key_held_down(coda.pygame.K_UP): MY.player2.add_velocity(MY.player2.rotation, SHIP_ACCEL, SHIP_MAX_SPEED) elif coda.event.key_held_down(coda.pygame.K_DOWN): MY.player2.add_velocity(MY.player2.rotation, -SHIP_ACCEL, SHIP_MAX_SPEED) MY.player1.update(delta_time) MY.player2.update(delta_time) for i in range(len(MY.asteroids)): if MY.asteroids[i].active: MY.asteroids[i].update(delta_time) screen_wrap(MY.asteroids[i], MY.window) # Check if players are outside of the screen! screen_wrap(MY.player1, MY.window) screen_wrap(MY.player2, MY.window) # Update bullets for i in range(len(MY.bullets)): # ignore if not active if MY.bullets[i].active: MY.bullets[i].update(delta_time) # Destroy bullets that hit the screen edge. if screen_wrap(MY.bullets[i], MY.window): MY.bullets[i].active = False continue for j in range(len(MY.asteroids)): if MY.bullets[i].collides_with(MY.asteroids[j]): MY.bullets[i].active = False #check collisions if MY.bullet_owner[i] == 1 and MY.bullets[i].collides_with(MY.player2): MY.player2_hp = MY.player2_hp - 1 MY.bullets[i].active = False SOUND_EXPLOSIONS[coda.utilities.rand(0, len(SOUND_EXPLOSIONS) - 1)].play() elif MY.bullet_owner[i] == 2 and MY.bullets[i].collides_with(MY.player1): MY.player1_hp = MY.player1_hp - 1 MY.bullets[i].active = False SOUND_EXPLOSIONS[coda.utilities.rand(0, len(SOUND_EXPLOSIONS) - 1)].play() for asteroid in MY.asteroids: if MY.player1.collides_with(asteroid): MY.player1.velocity = coda.Vector2(0, 0) if MY.player2.collides_with(asteroid): MY.player2.velocity = coda.Vector2(0, 0) # Check win condition if MY.player1_hp < 1: coda.state.change(2) elif MY.player2_hp < 1: coda.state.change(1)
"""This file is used to set up and register the state machine.""" import coda_kids as coda # setup WINDOW = coda.Vector2(800, 608) SCREEN = coda.start(WINDOW, "Demo Project 3") # states import introduction coda.state.Manager.register(introduction) coda.state.Manager.run(SCREEN, WINDOW, coda.color.BLACK)
"""This file is used to set up and register the state machine.""" import coda_kids as coda # setup WINDOW = coda.Vector2(632, 825) SCREEN = coda.start(WINDOW, "Demo Project 3") # states import introduction coda.state.Manager.register(introduction) coda.state.Manager.run(SCREEN, WINDOW, coda.color.BLACK)
"""This file is used to set up and register the state machine.""" import coda_kids as coda # setup WINDOW = coda.Vector2(1600, 900) SCREEN = coda.start(WINDOW, "Space Wars Tournament") # states import shooter coda.state.Manager.register(shooter) import restarter1 coda.state.Manager.register(restarter1) import restarter2 coda.state.Manager.register(restarter2) # run the game! coda.state.Manager.run(SCREEN, WINDOW, coda.color.BLACK)
def draw(screen): """Draws the state to the given screen.""" MY.player1.draw(coda.Vector2(800, 700), screen, True) MY.player2.draw(coda.Vector2(800, 100), screen, False) CONSOLE.draw(screen)
def update(delta_time): """Update method for boss battle state.""" for event in coda.event.listing(): if coda.event.quit_game(event): coda.stop() elif coda.event.key_down(event, " "): MY.player_logic.current_state = 1 x_value = 0 y_value = 0 temp = 35 MY.boss_logic.update(delta_time) MY.player_logic.update(delta_time) if MY.player_dir == coda.dir.UP: x_value = MY.player.location.x y_value = MY.player.location.y - temp elif MY.player_dir == coda.dir.DOWN: x_value = MY.player.location.x y_value = MY.player.location.y + temp elif MY.player_dir == coda.dir.LEFT: x_value = MY.player.location.x - temp y_value = MY.player.location.y elif MY.player_dir == coda.dir.RIGHT: x_value = MY.player.location.x + temp y_value = MY.player.location.y MY.player_hitbox.location = coda.Vector2(x_value, y_value) for wall in MY.walls: if MY.player.collides_with(wall): if MY.player.collision[coda.dir.DOWN]: MY.player.snap_to_object_y(wall, coda.dir.DOWN) continue if MY.player.collision[coda.dir.LEFT]: MY.player.snap_to_object_x(wall, coda.dir.LEFT) continue if MY.player.collision[coda.dir.RIGHT]: MY.player.snap_to_object_x(wall, coda.dir.RIGHT) continue if MY.player.collision[coda.dir.UP]: MY.player.snap_to_object_y(wall, coda.dir.UP) continue count = -1 for bullet in MY.bullets: count += 1 if bullet.active: if MY.bullet_owner[count] == BOSS and bullet.collides_with(MY.player): MY.player_health -= 5 bullet.active = False continue elif MY.bullet_owner[count] == PLAYER and bullet.collides_with(MY.boss): MY.boss_health -= 5 bullet.active = False continue for wall in MY.walls: if bullet.collides_with(wall): bullet.active = False continue if MY.player_hitbox.active and MY.boss.collides_with(MY.player_hitbox): MY.boss_health -= 10 MY.player_hitbox.active = False