class SlidingPuzzle: def __init__(self): self.game = Game() self.tm = TiledMap(self.game) self.gap = Vector(4, 4) self.tm.set_map(PUZZLEMAP) self.tm.draw() self.events = None self.game.frame.print_text("Build horizontal rows", (0, 220)) pygame.display.update() self.game.event_loop(figure_moves=self.move) def move(self, direction): start = self.gap - direction if self.tm.at(start) == '#': return move = MapMove(self.tm, start, direction, 2) self.game.wait_for_move(move, self.tm.draw, 0.01) self.gap = start self.check_complete() def get_same(self, row): counter = Counter(row) return counter.most_common(1)[0][1] def check_complete(self): s = self.tm.get_map() rows = s.split('\n') same = [self.get_same(row) for row in rows[1:5]] if sum(same) == 15: self.game.frame.print_text("Congratulations", (0, 220)) pygame.display.update() time.sleep(3) self.game.exit()
class MazeGame: def __init__(self): self.game = Game() self.map = TiledMap(self.game) self.map.set_map(MAZE) self.fruechte = self.fruechte_zaehlen(MAZE) self.sprite = Sprite(self.game, 'b.pac_right', (1, 1), speed=4) self.geist = Sprite(self.game, 'b.ghost_center', (8, 8), speed=4) self.game.event_loop(figure_moves=self.move, draw_func=self.draw) def fruechte_zaehlen(self, level): """Zählt die einzusammelnden Obststücke""" return len([c for c in level if c in "abcdefgh"]) def draw(self): """ Alles bewegen und zeichnen. Wird von event_loop() regelmäßig aufgerufen """ self.map.draw() self.sprite.move() self.sprite.draw() self.geist_bewegen() self.geist.draw() self.frucht_fressen() self.kollision() def geist_bewegen(self): """Bewegt den Geist, aber nicht durch Wände""" if self.geist.finished: richtung = random.choice([UP, DOWN, LEFT, RIGHT]) neues_feld = self.geist.pos + richtung if self.map.at(neues_feld) != '#': self.geist.add_move(richtung) self.geist.move() def kollision(self): """Prüft, ob Geist und Spieler auf dem gleichen Feld sind.""" if self.sprite.pos == self.geist.pos: self.game.frame.print_text('VERLOREN!!!', (50, 400)) pygame.display.update() time.sleep(5) self.game.exit() def frucht_fressen(self): """Mampft ein Obststück weg""" if self.map.at(self.sprite.pos) in 'abcdefgh': self.map.set_tile(self.sprite.pos, '.') self.fruechte -= 1 if self.fruechte == 0: self.game.frame.print_text('GEWONNEN!!!', (50, 400)) pygame.display.update() time.sleep(5) self.game.exit() def move(self, direction): """Bewegt die Spielfigur. Wird von event_loop() aufgerufen, wenn Pfeiltaste gedrückt""" neues_feld = self.sprite.pos + direction if self.sprite.finished and \ not self.map.at(neues_feld) == '#': self.sprite.add_move(direction)
class FrutrisGame: def __init__(self): config.FRAME = Rect(250, 10, 640, 512) self.game = Game() self.level_counter = LEVEL_COUNTER_INIT play_effect('frutris') self.frutris_box = FrutrisBox(self.game, LEVEL) # frame = Frame(self.screen, Rect(660, 220, 200, 200)) self.data = { 'score': 0, 'level': 1, } self.status_box = self.create_status_box() # Music self.music_counter = 50 # periodically check for expiring track self.current_music = ('a', 1) self.music = MusicPlayer() self.music.play_music('/home/krother/projects/frutris/frutris/music/a1.ogg') def choose_next_music(self): MUSIC = '/home/krother/projects/frutris/frutris/music/{}{}.ogg' LETTERS = 'aaaabbbbcccccccc' letter, number = self.current_music number = 3 - number stack_size = self.frutris_box.level.get_stack_size() letter = LETTERS[stack_size] #if len(self.players) >= 1: # stack_size = self.players[0].get_stack_size() self.current_music = (letter, number) next_track = MUSIC.format(letter, number) print('NEXT TRACK: ', next_track) return next_track def update_music(self): self.music_counter -= 1 if self.music_counter == 0: self.music_counter = 50 if self.music.check_music_status() == CLOSE_TO_END: next_track = self.choose_next_music() self.music.next_music(next_track) def update_level(self): self.level_counter -= 1 if self.level_counter == 0: self.data['level'] += 1 self.level_counter = LEVEL_COUNTER_INIT self.frutris_box.make_blocks_drop_faster() @property def score(self): return self.data['score'] def create_status_box(self): frame = Frame(self.game.screen, Rect(660, 20, 200, 200)) return DictBox(frame, self.data) def draw(self): self.data['score'] += self.frutris_box.counter.pull_score() self.frutris_box.draw() self.status_box.draw() if self.frutris_box.game_over: self.game.exit() self.update_level() self.update_music() def run(self): self.game.event_loop(figure_moves=self.frutris_box.store_command, draw_func=self.draw)