def __init__(self): pygame.init() pygame.display.set_caption('Alt-Land-Is') pygame.event.set_blocked(pygame.MOUSEMOTION) self.screen = pygame.display.set_mode(self.SIZE) self.game = game_logic.Game('Fenriz', 1) self.eventManager = EventManager() self.drawables = [] # List of objects to be drawn to the screen on every cycle self.updateables = {} # Dict of object, that can have their data updated playerSprite = Sprite( self.screen, (self.VIEWRANGE * TILE_SIZE + self.MAP_MARGIN, self.VIEWRANGE * TILE_SIZE + self.MAP_MARGIN), self.CHAR_IMG) charStatusBox = CharactorStatusBox(self.screen, pygame.Rect(750, 20, 200, 120), pygame.Color('gray25'), self.BORDER_COLORS) statusBox = StatusBox(5, self.screen, pygame.Rect(50, 650, 450, 100)) bActivate = Button(self.screen, pygame.Rect(760, 250, 40, 40), 'images/gui/b_activate.png') bActivate.bind(EVT_LEFT_CLICK, self._activateButtonHandler) coordBox = TextBox(self.screen, pygame.Rect(750, 710, 40, 40)) mapp = Map(surface = self.screen, topLeft = (self.MAP_MARGIN, self.MAP_MARGIN), mapRange = self.VIEWRANGE, tileSize = TILE_SIZE, fowTileName = 'images/terrain/smoke.png', borderColors = self.BORDER_COLORS, terrains = self.game.terrains ) inv = Inventory(surface = self.screen, topLeft = (750,500), tileSize = TILE_SIZE, borderColors = self.BORDER_COLORS, size = (4, 2) ) floorItems = Inventory(surface = self.screen, topLeft = (750,610), tileSize = TILE_SIZE, borderColors = self.BORDER_COLORS, size = (4, 1) ) self.drawables.append(mapp) # Add map first, so that other stuff can be drawn on it self.drawables.append(bActivate) self.drawables.append(statusBox) self.drawables.append(coordBox) #self.drawables.append(playerSprite) self.drawables.append(inv) self.drawables.append(floorItems) self.drawables.append(charStatusBox) self.updateables['coordBox'] = coordBox self.updateables['statusBox'] = statusBox self.updateables['charStatusBox'] = charStatusBox self.updateables['map'] = mapp self.updateables['inv'] = inv self.updateables['floorItems'] = floorItems self.eventManager.registerListener(KeyboardController(self.eventManager)) self.eventManager.registerListener(GameController(self.game)) self.eventManager.registerListener(bActivate) self.oneTimers = [] # Listeners that only need to live for one cycle
class Game: ''' The main game class. Instance it once and run ''' SCREEN_WIDTH = 1024 SCREEN_HEIGHT = 800 BORDER_COLORS = (60, 50, 40), (150, 110, 50), (210, 190, 100) VIEWRANGE = 7 SIZE = SCREEN_WIDTH, SCREEN_HEIGHT MAP_MARGIN = 10 CHAR_IMG = 'images/c1.gif' def __init__(self): pygame.init() pygame.display.set_caption('Alt-Land-Is') pygame.event.set_blocked(pygame.MOUSEMOTION) self.screen = pygame.display.set_mode(self.SIZE) self.game = game_logic.Game('Fenriz', 1) self.eventManager = EventManager() self.drawables = [] # List of objects to be drawn to the screen on every cycle self.updateables = {} # Dict of object, that can have their data updated playerSprite = Sprite( self.screen, (self.VIEWRANGE * TILE_SIZE + self.MAP_MARGIN, self.VIEWRANGE * TILE_SIZE + self.MAP_MARGIN), self.CHAR_IMG) charStatusBox = CharactorStatusBox(self.screen, pygame.Rect(750, 20, 200, 120), pygame.Color('gray25'), self.BORDER_COLORS) statusBox = StatusBox(5, self.screen, pygame.Rect(50, 650, 450, 100)) bActivate = Button(self.screen, pygame.Rect(760, 250, 40, 40), 'images/gui/b_activate.png') bActivate.bind(EVT_LEFT_CLICK, self._activateButtonHandler) coordBox = TextBox(self.screen, pygame.Rect(750, 710, 40, 40)) mapp = Map(surface = self.screen, topLeft = (self.MAP_MARGIN, self.MAP_MARGIN), mapRange = self.VIEWRANGE, tileSize = TILE_SIZE, fowTileName = 'images/terrain/smoke.png', borderColors = self.BORDER_COLORS, terrains = self.game.terrains ) inv = Inventory(surface = self.screen, topLeft = (750,500), tileSize = TILE_SIZE, borderColors = self.BORDER_COLORS, size = (4, 2) ) floorItems = Inventory(surface = self.screen, topLeft = (750,610), tileSize = TILE_SIZE, borderColors = self.BORDER_COLORS, size = (4, 1) ) self.drawables.append(mapp) # Add map first, so that other stuff can be drawn on it self.drawables.append(bActivate) self.drawables.append(statusBox) self.drawables.append(coordBox) #self.drawables.append(playerSprite) self.drawables.append(inv) self.drawables.append(floorItems) self.drawables.append(charStatusBox) self.updateables['coordBox'] = coordBox self.updateables['statusBox'] = statusBox self.updateables['charStatusBox'] = charStatusBox self.updateables['map'] = mapp self.updateables['inv'] = inv self.updateables['floorItems'] = floorItems self.eventManager.registerListener(KeyboardController(self.eventManager)) self.eventManager.registerListener(GameController(self.game)) self.eventManager.registerListener(bActivate) self.oneTimers = [] # Listeners that only need to live for one cycle def _genInventoryItems(self, invDict, ItemClass): ''' generates and returns a list of inventory items ''' output = [] for itemDict in invDict.values(): j = ItemClass(self.screen, itemDict, self.game) output.append(j) self.eventManager.registerListener(j) self.oneTimers.append(j) return output def _drawGUIElements(self): ''' Draws all the GUI elements ''' for drawable in self.drawables: drawable.draw() def _update(self): ''' update the updateable gui elements ''' while self.oneTimers: self.eventManager.unregisterListener(self.oneTimers.pop()) charInfo = self.game.getCharInfo() self.updateables['coordBox'].setText('X: %s\nY: %s' % (charInfo['x'], charInfo['y']) ) self.updateables['statusBox'].setText(charInfo['status']['old']) self.updateables['map'].setMap(self.game.getAreaDict()) self.updateables['inv'].setInv(self._genInventoryItems(self.game.getInventory(), InvItem)) self.updateables['floorItems'].setInv(self._genInventoryItems(self.game.getFloorItems(), FloorItem)) self.updateables['charStatusBox'].setData(charInfo) def draw(self): ''' Updates the screen ''' self.screen.fill(pygame.Color('black')) self._update() self._drawGUIElements() pygame.display.flip() def _activateButtonHandler(self, evt): # pylint: disable-msg=W0613 ''' Called when the activate button is pressed ''' self.game.activate() def run(self): ''' Starts the game and keeps it running ''' clock = pygame.time.Clock() sinceKeydown = 0 # Milliseconds since the last keydown event has happened self.draw() while 1: sinceKeydown += clock.tick(50) #moved = False #self.draw() for event in pygame.event.get(): self.draw() sinceKeydown = 0 self.eventManager.post(event) # If less than 500 ms since the prevous KEYDOWN, do not process continuous movement if sinceKeydown > 500: # Handle continuous moving when the user keeps a key pressed pygame.event.pump() pressedKey = getPressedKey(pygame.key.get_pressed()) if pressedKey: clock.tick(10) self.eventManager.post( EventKeyDown(pressedKey) )