def setPos(self, cell): r = Actor.setPos(self, cell) # Spawn fruit for c in self.world.surroundingCells(cell): self.doIn(random.randint(2, 15), "replenish", c) return r
class GameState(State): ''' State for game playing mode. ''' bgGroup = pygame.sprite.OrderedUpdates() playerGroup = pygame.sprite.RenderPlain() guiGroup = pygame.sprite.OrderedUpdates() def __init__(self, main): # transition from another state State.__init__(self,main) self.loadPlayer() self.hud = Actor(IMG_HUD,-1) self.hud2 = Actor(IMG_HUD2) self.hud.setPos(32,HEIGHT/2) self.hud2.setPos(WIDTH-32,HEIGHT/2) GameState.guiGroup.add(self.hud) GameState.guiGroup.add(self.hud2) self.health = 7 self.hudHearts = [] self.hudHeartsHalf = Actor(IMG_HEART2,-1) self.hudSlot = [None]*3 self.wl = WorldLoader('new.world') self.background = TerrainLayer("0_0.map") self.currentMap = "0_0.map" for i in range(0,3): self.hudSlot[i] = Actor(IMG_SLOT,-1) self.hudSlot[i].setPos(50,120+i*120) self.guiGroup.add(self.hudSlot[i]) self.updateHudHealth() pygame.mixer.init() filename = "worldAmbient.mp3" path = os.path.join(util.GAME_SOUNDS, filename) path = util.filepath(path) pygame.mixer.music.load(path) pygame.mixer.music.play() def __del__(self): # transition to another state pass def loadPlayer(self): self.player = Player(self) GameState.playerGroup.add(self.player) def update(self): self.checkCollisions() State.update(self); GameState.guiGroup.update() GameState.playerGroup.update() def updateHudHealth(self): if self.health < 1 or self.health > 20: return full = self.health/2 halve = self.health%2 if len(self.hudHearts) != full: while len(self.hudHearts) < full: self.hudHearts.append(Actor(IMG_HEART,-1)) GameState.guiGroup.add(self.hudHearts[-1]) while len(self.hudHearts) > full: GameState.guiGroup.remove(self.hudHearts.pop()) for i in range(0,full): self.hudHearts[i].setPos(WIDTH-25,HEIGHT-50-i*60) if halve == 1: GameState.guiGroup.add(self.hudHeartsHalf) self.hudHeartsHalf.setPos(WIDTH-25, HEIGHT-50-full*60) else: self.hudHeartsHalf.kill() def handleEvent(self): # handle mouse mousePos = Vector2(pygame.mouse.get_pos()) self.player.orient(mousePos) # For each event that occurs this frame for event in pygame.event.get(): # If user exits the window if event.type == QUIT: sys.exit(0) # monitor keyboard self.handleKey(event) def handleKey(self, event): ''' Handle input from user keyboard ''' if event.type == pygame.KEYDOWN: # exit game if event.key == K_ESCAPE: sys.exit(1) if event.key == MOVEMENT_KEYS[0]: self.player.move(0) if event.key == MOVEMENT_KEYS[1]: self.player.move(1) if event.key == MOVEMENT_KEYS[2]: self.player.move(2) if event.key == MOVEMENT_KEYS[3]: self.player.move(3) if event.key == MAGIC_ATTACK_KEY: self.player.useMagic() # testing if event.key == K_DOWN: self.health -= 1 self.updateHudHealth() if event.key == K_UP: self.health += 1 self.updateHudHealth() elif event.type == pygame.KEYUP: if event.key == MOVEMENT_KEYS[0]: self.player.unMove(0) if event.key == MOVEMENT_KEYS[1]: self.player.unMove(1) if event.key == MOVEMENT_KEYS[2]: self.player.unMove(2) if event.key == MOVEMENT_KEYS[3]: self.player.unMove(3) elif event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed()[0]: self.player.swingSword() if pygame.mouse.get_pressed()[2]: self.player.shootBow() def checkCollisions(self): # Check for atLayer collisions for hit in pygame.sprite.spritecollide(self.player, self.background.atGroup, 0): self.player.collideWall(hit) def nextMap(self, direction, pos): # print "moving to: " + direction + " via: " + str(pos) mmap = "" if direction == 'up': mmap = self.wl.north[self.currentMap] # position player at bottom minus almost half a tile if mmap is not 'none': self.player.setPos(pos[0], HEIGHT-17) elif direction == 'down': mmap = self.wl.south[self.currentMap] if mmap is not 'none': self.player.setPos(pos[0], 17) elif direction == 'right': mmap = self.wl.east[self.currentMap] if mmap is not 'none': self.player.setPos(64+17, pos[1]) # just not touching the hud elif direction == 'left': mmap = self.wl.west[self.currentMap] if mmap is not 'none': self.player.setPos(WIDTH-(64+17), pos[1]) if not mmap == 'none': self.currentMap = mmap self.background = TerrainLayer(mmap) # Added for debugging purposes. Remove when not needed print "MAP: ",mmap def draw(self): #draw background #self.main.screen.blit(self.background, self.background.get_rect()) self.background.drawTerrain(self.main.screen); # draw player GameState.playerGroup.draw(self.main.screen) # draw gui GameState.guiGroup.draw(self.main.screen) # flip screen State.draw(self)
class TitleState(State): titleGroup = pygame.sprite.GroupSingle() btnStartGroup = pygame.sprite.GroupSingle() def __init__(self, main): # transition from another state State.__init__(self,main) self.titleScreen = Actor(IMG_TITLE_SCREEN) self.btnStart = Actor(IMG_LABEL_START) self.btnStart2 = Actor(IMG_LABEL_START2) self.btnStart.setPos(WIDTH/2,HEIGHT-300) self.btnStart2.setPos(WIDTH/2,HEIGHT-300) self.tick = 0 self.tickInterval = 60 self.ready = False TitleState.titleGroup.add(self.titleScreen) TitleState.btnStartGroup.add(self.btnStart) pygame.mixer.init() pygame.mixer.music.load("../data/sounds/godspeed.mid") pygame.mixer.music.play() def __del__(self): # transition to another state TitleState.titleGroup.empty() TitleState.btnStartGroup.empty() pygame.mixer.music.stop() def update(self): self.tick += 1 if self.tick < self.tickInterval/4: TitleState.btnStartGroup.add(self.btnStart2) else: TitleState.btnStartGroup.add(self.btnStart) if self.tick > self.tickInterval: self.tick = 0 if self.ready: self.main.changeState(GameState(self.main)) TitleState.btnStartGroup.update() TitleState.titleGroup.update() State.update(self) def handleEvent(self): for event in pygame.event.get(): if event.type == QUIT: sys.exit(0) if event.type == pygame.KEYDOWN: if event.key == K_ESCAPE: sys.exit(0) if event.key == K_RETURN: pygame.mixer.music.stop() pygame.mixer.music.load("../data/sounds/HeroOh.mp3") pygame.mixer.music.play() self.ready = True self.tick = 0 self.tickInterval = 120 def draw(self): # draw group stuff TitleState.titleGroup.draw(self.main.screen) TitleState.btnStartGroup.draw(self.main.screen) State.draw(self)
def setPos(self, cell): (x, y) = self.cell Actor.setPos(self, cell) # If we have moved, update the direction if self.x != x or self.y != y: self.direction = (self.constrain(self.x - x, -1, 1), self.constrain(self.y - y, -1, 1))
class TitleState(State): titleGroup = pygame.sprite.GroupSingle() btnStartGroup = pygame.sprite.GroupSingle() def __init__(self, main): # transition from another state super(TitleState, self).__init__(main) self.titleScreen = Actor(IMG_TITLE_SCREEN) self.btnStart = Actor(IMG_LABEL_START) self.btnStart2 = Actor(IMG_LABEL_START2) self.btnStart.setPos(config.WIDTH/2, config.HEIGHT - 300) self.btnStart2.setPos(config.WIDTH/2, config.HEIGHT - 300) self.tick = 0 self.tickInterval = 60 TitleState.titleGroup.add(self.titleScreen) TitleState.btnStartGroup.add(self.btnStart) '''TODO: FIX MUSIC pygame.mixer.init() pygame.mixer.music.load("../data/sounds/godspeed.mid") pygame.mixer.music.play() ''' def __del__(self): # transition to another state TitleState.titleGroup.empty() TitleState.btnStartGroup.empty() '''TODO: FIX MUSIC pygame.mixer.music.stop() ''' super(TitleState, self).__del__() def update(self, clock): from config import keyboard, keymap super(TitleState, self).update(clock) self.tick += 1 if self.tick < self.tickInterval/4: TitleState.btnStartGroup.add(self.btnStart2) else: TitleState.btnStartGroup.add(self.btnStart) if keyboard.downup(keymap.START): self.main.changeState(GameState(self.main)) TitleState.btnStartGroup.update(clock) TitleState.titleGroup.update(clock) def handleEvent(self): super(TitleState, self).handleEvent() '''TODO: FIX MUSIC pygame.mixer.music.stop() pygame.mixer.music.load("../data/sounds/HeroOh.ogg") pygame.mixer.music.play() ''' def draw(self): # draw group stuff TitleState.titleGroup.draw(self.main.screen) TitleState.btnStartGroup.draw(self.main.screen) super(TitleState, self).draw()