class Win(State): def __init__(self,level): State.__init__(self) self.level = level self.audio = Audio() self.audio.volume = 0.5 self.music = Music() self.music.volume = 0.7 seconds = 5 self.delay = seconds*40 def start(self): self.music.stop(500) self.music.play('data/sounds/winner.wav') fnt = pygame.font.SysFont('Verdana',30,italic=True) text = fnt.render('Level Completed!',True,(0,255,0)) disp = pygame.display.get_surface() disp.blit(text,(400-text.get_width()/2, 300-text.get_height()/2)) pygame.display.flip() State.start(self) def EVT_tick(self,event): if self.delay: self.delay -= 1 else: filename = Campaign.next(self.level.replace('\\','/')) if filename: lvl = level.load(filename) level.save(lvl,'save.lvl') self.quit(GameState(lvl)) else: self.quit(Ending())
class Lose(State): def __init__(self,level): State.__init__(self) self.level = level self.audio = Audio() self.audio.volume = 0.5 self.music = Music() self.music.volume = 0.7 seconds = 5 self.delay = seconds*40 def start(self): self.music.stop(500) self.music.play('data/sounds/loser.wav') fnt = pygame.font.SysFont('Verdana',30,italic=True) text = fnt.render('You Got Knocked Out!',True,(255,0,0)) disp = pygame.display.get_surface() disp.blit(text,(400-text.get_width()/2, 300-text.get_height()/2)) pygame.display.flip() State.start(self) def EVT_credits(self,event): self.quit() def EVT_tick(self,event): if self.delay: self.delay -= 1 else: lvl = level.load(self.level) self.quit(GameState(lvl))
class Ending(State): def __init__(self): State.__init__(self) self.audio = Audio() self.audio.volume = 0.5 self.music = Music() self.music.volume = 0.7 self.music.stop(500) self.delay = 10*40 self.playing = False self.image = data.candy self.image.set_alpha(0) def start(self): State.start(self) self.music.play('data/sounds/winner.wav') pygame.display.get_surface().fill((0,0,0)) pygame.display.get_surface().blit(self.intro(),(0,0)) pygame.display.flip() def EVT_KeyDown(self,event): self.playing = True self.music.stop(500) self.audio.play('data/sounds/fanfare.wav') def intro(self): ''' Copied almost straight from level.py ''' surf = pygame.Surface((800,600)) surf.fill((0,0,0)) surf.set_colorkey((0,0,0)) try: intro = '''You: At... last... after such pain and toil, I have reclaimed my own. Boss: All right already, you can have it, it's stale anyway.''' intro = [line.strip() for line in intro.split('\n')] fnt = pygame.font.SysFont('Verdana',20,italic=True) y = 5 for line in intro: line = fnt.render(line,True,(0,255,0)) surf.blit(line,(400-(line.get_width()/2),y)) y += line.get_height()+5 except: pass return surf def EVT_tick(self,event): if not self.playing: return disp = pygame.display.get_surface() if self.image.get_alpha() != 255: self.image.set_alpha(self.image.get_alpha()+1) disp.fill((0,0,0)) disp.blit(self.image,(400-self.image.get_width()/2, 300-self.image.get_height()/2)) pygame.display.flip() if self.delay: self.delay -= 1 else: self.quit()
class GameState(State): def __init__(self,lvl=None): State.__init__(self) if lvl == None: self.level = level.load(Campaign.next()) else: self.level = lvl self.bg = pygame.sprite.Group(self.level.s) self.selected = None self.audio = Audio() self.music = Music() self.music.volume = 1 self.audio.volume = 0.5 self.alarm = 0 self.music.stop(500) self.music.play('data/music/play.ogg',-1) self.playing = True def start(self): State.start(self) self.EVT_tick(None) self.playing = False pygame.display.get_surface().blit(self.level.intro(),(0,0)) pygame.display.flip() def EVT_win(self,event): self.quit(Win(self.level.filename)) def EVT_lose(self,event): self.quit(Lose(self.level.filename)) def EVT_tick(self,event): if not self.playing: return disp = pygame.display.get_surface() disp.fill((0,0,0)) self.level.clear() self.level.update() self.level.draw() self.level.s.rect.left = -self.level.player.rect.left + pygame.display.get_surface().get_width()/2 self.level.s.rect.top = -self.level.player.rect.top + pygame.display.get_surface().get_height()/2 self.bg.clear(disp,self.level.background) self.bg.draw(disp) pygame.display.flip() if self.alarm: self.alarm -= 1 def EVT_KeyDown(self,event): if not self.playing: self.playing = True elif event.key == pygame.K_ESCAPE: self.quit(PauseMenu(self)) def EVT_alarm(self,event): if not self.alarm: self.audio.play('data/sounds/alarm.wav') self.alarm = 520 def quit(self,next=None): self.audio.stop(500) self.music.stop(500) self.music.play('data/music/menu.ogg',-1) State.quit(self,next)
class Game(directicus.engine.State): def __init__(self,enemies=1,size=(3000,3000)): directicus.engine.State.__init__(self) reload(level) self.level = level.create(size,enemies=enemies) self.audio = Audio() self.music = Music() self.audio.volume = 0.7 self.music.volume = 0.5 self.level.render() self.selected = [] self.sprite = pygame.sprite.Sprite() self.sprite.image = self.level.surface self.sprite.rect = self.sprite.image.get_rect() self.sprite.rect.topleft = (0,0) self.keys = [0,0,0,0] self.interface = pygame.sprite.Group() self.paused = False self.mouse = 0 def start(self): directicus.engine.State.start(self) self.music.stop(500) self.music.play('data/music/battle.mp3',-1) self.level.game() self.mini = sprites.MiniMap() self.mini.level = self.level def quit(self,next=None): self.music.stop(500) self.music.play('data/music/menu.mp3') directicus.engine.State.quit(self,next) def EVT_tick(self,event): self.level.clear() self.level.rangers.update(self.level) self.level.arrows.update(self.level) allies = list() for army in self.level.player.armies: allies.extend(army.rangers) if not allies: self.quit(Lose()) return else: enemies = list() for enemy in self.level.enemies: for army in enemy.armies: enemies.extend(army.rangers) if not enemies: self.quit(Win()) return for sprite in self.interface: sprite.kill() for sprite in self.selected: if not sprite.groups(): # ranger has died self.selected.remove(sprite) continue base = pygame.sprite.Sprite(self.level.sprites,self.interface) base.rect = pygame.rect.Rect(sprite) base.rect.height = sprite.rect.height/2 base.rect.bottom = sprite.rect.bottom-1 base.image = pygame.Surface(base.rect.size) base.image.fill((255,0,0)) base.image.set_colorkey((255,0,0)) pygame.draw.ellipse(base.image,(0,255,0),base.image.get_rect(),2) pygame.display.get_surface().fill((0,0,0)) self.level.draw() pygame.display.get_surface().blit(self.sprite.image,self.sprite.rect) if self.mouse: msPos = pygame.mouse.get_pos() r = pygame.rect.Rect(0,0,0,0) r.width = msPos[0]-self.mouse[0] r.height = msPos[1]-self.mouse[1] r.normalize() if msPos[0] > self.mouse[0] and msPos[1] > self.mouse[1]: r.topleft = self.mouse r.bottomright = msPos elif msPos[0] < self.mouse[0] and msPos[1] < self.mouse[1]: r.bottomright = self.mouse r.topleft = msPos elif msPos[0] > self.mouse[0] and msPos[1] < self.mouse[1]: r.topright = msPos r.bottomleft = self.mouse else: r.bottomleft = msPos r.topright = self.mouse pygame.draw.rect(pygame.display.get_surface(),(0,255,0),r,1) if self.keys[2]: if -self.sprite.rect.x+pygame.display.get_surface().get_width() < self.level.size[0]: self.sprite.rect.x -= 30 if self.keys[3]: if -self.sprite.rect.x > 0: self.sprite.rect.x += 30 if self.keys[0]: if -self.sprite.rect.y > 0: self.sprite.rect.y += 30 if self.keys[1]: if -self.sprite.rect.y+pygame.display.get_surface().get_height() < self.level.size[1]: self.sprite.rect.y -= 30 self.mini.pos = (-self.sprite.rect.left, -self.sprite.rect.top) self.mini.tick() self.mini.draw() pygame.display.flip() def EVT_KeyUp(self,event): key = pygame.key.name(event.key) if key == 'right': self.keys[2] = 0 elif key == 'left': self.keys[3] = 0 elif key == 'up': self.keys[0] = 0 elif key == 'down': self.keys[1] = 0 def EVT_MouseButtonDown(self,event): if self.mini.rect.collidepoint(event.pos): relPos = self.mini.grow( (event.pos[0]-self.mini.rect.centerx, event.pos[1]-self.mini.rect.centery)) s = pygame.display.get_surface().get_size() self.sprite.rect.center = (-relPos[0]+s[0]/2, -relPos[1]+s[1]/2) else: pos = (event.pos[0]-self.sprite.rect.left, event.pos[1]-self.sprite.rect.top) if event.button == 1: self.mouse = event.pos self.selected = [] rangers = list() for army in self.level.player.armies: rangers.extend(army.rangers) for ranger in rangers: if ranger.rect.collidepoint(pos) and ranger.collidePoint(pos): self.selected.append(ranger) elif self.selected: target = None for enemy in self.level.enemies: for army in enemy.armies: for ranger in army.rangers.sprites(): if ranger.rect.collidepoint(pos): target = ranger self.audio.play('data/sounds/sword-draw.wav') break if not target: target = (event.pos[0]-self.sprite.rect.left, event.pos[1]-self.sprite.rect.top) for r in self.selected: r.target(target) def EVT_MouseButtonUp(self,event): if event.button == 1 and self.mouse: msPos = event.pos r = pygame.rect.Rect(0,0,0,0) r.width = msPos[0]-self.mouse[0] r.height = msPos[1]-self.mouse[1] r.normalize() if msPos[0] > self.mouse[0] and msPos[1] > self.mouse[1]: r.topleft = self.mouse r.bottomright = msPos elif msPos[0] < self.mouse[0] and msPos[1] < self.mouse[1]: r.bottomright = self.mouse r.topleft = msPos elif msPos[0] > self.mouse[0] and msPos[1] < self.mouse[1]: r.topright = msPos r.bottomleft = self.mouse else: r.bottomleft = msPos r.topright = self.mouse r.left -= self.sprite.rect.left r.top -= self.sprite.rect.top rangers = list() for army in self.level.player.armies: rangers.extend(army.rangers) for ranger in rangers: if ranger.rect.colliderect(r): self.selected.append(ranger) self.mouse = None def EVT_KeyDown(self,event): key = pygame.key.name(event.key) if key == 'right': self.keys[2] = 1 elif key == 'left': self.keys[3] = 1 elif key == 'up': self.keys[0] = 1 elif key == 'down': self.keys[1] = 1 elif key == 'escape': self.quit()