class GameState(): def __init__(self, screen): self.screen = screen self.wall = GameWall(screen) self.piece = None self.next_piece = None self.timer_interval = TIMER_INTERVAL #1000ms self.game_score = 0 self.stopped = True self.paused = False self.session_count = 0 self.difficulty = 1 def set_timer(self, timer_interval): pygame.time.set_timer(pygame.USEREVENT, timer_interval) def stop_timer(self): pygame.time.set_timer(pygame.USEREVENT, 0) #传入0表示清除定时器 def add_score(self, score): self.game_score += score difficulty = self.game_score // DIFFICULTY_LEVEL_INTERVAL + 1 if difficulty > self.difficulty: self.difficulty += 1 self.timer_interval -= TIMER_DECREASE_VALUE pygame.time.set_timer(pygame.USEREVENT, self.timer_interval) def start_game(self): self.stopped = False self.set_timer(TIMER_INTERVAL) self.timer_interval = TIMER_INTERVAL self.piece = self.new_piece( ) #生成第一个方块。此时self.piece=None, self.next_piece引用方块对象。 self.piece = self.new_piece() #生成第二个方块,此时self.piece引用方块对象。 self.session_count += 1 self.wall.clear() self.game_score = 0 self.paused = False random.seed(int(time.time())) #每次游戏,使用不同的随机数序列 def pause_game(self): self.stop_timer() self.paused = True def resume_game(self): self.set_timer(self.timer_interval) self.paused = False def touch_bottom(self): self.wall.add_to_wall(self.piece) self.add_score(self.wall.eliminate_lines()) for c in range(COLUMN_NUM): if self.wall.is_wall(0, c): self.stopped = True break if not self.stopped: self.piece = self.new_piece() if self.piece.hit_wall(): self.stopped = True if self.stopped: self.stop_timer() def new_piece(self): self.piece = self.next_piece self.next_piece = Piece(random.choice(PIECE_TYPES), self.screen, self.wall) return self.piece
class GameState(object): def __init__(self, screen, game_resource): self.screen = screen self.game_wall = GameWall() self.game_resource = game_resource self.piece = None self.next_piece = Piece(random.choice(PIECE_TYPES), self.screen, self.game_wall) self.score = 0 self.game_round = 0 self.difficulty = ORIGIN_DIFFICULTY self.timer_interval = TIMER_INTERVAL // self.difficulty self.game_timer = None self.stopped = True self.paused = False self.rank = [] def set_timer(self, timer_interval): self.game_timer = pygame.time.set_timer(pygame.USEREVENT, timer_interval) def add_piece(self): self.piece = self.next_piece self.next_piece = Piece(random.choice(PIECE_TYPES), self.screen, self.game_wall) def add_score(self, score): self.score += score def add_difficulty(self): self.difficulty += 1 self.set_timer(self.timer_interval // self.difficulty) def start_game(self): if self.game_round > 0: self.game_resource.play_bg_music() if self.game_resource.music_muted: pygame.mixer.music.pause() self.game_resource.gameover_music_played = False self.stopped = False random.seed(time.time()) self.set_timer(self.timer_interval) self.add_piece() self.game_wall.clear() if self.score: self.rank.append(self.score) self.rank.sort() self.rank.reverse() self.score = 0 self.paused = False self.difficulty = ORIGIN_DIFFICULTY self.game_round += 1 def pause_game(self): self.set_timer(0) self.paused = True def resume_game(self): self.set_timer(self.timer_interval) self.paused = False def touch_bottom(self): self.game_wall.add_to_wall(self.piece) self.add_score(self.game_wall.eliminate_lines()) if not self.stopped: for c in range(COLUMN_NUM): if self.game_wall.is_wall(0, c): self.stopped = True self.game_resource.gameover_music_played = False break if not self.stopped: self.add_piece() if self.piece.hit_wall(): self.stopped = True self.game_resource.gameover_music_played = False if self.stopped: self.set_timer(0) if not self.game_resource.gameover_music_played and not self.game_resource.music_muted: self.game_resource.play_gameover_music() self.game_resource.gameover_music_played = True
class GameState(): def __init__(self, screen): self.screen = screen self.wall = GameWall(screen) self.piece = None #Piece(random.choice(PIECE_TYPES), screen, self.wall) self.next_piece = None self.timer_interval = TIMER_INTERVAL #1000ms #self.set_timer(self.timer_interval) self.game_score = 0 self.stopped = True self.paused = False self.session_count = 0 def set_timer(self, timer_interval): self.game_timer = pygame.time.set_timer(pygame.USEREVENT, timer_interval) def add_score(self, score): self.game_score += score def start_game(self): self.stopped = False self.set_timer(TIMER_INTERVAL) self.timer_interval = TIMER_INTERVAL #self.piece = Piece(random.choice(PIECE_TYPES), self.screen, self.wall) self.piece = self.new_piece() # the first block for hint self.piece = self.new_piece() # second self.session_count += 1 self.wall.clear() self.game_score = 0 self.paused = False def pause_game(self): pygame.time.set_timer(pygame.USEREVENT, 0) # 0 to clear timer self.paused = True def resume_game(self): self.set_timer(self.timer_interval) self.paused = False def stop_timer(self): pygame.time.set_timer(pygame.USEREVENT, 0) def touch_bottom(self): self.wall.add_to_wall(self.piece) self.add_score(self.wall.eliminate_lines()) # print(game_state.game_score) for c in range(COLUMN_NUM): if self.wall.is_wall(0, c): # game_area.draw_gameover() self.stopped = True break if not self.stopped: self.piece = self.new_piece() # game over if self.piece.hit_wall(): self.stopped = True else: self.stop_timer() def new_piece(self): self.piece = self.next_piece self.next_piece = Piece(random.choice(PIECE_TYPES), self.screen, self.wall) return self.piece
class GameState(): def __init__(self, screen): self.is_gameover = True #按s键开始游戏 self.is_paused = False self.screen = screen self.wall = GameWall(screen) self.piece = None self.next_piece = None self.score = 0 self.timer_interval = TIMER_INTERVAL #1000ms self.session_count = 0 #玩第几轮? self.level = 1 #难度等级。一共20级。得到2000分升一级。每升一级提速50ms(即timer_interval减少50) def gameover(self): self.is_gameover = True def start_game(self): self.is_gameover = False self.set_timer(TIMER_INTERVAL) self.timer_interval = TIMER_INTERVAL self.session_count += 1 self.new_piece() #此时, self.piece = None self.new_piece() #此时,self.piece, self.next_piece都不等于None self.score = 0 def restart_game(self): self.wall.clear() self.start_game() def pause_game(self): self.remove_timer() self.is_paused = True def resume_game(self): self.set_timer(self.timer_interval) self.is_paused = False def set_timer(self, timer_interval): self.game_timer = pygame.time.set_timer(pygame.USEREVENT, timer_interval) def remove_timer(self): self.game_timer = pygame.time.set_timer(pygame.USEREVENT, 0) def new_piece(self): shape = random.randint(0, len(SHAPES) - 1) self.piece = self.next_piece self.next_piece = Piece(SHAPES[shape], self.screen, self.wall) return self.piece def add_score(self, eliminated_line_num): '''消行计分''' ''' 计分规则: 消掉1行:100分 消掉2行:200分 消掉3行:400分 消掉4行:800分 ''' assert (eliminated_line_num <= 4 and eliminated_line_num >= 0) if eliminated_line_num < 3: self.score += eliminated_line_num * 100 elif eliminated_line_num == 3: self.score += 400 else: self.score += 800 level = self.score // SCORE_PER_LEVEL + 1 if level > self.level: self.set_timer(TIMER_INTERVAL - (level - 1) * 50) self.level = level def touch_bottom(self): '''方块落到底部时,要消行,要生成新方块。如果触到顶部,游戏终止。''' self.add_score(self.wall.eliminate_lines()) for c in range(COLUMN_NUM): if self.wall.is_wall(0, c): # game_area.draw_gameover() #在这里绘制文字是不起作用的。必须放到主循环中。 self.gameover() break if not self.is_gameover: self.new_piece()