def __init__(self, size, ui): Ship.imageCache = Screen.imageCache Boulder.imageCache = Screen.imageCache BoulderFragment.imageCache = Screen.imageCache ScrollingCodeBackground.textCache = Screen.textCache ScrollingCodeBackground.resolution = Screen.resolution Counter.textCache = Screen.textCache Counter.resolution = Screen.resolution background = ScrollingCodeBackground() Screen.__init__(self, background, size, ui) self.ships = pygame.sprite.Group() ship = Ship(self, pos=(size[0]/2,size[1]), screenBoundaries=(0,0)+size) self.ships.add(ship) #May change if we add more ships (multiplayer?) for ship in self.ships: ship.move((0,-ship.rect.height/2)) ship.targetPosition = ship.position self.boulders = pygame.sprite.Group() self.nextBoulderTime = 0 self.boulderFragments = pygame.sprite.Group() self.healthBar = Bar(100,int(size[0]*0.72),int(size[1]*0.05),fullColor=(255,0,0),emptyColor=(0,0,0), borderSize=int(size[1]*0.005), borderColor=(255,255,255)) self.scoreDisplay = Counter(0,(self.healthBar.surface.get_rect().width,0)) musicPath = pathJoin(('music','Music.ogg')) pygame.mixer.music.load(musicPath) pygame.mixer.music.play(-1)
def __init__(self, size, ui): Ship.imageCache = Screen.imageCache Boulder.imageCache = Screen.imageCache BoulderFragment.imageCache = Screen.imageCache ScrollingCodeBackground.textCache = Screen.textCache ScrollingCodeBackground.resolution = Screen.resolution Counter.textCache = Screen.textCache Counter.resolution = Screen.resolution background = ScrollingCodeBackground() Screen.__init__(self, background, size, ui) self.ships = pygame.sprite.Group() ship = Ship(self, pos=(size[0] / 2, size[1]), screenBoundaries=(0, 0) + size) self.ships.add(ship) #May change if we add more ships (multiplayer?) for ship in self.ships: ship.move((0, -ship.rect.height / 2)) ship.targetPosition = ship.position self.boulders = pygame.sprite.Group() self.nextBoulderTime = 0 self.boulderFragments = pygame.sprite.Group() self.healthBar = Bar(100, int(size[0] * 0.72), int(size[1] * 0.05), fullColor=(255, 0, 0), emptyColor=(0, 0, 0), borderSize=int(size[1] * 0.005), borderColor=(255, 255, 255)) self.scoreDisplay = Counter( 0, (self.healthBar.surface.get_rect().width, 0)) musicPath = pathJoin(('music', 'Music.ogg')) pygame.mixer.music.load(musicPath) pygame.mixer.music.play(-1)
class GameScreen(Screen): def __init__(self, size, ui): Ship.imageCache = Screen.imageCache Boulder.imageCache = Screen.imageCache BoulderFragment.imageCache = Screen.imageCache ScrollingCodeBackground.textCache = Screen.textCache ScrollingCodeBackground.resolution = Screen.resolution Counter.textCache = Screen.textCache Counter.resolution = Screen.resolution background = ScrollingCodeBackground() Screen.__init__(self, background, size, ui) self.ships = pygame.sprite.Group() ship = Ship(self, pos=(size[0]/2,size[1]), screenBoundaries=(0,0)+size) self.ships.add(ship) #May change if we add more ships (multiplayer?) for ship in self.ships: ship.move((0,-ship.rect.height/2)) ship.targetPosition = ship.position self.boulders = pygame.sprite.Group() self.nextBoulderTime = 0 self.boulderFragments = pygame.sprite.Group() self.healthBar = Bar(100,int(size[0]*0.72),int(size[1]*0.05),fullColor=(255,0,0),emptyColor=(0,0,0), borderSize=int(size[1]*0.005), borderColor=(255,255,255)) self.scoreDisplay = Counter(0,(self.healthBar.surface.get_rect().width,0)) musicPath = pathJoin(('music','Music.ogg')) pygame.mixer.music.load(musicPath) pygame.mixer.music.play(-1) def initializeCallbackDict(self): self.callbackDict = {} self.callbackDict['look'] = ('deviceString', self.steer) self.callbackDict['exit'] = ('deviceString', self.exit) def steer(self, event): #move the spaceship in this method if hasattr(self._ui, 'getShipPosition'): for ship in self.ships: ship.targetPosition = (self._ui.getShipPosition(event.values[0]), ship.targetPosition[1]) else: for ship in self.ships: ship.targetPosition = (event.values[0], ship.targetPosition[1]) def exit(self): self._ui.clearTopScreen() pygame.mixer.music.stop() def addBoulderFragment(self, pos=(0,0), vel=(0,0), id=0): newBoulderFragment = BoulderFragment(self, pos=pos, vel=vel, id=id, screenBoundaries=(0,0)+self.resolution) self.boulderFragments.add(newBoulderFragment) def killBoulder(self, boulder): for ship in self.ships: ship.score += boulder.value self.scoreDisplay.updateValue(ship.score) boulder.kill() def draw(self, surf): Screen.draw(self, surf) self.ships.draw(surf) self.boulders.draw(surf) self.boulderFragments.draw(surf) self.healthBar.draw(surf,(0,0)) self.scoreDisplay.draw(surf) def update(self, *args): gameTime, frameTime = args[:2] Screen.update(self, *args) self.ships.update(*args) self.boulders.update(*args) self.boulderFragments.update(*args) #For every boulder colliding with a ship, #kill the boulder & lose health for ship in self.ships: for boulder in ship.testMaskCollision(self.boulders): ship.health -= boulder.damage self.healthBar.updateBarWithValue(ship.health) boulder.kill() if ship.health <= 0: #Kill ship, etc... for ship in self.ships: deadScreen = DeadScreen(self.resolution, self._ui, ship.score) self._ui.clearTopScreen() self._ui.addActiveScreens(deadScreen) pygame.mixer.music.stop() if gameTime >= self.nextBoulderTime: boulderPos = random.randint(0,self.resolution[0]), 0 a = (4**random.random()-1)/2 boulderVel = (a,abs(1-a)) self.boulders.add(Boulder(self, pos=boulderPos, vel=boulderVel, screenBoundaries=(0,0)+self.resolution)) self.nextBoulderTime = gameTime + random.randint(200,1000)
class GameScreen(Screen): def __init__(self, size, ui): Ship.imageCache = Screen.imageCache Boulder.imageCache = Screen.imageCache BoulderFragment.imageCache = Screen.imageCache ScrollingCodeBackground.textCache = Screen.textCache ScrollingCodeBackground.resolution = Screen.resolution Counter.textCache = Screen.textCache Counter.resolution = Screen.resolution background = ScrollingCodeBackground() Screen.__init__(self, background, size, ui) self.ships = pygame.sprite.Group() ship = Ship(self, pos=(size[0] / 2, size[1]), screenBoundaries=(0, 0) + size) self.ships.add(ship) #May change if we add more ships (multiplayer?) for ship in self.ships: ship.move((0, -ship.rect.height / 2)) ship.targetPosition = ship.position self.boulders = pygame.sprite.Group() self.nextBoulderTime = 0 self.boulderFragments = pygame.sprite.Group() self.healthBar = Bar(100, int(size[0] * 0.72), int(size[1] * 0.05), fullColor=(255, 0, 0), emptyColor=(0, 0, 0), borderSize=int(size[1] * 0.005), borderColor=(255, 255, 255)) self.scoreDisplay = Counter( 0, (self.healthBar.surface.get_rect().width, 0)) musicPath = pathJoin(('music', 'Music.ogg')) pygame.mixer.music.load(musicPath) pygame.mixer.music.play(-1) def initializeCallbackDict(self): self.callbackDict = {} self.callbackDict['look'] = ('deviceString', self.steer) self.callbackDict['exit'] = ('deviceString', self.exit) def steer(self, event): #move the spaceship in this method if hasattr(self._ui, 'getShipPosition'): for ship in self.ships: ship.targetPosition = (self._ui.getShipPosition( event.values[0]), ship.targetPosition[1]) else: for ship in self.ships: ship.targetPosition = (event.values[0], ship.targetPosition[1]) def exit(self): self._ui.clearTopScreen() pygame.mixer.music.stop() def addBoulderFragment(self, pos=(0, 0), vel=(0, 0), id=0): newBoulderFragment = BoulderFragment(self, pos=pos, vel=vel, id=id, screenBoundaries=(0, 0) + self.resolution) self.boulderFragments.add(newBoulderFragment) def killBoulder(self, boulder): for ship in self.ships: ship.score += boulder.value self.scoreDisplay.updateValue(ship.score) boulder.kill() def draw(self, surf): Screen.draw(self, surf) self.ships.draw(surf) self.boulders.draw(surf) self.boulderFragments.draw(surf) self.healthBar.draw(surf, (0, 0)) self.scoreDisplay.draw(surf) def update(self, *args): gameTime, frameTime = args[:2] Screen.update(self, *args) self.ships.update(*args) self.boulders.update(*args) self.boulderFragments.update(*args) #For every boulder colliding with a ship, #kill the boulder & lose health for ship in self.ships: for boulder in ship.testMaskCollision(self.boulders): ship.health -= boulder.damage self.healthBar.updateBarWithValue(ship.health) boulder.kill() if ship.health <= 0: #Kill ship, etc... for ship in self.ships: deadScreen = DeadScreen(self.resolution, self._ui, ship.score) self._ui.clearTopScreen() self._ui.addActiveScreens(deadScreen) pygame.mixer.music.stop() if gameTime >= self.nextBoulderTime: boulderPos = random.randint(0, self.resolution[0]), 0 a = (4**random.random() - 1) / 2 boulderVel = (a, abs(1 - a)) self.boulders.add( Boulder(self, pos=boulderPos, vel=boulderVel, screenBoundaries=(0, 0) + self.resolution)) self.nextBoulderTime = gameTime + random.randint(200, 1000)