def __init__(self, width=600, height=400, fps=50, title="Planet Invader"): self.width = width self.height = height self.screen = pygame.display.set_mode((self.width, self.height)) self.fps = fps self.title = title self.bgColor = (255, 255, 255) pygame.init() self.highScore=0 self.score=0 self.viewScores=Scores(self.highScore) self.scoreList=[] self.scoreList=self.viewScores.getScores('scores.txt') self.scoreList=self.getNumList(self.scoreList) self.highScore=max(self.scoreList) self.viewScores.drawList() self.intro=Start(self.highScore) self.tutorial=Instructions() self.gamePlay=Game(self.highScore) self.over=GameOver(self.highScore,self.score) self.clear=GameClear(self.highScore,self.score) self.start=True self.play=False self.howTo=False self.isOver=False self.congrats=False self.scores=False self._keys = dict() pygame.mixer.pre_init(44100,16,5,4096) pygame.mixer.init()
def run(self): playing = True mode = 'intro' count = 0 while playing: # print(self.pipegap) if mode == 'intro' or mode[0] == 'intro': if count > 0: self.__init__() if mode != 'intro': print(mode[1]) self.pipegap = mode[1] mode = self.intro.run() elif mode == 'game': self.game = Game(self.screen, self.pipegap) print(self.pipegap) mode = self.game.run() elif mode == 'highscore': mode = self.highScore.run() elif mode[0] == 'gameover': count += 1 self.scoreAppend = True self.gameOver = GameOver(self.screen, mode[1]) mode = self.gameOver.run() elif mode == 'settings': mode = self.settings.run() elif mode == 'quit': playing = False pygame.quit()
def __init__(self): self.states = [ GameOver(), Menu(self), Level1(self) ] self.state_index = 1
def update(self): for obj in self.gameObjects: obj.update() self.particleManager.update() if self.spawnTimer > 0: self.spawnTimer -= 1 if self.spawnTimer is 0: if self.lives < 0: self.gameEngine.scene = GameOver(self.gameEngine) return player = Player(self, self.spawn[0], self.spawn[1]) self.gameObjects.append(player)
def runGameOver(self): gameover = GameOver(self.gameMode, self.finalScore) gameover.display(self.screen) if self.gameMode == 2: playMusic("you lose") elif self.gameMode == 3: playMusic("you win") elif self.gameMode == 4: playMusic("time out") while self.gameMode >= 2: update = gameover.update() if update == 1: self.quit = True return False elif update == "play again": return True elif update == "show scores": gameover.displayScores(self.screen) return False
def loadLevel(self): """ Loads the level specified by the level argument. """ #Try open the level, if it doesn't exist go to the win screen try: f = open("data/levels/level_%03d" % self.level) except IOError: self.gameEngine.scene = GameOver(self.gameEngine, True) return del self.gameObjects[:] x = y = 0 while 1: row = f.readline() if not row: break for col in row: obj = None #object to be added at the end of each iteration if col == "W": obj = Wall(self, x, y) if col == "E": obj = Goal(self, x, y) if col == "L": obj = FreeLife(self, x, y) if col == "B": obj = Player(self, x, y) self.spawn = (x, y) if obj is not None: self.gameObjects.append(obj) x += 16 y += 16 x = 0
class ScreamingBird(object): def __init__(self): self.screen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT)) pygame.display.set_caption('Screaming Bird') self.pipegap = 120 self.intro = Intro(self.screen) self.highScore = HighScore(self.screen) self.settings = Setting(self.screen) def run(self): playing = True mode = 'intro' count = 0 while playing: # print(self.pipegap) if mode == 'intro' or mode[0] == 'intro': if count > 0: self.__init__() if mode != 'intro': print(mode[1]) self.pipegap = mode[1] mode = self.intro.run() elif mode == 'game': self.game = Game(self.screen, self.pipegap) print(self.pipegap) mode = self.game.run() elif mode == 'highscore': mode = self.highScore.run() elif mode[0] == 'gameover': count += 1 self.scoreAppend = True self.gameOver = GameOver(self.screen, mode[1]) mode = self.gameOver.run() elif mode == 'settings': mode = self.settings.run() elif mode == 'quit': playing = False pygame.quit()
def changeState(s): if s == "menu": mS = MenuScreen() return mS elif s == "levelZero": l0 = LevelZero() return l0 elif s == "gameOver": gO = GameOver() return gO elif s == "youWin": yW = WinScreen() return yW elif s == "levelTwo": l2 = LevelTwo() return l2 elif s == "levelThree": l3 = LevelThree() return l3 elif s == "levelFour": l4 = LevelFour() return l4 elif s == "levelFive": l5 = LevelFive() return l5 elif s == "pvp": l6 = Pvp() return l6 elif s == "highScore": hS = HighScore() return hS elif s == "controls": cT = Controls() return cT elif s == "options": oP = Options() return oP
def loadLevel(self): try: f = open("data/levels/%03d"%self.level) except IOError: self.gameEngine.scene = GameOver(self.gameEngine) return del self.objects[:] x=y=0 while 1: row = f.readline() if not row: break for col in row: obj=None #S == start if col == "S": self.player = Player(self,x,y) self.spawn = (x,y) #E == end if col == "E": obj = Goal(self,x,y) #walls if col == "W": obj = Wall(self,x,y,(255,255,255)) if col == "R": obj = Wall(self,x,y,(255,0,0)) if col == "G": obj = Wall(self,x,y,(0,255,0)) if col == "B": obj = Wall(self,x,y,(0,0,255)) if col == "M": obj = Wall(self,x,y,(255,0,255)) if col == "C": obj = Wall(self,x,y,(0,255,255)) if col == "Y": obj = Wall(self,x,y,(255,255,0)) #spikes if col == "w": obj = Spike(self,x,y,(255,255,255)) if col == "r": obj = Spike(self,x,y,(255,0,0)) if col == "g": obj = Spike(self,x,y,(0,255,0)) if col == "b": obj = Spike(self,x,y,(0,0,255)) if col == "m": obj = Spike(self,x,y,(255,0,255)) if col == "c": obj = Spike(self,x,y,(0,255,255)) if col == "y": obj = Spike(self,x,y,(255,255,0)) if obj is not None: self.objects.append(obj) x+=16 y+=16 x=0
class Main(object): # returns a list of past scores in order to generate score page and high score def getNumList(self,list): new=[] for i in range(len(list)): num=int(list[i][:-1]) new.append(num) return new def __init__(self, width=600, height=400, fps=50, title="Planet Invader"): self.width = width self.height = height self.screen = pygame.display.set_mode((self.width, self.height)) self.fps = fps self.title = title self.bgColor = (255, 255, 255) pygame.init() self.highScore=0 self.score=0 self.viewScores=Scores(self.highScore) self.scoreList=[] self.scoreList=self.viewScores.getScores('scores.txt') self.scoreList=self.getNumList(self.scoreList) self.highScore=max(self.scoreList) self.viewScores.drawList() self.intro=Start(self.highScore) self.tutorial=Instructions() self.gamePlay=Game(self.highScore) self.over=GameOver(self.highScore,self.score) self.clear=GameClear(self.highScore,self.score) self.start=True self.play=False self.howTo=False self.isOver=False self.congrats=False self.scores=False self._keys = dict() pygame.mixer.pre_init(44100,16,5,4096) pygame.mixer.init() def timerFired(self, dt): self.gamePlay.timerFired(dt) # reset values after each mode change def resetTrans(self): self.intro.enterPlay=False self.intro.enterHowTo=False self.intro.enterScores=False self.tutorial.enterStart=False self.tutorial.pageOne=True self.viewScores.backMenu=False self.viewScores.drawList() self.gamePlay.gameOver=False self.gamePlay.gameClear=False self.gamePlay.pause=False self.gamePlay.player.x=260 self.gamePlay.player.y=160 self.gamePlay.gauge=0 self.gamePlay.score=0 self.gamePlay.time=0 self.gamePlay.missileList.empty() self.gamePlay.humans.empty() self.gamePlay.houses.empty() self.gamePlay.buildings.empty() self.gamePlay.player.__init__(3,260,160) self.gamePlay.player.update(0,0) self.gamePlay.beam.__init__(230,240) self.gamePlay.beam.update(0,0) self.gamePlay.player.livesLeft=3 self.over.back=False self.clear.back=False # run all of the modes using booleans and while loops def run(self): clock = pygame.time.Clock() # screen = pygame.display.set_mode((self.width, self.height)) # set the title of the window pygame.display.set_caption(self.title) # call game-specific initialization playing = True while playing: self.screen.fill(self.bgColor) while self.start: self.intro.getEvent() if self.intro.enterPlay: self.start=False self.play=True elif self.intro.enterHowTo: self.start=False self.howTo=True elif self.intro.exitGame: playing=False self.start=False elif self.intro.enterScores: self.start=False self.scores=True self.intro.redrawAll(self.screen) pygame.display.flip() while self.howTo: if self.tutorial.enterStart: self.howTo=False self.start=True self.tutorial.getEvent() self.tutorial.redrawAll(self.screen) pygame.display.flip() while self.scores: if self.viewScores.backMenu: self.scores=False self.start=True self.viewScores.getEvent() self.viewScores.redrawAll(self.screen) pygame.display.flip() while self.play: time = clock.tick(self.fps) self.timerFired(time) if not pygame.mixer.music.get_busy(): self.gamePlay.playMusic() if self.gamePlay.gameOver: pygame.mixer.music.stop() if self.gamePlay.score>self.highScore: self.highScore=self.gamePlay.score self.intro=Start(self.highScore) self.viewScores.drawList() if self.gamePlay.score>self.viewScores.scoreList[-9]: self.viewScores.writeFile('scores.txt',"%d\n" \ % self.gamePlay.score) self.play=False self.isOver=True if self.gamePlay.gameClear: pygame.mixer.music.stop() if self.gamePlay.score>self.highScore: self.highScore=self.gamePlay.score self.intro=Start(self.highScore) self.viewScores.drawList() if self.gamePlay.score>self.viewScores.scoreList[-9]: self.viewScores.writeFile('scores.txt',"%d\n" \ % self.gamePlay.score) self.viewScores.writeFile('scores.txt',"%d\n" \ % self.gamePlay.score) self.play=False self.congrats=True self.gamePlay.getEvent() self.gamePlay.keys() self.gamePlay.redrawAll(self.screen) pygame.display.flip() while self.isOver: self.gamePlay.beamSound.stop() if self.over.back: self.isOver=False self.start=True self.over.getEvent() self.over.redrawAll(self.screen) pygame.display.flip() while self.congrats: self.gamePlay.beamSound.stop() if self.clear.back: self.congrats=False self.start=True self.clear.getEvent() self.clear.redrawAll(self.screen) pygame.display.flip() self.resetTrans() pygame.quit()