def mega_run(id): if id == 'game': run_game() if id == 'menu': run_menu(commons.menu_id) if id == 'end': run_end() game_FSM = FSM() game_FSM.add_state(state_('menu', 'game')) game_FSM.add_state(state_('game', 'end')) game_FSM.add_state(state_('end', 'menu')) game_FSM.change_state('menu') while True: if commons.isQuit: run_exit() elif commons.pause: run_pause() elif commons.isDead: run_over() else: game_FSM.think() if commons.restart: reset() if commons.giveup: retry()
class World(object): def __init__(self,size,enemy_list,font): self.size=size self.font=font self.entities={} self.bullets={} self.champ=None self.chapters=[] self.bullet_id=0 self.entity_id=0 self.background=pygame.surface.Surface(commons.screen_size).convert() self.background.fill((200,255,255)) self.enemy_freq={} self.enemy_list=enemy_list self.boss=None self.status=False #pass or not self.FSM=FSM() self.time=0 self.current_chapter=None self.clear=False self.FSM.add_state(pushing(self)) self.FSM.add_state(last_enemy(self)) self.FSM.add_state(complete(self)) self.FSM.add_state(endless(self)) self.FSM.add_state(gameover(self)) self.FSM.add_state(congrats(self)) self.check_point=0 self.pannel=None #reset the world before a new game def reset(self): self.status=False self.FSM.change_state(commons.mode[0]) self.entities={} self.bullets={} if self.current_chapter.id=='Endless': self.champ.reset() else: self.champ.reload() self.bullet_id=0 self.entity_id=0 self.status=False self.pannel.reset() #add and delete the characters to the world def add_pannel(self,pannel): self.pannel=pannel def add_champ(self,champ): self.champ=champ def add_entity(self,entity): self.entities[self.entity_id]=entity entity.id=self.entity_id self.entity_id+=1 def add_bullet(self,bullet): self.bullets[self.bullet_id]=bullet bullet.id=self.bullet_id self.bullet_id+=1 def add_chapter(self,chapter): self.chapters.append(chapter) def change_chapter(self): self.current_chapter=self.chapters[commons.mode[1]] def injert_boss(self): boss_data=self.current_chapter.boss_data self.boss=Boss(self,boss_data) self.add_entity(self.boss) def delete_entity(self,id): del self.entities[id] def delete_bullet(self,id): del self.bullets[id] #like it says def process(self,time): time_tick=time/1000.0 self.time+=time_tick for i in self.current_chapter.enemy: self.enemy_freq[i[0]]-=time_tick entities=self.entities.copy() bullets=self.bullets.copy() if not self.champ.dead and not self.status: self.champ.process(time_tick) for key in entities.keys(): self.entities[key].process(time_tick) for key in bullets.keys(): self.bullets[key].process(time_tick) self.FSM.think() self.pannel.process(time_tick) def display(self,surface): surface.blit(self.background,(0,0)) for entity in self.entities.values(): entity.display(surface) for bullet in self.bullets.values(): bullet.display(surface) self.champ.display(surface) #display the clearance messages if self.clear: message=self.font.render(str(self.current_chapter.id)+" completed",True,(150,150,150)) position=(self.size-message.get_size())/2 if (self.time%3>0.5): surface.blit(message,position) self.pannel.display(surface) def generate_enemy(self): for i in self.current_chapter.enemy: id=i[0] freq=i[1] if self.enemy_freq[id]<=0: y=0-self.enemy_list[id][0].get_height() #story mode if self.current_chapter.id is not 'Endless': x=random.randint(0,self.size[0]-self.enemy_list[id][0].get_width()) position=Vector2(x,y) self.add_entity(enemy(self,position,self.enemy_list[id])) #endless mode else: num=int(self.size[0]/self.enemy_list[-1][0].get_width()) temp=random.randint(0,4) for i in range(num): self.enemy_list[-1][2]=1+random.randint(int(self.time/2.5),int(self.time/2.5)+10) if temp==i: self.enemy_list[-1][2]=1+int(self.time/8) self.enemy_list[-1][3]=self.enemy_list[-1][2]*3 x=0+i*self.enemy_list[-1][0].get_width() position=Vector2(x,y) self.add_entity(Brick(self,position,self.enemy_list[-1])) freq=self.current_chapter.enemy[0][1]-self.champ.level*0.3 self.enemy_freq[id]=freq def initial_enemy(self): self.enemy_freq={} for i in self.current_chapter.enemy: self.enemy_freq[i[0]]=i[1]
class Space(object): def __init__(self): self.background = pygame.surface.Surface((780, 720)).convert() self.background.fill((0, 0, 0)) self.time = 0 self.size = Vector2(780, 720) self.chapter = None self.position = (0, 0) self.panel = None self.ship = None self.enemies = {} self.bullets = {} self.bullet_id = 0 self.enemy_id = 0 self.FSM = FSM() self.FSM.add_state(Gameover(self)) self.FSM.add_state(Complete(self)) self.FSM.add_state(Begin(self)) self.stars = [] def reset(self): self.time = 0 self.enemys = {} self.bullets = {} self.bullet_id = 0 self.enemy_id = 0 self.panel.reset() def process(self, time): self.time += time enemys = self.enemies.copy() bullets = self.bullets.copy() if not self.ship.dead: if random.randint(0, 3) == 1: self.set_star() for star in self.stars: star.next_y = star.y + time * star.speed for key in enemys.keys(): self.enemies[key].process(time) for key in bullets.keys(): self.bullets[key].process(time) self.ship.process(time) self.chapter.process(time) self.FSM.think() if self.chapter.complete: self.panel.move(time) def display(self, surface): surface.blit(self.background, (0, 0)) #awesome background for star in self.stars: if self.ship.position.y <= star.y: G = 150 R = 255 B = 150 else: G = 150 B = 255 R = 150 if star.speed <= 200: R = 255 G = 255 B = 255 pygame.draw.aaline(surface, (R, G, B), (star.x, star.next_y), (star.x, star.y - 1)) star.y = star.next_y for enemy in self.enemies.values(): enemy.display(surface) for bullet in self.bullets.values(): bullet.display(surface) self.ship.display(surface) self.panel.display(surface) def add_panel(self, panel): self.panel = panel def add_enemy(self, enemy): self.enemies[self.enemy_id] = enemy enemy.id = self.enemy_id self.enemy_id += 1 def add_bullet(self, bullet): self.bullets[self.bullet_id] = bullet bullet.id = self.bullet_id self.bullet_id += 1 def delete_enemy(self, id): del self.enemies[id] def delete_bullet(self, id): del self.bullets[id] def set_universe(self, chapter): self.reset() self.chapter = chapter self.FSM.change_state('Begin') def set_star(self): x = random.randint(0, self.size.x) y = 0 speed = random.randint(10, 110 + int(self.ship.velocity)) self.stars.append(Star(x, y, speed)) self.stars = list(filter(self.del_star, self.stars)) #filter function def del_star(self, star): return star.y < min(self.size.y, self.panel.position.y)