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.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