class CollectFruit: def __init__(self): self.game = Game() self.tm = TiledMap(self.game) self.player = Sprite(self.game, 'b.pac_right', (4, 1), speed=4) self.tm.set_map(FRUITMAP) self.draw() self.events = None self.score = 0 def draw(self): self.player.move() self.tm.draw() self.player.draw() self.check_player_square() def move(self, direction): if self.player.finished: nearpos = self.player.pos + direction near = self.tm.at(nearpos) if near == WALL_TILE: return self.player.add_move(direction) def check_player_square(self): field = self.tm.at(self.player.pos) if field == EXIT_TILE: self.game.exit() elif field in FRUIT: self.score += 100 self.tm.set_tile(self.player.pos, '.') def run(self): self.game.event_loop(figure_moves=self.move, draw_func=self.draw)
class Boxes: def __init__(self): self.screen = Screen(Vector(600, 400), 'data/background.png') self.frame = Frame(self.screen, Rect(64, 64, 320, 320)) tile_factory = TileFactory('data/tiles.conf') self.tm = TiledMap(self.frame, tile_factory) self.player = Sprite(self.frame, tile_factory.get('b.tail'), Vector(4, 1), speed=2) self.tm.set_map(BOXMAP) self.draw() self.events = None def draw(self): self.tm.draw() self.player.draw() pygame.display.update() def move(self, direction): nearpos = self.player.pos + direction farpos = nearpos + direction near = self.tm.at(nearpos) far = self.tm.at(farpos) if near == '#': return if near in 'xX' and far in '#xX': return else: # move possible moves = MoveGroup() self.player.add_move(direction) moves.add(self.player) if near in 'xX': # crate moved floor = near=='x' and '.' or '*' insert = far=='*' and 'X' or 'x' moves.add(MapMove(self.tm, nearpos, direction, 1, floor_tile=floor, insert_tile=insert)) wait_for_move(moves, self.screen, self.draw, 0.01) self.tm.cache_map() self.draw() self.check_complete() def check_complete(self): s = self.tm.get_map() if s.count('X') == 4: print("\nCongratulations!\n") time.sleep(2) self.events.exit_signalled() def run(self): self.events = EventGenerator() self.events.add_listener(FigureMoveListener(self.move)) self.events.add_listener(ExitListener(self.events.exit_signalled)) with draw_timer(self, self.events): self.events.event_loop()
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 Boxes: def __init__(self): self.game = Game() self.tm = TiledMap(self.game) self.player = Sprite(self.game, 'b.tail', (4, 1), speed=4) self.tm.set_map(BOXMAP) def draw(self): self.tm.draw() self.player.draw() def move(self, direction): nearpos = self.player.pos + direction farpos = nearpos + direction near = self.tm.at(nearpos) far = self.tm.at(farpos) if near == '#': return if near in 'xX' and far in '#xX': return else: # move possible self.player.add_move(direction) moves = [self.player] if near in 'xX': # crate moved floor = '.' if near == 'x' else '*' insert = 'X' if far == '*' else 'x' moves.append(MapMove(self.tm, nearpos, direction, 4, floor_tile=floor, insert_tile=insert)) self.game.wait_for_move(moves, self.draw, 0.02) self.draw() self.check_complete() def check_complete(self): s = self.tm.get_map() if s.count('X') == 4: print("\nCongratulations!\n") time.sleep(2) self.game.exit() def run(self): self.game.event_loop(figure_moves=self.move, draw_func=self.draw)
class CollectFruit: def __init__(self, screen): self.screen = screen self.frame = Frame(self.screen, Rect(64, 64, 320, 320)) tile_factory = TileFactory('data/tiles.conf') self.tm = TiledMap(self.frame, tile_factory) self.player = Sprite(self.frame, tile_factory.get('b.pac_right'), Vector(4, 1), speed=2) self.tm.set_map(FRUITMAP) self.draw() self.events = None self.score = 0 def draw(self): self.tm.draw() self.player.draw() pygame.display.update() def move(self, direction): nearpos = self.player.pos + direction near = self.tm.at(nearpos) if near == '#': return self.player.add_move(direction) wait_for_move(self.player, self.screen, self.draw, 0.01) self.check_player_square() def check_player_square(self): field = self.tm.at(self.player.pos) if field == '*': time.sleep(1) self.events.exit_signalled() elif field in 'abcdefgh': self.score += 100 self.tm.set_tile(self.player.pos, '.') self.tm.cache_map() self.draw() def run(self): self.events = EventGenerator() self.events.add_listener(FigureMoveListener(self.move)) self.events.add_listener(ExitListener(self.events.exit_signalled)) with draw_timer(self, self.events): self.events.event_loop()
class Traversal(Game): def __init__(self): super().__init__() self.map = TiledMap(self.tiles, PUZZLEMAP, offset=Vector(50, 50)) self.sprite = TileSprite(self.tiles['b.pac_right'], Vector(0, 0), speed=2, offset=Vector(50, 10 * 32 + 50)) self.trees_counted = 0 self.path = DEFAULT_PATH.copy() def next_move(self): if not self.path: self.path = DEFAULT_PATH.copy() if self.map.at(self.sprite.pos) == '#': self.trees_counted += 1 self.map.set(self.sprite.pos, 'a') print('TREE!') vec = self.path.pop(0) self.sprite.add_move(vec) def check_complete(self): if self.sprite.pos.y == self.map.size.y - 1: print(self.trees_counted) self.exit() def on_draw(self): """automatically called to draw everything""" arcade.start_render() self.map.draw() self.sprite.draw() def update(self, delta_time): """automatically called every frame""" if self.sprite.is_moving: self.sprite.update() else: self.next_move() self.check_complete()
class SlidingPuzzle: def __init__(self): self.screen = Screen(Vector(350, 350), 'data/background.png') frame = Frame(self.screen, Rect(64, 64, 320, 320)) tile_factory = TileFactory('data/tiles.conf') self.tm = TiledMap(frame, tile_factory) self.gap = Vector(4, 4) self.tm.set_map(PUZZLEMAP) self.tm.draw() self.events = None pygame.display.update() def move(self, direction): start = self.gap - direction if self.tm.at(start) == '#': return move = MapMove(self.tm, start, direction, 2) wait_for_move(move, self.screen, 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: print("\nCongratulations!\n") self.events.exit_signalled() def run(self): self.events = EventGenerator() self.events.add_listener(FigureMoveListener(self.move)) self.events.event_loop()
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 FrutrisLevel: def __init__(self, game, level): self.tmap = TiledMap(game) self.tmap.set_map(level) def insert(self, pos, fruit): self.tmap.set_tile(pos, fruit) def is_pos_in_box(self, pos): if (0 <= pos.x < self.tmap.size.x) and \ (0 <= pos.y < self.tmap.size.y): return True def are_positions_empty(self, positions): for pos in positions: if self.tmap.at(pos) != '.': return False return True def trace_multiplets(self, pos, trace, char, taboo): """Recursively looks for quartets of identical bricks.""" if not self.is_pos_in_box(pos) \ or pos in taboo \ or self.tmap.at(pos) != char or pos in trace: return trace.add(pos) self.trace_multiplets(pos + LEFT, trace, char, taboo) self.trace_multiplets(pos + RIGHT, trace, char, taboo) self.trace_multiplets(pos + DOWN, trace, char, taboo) def find_multiplets(self): """Returns a list of multiplet positions""" multiplets = FruitMultiplets() taboo = [] for x in range(1, self.tmap.size.x - 1): for y in range(self.tmap.size.y): pos = Vector(x, y) if self.tmap.at(pos) in ('.', '#'): continue found = set() char = self.tmap.at(pos) self.trace_multiplets(pos, found, char, taboo) if len(found) >= 4: multiplets.add_multiplet(found) taboo.extend(found) return multiplets def get_dropped_bricks(self): moves = [] for x in range(self.tmap.size.x): pos = Vector(x, self.tmap.size.y - 1) while pos.y > 0: pos_above = pos + UP if self.tmap.at(pos) == '.' and self.tmap.at(pos_above) != '.': moves.append(MapMove(self.tmap, pos_above, DOWN, speed=4)) pos = pos_above return MoveGroup(moves) def get_explosions(self, multiplets): explosions = [] for pos in multiplets.get_positions(): self.tmap.set_tile(pos, '.') explosions.append(AnimatedTile(EXPLOSION, self.tmap.tile_factory, self.tmap.frame, pos)) if len(explosions) == 0: return None explosions = MoveGroup(explosions) return explosions def box_overflow(self): if self.get_stack_size() == self.tmap.size.y - 1: return True def get_stack_size(self): """Returns height of the fruit stack.""" y = 0 while y < self.tmap.size.y: for x in range(1, self.tmap.size.x - 1): char = self.tmap.at((x, y)) if char != '.': return self.tmap.size.y - y - 1 y += 1 return 0 def draw(self): self.tmap.draw()