def attack_with_spell_range(hero: Hero, enemy: Enemy): spell_attack_counter = 0 while hero.can_cast() and spell_attack_counter < hero.spell.cast_range: enemy.take_damage(hero.attack_by_spell()) spell_attack_counter += 1 print_entity_name_and_health('Enemy', enemy.health) if not enemy.is_alive(): print_has_been_slain('Enemy') break print(f'Enemy moves closer!')
def __init__(self, window, first=True): self.window = window self.window_width, self.window_height = window.get_size() self.running = True self.willTabQuit = True self.score = 0 self.bg = pygame.image.load(str(Path('./assets/bg.jpg'))) self.font = pygame.font.SysFont('Hack', 14, True) self.midFont = pygame.font.SysFont('Hack', 24, True) self.bigFont = pygame.font.SysFont('Hack', 80, True) self.player = Player(self.window) self.bullet = Bullet(self.window, self.player) self.enemy = Enemy(self.window) self.display = True self.count = 0 self.state = 'menu' if first else 'start'
def regular_fight(hero: Hero, enemy: Enemy): while True: enemy.take_damage(hero.attack()) print_entity_name_and_health('Enemy', enemy.health) if not enemy.is_alive(): print_has_been_slain('Enemy') break hero.take_damage(enemy.attack()) print_hero_takes_damage(hero, enemy) print_entity_name_and_health(hero.name, hero.health) if not hero.is_alive(): print_has_been_slain(hero.known_as()) break
def test_if_attack_with_spell_range_works_when_cast_range_is_0(self): health = 1 hero = Hero() hero.spell = Spell(cast_range=0) enemy = Enemy(health=health) attack_with_spell_range(hero, enemy) self.assertEqual(enemy.health, health)
def test_if_regular_fight_works_when_hero_is_killed_during_the_fight(self): health = 1 hero = Hero(health=health) enemy = Enemy(health=health, damage=1) exp = 0 regular_fight(hero, enemy) self.assertEqual(hero.health, exp)
def test_if_attack_with_spell_range_works_when_enemy_is_killed_during_the_attack(self): health = 1 hero = Hero() hero.spell = Spell(damage=1, cast_range=1) enemy = Enemy(health=health) exp = 0 attack_with_spell_range(hero, enemy) self.assertEqual(enemy.health, exp)
def fight_enemy(hero: Hero): from entities.enemy import Enemy enemy = Enemy.spawn_enemy() attack_with_spell_range(hero, enemy) if not enemy.is_alive(): input('\nPress Enter to continue... ') return regular_fight(hero, enemy) input('\nPress Enter to continue... ')
def level_4_preboss(enemy_sprite_group, player): player.reset_position() enemy_spawn_y = 40 for i in range(3): enemy_spawn_x = 0 for x in range(15): new_enemy = Enemy(200, basic_enemy_four.convert(), enemy_spawn_x, enemy_spawn_y) enemy_sprite_group.add(new_enemy) enemy_spawn_x += 40 enemy_spawn_y += 40
def loadEnemy(enemyElem, textures, guns, entities): texture = textures[enemyElem.findtext("texture")] posX = displayManager.WIDTH * float(enemyElem.findtext("posX")) posZ = 1000 * displayManager.SCALE * float(enemyElem.findtext("posZ")) health = float(enemyElem.findtext("health")) gunData = enemyElem.find("gun") gun = guns[gunData.findtext("name")].copy() gun.offsetX = int(gunData.findtext("offsetX")) * displayManager.SCALE offsetY = int(gunData.findtext("offsetY")) * displayManager.SCALE gun.offsetY = offsetY gun.mainOffsetY = offsetY enemy = Enemy(posX, posZ, texture, health, gun) name = enemyElem.findtext("name") if name is not None: entities[name] = enemy return enemy
def load_level(level_loaded: Level): tile_x = level_loaded.TILE_X tile_y = level_loaded.TILE_Y offset_w = level_loaded.offset_w offset_h = level_loaded.offset_h entities = pygame.sprite.Group() platforms = [] enemies = [] players = [] # block : -1 # empty : 0 # ground : 1 # stairs : 2 # bar : 3 # fly : 4 # build the level y = offset_h for i_row in range(0, level_loaded.TILE_Y_NUM): x = offset_w level_row = level_loaded.level[i_row] for j_col in range(0, level_loaded.TILE_X_NUM): level_block = level_row[j_col] level_loaded.level_matrix[i_row][j_col] = 0 if level_block == "▉" or level_block == "P": e = PlatformBlock(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = -1 if level_block == "╬" or level_block == "E": e = StairsBlock(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = 2 if level_block == "-" or level_block == "B": e = BarBlock(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = 3 if level_block == "_" or level_block == "H": e = BarHBlock(x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = 3 if level_block == "⊟" or level_block == "G": e = GoalBlock(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = -1 if level_block == "⊏" or level_block == "L": e = GoalBlockLeft(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = -1 if level_block == "⊐" or level_block == "R": e = GoalBlockRight(level_loaded, x, y) platforms.append(e) entities.add(e) level_loaded.level_matrix[i_row][j_col] = -1 if level_block == "⭐" or level_block == "S": e = StarItem(level_loaded, x, y) platforms.append(e) entities.add(e) if level_block == "g": e = Ghost( level_loaded, x, y, "Ghost", bg_color="#72A877", image_file='images/sprites/characters/enemies/ghost.png', flip=True, jump_sound=constants.PLAYER_P1_JUMP ) entities.add(e) enemies.append(e) if level_block == "t": e = Enemy( level_loaded, x, y, "Tritannus", None, None, None, None, bg_color="#72A877", image_file='images/winx_raw/tritannus_01.png', flip=True, jump_sound=constants.PLAYER_P1_JUMP ) entities.add(e) enemies.append(e) if level_block == "1" or level_block == "Y": e = Player( level_loaded, x, y, "Y", K_UP, K_DOWN, K_RIGHT, K_LEFT, constants.PLAYER_P1_COLOR_BG, 'images/winx_raw/tritannus_01.png', 'images/winx_raw/tritannus_01.png', # constants.IMAGE_P1, # constants.IMAGE_P1_TRANSFORMED, flip=True, jump_sound=constants.PLAYER_P1_JUMP ) level_loaded.num_players = level_loaded.num_players + 1 players.append(e) player_p1 = e if level_block == "2" or level_block == "X": e = Player( level_loaded, x, y, "X", K_w, K_s, K_d, K_a, constants.PLAYER_P2_COLOR_BG, constants.IMAGE_P2, constants.IMAGE_P2_TRANSFORMED, flip=True, jump_sound=constants.PLAYER_P2_JUMP ) level_loaded.num_players = level_loaded.num_players + 1 players.append(e) player_p2 = e x += tile_x y += tile_y for i_row in range(0, level_loaded.TILE_Y_NUM-2): for j_col in range(0, level_loaded.TILE_X_NUM): if i_row < level_loaded.TILE_Y_NUM-1: if \ level_loaded.level_matrix[i_row+2][j_col] in [0, 4] and \ level_loaded.level_matrix[i_row+1][j_col] in [0, 4] and \ level_loaded.level_matrix[i_row][j_col] == 0: level_loaded.level_matrix[i_row][j_col] = 4 for p in players: entities.add(p) platforms.append(p) return entities, platforms, enemies, players
class Game: def __init__(self, window, first=True): self.window = window self.window_width, self.window_height = window.get_size() self.running = True self.willTabQuit = True self.score = 0 self.bg = pygame.image.load(str(Path('./assets/bg.jpg'))) self.font = pygame.font.SysFont('Hack', 14, True) self.midFont = pygame.font.SysFont('Hack', 24, True) self.bigFont = pygame.font.SysFont('Hack', 80, True) self.player = Player(self.window) self.bullet = Bullet(self.window, self.player) self.enemy = Enemy(self.window) self.display = True self.count = 0 self.state = 'menu' if first else 'start' # Main Game State def run(self): if self.state == 'menu': self.menu() elif self.state == 'start': self.start() elif self.state == 'over': self.game_over() # Starts game def start(self): for event in pygame.event.get(): if event.type is pygame.QUIT: self.running = False if event.type is pygame.KEYDOWN: if event.key is pygame.K_TAB and self.willTabQuit: self.running = False if event.key is pygame.K_a: self.player.toggleMoving(True) self.player.moveLeft() if event.key is pygame.K_d: self.player.toggleMoving(True) self.player.moveRight() if event.key is pygame.K_w: self.player.toggleMoving(True) self.player.moveUp() if event.key is pygame.K_s: self.player.toggleMoving(True) self.player.moveDown() if event.key is pygame.K_SPACE: if self.bullet.bullet_state == 'ready': self.bullet.setShootingPos(self.player.playerX) self.bullet.bullet_state = 'shoot' if event.type is pygame.KEYUP: self.player.toggleMoving(False) # Draw Entities self.draw_window() # Draws All Entities On Window def draw_window(self): # Moves Player When Key Pressed if self.player.moving: self.player.move() # Draws Bullet On State self.bullet.draw() # Draws Enemy self.enemy.move() self.enemy.draw() # Checks Bullet-Enemy Collide if self.bullet.checkCollide(self.enemy): self.enemy.randomizePosition() self.score += 1 # Game Over State if self.enemy.isGameOver(): self.state = 'over' # Draws Player self.player.draw() # Update Player Position self.window.blit(self.update_pos(), (self.window_width - 140, 0)) # Draw Score self.window.blit(self.update_score(), (5, 20)) # Start Menu def menu(self): for event in pygame.event.get(): if event.type is pygame.QUIT: self.running = False if event.type is pygame.KEYDOWN: if event.key is pygame.K_TAB and self.willTabQuit: self.running = False if event.key is pygame.K_SPACE: self.__init__(self.window, False) self.state = 'start' # Game Title text title = 'Space Shooter' title_text = self.bigFont.render(title, 1, pygame.Color('white')) title_width, title_height = title_text.get_size() title_x, title_y = self.window_width / 2 - title_width / \ 2, self.window_height/2 - title_height/2 # Start Prompt Text start = 'Press Space To Start' start_text = self.midFont.render(start, 1, pygame.Color('white')) start_width, start_height = start_text.get_size() start_x, start_y = self.window_width / 2 - start_width / \ 2, self.window_height/2 - start_height/2 + (title_height/2) + 10 self.window.blit(title_text, (title_x, title_y)) # Blinking text self.count += 1 if self.count > 30: self.count = 0 self.display = not self.display if self.display: self.window.blit(start_text, (start_x, start_y)) # Game Over Menu def game_over(self): for event in pygame.event.get(): if event.type is pygame.QUIT: self.running = False if event.type is pygame.KEYDOWN: if event.key is pygame.K_TAB and self.willTabQuit: self.running = False if event.key is pygame.K_SPACE: self.__init__(self.window, False) self.state = 'start' # Game Over Text over = 'GAME OVER' game_over_text = self.bigFont.render(over, 1, pygame.Color('white')) over_width, over_height = game_over_text.get_size() over_x, over_y = self.window_width / 2 - over_width / \ 2, self.window_height/2 - over_height/2 # Continue Prompt Text con = 'Press Space To Continue' continue_text = self.midFont.render(con, 1, pygame.Color('white')) con_width, con_height = continue_text.get_size() con_x, con_y = self.window_width / 2 - con_width / \ 2, self.window_height/2 - con_height/2 + (over_height/2) + 10 # Score Text score = f'Score: {self.score}' score_text = self.midFont.render(score, 1, pygame.Color('white')) score_width, score_height = score_text.get_size() score_x, score_y = self.window_width / 2 - score_width / \ 2, self.window_height/2 - score_height/2 - (over_height/2) - 10 self.window.blit(game_over_text, (over_x, over_y)) self.window.blit(score_text, (score_x, score_y)) # Blinking Text self.count += 1 if self.count > 30: self.count = 0 self.display = not self.display if self.display: self.window.blit(continue_text, (con_x, con_y)) def update_pos(self): pos = f'X: {self.player.playerX} Y: {self.player.playerY}' pos_text = self.font.render(pos, 1, pygame.Color("white")) return pos_text def update_score(self): score = f'Score: {self.score}' score_text = self.font.render(score, 1, pygame.Color("white")) return score_text
def level_4_boss(enemy_sprite_group): boss_spawn_x = 50 for i in range(3): boss = Enemy(6666, kanna_stage_one, boss_spawn_x, 10) boss_spawn_x += 350 enemy_sprite_group.add(boss)
# Game over game_over_font = pygame.font.Font('freesansbold.ttf', 100) game_over_posX = 100 game_over_posY = 200 game_over = False def render_game_over(x, y): game_over = game_over_font.render('Game Over', True, (255, 255, 255)) screen.blit(game_over, (x, y)) # Enemy enemies = [] for i in range(0, 6): enemies.append(Enemy(screenX, screenY)) # Collision check def collision(x1, y1, x2, y2): dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2) if dist < 30: return True return False # Game loop running = True while running: screen.fill((0, 0, 0))
screen = pygame.display.set_mode(SCREEN_SIZE) screen_rect = screen.get_rect() screen.fill(WHITE) pygame.display.set_caption("Pygar.io") player = Player() food = [] poison = [] enemies = [] score = 0 pygame.time.set_timer(generate_food, 600) pygame.time.set_timer(generate_poison, 1200) pygame.time.set_timer(increase_score, 200) # for i in range(0, 4): enemies.append(Enemy()) food.append(Food()) ## TODO: Make a separate GameScreen class. def is_colliding(player, cell): dist = math.sqrt(((player.get_x() - cell.get_x())**2) + ((player.get_y() - cell.get_y())**2)) if (dist < (player.get_size() + cell.get_size() - 1) / 2): return True else: return False def clear_foods(food_list):