def startGame(cont=False, registry=None): registry = getRegistry(cont, registry) # create fixed sprites global fixedSprites fixedSprites = pygame.sprite.Group() fixedCoin = FixedCoin((27, 3)) coinCount = CoinCount(registry.coinCount, (38, 3)) keyCount = KeyCount(registry.keyCount, (0, 3)) lives = Lives(2, (3, 3)) checkpointIcon = CheckpointIcon((-11, -11)) fixedSprites.add(fixedCoin, lives, coinCount, keyCount, checkpointIcon) # create player global player player = Ulmo() player.coinCount = coinCount player.keyCount = keyCount player.lives = lives player.checkpointIcon = checkpointIcon # create the map rpgMap = parser.loadRpgMap(registry.mapName) player.setup("ulmo", rpgMap, eventBus) # set the start position player.setTilePosition(registry.playerPosition[0], registry.playerPosition[1], registry.playerLevel) # return the play state return PlayState()
def startGame(cont = False, registry = None): registry = getRegistry(cont, registry) # create fixed sprites global fixedSprites fixedSprites = pygame.sprite.Group() fixedCoin = FixedCoin((27, 3)) coinCount = CoinCount(registry.coinCount, (38, 3)) keyCount = KeyCount(registry.keyCount, (0, 3)) lives = Lives(2, (3, 3)) checkpointIcon = CheckpointIcon((-11, -11)) fixedSprites.add(fixedCoin, lives, coinCount, keyCount, checkpointIcon) # create player global player player = Ulmo() player.coinCount = coinCount player.keyCount = keyCount player.lives = lives player.checkpointIcon = checkpointIcon # create the map rpgMap = parser.loadRpgMap(registry.mapName) player.setup("ulmo", rpgMap, eventBus) # set the start position player.setTilePosition(registry.playerPosition[0], registry.playerPosition[1], registry.playerLevel) # return the play state return PlayState()
def execute(self, keyPresses): if self.ticks == 0: eventBus.dispatchMapTransitionEvent(MapTransitionEvent()) self.oldImage = screen.copy() # load another map nextRpgMap = parser.loadRpgMap(self.transition.mapName) player.rpgMap = nextRpgMap player.spriteFrames.direction = self.boundary # set the new position hidePlayer(self.boundary, nextRpgMap.mapRect, self.transition.modifier) # create play state self.nextState = PlayState(self.createReplayTransition()) # extract the next image from the state self.nextState.drawMapView(self.nextImage, 0) elif self.ticks < 32: xSlice, ySlice = self.ticks * X_MULT * 2, self.ticks * Y_MULT * 2 if self.boundary == UP: screen.blit(self.oldImage.subsurface(0, 0, VIEW_WIDTH, VIEW_HEIGHT - ySlice), (0, ySlice)) screen.blit(self.nextImage.subsurface(0, VIEW_HEIGHT - ySlice, VIEW_WIDTH, ySlice), ORIGIN) elif self.boundary == DOWN: screen.blit(self.oldImage.subsurface(0, ySlice, VIEW_WIDTH, VIEW_HEIGHT - ySlice), ORIGIN) screen.blit(self.nextImage.subsurface(0, 0, VIEW_WIDTH, ySlice), (0, VIEW_HEIGHT - ySlice)) elif self.boundary == LEFT: screen.blit(self.oldImage.subsurface(0, 0, VIEW_WIDTH - xSlice, VIEW_HEIGHT), (xSlice, 0)) screen.blit(self.nextImage.subsurface(VIEW_WIDTH - xSlice, 0, xSlice, VIEW_HEIGHT), ORIGIN) else: # self.boundary == RIGHT screen.blit(self.oldImage.subsurface(xSlice, 0, VIEW_WIDTH - xSlice, VIEW_HEIGHT), ORIGIN) screen.blit(self.nextImage.subsurface(0, 0, xSlice, VIEW_HEIGHT), (VIEW_WIDTH - xSlice, 0)) pygame.display.flip() else: return ShowPlayerState(self.boundary, self.nextState, BOUNDARY_TICKS[self.boundary]) # return self.nextState self.ticks += 1 return None
def execute(self, keyPresses): if self.ticks < THIRTY_TWO: if self.ticks == 0 and self.transition.type == SCENE_TRANSITION: eventBus.dispatchMapTransitionEvent(MapTransitionEvent()) sceneZoomIn(self.screenImage, self.ticks) elif self.ticks == THIRTY_TWO: # load the next map nextRpgMap = parser.loadRpgMap(self.transition.mapName) player.rpgMap = nextRpgMap # set player position player.setTilePosition(self.transition.tilePosition[0], self.transition.tilePosition[1], self.transition.level) # hide player if required if self.transition.boundary: hidePlayer(self.transition.boundary, nextRpgMap.mapRect) # create play state self.nextState = PlayState() # setting the direction will also apply masks player.setDirection(self.transition.direction) # extract the next image from the state self.nextState.drawMapView(self.screenImage, 0) elif self.ticks < SIXTY_FOUR: sceneZoomOut(self.screenImage, self.ticks) else: if self.transition.type == LIFE_LOST_TRANSITION: return self.nextState # else just a regular scene transition direction = player.spriteFrames.direction if self.transition.boundary: return ShowPlayerState(direction, self.nextState, BOUNDARY_TICKS[direction]) return ShowPlayerState(direction, self.nextState, DOORWAY_TICKS[direction]) self.ticks += 1 return None
def initPlayState(self): # load the next map nextRpgMap = parser.loadRpgMap(self.transition.mapName) player.rpgMap = nextRpgMap player.spriteFrames.direction = self.boundary # set the new position hidePlayer(self.boundary, nextRpgMap.mapRect, self.transition.modifier) # create play state self.playState = PlayState() # extract the next image from the play state self.playState.drawMapView(self.nextImage, player.viewRect, 0)
def execute(self, keyPresses): if self.ticks == 0: eventBus.dispatchMapTransitionEvent(MapTransitionEvent()) self.oldImage = screen.copy() # load another map nextRpgMap = parser.loadRpgMap(self.transition.mapName) player.rpgMap = nextRpgMap player.spriteFrames.direction = self.boundary # set the new position hidePlayer(self.boundary, nextRpgMap.mapRect, self.transition.modifier) # create play state self.nextState = PlayState() # extract the next image from the state self.nextState.drawMapView(self.nextImage, 0) elif self.ticks < THIRTY_TWO: xSlice, ySlice = self.ticks * X_MULT * 2, self.ticks * Y_MULT * 2 if self.boundary == UP: screen.blit( self.oldImage.subsurface(0, 0, VIEW_WIDTH, VIEW_HEIGHT - ySlice), (0, ySlice)) screen.blit( self.nextImage.subsurface(0, VIEW_HEIGHT - ySlice, VIEW_WIDTH, ySlice), ORIGIN) elif self.boundary == DOWN: screen.blit( self.oldImage.subsurface(0, ySlice, VIEW_WIDTH, VIEW_HEIGHT - ySlice), ORIGIN) screen.blit( self.nextImage.subsurface(0, 0, VIEW_WIDTH, ySlice), (0, VIEW_HEIGHT - ySlice)) elif self.boundary == LEFT: screen.blit( self.oldImage.subsurface(0, 0, VIEW_WIDTH - xSlice, VIEW_HEIGHT), (xSlice, 0)) screen.blit( self.nextImage.subsurface(VIEW_WIDTH - xSlice, 0, xSlice, VIEW_HEIGHT), ORIGIN) else: # self.boundary == RIGHT screen.blit( self.oldImage.subsurface(xSlice, 0, VIEW_WIDTH - xSlice, VIEW_HEIGHT), ORIGIN) screen.blit( self.nextImage.subsurface(0, 0, xSlice, VIEW_HEIGHT), (VIEW_WIDTH - xSlice, 0)) pygame.display.flip() else: return ShowPlayerState(self.boundary, self.nextState, BOUNDARY_TICKS[self.boundary]) # return self.nextState self.ticks += 1 return None
def startGame(): global eventBus eventBus = EventBus() # create registry global registry registry = Registry() eventBus.addCoinCollectedListener(registry) eventBus.addKeyCollectedListener(registry) eventBus.addDoorOpenedListener(registry) global soundHandler soundHandler = SoundHandler() eventBus.addCoinCollectedListener(soundHandler) eventBus.addKeyCollectedListener(soundHandler) eventBus.addDoorOpeningListener(soundHandler) eventBus.addPlayerFootstepListener(soundHandler) eventBus.addMapTransitionListener(soundHandler) eventBus.addEndGameListener(soundHandler) eventBus.addLifeLostListener(soundHandler) eventBus.addWaspZoomingListener(soundHandler) eventBus.addBeetleCrawlingListener(soundHandler) # create fixed sprites global fixedSprites fixedSprites = pygame.sprite.Group() fixedCoin = FixedCoin((27, 3)) coinCount = CoinCount(0, (38, 3)) keyCount = KeyCount(0, (VIEW_WIDTH - 3, 3)) lives = Lives(2, (3, 3)) fixedSprites.add(fixedCoin, lives, coinCount, keyCount) # create player global player player = Ulmo() player.coinCount = coinCount player.keyCount = keyCount player.lives = lives # create the map rpgMap = parser.loadRpgMap("central") player.setup("ulmo", rpgMap, eventBus) # set the start position #player.setTilePosition(2, 16, 2) player.setTilePosition(6, 20, 2) #player.setTilePosition(30, 21, 3) #player.setTilePosition(5, 3, 4) # return the play state return PlayState()
def initPlayState(self): # load the next map nextRpgMap = parser.loadRpgMap(self.transition.mapName) player.rpgMap = nextRpgMap # set player position player.setTilePosition(self.transition.tilePosition[0], self.transition.tilePosition[1], self.transition.level) # hide player if required if self.transition.boundary: hidePlayer(self.transition.boundary, nextRpgMap.mapRect) # create play state self.playState = PlayState() # setting the direction will also apply masks player.setDirection(self.transition.direction) # extract the next image from the play state self.playState.drawMapView(self.screenImage, player.viewRect, 0)
def execute(self, keyPresses): if self.ticks < 32: if self.ticks == 0 and self.transition.type == SCENE_TRANSITION: eventBus.dispatchMapTransitionEvent(MapTransitionEvent()) sceneZoomIn(self.screenImage, self.ticks) elif self.ticks == 32: # load the next map nextRpgMap = parser.loadRpgMap(self.transition.mapName) player.rpgMap = nextRpgMap # set player position if self.transition.type == REPLAY_TRANSITION: player.setPixelPosition(self.transition.pixelPosition[0], self.transition.pixelPosition[1], self.transition.level) # player is already hidden else: # self.transition.type == SCENE_TRANSITION player.setTilePosition(self.transition.tilePosition[0], self.transition.tilePosition[1], self.transition.level) if self.transition.boundary: hidePlayer(self.transition.boundary, nextRpgMap.mapRect) # setting the direction will also apply masks player.setDirection(self.transition.direction) # create play state self.nextState = PlayState(self.createReplayTransition()) # extract the next image from the state self.nextState.drawMapView(self.screenImage, 0) elif self.ticks < 64: sceneZoomOut(self.screenImage, self.ticks) else: if self.transition.firstMap: return self.nextState direction = player.spriteFrames.direction if self.transition.boundary: return ShowPlayerState(direction, self.nextState, BOUNDARY_TICKS[direction]) return ShowPlayerState(direction, self.nextState, DOORWAY_TICKS[direction]) self.ticks += 1 return None
import unittest import pygame import parser import view from pygame.locals import Rect # initialize everything pygame.init() screen = pygame.display.set_mode((1, 1)) # this feels a bit hacky - is there a better way to do it? parser.MAPS_FOLDER = "../maps" parser.TILES_FOLDER = "../tiles" rpgMap = parser.loadRpgMap("islands") class SpriteInfo: def __init__(self, mapRect, level): self.mapRect = mapRect self.level = level def move(self, px, py): self.mapRect.move_ip(px, py) # pseudo z order that is used to test if one sprite is behind another self.z = int(self.mapRect.bottom + self.level * 32) class GetMasksTest(unittest.TestCase):
import parser import view from pygame.locals import Rect from view import TILE_SIZE # initialize everything pygame.init() screen = pygame.display.set_mode((1, 1)) # this feels a bit hacky - is there a better way to do it? parser.MAPS_FOLDER = "../maps" parser.TILES_FOLDER = "../tiles" rpgMap = parser.loadRpgMap("unit") class MockSprite: def __init__(self, mapRect, level): self.mapRect = mapRect self.level = level self.upright = True def move(self, px, py): self.mapRect.move_ip(px, py) # pseudo z order that is used to test if one sprite is behind another self.z = int(self.mapRect.bottom + self.level * TILE_SIZE) class GetMasksTest(unittest.TestCase):
def startGame(cont=False): global eventBus eventBus = EventBus() # create registry handler global registryHandler if cont: registryHandler.switchToSnapshot() else: #registry = Registry("unit", (4, 6), 1) registry = Registry("central", (6, 22), 2) #registry = Registry("central", (22, 20), 3) #registry = Registry("east", (10, 18), 1) #registry = Registry("wasps", (12, 10), 5) registryHandler = RegistryHandler(registry) # grab this for later registry = registryHandler.registry # add event listeners eventBus.addCoinCollectedListener(registryHandler) eventBus.addKeyCollectedListener(registryHandler) eventBus.addDoorOpenedListener(registryHandler) eventBus.addCheckpointReachedListener(registryHandler) global soundHandler soundHandler = SoundHandler() eventBus.addCoinCollectedListener(soundHandler) eventBus.addKeyCollectedListener(soundHandler) eventBus.addDoorOpeningListener(soundHandler) eventBus.addPlayerFootstepListener(soundHandler) eventBus.addMapTransitionListener(soundHandler) eventBus.addEndGameListener(soundHandler) eventBus.addLifeLostListener(soundHandler) eventBus.addWaspZoomingListener(soundHandler) eventBus.addBeetleCrawlingListener(soundHandler) eventBus.addCheckpointReachedListener(soundHandler) eventBus.addPlayerFallingListener(soundHandler) # create fixed sprites global fixedSprites fixedSprites = pygame.sprite.Group() fixedCoin = FixedCoin((27, 3)) coinCount = CoinCount(registry.coinCount, (38, 3)) keyCount = KeyCount(registry.keyCount, (0, 3)) lives = Lives(2, (3, 3)) checkpointIcon = CheckpointIcon((-11, -11)) fixedSprites.add(fixedCoin, lives, coinCount, keyCount, checkpointIcon) # create player global player player = Ulmo() player.coinCount = coinCount player.keyCount = keyCount player.lives = lives player.checkpointIcon = checkpointIcon # create the map rpgMap = parser.loadRpgMap(registry.mapName) player.setup("ulmo", rpgMap, eventBus) # set the start position player.setTilePosition(registry.playerPosition[0], registry.playerPosition[1], registry.playerLevel) # return the play state return PlayState()
def startGame(cont = False): global eventBus eventBus = EventBus() # create registry handler global registryHandler if cont: registryHandler.switchToSnapshot() else: #registry = Registry("unit", (4, 6), 1) registry = Registry("central", (6, 22), 2) #registry = Registry("central", (22, 20), 3) #registry = Registry("east", (10, 18), 1) #registry = Registry("wasps", (12, 10), 5) registryHandler = RegistryHandler(registry) # grab this for later registry = registryHandler.registry # add event listeners eventBus.addCoinCollectedListener(registryHandler) eventBus.addKeyCollectedListener(registryHandler) eventBus.addDoorOpenedListener(registryHandler) eventBus.addCheckpointReachedListener(registryHandler) global soundHandler soundHandler = SoundHandler() eventBus.addCoinCollectedListener(soundHandler) eventBus.addKeyCollectedListener(soundHandler) eventBus.addDoorOpeningListener(soundHandler) eventBus.addPlayerFootstepListener(soundHandler) eventBus.addMapTransitionListener(soundHandler) eventBus.addEndGameListener(soundHandler) eventBus.addLifeLostListener(soundHandler) eventBus.addWaspZoomingListener(soundHandler) eventBus.addBeetleCrawlingListener(soundHandler) eventBus.addCheckpointReachedListener(soundHandler) eventBus.addPlayerFallingListener(soundHandler) # create fixed sprites global fixedSprites fixedSprites = pygame.sprite.Group() fixedCoin = FixedCoin((27, 3)) coinCount = CoinCount(registry.coinCount, (38, 3)) keyCount = KeyCount(registry.keyCount, (0, 3)) lives = Lives(2, (3, 3)) checkpointIcon = CheckpointIcon((-11, -11)) fixedSprites.add(fixedCoin, lives, coinCount, keyCount, checkpointIcon) # create player global player player = Ulmo() player.coinCount = coinCount player.keyCount = keyCount player.lives = lives player.checkpointIcon = checkpointIcon # create the map rpgMap = parser.loadRpgMap(registry.mapName) player.setup("ulmo", rpgMap, eventBus) # set the start position player.setTilePosition(registry.playerPosition[0], registry.playerPosition[1], registry.playerLevel) # return the play state return PlayState()