def init(self): self.HealthBars = [HealthBar(0), HealthBar(1)] self.attacks = [] self.Background = pygame.sprite.Group(Background()) self.players = [] self.whoAmI = None self.gameOver = False self.gameOverFont = pygame.font.Font(pygame.font.get_default_font(), 48) self.instructFont = pygame.font.Font(pygame.font.get_default_font(), 24)
class Character(pygame.sprite.Sprite): def __init__(self, x, idle_anim, walk_anim, attack_anim): pygame.sprite.Sprite.__init__(self) self.idle_anim = idle_anim self.walk_anim = walk_anim self.attack_anim = attack_anim self.image = self.idle_anim[0] self.current_anim = self.idle_anim self.rect = self.image.get_rect() self.sprite_id = 0 self.health = 100 self.total_health = 100 self.healthbar = HealthBar(self, (20, 131, 7)) self.hitbox = pygame.Rect( (x, FLOOR - self.image.get_height() + 20 + HITBOX_OFFSET), HITBOX) self.dead = False def change_anim(self, anim): if anim != self.current_anim: self.current_anim = anim self.sprite_id = 0 self.image = self.current_anim[(self.sprite_id)//12] self.rect = self.image.get_rect() def update(self): if self.dead: self.image.set_alpha(self.image.get_alpha() - 10) if self.image.get_alpha() <= 10: self.kill() self.healthbar.kill() else: self.sprite_id += 1 if self.sprite_id >= len(self.current_anim)*12: self.sprite_id = 0 self.image = self.current_anim[(self.sprite_id)//12] self.image.set_alpha(255) self.rect.centerx = self.hitbox.centerx self.rect.y = self.hitbox.y - HITBOX_OFFSET if self.health < 0: self.dead = True self.health = 0 if self.health > 100: self.health = 100 self.healthbar.update()
def init(self): self.fighterGroup0 = pygame.sprite.Group( Fighter(self.width // 4, self.height, 0, 1)) self.fighterGroup1 = pygame.sprite.Group( Fighter(3 * self.width // 4, self.height, 1, -1)) self.fighters = [self.fighterGroup0, self.fighterGroup1] self.HealthBars = [HealthBar(0), HealthBar(1)] self.attackGroup0 = pygame.sprite.Group() self.attackGroup1 = pygame.sprite.Group() self.attacks = [self.attackGroup0, self.attackGroup1] self.Background = pygame.sprite.Group(Background()) #Text Stuff self.gameOver = False self.gameOverFont = pygame.font.Font(pygame.font.get_default_font(), 48) self.instructFont = pygame.font.Font(pygame.font.get_default_font(), 24)
class City(Tile): def __init__(self,dicParams): Tile.__init__(self,dicParams) self.hp = HealthBar(10) self.hp.addToEntityManager() self.hp.setAnimation("TowerHealthBarStart", "TowerHealthBarEnd", "TowerHealthBarMiddle") self.rBoundingCircle = 32 self.setCollisionBlock(Vec2d(tileWidth *2,tileHeigth*2)) self.isDead = False self.lastShoot = mE.getGameTime() # - self.cooldownShoot self.cooldownShoot = 5.0 self.damage = 1 self.range = 100 self.target = None def update(self): lMonsters = mE.mEntityManager.getTagEntitys("Monster") for m in lMonsters: if isOnCollision(m,self): self.hp.takeDamage(0.1) if(self.hp.health <= 0 ): self.die() if(self.hp.health == 5): mE.mGlobalVariables["General"].laugh() diffLastShoot = mE.getGameTime() - self.lastShoot if(diffLastShoot >= self.cooldownShoot): if(self.target == None or (self.target != None and (self.target.isDead or distanceEntity(self, self.target) < self.range))): chooseTarget(self) if(self.target != None): chooseTarget(self) self.lastShoot = mE.getGameTime() projectile = Projectile(self.target) mE.mEntityManager.addEntity(projectile, "Projectil", "Monsters") mE.mAnimationManager.setEntityAnimation(projectile, "SimpleProjectil") projectile.tower = self projectile.setCollisionBlock(Vec2d(10,10)) projectile.setPosition(self.position + Vec2d(25,25)) def setPosition(self,x,y): Tile.setPosition(self,x,y) self.hp.setPosition(self.position) self.setCollisionBlock(Vec2d(tileWidth *2,tileHeigth*2)) def die(self): mE.mGlobalVariables["EndGame"] = True self.isDead = True
def startArena(self, monsterIndex): """ Instantiates the arena against a monster and changes the stage :param monster: """ self.isOverworld = False self.arena = Arena(self, self.player, monsterIndex) self.currentMonsterHealthBar = HealthBar(self.monsters[monsterIndex]) self.arena.draw(self.screen) self.screen.print()
def __init__(self, x, y, order, speed, hp, bounty): pygame.sprite.Sprite.__init__(self) #call Sprite initializer self.image, self.rect = RandomTowerDefense.load_image( 'battlecruiser2.png', -1) self.rect.topleft = (x, y) self.velocity = [0.0, 0.0] self.order = order self.x_speed = speed self.accellerate(self.x_speed, 0) self.health = hp self.hp_bar = HealthBar.HealthBar(hp, x, y) self.bounty = bounty
def __init__(self,dicParams): Tile.__init__(self,dicParams) self.hp = HealthBar(10) self.hp.addToEntityManager() self.hp.setAnimation("TowerHealthBarStart", "TowerHealthBarEnd", "TowerHealthBarMiddle") self.rBoundingCircle = 32 self.setCollisionBlock(Vec2d(tileWidth *2,tileHeigth*2)) self.isDead = False self.lastShoot = mE.getGameTime() # - self.cooldownShoot self.cooldownShoot = 5.0 self.damage = 1 self.range = 100 self.target = None
def __init__(self, x, idle_anim, walk_anim, attack_anim): pygame.sprite.Sprite.__init__(self) self.idle_anim = idle_anim self.walk_anim = walk_anim self.attack_anim = attack_anim self.image = self.idle_anim[0] self.current_anim = self.idle_anim self.rect = self.image.get_rect() self.sprite_id = 0 self.health = 100 self.total_health = 100 self.healthbar = HealthBar(self, (20, 131, 7)) self.hitbox = pygame.Rect( (x, FLOOR - self.image.get_height() + 20 + HITBOX_OFFSET), HITBOX) self.dead = False
def __init__(self, x, idle_anim, walk_anim, attack_anim): pygame.sprite.Sprite.__init__(self) self.idle_anim = idle_anim self.walk_anim = walk_anim self.attack_anim = attack_anim self.current_anim = walk_anim self.sprite_id = 0 self.image = self.current_anim[self.sprite_id] self.rect = self.image.get_rect() self.healthbar = HealthBar(self, (153, 51, 102)) self.hitbox = pygame.Rect( (x, FLOOR - self.image.get_height() + 20 + HITBOX_OFFSET), HITBOX) self.can_move = True self.attacking = False self.stun = 0
def game(CURRENT_LEVEL, post): global hasname hero = Player(55, 55) entities = pygame.sprite.Group() platforms = [] traps = [] enemies = [] bullets = [] effects = [] bonuses = [] spauns = [] entities.add(hero) is_w = False level = [] finish = Finish(0, 0) loadLevel(str(CURRENT_LEVEL.GetNum()), level) pygame.init() screen = pygame.display.set_mode(DISPLAY) pygame.display.set_caption("GAME!!!") info_string = pygame.Surface((1000, 50)) info_string.fill((204, 204, 255)) inf_font = pygame.font.Font(None, 24) big_inf_font = pygame.font.Font(None, 40) bgi = image.load('images/background' + str(CURRENT_LEVEL.GetNum()) + '.jpg') bg = pygame.Surface((4000, 800)) heroBar = HealthBar(hero.rect.x, hero.rect.y) bg.blit(bgi, (0, 0)) punkts = [(370, 200, u'Play (%s level)' % CURRENT_LEVEL.GetNum(), (250, 250, 30), (250, 30, 250), 0), (370, 300, u'Records', (250, 250, 30), (250, 30, 250), 1), (370, 400, u'Exit', (250, 250, 30), (250, 30, 250), 2)] game = menu.Menu((punkts)) game.menu(screen) total_level_width = len(level[0]) * PLATFORM_WIDTH total_level_height = len(level) * PLATFORM_HEIGHT camera = Camera(camera_configure, total_level_width, total_level_height) left = right = up = f_up = f_dwn = shoot = ch_w = False go = True parse_level(level, entities, platforms, enemies, traps, finish, bonuses, spauns) hero.SetAnimEvery(post) while go: for e in pygame.event.get(): if e.type == QUIT: go = False if e.type == KEYDOWN and e.key == K_LEFT: left = True if e.type == KEYDOWN and e.key == K_RIGHT: right = True if e.type == KEYUP and e.key == K_RIGHT: right = False if e.type == KEYUP and e.key == K_LEFT: left = False if e.type == KEYDOWN and e.key == K_SPACE: up = True if e.type == KEYUP and e.key == K_SPACE: up = False if e.type == KEYUP and e.key == K_ESCAPE: game.menu(screen) if e.type == KEYDOWN and e.key == K_UP: f_up = True if e.type == KEYUP and e.key == K_UP: f_up = False if e.type == KEYDOWN and e.key == K_DOWN: f_dwn = True if e.type == KEYUP and e.key == K_DOWN: f_dwn = False if e.type == KEYDOWN and e.key == K_f: shoot = True if e.type == KEYUP and e.key == K_f: shoot = False hero.fire(bullets, f_up, f_dwn) if e.type == KEYUP and e.key == K_r: hero.change_weapon() if e.type == KEYUP and e.key == K_CAPSLOCK: ch_w = True dh = 0 if (hero.rect.y) / 5 > 100: dh = -100 else: dh = -hero.rect.y / 5 screen.blit(bg, (-hero.rect.x / 8, dh + 50)) camera.update(hero) for en in enemies: en.update(bullets, hero.rect.x, hero.rect.y, effects, platforms) if en.obj.rect.x == -1000: enemies.remove(en) del en for e in entities: screen.blit(e.image, camera.apply(e)) screen.blit(heroBar.image, camera.apply(heroBar)) screen.blit(hero.weapon.image, camera.apply(hero.weapon)) hero.update(left, right, up, shoot, platforms, traps, enemies, bonuses, effects) for b in bullets: b.update(platforms, effects) screen.blit(b.image, camera.apply(b)) if b.rect.x == -1000: bullets.remove(b) del b for b in bonuses: b.update() for s in spauns: s.update(enemies, entities, (s.rect.x * s.rect.y) % (time.get_ticks())) screen.blit(s.image, camera.apply(s)) if s.rect.x == -1000: spauns.remove(s) del s for eff in effects: eff.update() screen.blit(eff.image, camera.apply(eff)) if eff.rect.x == -1000: effects.remove(eff) del eff for t in traps: t.update() if t.rect.x == -1000: traps.remove(t) del t heroBar.update(hero.rect.x, hero.rect.y, hero.xp) if (shoot and hero.weapon.id == 2 and not is_w): water_sprite = Water(hero.rect.x, hero.rect.y, hero.rotation) is_w = True if (shoot and hero.weapon.id == 2 and is_w): water_sprite.update(hero.rect.x, hero.rect.y, hero.rotation) screen.blit(water_sprite.image, camera.apply(water_sprite)) for t in traps: t.try_water(water_sprite) for s in spauns: s.try_water(water_sprite) if (not shoot and hero.weapon.id == 2 and is_w): is_w = False info_string.fill((204, 204, 255)) info_string.blit(xp_image, (10, 5)) info_string.blit(inf_font.render(str(hero.xp), 1, (255, 0, 0)), (40, 5)) info_string.blit( inf_font.render(u'bullets: ' + str(hero.weapon.bls), 1, (255, 0, 0)), (10, 30)) info_string.blit(hero.weapon.imager, (80, 20)) info_string.blit( inf_font.render(u'Leverl ' + str(CURRENT_LEVEL.GetName()), 1, (0, 0, 0)), (300, 0)) info_string.blit( big_inf_font.render(u'HP: ' + str(POINTS.GetPoints()), 1, (255, 100, 0)), (300, 15)) screen.blit(info_string, (0, 0)) pygame.display.update() timer.tick(80) #print(timer.get_fps()) if sprite.collide_rect(hero, finish) or hero.xp <= 0 or ch_w: if CURRENT_LEVEL.GetNum() == 4 or hero.xp <= 0: if CURRENT_LEVEL.GetNum() == 4: screen.blit( big_inf_font.render(u'Level! ', 1, (255, 100, 0)), (300, 200)) pygame.display.update() time.wait(2000) else: ShowIfDie(screen, hero, POINTS.GetPoints()) records = HighScores() records.AddRecord(CURRENT_LEVEL.GetName(), POINTS.GetPoints()) CURRENT_LEVEL.SetNum(CURRENT_LEVEL.GetNum() - CURRENT_LEVEL.GetNum() + 1) else: screen.blit( big_inf_font.render( u'You have %s levels! ' % CURRENT_LEVEL.GetNum(), 1, (255, 100, 0)), (300, 200)) POINTS.ChangePoints(10000) pygame.display.update() time.wait(2000) CURRENT_LEVEL.SetNum(CURRENT_LEVEL.GetNum() + 1) pygame.display.update() go = False
(y * blocksize[1]) + blocksize[1] / 2], playersize, size) elif c == "e": Enemy("rsc/enemy/slime 1.png", [(x * blocksize[0]) + blocksize[0] / 2, (y * blocksize[1]) + blocksize[1] / 2], playersize) for each in all.sprites(): each.fixLocation(player.offsetx, player.offsety) levels = ["rsc/levels/level1", "rsc/levels/level2"] level = 0 loadLevel(levels[level]) player1 = players.sprites()[0] moneycounter = CounterDisplay(player1.money, (20, height - 10)) healthbar = HealthBar(player1, (50, 50)) player1.living = False while True: while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: start = True if (event.key == pygame.K_RSHIFT or event.key == pygame.K_LSHIFT): if fullscreen == 0: fullscreen = pygame.FULLSCREEN else:
clock.tick(60) #-----GAME SETUP------------- bg.kill() bg = Background("Screen Display/Background/images/space.png") player1 = PlayerShip(2) missile = None PowerShield("PowerUps/Shield/images/shield.png", [random.randint(50, width - 50), (500)]) RepairKit("PowerUps/RepairKit/images/repairkit.png", [random.randint(50, width - 50), (200)]) EndLine("Screen Display/Background/images/greenComplete.png", startPos=[width / 2, 50]) MissileBar(player1.missiles, [1000, height - 30]) #ShieldBar(PowerShield, [1000, height - 80]) HealthBar(player1.lives, [100, height - 25]) Hyperspeed("PowerUps/Boost/images/powerup.png", [random.randint(50, width - 50), (200)]) Nuke("PowerUps/GuidedMissile/images/nuke.png", [550, 50]) SlowMo("PowerUps/Boost/images/slowdown.png", [random.randint(50, width - 50), (200)]) while mode == "play" and player1.lives > 0: for event in pygame.event.get(): #print event.type if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if player1.missiles > 0: missile = Missile([ player1.rect.centerx - 59, player1.rect.centery - 115
class Enemy(pygame.sprite.Sprite): def __init__(self, x, idle_anim, walk_anim, attack_anim): pygame.sprite.Sprite.__init__(self) self.idle_anim = idle_anim self.walk_anim = walk_anim self.attack_anim = attack_anim self.current_anim = walk_anim self.sprite_id = 0 self.image = self.current_anim[self.sprite_id] self.rect = self.image.get_rect() self.healthbar = HealthBar(self, (153, 51, 102)) self.hitbox = pygame.Rect( (x, FLOOR - self.image.get_height() + 20 + HITBOX_OFFSET), HITBOX) self.can_move = True self.attacking = False self.stun = 0 def change_anim(self, anim): if anim != self.current_anim: self.current_anim = anim self.sprite_id = 0 self.image = self.current_anim[(self.sprite_id)//12] self.rect = self.image.get_rect() def check_can_move(self, limit, unit_list): if self.hitbox.colliderect(limit): self.can_move = False else: hitbox_list = [] for i in unit_list: if type(i)==type(self): hitbox_list.append(i.hitbox) hitbox_list.remove(self.hitbox) collisions = self.hitbox.collidelistall(hitbox_list) if not collisions: self.can_move = True else: for hitbox_idx in collisions: if hitbox_list[hitbox_idx].x < self.hitbox.x: self.can_move = False break self.can_move = True if self.can_move: self.change_anim(self.walk_anim) else: self.change_anim(self.attack_anim) def check_can_move_ranged(self, limit, unit_list): if self.has_frontline: if self.hitbox.colliderect(limit): self.can_move = False self.change_anim(self.attack_anim) return hitbox_list = [] for i in unit_list: hitbox_list.append(i.hitbox) hitbox_list.remove(self.hitbox) if self.hitbox.collidelist(hitbox_list) == -1: self.change_anim(self.walk_anim) self.can_move = True else: self.can_move = False self.change_anim(self.attack_anim) else: self.can_move = False self.change_anim(self.attack_anim) def update(self): if self.stun > 0: self.stun -= 1 self.can_move = False self.change_anim(self.idle_anim) self.rect.centerx = self.hitbox.centerx self.rect.y = self.hitbox.y - HITBOX_OFFSET self.healthbar.update() self.sprite_id += 1 if self.sprite_id >= len(self.current_anim)*12: self.sprite_id = 0 self.image = self.current_anim[(self.sprite_id)//12] self.attacking = not (self.can_move or self.stun > 0)
def __init__(self, screen_width, screen_height): self.screen = Screen(self, screen_width, screen_height) self.screen_width = screen_width self.screen_height = screen_height start = Entity(0, 0, "", "", 45, 0) start.setSprite("sprites/startingScreen.txt", ) start.drawArena(self.screen) self.screen.print() self.textBox = TextBox(0, 15) self.isOverworld = True # False if the current stage is the Arena self.overworld = Overworld(self, width=40, height=11, overworld_x=60, overworld_y=4) self.player = Player( overworld_x=81, overworld_y=9, sprites_path="sprites/player.txt", overworldChar="P", arena_x=50, arena_y=10, defensePower=100, evade=0.2, health=1000, crit=0.2, moveset=[ Attack(name='Heavy Attack', damage=450, hitChance=0.3, statusEffect=StatusEffect( name="Shock", duration=6, damagePerTurn=40, sprite_path="sprites/shockEffect.csv")), Attack(name='Regular Attack', damage=150, hitChance=0.7, statusEffect=StatusEffect( name="Poison", duration=4, damagePerTurn=20, sprite_path="sprites/poisonEffect.csv")), Attack(name='Light Attack', damage=60, hitChance=1, statusEffect=StatusEffect( name="Spice", duration=6, damagePerTurn=40, sprite_path="sprites/spiceEffect.csv")) ]) # array containing all monsters self.monsters = [ Monster(overworld_x=84, overworld_y=11, sprites_path='sprites/pepperSprite.txt', overworldChar="M", arena_x=30, arena_y=10, defensePower=20, health=1000, evade=0.1, crit=0.3, moveset=[ Attack(name='Ultimate attack', damage=300, hitChance=0.9999, statusEffect=StatusEffect( name="Poison", duration=4, damagePerTurn=37, sprite_path="sprites/poisonEffect.csv")), Attack(name='Spice Attack', damage=160, hitChance=0.7, statusEffect=StatusEffect( name="Shock", duration=6, damagePerTurn=8, sprite_path="sprites/shockEffect.csv")), Attack(name='Light Attack', damage=120, hitChance=1, statusEffect=StatusEffect( name="Spice", duration=3, damagePerTurn=14, sprite_path="sprites/spiceEffect.csv")) ]), #devil pepper ascci Monster(overworld_x=74, overworld_y=8, sprites_path='sprites/pepperSprite.txt', overworldChar="M", arena_x=30, arena_y=10, defensePower=10, health=600, evade=0.1, crit=0.6, moveset=[ Attack(name='Ultimate attack', damage=250, hitChance=0.9999, statusEffect=StatusEffect( name="Spice", duration=6, damagePerTurn=38, sprite_path="sprites/spiceEffect.csv")), Attack(name='Regular Attack', damage=240, hitChance=0.6), Attack(name='Light Attack', damage=200, hitChance=1, statusEffect=StatusEffect( name="Spice", duration=3, damagePerTurn=6, sprite_path="sprites/spiceEffect.csv")) ]), #fire pepper assci orsomething Monster(overworld_x=88, overworld_y=8, sprites_path='sprites/pepperSprite.txt', overworldChar="M", arena_x=30, arena_y=10, defensePower=50, health=1500, evade=0.1, crit=0.3, moveset=[ Attack(name='Ultimate attack', damage=100, hitChance=0.9999, statusEffect=StatusEffect( name="Spice", duration=4, damagePerTurn=78, sprite_path="sprites/spiceEffect.csv")), Attack(name='Regular Attack', damage=50, hitChance=0.7, statusEffect=StatusEffect( name="Shock", duration=4, damagePerTurn=70, sprite_path="sprites/shockEffect.csv")), Attack(name='Light Attack', damage=30, hitChance=1, statusEffect=StatusEffect( name="Poison", duration=5, damagePerTurn=68, sprite_path="sprites/poisonEffect.csv")) ]) ] self.playerHealthBar = HealthBar(self.player) self.healthpot = [ HealthPot(overworld_x=78, overworld_y=11, overworldChar="+", ASCII=["+"], health=600), HealthPot(overworld_x=84, overworld_y=7, overworldChar="+", ASCII=["+"], health=600) ] #Counters for stats self.damageInflicted = 0 self.damageReceived = 0 self.score = 0 self.arena = None self.reset = False self.exit = False self.loop()