def __init__(self): self.screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('DanceDanceCV') self.intro = Intro(self.screen) self.choose = Choose(self.screen) self.highScore = HighScore(self.screen) self.song1 = 'fire' self.song2 = 'summer'
class DanceDanceCV(object): def __init__(self): self.screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('DanceDanceCV') self.intro = Intro(self.screen) self.choose = Choose(self.screen) self.highScore = HighScore(self.screen) self.song1 = 'fire' self.song2 = 'summer' def run(self): playing = True mode = 'intro' while playing: if mode == 'intro': mode = self.intro.run() elif mode == 'choose': mode = self.choose.run() elif mode == 'high score': mode = self.highScore.run() elif mode == 'play1': self.play = Play(self.song1) mode = self.play.run() elif mode == 'play2': self.play = Play(self.song2) mode = self.play.run() elif mode == 'quit': playing = False pygame.quit()
def render(self): self.getGame().screen.blit(self.__highScoreSprite, (50, 50)) self.clearText() highscore = HighScore() x = 350 y = 100 for score in highscore.getScores(): self.addText(score[0], x, y, size=30) # print the player name self.addText(str(score[1]), x + 200, y, size=30) # print the player score y += 30 self.addText("Press F1 to start a new game", x, y + 60, size=30) super(HighScoreScene, self).render()
def setup(self): self.clock = pygame.time.Clock() self.main_menu = MainMenu() self.game_over_menu = GameOverMenu() self.highscore = HighScore() self.fish = Fish() self.pipes = [] self.score_text = Text(C.SCORE_TEXT+str(var.score), C.SCORE_TEXT_FONT_SIZE, C.SCORE_TEXT_COLOR) self.highscore_text = Text(C.HIGHSCORE_TEXT+str(self.highscore.highscore), C.SCORE_TEXT_FONT_SIZE, C.SCORE_TEXT_COLOR) score_size = self.score_text.get_size() highscore_size = self.highscore_text.get_size() self.score_text.set_pos(Vector(C.SCORE_TEXT_POS.x, C.SCORE_TEXT_POS.y-score_size.y/2)) self.highscore_text.set_pos(Vector(C.SCREEN_SIZE.x-highscore_size.x-C.SCORE_TEXT_POS.x, C.SCORE_TEXT_POS.y-highscore_size.y/2))
def save_high_score(): try: name = request.json['name'] time = request.json['time'] score = request.json['score'] new_high_score = HighScore(name, score, time) high_scores.append(new_high_score) return jsonify({'code': 200, 'message': 'success'}) except: error = sys.exc_info()[0] app.logger.error('error => %s', error) return jsonify({ 'code': 400, 'message': 'Error, unable to process request' })
def handleEvents(self, events): super(GameOverScene, self).handleEvents(events) for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: game = self.getGame() HighScore().add(self.__playerName, int(self.getGame().getScore())) game.reset() game.changeScene(GameConstants.HIGHSCORE_SCENE) elif 65 <= event.key <= 122: # limit to a-z characters self.__playerName += chr(event.key) if event.key == pygame.K_F1: self.getGame().reset() self.getGame().changeScene(GameConstants.PLAYING_SCENE)
class Game(): def __init__(self): self.setup() self.run() def setup(self): self.clock = pygame.time.Clock() self.main_menu = MainMenu() self.game_over_menu = GameOverMenu() self.highscore = HighScore() self.fish = Fish() self.pipes = [] self.score_text = Text(C.SCORE_TEXT+str(var.score), C.SCORE_TEXT_FONT_SIZE, C.SCORE_TEXT_COLOR) self.highscore_text = Text(C.HIGHSCORE_TEXT+str(self.highscore.highscore), C.SCORE_TEXT_FONT_SIZE, C.SCORE_TEXT_COLOR) score_size = self.score_text.get_size() highscore_size = self.highscore_text.get_size() self.score_text.set_pos(Vector(C.SCORE_TEXT_POS.x, C.SCORE_TEXT_POS.y-score_size.y/2)) self.highscore_text.set_pos(Vector(C.SCREEN_SIZE.x-highscore_size.x-C.SCORE_TEXT_POS.x, C.SCORE_TEXT_POS.y-highscore_size.y/2)) def run(self): while(True): #show menu while(not var.game_started or var.paused): time_passed_seconds = self.fps_count() self.detect_events(time_passed_seconds) self.draw_background() self.main_menu.draw() self.update_screen() #game started while(var.game_started and not var.paused): distance = random.randint(C.MIN_PIPE_DISTANCE, C.MAX_PIPE_DISTANCE) if len(self.pipes) == 0 or self.pipes[-1].get_x_pos() < C.SCREEN_SIZE.x-distance: self.gen_pipe_pair() self.check_for_passed_pipes() time_passed_seconds = self.fps_count() self.detect_events(time_passed_seconds) self.draw_background() self.draw_playground() self.update_playground(time_passed_seconds) self.remove_pipes_outside_screen() self.check_for_pipe_collision() if(var.game_over): self.game_over() def detect_events(self, time_passed_seconds): for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == pygame.MOUSEBUTTONDOWN: if not var.game_started or var.paused: self.main_menu.button_press_actions(pygame.mouse.get_pos()) elif var.game_over: self.game_over_menu.button_press_actions(pygame.mouse.get_pos(), self.fish) if event.type == pygame.KEYDOWN: if event.key == pygame.K_p: var.paused = True if event.key == pygame.K_SPACE: self.fish.dir.y = C.FISH_THRUST*time_passed_seconds def fps_count(self): time_passed = self.clock.tick(C.FPS) return time_passed / 1000.0 def check_for_passed_pipes(self): for pipe in self.pipes: if not pipe.passed and pipe.get_x_pos() <= C.FISH_CONSTANT_POS_X: pipe.passed = True var.score += 0.5 def draw_background(self): area = (0, 0, C.SCREEN_SIZE.x, C.SCREEN_SIZE.y) pygame.draw.rect(C.SCREEN, (144,211,255), area) def draw_score_screen(self): area = (0,0, C.SCREEN_SIZE.x, C.SCORE_SCREEN_HEIGHT) pygame.draw.rect(C.SCREEN, C.SCORE_SCREEN_COLOR, area) self.score_text.draw() self.highscore_text.draw() def draw_playground(self): self.draw_score_screen() self.fish.draw() for pipe in self.pipes: pipe.draw() def update_playground(self, time_passed_seconds): self.score_text.update(C.SCORE_TEXT+str(int(var.score))) highscore_size = self.highscore_text.get_size() self.highscore_text.set_pos(Vector(C.SCREEN_SIZE.x-highscore_size.x-C.SCORE_TEXT_POS.x, C.SCORE_TEXT_POS.y-highscore_size.y/2)) self.fish.update(time_passed_seconds) for pipe in self.pipes: pipe.update(time_passed_seconds) self.update_screen() def update_screen(self): pygame.display.update() def gen_pipe_pair(self): floor_body_num = random.randint(0, C.AVAIL_PIPE_BODY_SPACE) roof_body_num = C.AVAIL_PIPE_BODY_SPACE-floor_body_num floor_pipe = Pipe(floor_body_num) roof_pipe = Pipe(roof_body_num) floor_pipe.set_pos_floor(Vector(C.SCREEN_SIZE.x, C.SCREEN_SIZE.y-C.PIPE_HEIGHT)) roof_pipe.set_pos_roof(Vector(C.SCREEN_SIZE.x, C.SCORE_SCREEN_HEIGHT)) self.pipes.append(floor_pipe) self.pipes.append(roof_pipe) def check_for_pipe_collision(self): for pipe in self.pipes: if pipe.collide_any(self.fish): var.game_over = True return def remove_pipes_outside_screen(self): for pipe in self.pipes: if pipe.get_x_pos()+C.PIPE_WIDTH <= 0: self.pipes.remove(pipe) def remove_all_pipes(self): for pipe in self.pipes: self.pipes.remove(pipe) self.pipes = [] def game_over(self): game_over_text = Text(C.GAME_OVER_TEXT, C.GAME_OVER_FONT_SIZE, C.GAME_OVER_TEXT_COLOR) final_score_text = Text(C.SCORE_TEXT+str(int(var.score)), C.GAME_OVER_FONT_SIZE, C.GAME_OVER_TEXT_COLOR) game_over_text_size = game_over_text.get_size() final_score_text_size = final_score_text.get_size() game_over_menu_height = self.game_over_menu.get_height() text_pos_y = C.SCREEN_SIZE.y/2-(C.DISTANCE_BETWEEN_GAME_OVER_UNITS + game_over_text_size.y + final_score_text_size.y + game_over_menu_height)/2 game_over_text.set_pos(Vector(C.SCREEN_SIZE.x/2-game_over_text_size.x/2, text_pos_y)) text_pos_y += game_over_text_size.y + C.DISTANCE_BETWEEN_GAME_OVER_UNITS final_score_text.set_pos(Vector(C.SCREEN_SIZE.x/2-final_score_text_size.x/2, text_pos_y)) text_pos_y += final_score_text_size.y + C.DISTANCE_BETWEEN_GAME_OVER_UNITS self.game_over_menu.set_pos(Vector(C.SCREEN_SIZE.x/2-C.BTN_WIDTH/2, text_pos_y)) self.remove_all_pipes() self.highscore.set_highscore_if_needed(var.score) self.highscore_text.update(C.HIGHSCORE_TEXT+str(int(self.highscore.highscore))) while(var.game_over): time_passed_seconds = self.fps_count() self.detect_events(time_passed_seconds) self.draw_background() game_over_text.draw() final_score_text.draw() self.game_over_menu.draw() self.update_screen()
run = False run2 = False run3 = False run4 = False while True: while not run and not run2 and not run3 and not run4: winstr = "You beat the game congratulations!" namept1 = Score([200, 200], screenSize) namept1.selected = True namept2 = Score([250, 200], screenSize) namept3 = Score([300, 200], screenSize) namept4 = Score([350, 200], screenSize) score1 = HighScore([300, 50], [""], screenSize) score2 = HighScore([300, 100], [""], screenSize) score3 = HighScore([300, 150], [""], screenSize) score4 = HighScore([300, 200], [""], screenSize) score5 = HighScore([300, 250], [""], screenSize) score6 = HighScore([300, 300], [""], screenSize) score7 = HighScore([300, 350], [""], screenSize) score8 = HighScore([300, 400], [""], screenSize) score9 = HighScore([300, 450], [""], screenSize) score10 = HighScore([300, 500], [""], screenSize) gamewin = HighScore([400, 300], winstr, screenSize) yourscore = HighScore([100, 25], [""], screenSize) counter = Counter([45,25], screenSize) highscorereader = HighScoreReader("rcs/scores.txt", yourscore, counter) background = Screen(["rcs/imgs/screens/Background.png"], [0,0], screenSize, 10)