def setUp(self): self.name = 'Rado' self.title = 'RadoRado' self.health = 70 self.mana = 80 self.mana_regen_rate = 15 self.hero = Hero( self.name, self.title, self.health, self.mana, self.mana_regen_rate ) self.spell = Spell('Kodene', 23, 30, 2) self.weapon = Weapon('AXE', 23)
fleet_1.fleet_stats() # instantiate a herd herd_1 = Herd() # assign the herd a name herd_1.herd_name = "land before time" # add dinosaurs to herd --> for multiple, separate with commas print(herd_1.add_to_herd(dino, gino, nino_brown)) # show herd stats herd_1.herd_stats() # Instantiate a weapon object. Display weapon. Generate a weapon weapon_1 = Weapon() weapon_1.display_weapon() print(weapon_1.generate_weapon()) # display weapon and attain weapon weapon_1.display_weapon() print(dex.attain_weapon(weapon_1)) # display robot full stats dex.robot_stats() # attack dinosaur print(dex.attack(nino_brown)) print(dex.attack(nino_brown)) print(dex.attack(nino_brown)) print(dex.attack(nino_brown))
def main(): """Main game function""" # docstring because I keep forgetting the word pygame.init() # Starts pygame size = (SCREENWIDTH, SCREENHEIGHT) screen = pygame.display.set_mode(size) # Creates window pygame.display.set_caption("Through The Ages") # important variable xcoord = 0 ycoord = 0 speed = 8 debug = False render_tile_list = pygame.sprite.Group() render_tree_list = pygame.sprite.Group() # Sprite and terrain generation tile_sheet = SpriteSheet("tile.png") tile_image = tile_sheet.get_image(0, 0, 64, 64) tree_sheet = SpriteSheet("plant.png") tree_image = tree_sheet.get_image(0, 0, 65, 155), tree_sheet.get_image(128, 289, 97, 125) trunk_image = tree_sheet.get_image(9, 435, 65, 155), tree_sheet.get_image(102, 469, 97, 125) map_size = 100 tile_size = 64 # Generates terrain and tree font = pygame.font.Font("freesansbold.ttf", 72) # Generating Terrain text = font.render("Generating Terrain", 1, WHITE) text_width, text_height = font.size("Generating Terrain") screen.blit(text, ((SCREENWIDTH - text_width)/2, (SCREENHEIGHT - text_height)/2)) pygame.display.flip() terrain_gen = GenTerrain(tile_size, map_size, map_size, tile_image) tree_gen = GenTrees(tile_size, map_size, tree_image, trunk_image, 0.1) tile_list = pygame.sprite.Group() # Add sprites to sprite groups for i in terrain_gen.tile_list: tile_list.add(i) tree_list = pygame.sprite.Group() for i in tree_gen.tree_list: tree_list.add(i) # Adds player sprite player = Player() player_sprites = pygame.sprite.Group() player_sprites.add(player) weapon = Weapon(10, "Axe", weapon_axe_sheet) weapon_sprites = pygame.sprite.Group() weapon_sprites.add(weapon) enemy_list = pygame.sprite.Group() for i in range(5): i = Enemy(speed, enemy_sheet) i.rect.x = random.randint(player.rect.x, player.rect.x + tile_size*4) i.rect.y = random.randint(player.rect.y, player.rect.y + tile_size*4) enemy_list.add(i) print("Enemies", enemy_list) rand_x_pos = random.randint(tile_size, ((map_size-1)*tile_size)-SCREENWIDTH/2) rand_y_pos = random.randint(tile_size, ((map_size-1)*tile_size)-SCREENHEIGHT/2) for i in tile_list: i.movex(-rand_x_pos) i.movey(-rand_y_pos) for i in tree_list: i.movex(-rand_x_pos) i.movey(-rand_y_pos) # Creating GUI health_bar_back = HealthBarBack(player) health_bar = HealthBar(player) gui = GUI() gui_sprites = pygame.sprite.Group() gui_sprites.add(health_bar_back) gui_sprites.add(health_bar) gui_sprites.add(gui) carry_on = True # Allowing the user to close the window... clock = pygame.time.Clock() move_sprites = (tile_list, tree_list, enemy_list) vulnerability = True vul_count = 0 weapon_dead = False weapon_hit_list = [] while carry_on: # Main game loop for event in pygame.event.get(): if event.type == pygame.QUIT: carry_on = False if event.type == pygame.MOUSEBUTTONDOWN: buttons_pressed = pygame.mouse.get_pressed() if buttons_pressed[0] and not player.dead: weapon.state = "attack" screen.fill(BLACK) # Drawing on Screen # Movement code keys = pygame.key.get_pressed() vx = 0 vy = 0 if keys[pygame.K_w]: vy = speed if keys[pygame.K_a]: vx = speed if keys[pygame.K_s]: vy = -speed if keys[pygame.K_d]: vx = -speed """ bug with diagonal speed code causes tiles and trees to glitch and move weirdly, possibly due to rendering code? if vx != 0 and vy != 0: vx /= 1.414 vy /= 1.414 """ ycoord, xcoord = movexy(move_sprites, vx, vy, xcoord, ycoord) if keys[pygame.K_F3] and debug is False: debug = True pygame.time.wait(100) elif keys[pygame.K_F3] and debug is True: debug = False pygame.time.wait(100) pos = xcoord, ycoord # other keyboard and mouse input # rendering code for terrain for i in tile_list: if abs(i.rect.x - player.rect.x) < (SCREENWIDTH / 2 + tile_size): if abs(i.rect.y - player.rect.y) < (SCREENHEIGHT / 2 + tile_size): render_tile_list.add(i) for i in tree_list: if abs(i.rect.x - player.rect.x) < (SCREENWIDTH / 2 + tile_size * 2): if abs(i.rect.y - player.rect.y) < (SCREENHEIGHT / 2 + tile_size * 2): render_tree_list.add(i) # collision code """Tree Collisions""" tree_hit_list = pygame.sprite.spritecollide(player, render_tree_list, False, pygame.sprite.collide_mask) if not player.dead: for _ in tree_hit_list: ycoord, xcoord = movexy(move_sprites, -vx, -vy, xcoord, ycoord) """Enemy Collisions""" enemy_hit_list = pygame.sprite.spritecollide(player, enemy_list, False, pygame.sprite.collide_mask) for _ in enemy_hit_list: if vulnerability: player.health -= 10 vulnerability = False if not vulnerability: vul_count += 1 if vul_count > 10: vul_count = 0 vulnerability = True """Weapon Collisions""" if not player.dead: weapon_hit_list = pygame.sprite.spritecollide(weapon, enemy_list, False, pygame.sprite.collide_mask) for enemy in weapon_hit_list: if not player.dead: if not enemy.vulnerability and weapon.state == "attack": enemy.damaged(weapon.damage) print("damaged") for enemy in enemy_list: if enemy.dead: enemy.kill() enemy = "Dead" print(enemy) # Update sprite lists tile_list.update() tree_list.update() player_sprites.update() enemy_list.update(player) weapon_sprites.update(player) gui_sprites.update(player) render_tile_list.draw(screen) # Draw sprites (order matters) enemy_list.draw(screen) player_sprites.draw(screen) weapon_sprites.draw(screen) render_tree_list.draw(screen) gui_sprites.draw(screen) fps = clock.get_fps() # debug menu if debug: debug_menu(fps, screen, pos, SCREENWIDTH) # Inventory add_text(10, ("Health:" + " " + str(round(player.health))), WHITE, (70, 149), screen) # endgame if player.dead: text_width, text_height = text_size(72, "Game Over") add_text(72, "Game Over", WHITE, ((SCREENWIDTH - text_width)/2, (SCREENHEIGHT - text_height - 200)/2), screen) vulnerability = False if not weapon_dead: weapon_dead = True weapon.kill() weapon = None pygame.display.flip() # Refresh Screen clock.tick(30) # Number of frames per second e.g. 60 render_tile_list.empty() render_tree_list.empty() pygame.quit()
class TestHero(unittest.TestCase): def setUp(self): self.name = 'Rado' self.title = 'RadoRado' self.health = 70 self.mana = 80 self.mana_regen_rate = 15 self.hero = Hero( self.name, self.title, self.health, self.mana, self.mana_regen_rate ) self.spell = Spell('Kodene', 23, 30, 2) self.weapon = Weapon('AXE', 23) def test_init(self): self.assertEqual(self.name, self.hero._name) self.assertEqual(self.title, self.hero._title) self.assertEqual(self.health, self.hero._health) self.assertEqual(self.mana, self.hero._mana) self.assertEqual(self.mana_regen_rate, self.hero._mana_regen_rate) self.assertEqual(self.health, self.hero._max_health) self.assertEqual(self.mana, self.hero._max_mana) self.assertEqual(None, self.hero._weapon_equipped) self.assertEqual(None, self.hero._spell_learned) def test_is_alive(self): self.assertTrue(self.hero.is_alive()) self.hero._health = 0 self.assertFalse(self.hero.is_alive()) self.hero._health = -10 self.assertFalse(self.hero.is_alive()) def test_learn_spell(self): self.hero.learn_spell(self.spell) self.assertEqual(self.hero._spell_learned, self.spell) def test_equip_weapon(self): self.hero.equip_weapon(self.weapon) def test_can_cast(self): self.assertFalse(self.hero.can_cast()) self.hero._mana = 10 self.hero._spell_learned = self.spell self.assertFalse(self.hero.can_cast()) self.hero._mana = self.hero._max_mana self.assertTrue(self.hero.can_cast()) def test_known_as(self): self.assertEqual( self.hero.known_as(), 'Rado the RadoRado' ) def test_get_health(self): self.assertEqual(self.hero.get_health(), self.hero._health) def test_get_mana(self): self.assertEqual(self.hero.get_mana(), self.hero._mana) def test_take_damage(self): damage = 30 health = self.hero._health self.hero.take_damage(damage) self.assertEqual(self.hero._health, health - damage) self.hero.take_damage(9000) self.assertEqual(self.hero._health, 0) def test_take_healing(self): self.hero._health = 0 self.assertFalse(self.hero.take_healing(10)) self.hero._health = self.hero._max_health self.assertTrue(self.hero.take_healing(10)) self.assertEqual(self.hero._health, self.hero._max_health) health = 10 healing = 20 self.hero._health = health self.hero._max_health = health + healing * 2 self.assertTrue(self.hero.take_healing(healing)) self.assertEqual(self.hero._health, healing + health) def test_take_mana(self): self.hero._mana = 0 init = self.hero._mana mana = 10 a_lot_mana = 9000 self.hero.take_mana(mana) self.assertEqual(self.hero._mana, init + mana) self.hero.take_mana(a_lot_mana) self.assertEqual(self.hero._mana, self.hero._max_mana) def test_attack(self): self.hero._weapon_equipped = None self.assertEqual(self.hero.attack(), 0) self.hero.equip_weapon(self.weapon) self.assertEqual(self.hero.attack(), self.weapon.get_damage()) self.assertEqual(self.hero.attack('spell'), 0) self.hero.learn_spell(self.spell) self.assertEqual(self.hero.attack('spell'), self.spell.get_damage()) self.hero._mana = 0 self.assertEqual(self.hero.attack('spell'), 0)
#!/usr/bin/env python from weapon_class import Weapon airplane1 = Weapon( 'Mitsubishi A6M Zero', 5200, 750, 10 ) airplane2 = Weapon( 'Focke-Wulfe 190', 8000, 450, 13 ) airplane3 = Weapon( 'Grumman F6F Hellcat', 10000, 550, 4 ) print airplane1.weapon_type(), airplane1.weapon_weight(), airplane1.weapon_range(), airplane1.weapon_armor_class() print airplane2.weapon_type(), airplane2.weapon_weight(), airplane2.weapon_range(), airplane2.weapon_armor_class() print airplane3.weapon_type(), airplane3.weapon_weight(), airplane3.weapon_range(), airplane3.weapon_armor_class() def get_lowest_armor_class(): L = [ airplane1.weapon_armor_class(), airplane2.weapon_armor_class(), airplane3.weapon_armor_class() ] L.sort() return L[0] print "The winning airplane has an armor class of: ", get_lowest_armor_class() def find_airplane_type_with_lowest_armor_class(): L = [ airplane1, airplane2, airplane3 ] for i in L: if i.weapon_armor_class() == get_lowest_armor_class(): return i.weapon_type() winning_airplane_type = find_airplane_type_with_lowest_armor_class() print "the winning airplane is: ", winning_airplane_type def get_winning_airplane_weight(): L = [ airplane1, airplane2, airplane3 ] for i in L:
#!/usr/bin/env python from weapon_class import Weapon tank1 = Weapon( 'Sherman', 10000, 140, 5 ) tank2 = Weapon( 'Panzer', 15000, 120, 15 ) tank3 = Weapon( 'M1 Abrams', 20000, 180, 12 ) print tank1.weapon_type(), tank1.weapon_weight(), tank1.weapon_armor_class() print tank2.weapon_type(), tank2.weapon_weight(), tank2.weapon_armor_class() print tank3.weapon_type(), tank3.weapon_weight(), tank3.weapon_armor_class() def get_lowest_armor_class(): L = [ tank1.weapon_armor_class(), tank2.weapon_armor_class(), tank3.weapon_armor_class() ] L.sort() return L[0] winning_armor_class = get_lowest_armor_class() print "The winning tank has an armor class of: ", winning_armor_class def find_tank_type_with_lowest_armor_class(): L = [ tank1, tank2, tank3 ] for i in L: if i.weapon_armor_class() == get_lowest_armor_class(): return i.weapon_type() winning_tank_type = find_tank_type_with_lowest_armor_class() print "the winning tank is: ", winning_tank_type def get_winning_tank_weight(): L = [ tank1, tank2, tank3 ]