def __init__(self): super(GameLayer, self).__init__() self.hero = Hero() self.add(self.hero,z = 1) self.bullet_batch = batch.BatchNode() self.enemy_batch = batch.BatchNode() self.add(self.bullet_batch,z = 1) self.add(self.enemy_batch,z = 1) # 保存容器 self.bullet_resuable = [] self.enemy_resuable = [] # 子弹和敌机的碰撞 # 检测区域 self.enemy_bullet_collision = collision.CollisionManagerGrid(0,config.WINDOS_WIDTH,0,config.WINDOS_HEIGHT,110,110) self.enemy_hero_collosion = collision.CollisionManagerGrid(0,config.WINDOS_WIDTH,0,config.WINDOS_HEIGHT,self.hero.width*1.25,self.hero.height*1.25) # 在屏幕上的敌机 self.active_enemies = [] self.active_bullets = [] self.codes = Codes_text() self.add(self.codes) # 每一帧的事件调度 self.schedule_interval(self.fire_bullet,0.1) self.schedule_interval(self.enemy_update,0.3) self.schedule(self.run_schedule)
def __init__(self): win_width, win_height = config.WIN_WIDTH, config.WIN_HEIGHT super(GameLayer, self).__init__() self.bg1 = sprite.Sprite('imgs/bg.png') self.bg1.image_anchor = 0, 0 self.bg1.position = 0, 0 self.bg2 = sprite.Sprite('imgs/bg.png') self.bg2.image_anchor = 0, 0 self.bg2.position = 0, win_height # 添加背景 self.add(self.bg1) self.add(self.bg2) # 添加自己的飞机 self.hero = Hero() self.add(self.hero, z=1000) # 敌机 self.enemy_batch = batch.BatchNode() self.add(self.enemy_batch) self.resuable_small_enemy_set = set() self.active_small_enemy_set = set() self.all_small_enemy_set = set() # 积分榜 self.score_board = ScoreBoard() self.add(self.score_board) # 碰撞检测 self.enemy_collision = collision_model.CollisionManagerGrid( 0, config.WIN_WIDTH, 0, config.WIN_HEIGHT, 110, 110) self.enemy_bullet_collisoin = collision_model.CollisionManagerGrid( 0, config.WIN_WIDTH, 0, config.WIN_HEIGHT - 50, 10, 10) # 消息调度 self.schedule(self.run) self.schedule_interval(self.update_enemy, 0.2)
from dungeons import * from roles import Hero if __name__ == "__main__": hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=5) levels = [ "dungeons/level1.txt", "dungeons/level2.txt", "dungeons/level3.txt" ] treasures = [ 'treasures/level1_treasures.json', 'treasures/level2_treasures.json', 'treasures/level3_treasures.json' ] enemies = [ 'enemies/level1_enemies.json', 'enemies/level2_enemies.json', 'enemies/level3_enemies.json' ] for index in range(len(levels)): level = Dungeon(levels[index], treasures[index], enemies[index], hero) level.spawn() level.move_hero()
class GameLayer(layer.Layer): is_event_handler = True def __init__(self): super(GameLayer, self).__init__() self.hero = Hero() self.add(self.hero,z = 1) self.bullet_batch = batch.BatchNode() self.enemy_batch = batch.BatchNode() self.add(self.bullet_batch,z = 1) self.add(self.enemy_batch,z = 1) # 保存容器 self.bullet_resuable = [] self.enemy_resuable = [] # 子弹和敌机的碰撞 # 检测区域 self.enemy_bullet_collision = collision.CollisionManagerGrid(0,config.WINDOS_WIDTH,0,config.WINDOS_HEIGHT,110,110) self.enemy_hero_collosion = collision.CollisionManagerGrid(0,config.WINDOS_WIDTH,0,config.WINDOS_HEIGHT,self.hero.width*1.25,self.hero.height*1.25) # 在屏幕上的敌机 self.active_enemies = [] self.active_bullets = [] self.codes = Codes_text() self.add(self.codes) # 每一帧的事件调度 self.schedule_interval(self.fire_bullet,0.1) self.schedule_interval(self.enemy_update,0.3) self.schedule(self.run_schedule) def run_schedule(self,dt): self.enemy_bullet_collision.clear() self.enemy_hero_collosion.clear() for enemy in self.active_enemies: self.enemy_bullet_collision.add(enemy) self.enemy_hero_collosion.add(enemy) for bullet in self.active_bullets: self.enemy_bullet_collision.add(bullet) resu = self.enemy_hero_collosion.objs_colliding(self.hero) result = self.enemy_bullet_collision.iter_all_collisions() for one,two in result: if type(one) == type(two): continue one.explode() two.explode() self.codes.add_score(100) if len(resu) >0: self.hero.explode() def on_bullet_resuable(self,bullet): self.bullet_resuable.append(bullet) try: self.active_bullets.remove(bullet) except: pass def on_enter(self): super(GameLayer, self).on_enter() feiji_event.push_handlers(self) def on_exit(self): super(GameLayer, self).on_exit() feiji_event.remove_handlers(self) def fire_bullet(self,dt): bullet = None if len(self.bullet_resuable)<=0: bullet = Bullet() self.bullet_batch.add(bullet) self.active_bullets.append(bullet) else: bullet = self.bullet_resuable.pop() self.active_bullets.append(bullet) # bullet.bullet_sound.play() hero_x,hero_y = self.hero.position bullet.reset_status(hero_x,hero_y,self.hero.height) def enemy_update(self,dt): enemy = None if len(self.enemy_resuable)<=0: enemy = Enemy() self.enemy_batch.add(enemy) self.active_enemies.append(enemy) else: enemy = self.enemy_resuable.pop() self.active_enemies.append(enemy) # print(enemy) enemy.reset_status() def on_enemy_resuable(self,enemy): self.enemy_resuable.append(enemy) try: self.active_enemies.remove(enemy) except: pass def on_game_over(self): self.unschedule(self.enemy_update) self.unschedule(self.fire_bullet) director.director.replace(GameOverScene(self.codes.get_score())) def on_mouse_drag(self,x,y,*args,**kwargs): self.hero.update_position(x,y) def on_game_pause(self): self.pause_scheduler() for x in self.active_bullets: x.pause_scheduler() for x in self.active_enemies: x.pause_scheduler() def on_game_resume(self): self.resume_scheduler() for x in self.active_enemies: x.resume_scheduler() for x in self.active_bullets: x.resume_scheduler()
class GameLayer(layer.Layer): """ 游戏层 """ is_event_handler = True def __init__(self): win_width, win_height = config.WIN_WIDTH, config.WIN_HEIGHT super(GameLayer, self).__init__() self.bg1 = sprite.Sprite('imgs/bg.png') self.bg1.image_anchor = 0, 0 self.bg1.position = 0, 0 self.bg2 = sprite.Sprite('imgs/bg.png') self.bg2.image_anchor = 0, 0 self.bg2.position = 0, win_height # 添加背景 self.add(self.bg1) self.add(self.bg2) # 添加自己的飞机 self.hero = Hero() self.add(self.hero, z=1000) # 敌机 self.enemy_batch = batch.BatchNode() self.add(self.enemy_batch) self.resuable_small_enemy_set = set() self.active_small_enemy_set = set() self.all_small_enemy_set = set() # 积分榜 self.score_board = ScoreBoard() self.add(self.score_board) # 碰撞检测 self.enemy_collision = collision_model.CollisionManagerGrid( 0, config.WIN_WIDTH, 0, config.WIN_HEIGHT, 110, 110) self.enemy_bullet_collisoin = collision_model.CollisionManagerGrid( 0, config.WIN_WIDTH, 0, config.WIN_HEIGHT - 50, 10, 10) # 消息调度 self.schedule(self.run) self.schedule_interval(self.update_enemy, 0.2) def run(self, dt): """ 每帧都会执行的方法 """ # 1. 更新背景图片的位置 _, bg1_y = self.bg1.position bg1_y -= 1 height = config.WIN_HEIGHT if bg1_y <= -height: bg1_y = 0 self.bg1.set_position(0, bg1_y) self.bg2.set_position(0, bg1_y + height) # 2. 检测有没有敌机碰到自己的飞机 self.enemy_collision.clear() for x in self.all_small_enemy_set: self.enemy_collision.add(x) result = self.enemy_collision.objs_colliding(self.hero) # 如果检测到死亡 if result: self.hero.alive = False # 停止敌飞机 self.stop_enemy() # 发送游戏结束事件 def game_over(dt): fj_event_dispatcher.dispatch_event('on_game_over') self.schedule_interval(game_over, 2.5) # 3. 检测子弹和敌机是否碰撞 self.enemy_bullet_collisoin.clear() for x in self.hero.active_bullets: self.enemy_bullet_collisoin.add(x) for x in self.active_small_enemy_set: self.enemy_bullet_collisoin.add(x) result = self.enemy_bullet_collisoin.iter_all_collisions() for one, two in result: if type(one) == type(two): continue else: one.explode() two.explode() # 加100分 Hero.score += 100 # 更新分数 self.score_board.add_score(100) def stop_enemy(self): self.unschedule(self.update_enemy) def update_enemy(self, dt): """ 更新敌机 """ small_enemy = None if len(self.resuable_small_enemy_set) == 0: small_enemy = EnemySmall() # self.enemy_collision.add(small_enemy) self.all_small_enemy_set.add(small_enemy) self.enemy_batch.add(small_enemy, z=999) else: small_enemy = self.resuable_small_enemy_set.pop() print('重用了敌机:', id(small_enemy)) small_enemy.reset_state() self.active_small_enemy_set.add(small_enemy) def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): # 更新飞机的位置 self.hero.update_position(x, y) def on_small_enemy_reuseable(self, enemy): # enemy.reset_position() self.resuable_small_enemy_set.add(enemy) try: self.active_small_enemy_set.remove(enemy) except: pass def on_enter(self): super(GameLayer, self).on_enter() fj_event_dispatcher.push_handlers(self) def on_exit(self): super(GameLayer, self).on_exit() fj_event_dispatcher.remove_handlers(self) self.scheduled_calls = [] self.scheduled_interval_calls = []
def play(): # define the Hero. user_input = input("Now tell us your choice please ('Fisib','Musbit','Drog'): ") you = Hero(user_input) print(you) print(""" -------------------------------------------------------------- You need more time to adapt to your newly gained powers... But, there is NO TIME!!! Alien is approaching Nyuh Kuning, the heart of Codingnomads... -------------------------------------------------------------- """) # define the Alien user_input = input("Rumor says, the alien is called: ") alien = Alien(user_input) print(alien) find_weakness = False sidekick_here = False while int(alien.iq / 5) + int(alien.energy) > int(you.power) and find_weakness is False: print(""" -------------------------------------------------------------- Now, Alien is here!!!!! You have to do something. What are you going to choose? 1. Attack 2. Run away 3. Find some help -------------------------------------------------------------- """) user_input = input("Enter your choice (1 for Attack, 2 for Run, 3 for help) ") if user_input == "1": you.power /= max(rd.choice([.05, 0.1, 0.5, 1, 1.2, 1.5, 2, 2.5, 3, 4, 5]),0) alien.energy = max(alien.energy+rd.randint(-5, 10), 0) if sidekick_here is True: sidekick_effect(alien, helper) print("With the help of your sidekick...") print(sidekick_effect(alien, helper)) print(f"You, on the other hand, have power of {int(you.power)}. ") else: # display the status of you and your enemy print(f"Alien has an IQ of {int(alien.iq)} and energy level of {int(alien.energy)} " f"which means a power level of {alien.power}, " f"while you have power of {int(you.power)}.") if int(alien.iq / 5) + int(alien.energy) > int(you.power) >= int(alien.energy): print(f"You are kicking his ass! but the Alien is very smart with an IQ of {int(alien.iq)} " f"and power of {alien.power} now.") elif int(you.power) < int(alien.energy): print("Ohhhh, Noooooooooooooo\nYou are not his match right now! Sorry...You can try again though.") continue elif user_input == "2": alien.energy += 2 print("Alien is getting stronger by the minute!") if rd.randint(1, 4) == 4: you.power = max(you.power * 2, + 10) print("However, you have rested and re-gained some power!") elif user_input == "3": alien.energy += 1 print("Alien is getting stronger by the day!") # sidekick or weakness print(""" -------------------------------------------------------------- On your way to your friends in the Holy Spirited Monkey Forest, You saved a dying spy. With his last breath, he told you that "The alien has ONE WEAKNESS." Now you have to decide whether you want to: "go find a sidekick" Or "find the weakness alone" -------------------------------------------------------------- """) user_input = input("Please enter your choice (1 for sidekick, 2 for weakness): ") if user_input == "1": name = input("Please enter the name of your sidekick: ") func = input("Please enter the ability of your sidekick: ") helper = Sidekick(name, func) print(helper) sidekick_here = True print(f"From now on, you will be accompanied by your friend {name}! Bravo!") elif user_input == "2": print("Your bravery just gained you a chance to destroy him for good!") user_input = input("Please give me a number from 1 to 10: ") if alien.weakness == int(user_input): print("Wowowow! You have found the weakness!") find_weakness = True elif alien.weakness in [int(user_input)+1, int(user_input)-1]: print("Good job! Although you have not found the weakness of the alien " "but you discovered something else...") print(alien_purpose(alien, int(user_input))) else: print("Sorry, maybe you will find it next time!") else: print("I see you have decided to move on from this opportunity...") alien.energy += 1 print("Alien is getting stronger by the day!") elif user_input == "q": print("Thank you for trying to help the land of Codingnomads. Hope to see you soon.") break else: print("You are the chosen one! You have to do something! Luckily, " "you have another chance before it's too late!") print("Congratulations! You have defeated the alien and saved the land of Codingnomads!")
# -*- coding: utf-8 -*- ''' Jerry's games ''' import map import actor_lines as al from roles import Hero # 欢迎词 print(al.welcome) # 故事背景 print(al.background) # 创建角色Jerry Jerry=Hero('Jerry', '男', 9) Jerry.detail() Jerry.look(Jerry)