def test_is_done(self): hero = Hero() beast = Beast() self.assertTrue(is_done(hero, beast, 25)) self.assertFalse(is_done(hero, beast, 18)) hero.health = -1 self.assertTrue(is_done(hero, beast, 18)) hero.health = 1 beast.health = -2 self.assertTrue(is_done(hero, beast, 18))
def test_damage_done(self): hero = Hero() beast = Beast() # as much as the beast has defence hero.strength = 40 initial_health = beast.health attack(hero, beast) self.assertEqual(initial_health, beast.health)
def test_dmg_to_be_done(self): hero = Hero() beast = Beast() hero.strength = 80 initial_dmg = hero.damage_to_be_done beast.luck = 0 attack(hero, beast) self.assertNotEqual(initial_dmg, hero.damage_to_be_done)
def test_attack(self): hero = Hero() beast = Beast() hero.strength = 80 beast.luck = 0 initial_health = beast.health attack(hero, beast) self.assertNotEqual(initial_health, beast.health)
class TestHero(unittest.TestCase): hero = Hero() def test_name(self): self.assertEqual(self.hero.name, 'Orderus') def test_attribute_health(self): self.assertTrue(self.hero.health >= 70 and self.hero.health <= 100) def test_attribute_strength(self): self.assertTrue(self.hero.strength >= 70 and self.hero.strength <= 80) def test_attribute_defence(self): self.assertTrue(self.hero.defence >= 45 and self.hero.defence <= 55) def test_attribute_speed(self): self.assertTrue(self.hero.speed >= 40 and self.hero.speed <= 50) def test_attribute_luck(self): self.assertTrue(self.hero.luck >= 10 and self.hero.luck <= 30) def test_offensive_skills(self): self.assertTrue(self.hero.offensive_skills) def test_defensive_skills(self): self.assertTrue(self.hero.defensive_skills)
class SkillsActionsTest(unittest.TestCase): mock = Mock() hero = Hero() beast = Beast() def test_magic_shield_action(self): hero = Hero() beast = Beast() beast.luck = 0 attack(hero, beast) initial_dmg_to_be_done = hero.damage_to_be_done self.assertTrue(initial_dmg_to_be_done > 0) magic_shield_action(hero, beast) self.assertTrue(hero.damage_to_be_done == initial_dmg_to_be_done // 2) def test_rapid_strike_action(self): hero = Hero() beast = Beast() mock = Mock() expected = [attack(hero, beast)] mock.rapid_strike_action(hero, beast) mock.mock_calls == expected
def main_menu(self, hero): from characters.hero import Hero battle_menu_engine = Battle_Menu() about_characters_engine = About_Characters() store_engine = Store() upgrade_levels_engine = Upgrade_Levels() print("\033[3;30;47m==============") print(" MAIN MENU ") print("==============\033[0;37;40m") Hero().hero_status(hero) print("\033[1;30;40m1. Duel Arena", " " * 7) print("2. Store"," " * 12) print("3. Upgrade Levels", " " * 3) print("4. About Characters", " " * 1) print("5. Quit", " " * 13) print("\033[1;36;40m> ", end=' ') raw_input = input() print("\033[0;37;40m") if raw_input == "1": battle_menu_engine.menu_selection(hero) # Go to battle menu elif raw_input == "2": store_engine.shopping(hero) elif raw_input == "3": upgrade_levels_engine.upgrades(hero) elif raw_input == "4": about_characters_engine.character_description(hero) elif raw_input == "5": print("Goodbye.") quit(1) else: print("Invalid input {}".format(raw_input)) Menu().main_menu(hero)
def test_play_game(self): hero = Hero() beast = Beast() turn_number = 1 mock = Mock() expected = [ is_done(hero, beast, turn_number), load_round(), print_turn_number(turn_number), hero.do_skills(hero, beast, hero.offensive_skills, True), attack(hero, beast), beast.do_skills(beast, hero, beast.offensive_skills, True), attack(beast, hero) ] mock.play_game(hero, beast, turn_number) mock.mock_calls == expected
def __init__(self): super().__init__() # Title and Icon pygame.display.set_caption("Space Invader") # Boundary self.min_boundary_x = 0 self.max_boundary_x = self.width - 64 self.min_boundary_y = 0 self.max_boundary_y = self.height - 64 # Music mixer.music.load("sound/background.wav") mixer.music.play() self.bullet_sound = mixer.Sound("sound/laser.wav") self.collision_sound = mixer.Sound("sound/explosion.wav") # Player self.hero = Hero() self.hero_image = pygame.image.load(self.hero.get_image()) self.hero.set_mask(pygame.mask.from_surface(self.hero_image)) self.bullet = self.hero.get_bullet() self.bullet_image = pygame.image.load(self.bullet.get_image()) self.bullet.set_mask(pygame.mask.from_surface(self.bullet_image)) self.enemy = Enemy() self.enemy_image = pygame.image.load(self.enemy.get_image()) self.enemy.set_mask(pygame.mask.from_surface(self.enemy_image)) self.enemies = self.get_enemy(1) # Score and level self.score_value = 0 self.font = pygame.font.Font('freesansbold.ttf', 32) self.score_text = self.font.render(f"Score: {str(self.score_value)}", True, (0, 255, 0)) self.level = 1 self.level_text = self.font.render(f"Level: {self.level}", True, (0, 255, 0)) # Game over self.lose = False self.stop_count = 0 self.game_over_font = pygame.font.Font('freesansbold.ttf', 64) self.game_over_text = self.game_over_font.render( "GAME OVER", True, (255, 255, 255))
def test_lucky_dodge(self): hero = Hero() beast = Beast() initial_health = beast.health beast.luck = 100 attack(hero, beast) self.assertEqual(initial_health, beast.health)
def test_magic_shield_action(self): hero = Hero() beast = Beast() beast.luck = 0 attack(hero, beast) initial_dmg_to_be_done = hero.damage_to_be_done self.assertTrue(initial_dmg_to_be_done > 0) magic_shield_action(hero, beast) self.assertTrue(hero.damage_to_be_done == initial_dmg_to_be_done // 2)
def test_rapid_strike_action(self): hero = Hero() beast = Beast() mock = Mock() expected = [attack(hero, beast)] mock.rapid_strike_action(hero, beast) mock.mock_calls == expected
def upgrades(self, hero): from menus.main_menu import Menu from characters.hero import Hero raw_input = 0 while raw_input != 9: power_price_increase = round( ((hero.upgrades["Power_Level"] + 1) * (hero.upgrades["Power_Level"] + 1) + 20) * 2.3) defense_price_increase = round( ((hero.upgrades["Defense_Level"] + 1) * (hero.upgrades["Defense_Level"] + 1) + 20) * 2.3) health_price_increase = round( ((hero.upgrades["Health_Level"] + 1) * (hero.upgrades["Health_Level"] + 1) + 20) * 2.3) upgrade_price = { "Power_Level": power_price_increase, "Defense_Level": defense_price_increase, "Health_Level": health_price_increase, } print("\033[3;30;47m==================== ") print(" Upgrade Levels ") print("==================== \033[0;37;40m") Hero().hero_status(hero) print("\033[1;30;40m1. +1 Power Level ({} coins)".format( upgrade_price["Power_Level"])) print("2. +1 Defense Level ({} coins)".format( upgrade_price["Defense_Level"])) print("3. +1 Health Level ({} coins)".format( upgrade_price["Health_Level"])) print("9. Back to main menu") print("\033[1;36;40m> ", end=' ') raw_input = input() print("\033[0;37;40m") if raw_input == "1": upgrade_type = "Power_Level" elif raw_input == "2": upgrade_type = "Defense_Level" elif raw_input == "3": upgrade_type = "Health_Level" elif raw_input == "9": print("Back to Main Menu") Menu().main_menu(hero) else: print("Invalid input {}".format(raw_input)) Upgrade_Levels().upgrades(hero) if hero.coins < upgrade_price[upgrade_type]: print("Not enough coins") else: hero.coins -= upgrade_price[upgrade_type] hero.upgrades[upgrade_type] += 1 hero.upgrade_hero_levels(hero, upgrade_type)
def create_chars_dict(bg_width, bg_height, screen): goblins = 3 # creates a dictionary of the characters with the hero and the monster chars_dict = {"monster": Enemy("monster.png", increment = 30, bg = (bg_width, bg_height), time = 0.3, screen = screen), "hero": Hero("hero.png", increment = 1, bg = (bg_width, bg_height), screen = screen) } # creates the number of goblins desired and append to the characters' dictionary for goblin_count in range(1,goblins+1): key = "goblin" + str(goblin_count) chars_dict[key] = Enemy("goblin.png", increment = 10, bg = (bg_width, bg_height), time = 0.3, screen = screen) to_return = (chars_dict, goblin_count) return to_return
def menu_selection(self, hero): from menus.main_menu import Menu from characters.hero import Hero battle_engine = Battle() main_menu_engine = Menu() print("\033[3;30;47m============== ") print(" DUEL ARENA ") print("============== \033[0;37;40m") Hero().hero_status(hero) print("Choose your opponent ") print("\033[1;30;40m1. Goblin ") print("2. Medic ") print("3. Shadow ") print("4. Wizard ") print("5. Ranger ") print("6. Zombie ") print("7. Dragon ") print(" ") print("9. Drink Tonic ({}) ".format(hero.items["Tonic"])) print("0. Back to Main Menu ") print("\033[1;36;40m> ", end=' ') raw_input = input() print("\033[0;37;40m") if raw_input == "1": enemy = Goblin() elif raw_input == "2": enemy = Medic() elif raw_input == "3": enemy = Shadow() elif raw_input == "4": enemy = Wizard() elif raw_input == "5": enemy = Ranger() elif raw_input == "6": enemy = Zombie() elif raw_input == "7": enemy = Dragon() elif raw_input == "9": if hero.items["Tonic"] > 0: hero.restore_health(hero) else: print("No tonic in inventory") Battle_Menu().menu_selection(hero) elif raw_input == "0": main_menu_engine.main_menu(hero) else: print("Invalid input {}".format(raw_input)) Battle_Menu().menu_selection(hero) battle_engine.do_battle(hero, enemy) # go to battle
class ActionTest(unittest.TestCase): hero = Hero() beast = Beast() def test_lucky_dodge(self): hero = Hero() beast = Beast() initial_health = beast.health beast.luck = 100 attack(hero, beast) self.assertEqual(initial_health, beast.health) def test_damage_done(self): hero = Hero() beast = Beast() # as much as the beast has defence hero.strength = 40 initial_health = beast.health attack(hero, beast) self.assertEqual(initial_health, beast.health) def test_attack(self): hero = Hero() beast = Beast() hero.strength = 80 beast.luck = 0 initial_health = beast.health attack(hero, beast) self.assertNotEqual(initial_health, beast.health) def test_dmg_to_be_done(self): hero = Hero() beast = Beast() hero.strength = 80 initial_dmg = hero.damage_to_be_done beast.luck = 0 attack(hero, beast) self.assertNotEqual(initial_dmg, hero.damage_to_be_done) def test_is_done(self): hero = Hero() beast = Beast() self.assertTrue(is_done(hero, beast, 25)) self.assertFalse(is_done(hero, beast, 18)) hero.health = -1 self.assertTrue(is_done(hero, beast, 18)) hero.health = 1 beast.health = -2 self.assertTrue(is_done(hero, beast, 18)) def test_play_game(self): hero = Hero() beast = Beast() turn_number = 1 mock = Mock() expected = [ is_done(hero, beast, turn_number), load_round(), print_turn_number(turn_number), hero.do_skills(hero, beast, hero.offensive_skills, True), attack(hero, beast), beast.do_skills(beast, hero, beast.offensive_skills, True), attack(beast, hero) ] mock.play_game(hero, beast, turn_number) mock.mock_calls == expected
class GameWindow(BaseWindow): def __init__(self): super().__init__() # Title and Icon pygame.display.set_caption("Space Invader") # Boundary self.min_boundary_x = 0 self.max_boundary_x = self.width - 64 self.min_boundary_y = 0 self.max_boundary_y = self.height - 64 # Music mixer.music.load("sound/background.wav") mixer.music.play() self.bullet_sound = mixer.Sound("sound/laser.wav") self.collision_sound = mixer.Sound("sound/explosion.wav") # Player self.hero = Hero() self.hero_image = pygame.image.load(self.hero.get_image()) self.hero.set_mask(pygame.mask.from_surface(self.hero_image)) self.bullet = self.hero.get_bullet() self.bullet_image = pygame.image.load(self.bullet.get_image()) self.bullet.set_mask(pygame.mask.from_surface(self.bullet_image)) self.enemy = Enemy() self.enemy_image = pygame.image.load(self.enemy.get_image()) self.enemy.set_mask(pygame.mask.from_surface(self.enemy_image)) self.enemies = self.get_enemy(1) # Score and level self.score_value = 0 self.font = pygame.font.Font('freesansbold.ttf', 32) self.score_text = self.font.render(f"Score: {str(self.score_value)}", True, (0, 255, 0)) self.level = 1 self.level_text = self.font.render(f"Level: {self.level}", True, (0, 255, 0)) # Game over self.lose = False self.stop_count = 0 self.game_over_font = pygame.font.Font('freesansbold.ttf', 64) self.game_over_text = self.game_over_font.render( "GAME OVER", True, (255, 255, 255)) def start(self): # Game Loop clock = pygame.time.Clock() while self.running: # fps = 60 # clock.tick(fps) self.redraw_window() if self.lose: self.stop_count += 1 if self.stop_count > 180: self.running = False else: continue for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.hero.move_left() if self.hero.get_x_cord() <= self.min_boundary_x: self.hero.set_x_cord(self.min_boundary_x) if keys[pygame.K_RIGHT]: self.hero.move_right() if self.hero.get_x_cord() >= self.max_boundary_x: self.hero.set_x_cord(self.max_boundary_x) if keys[pygame.K_UP]: self.hero.move_up() if self.hero.get_y_cord() <= self.min_boundary_y: self.hero.set_y_cord(self.min_boundary_y) if keys[pygame.K_DOWN]: self.hero.move_down() if self.hero.get_y_cord() >= self.max_boundary_y: self.hero.set_y_cord(self.max_boundary_y) if keys[pygame.K_SPACE]: if not self.bullet.get_state(): self.bullet_sound.play() self.hero.load_bullet() if self.bullet.get_state(): self.hero.fire_bullet(self.enemies, self.update_scoreboard) self.start_attack() def redraw_window(self): self.screen.blit(self.background_image, (0, 0)) self.screen.blit(self.level_text, (10, 10)) self.screen.blit(self.score_text, (self.width - self.score_text.get_width() - 10, 10)) if self.lose: self.screen.blit( self.game_over_text, (self.width / 2 - self.game_over_text.get_width() / 2, 250)) if self.bullet.get_state(): self.screen.blit( self.bullet_image, (self.bullet.get_x_cord(), self.bullet.get_y_cord())) self.screen.blit(self.hero_image, (self.hero.get_x_cord(), self.hero.get_y_cord())) for enemy in self.enemies: self.screen.blit(self.enemy_image, (enemy.get_x_cord(), enemy.get_y_cord())) pygame.display.update() def start_attack(self): for enemy in self.enemies: enemy.start_moving() if enemy.get_x_cord() >= self.max_boundary_x: enemy.move_down() if enemy.get_x_cord() <= self.min_boundary_x: enemy.move_down() if enemy.get_y_cord() >= 400: self.game_over() def update_scoreboard(self): self.score_value += 1 self.score_text = self.font.render(f"Score: {str(self.score_value)}", True, (0, 255, 0)) def get_enemy(self, amount): index = 0 enemy_list: List[Enemy] = [] while index <= amount: enemy = copy.copy(self.enemy) enemy.set_coord() enemy_list.append(enemy) index += 1 return enemy_list def game_over(self): self.lose = True
def character_description(self, hero): from menus.main_menu import Menu from characters.hero import Hero #\u2694\uFE0F \033[1;36;40mDefense: {}\U0001F6E1 \033[1;31;40mHealth: {}/{} \u2764\uFE0F \033[0;37;40m".format(hero.power, hero.defense, hero.health, hero.max_health)) print("\033[3;30;47m======================") print("CHARACTER DESCRIPTIONS") print("======================\033[0;37;40m") print( "\033[1;34;40mName: \033[1;32;40m\u2694\uFE0F Power \033[1;36;40m\U0001F6E1 Defense \033[1;31;40m\u2764\uFE0F Health \n \033[1;34;40m-Special Ability/ Probability\033[0;37;40m" ) print("_________________________________________________") print( "\033[1;34;40m{}: \033[1;32;40m\u2694\uFE0F P: {} \033[1;36;40m\U0001F6E1 D: {} \033[1;31;40m\u2764\uFE0F H: {} \n \033[1;34;40m- {}\033[0;37;40m" .format(Hero().name, Hero().power, Hero().defense, Hero().health, Hero().special_desc)) print( "\033[1;35;40m{}: \033[1;32;40m\u2694\uFE0F P: {} \033[1;36;40m\U0001F6E1 D: {} \033[1;31;40m\u2764\uFE0F H: {} \n \033[1;35;40m- {}\033[0;37;40m" .format(Goblin().name, Goblin().power, Goblin().defense, Goblin().health, Goblin().special_desc)) print( "\033[1;34;40m{}: \033[1;32;40m\u2694\uFE0F P: {} \033[1;36;40m\U0001F6E1 D: {} \033[1;31;40m\u2764\uFE0F H: {} \n \033[1;34;40m- {}\033[0;37;40m" .format(Medic().name, Medic().power, Medic().defense, Medic().health, Medic().special_desc)) print( "\033[1;35;40m{}: \033[1;32;40m\u2694\uFE0F P: {} \033[1;36;40m\U0001F6E1 D: {} \033[1;31;40m\u2764\uFE0F H: {} \n \033[1;35;40m- {}\033[0;37;40m" .format(Shadow().name, Shadow().power, Shadow().defense, Shadow().health, Shadow().special_desc)) print( "\033[1;34;40m{}: \033[1;32;40m\u2694\uFE0F P: {} \033[1;36;40m\U0001F6E1 D: {} \033[1;31;40m\u2764\uFE0F H: {} \n \033[1;34;40m- {}\033[0;37;40m" .format(Wizard().name, Wizard().power, Wizard().defense, Wizard().health, Wizard().special_desc)) print( "\033[1;35;40m{}: \033[1;32;40m\u2694\uFE0F P: {} \033[1;36;40m\U0001F6E1 D: {} \033[1;31;40m\u2764\uFE0F H: {} \n \033[1;35;40m- {}\033[0;37;40m" .format(Ranger().name, Ranger().power, Ranger().defense, Ranger().health, Ranger().special_desc)) print( "\033[1;34;40m{}: \033[1;32;40m\u2694\uFE0F P: {} \033[1;36;40m\U0001F6E1 D: {} \033[1;31;40m\u2764\uFE0F H: {} \n \033[1;34;40m- {}\033[0;37;40m" .format(Zombie().name, Zombie().power, Zombie().defense, Zombie().health, Zombie().special_desc)) print( "\033[1;35;40m{}: \033[1;32;40m\u2694\uFE0F P: {} \033[1;36;40m\U0001F6E1 D: {} \033[1;31;40m\u2764\uFE0F H: {} \n \033[1;35;40m- {}\033[0;37;40m" .format(Dragon().name, Dragon().power, Dragon().defense, Dragon().health, Dragon().special_desc)) print("\n Press 1 to return to Main Menu") print("\033[1;36;40m> ", end=' ') raw_input = input() print("\033[0;37;40m") if raw_input == "1": Menu().main_menu(hero) else: About_Characters().character_description(hero)
def shopping(self, hero): from menus.main_menu import Menu from characters.hero import Hero price_list = { "Tonic": 8, "Helmet": 150, "Sword": 200, "Shield": 225, "Chainmail": 750, "Zombie_Axe": 2000, "Dragon_Fire_Shield": 5000, } raw_input = 0 while raw_input != 9: print("\033[3;30;47m===============") print(" Store ") print("===============\033[0;37;40m") Hero().hero_status(hero) print( "\033[1;34;40mInventory - \033[1;36;40mTonic: {} \U0001F943 \033[0;37;40m " .format(hero.items["Tonic"])) print("\033[1;34;40mEquipment - \033[1;36;40mHelmet: {} Sword: {}". format(hero.items["Helmet"], hero.items["Sword"])) print(" Shield: {} Chainmail: {} ".format(hero.items["Shield"], hero.items["Chainmail"])) print(" Zombie Axe: {} ".format(hero.items["Zombie_Axe"])) print(" Dragon Fire Shield: {} \033[0;37;40m ".format( hero.items["Dragon_Fire_Shield"])) print("What do you want to buy? ") print("\033[1;30;40m1. Tonic ({} coins)".format( price_list["Tonic"])) print("2. Helmet ({} coins)".format(price_list["Helmet"])) print("3. Sword ({} coins)".format(price_list["Sword"])) print("4. Shield ({} coins)".format(price_list["Shield"])) print("5. Chainmail ({} coins)".format(price_list["Chainmail"])) print("6. Zombie Axe ({} coins)".format(price_list["Zombie_Axe"])) print("7. Dragon Fire Shield ({} coins)".format( price_list["Dragon_Fire_Shield"])) print("9. Back to main menu") print("\033[1;36;40m> ", end=' ') raw_input = input() print("\033[0;37;40m") if raw_input == "1": item = "Tonic" elif raw_input == "2": item = "Helmet" elif raw_input == "3": item = "Sword" elif raw_input == "4": item = "Shield" elif raw_input == "5": item = "Chainmail" elif raw_input == "6": item = "Zombie_Axe" elif raw_input == "7": item = "Dragon_Fire_Shield" elif raw_input == "9": print("Back to Main Menu") Menu().main_menu(hero) else: print("Invalid input {}".format(raw_input)) Store().shopping(hero) if hero.coins < price_list[item]: print("Not enough coins") elif hero.items[item] == 1 and item != "Tonic": print("Already own", item) else: hero.coins -= price_list[item] hero.items[item] += 1 hero.equip_items(hero, item)
import random import time from characters.hero import Hero from menus.main_menu import Menu if __name__ == "__main__": hero = Hero() main_menu_engine = Menu() main_menu_engine.main_menu(hero) # Starts game at main menu