Exemple #1
0
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)
Exemple #2
0
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)
Exemple #3
0
class InvadersGame:

    def __init__(self):
        self.game = Game()
        floorframe = Frame(self.game.screen, Rect(0, 542, MAX_X + 36, 596))
        self.floor = TiledMap(self.game, floorframe)
        self.floor.fill_map('#', (25, 2))
        self.player = Player(self.game)
        self.aliens = Group()
        self.create_aliens()
        self.shots = Group()
        self.alien_shots = Group()

    def create_aliens(self):
        for i in range(4):
            for j in range(20):
                direction = [LEFT, RIGHT][i % 2]
                alien = Alien(self.game, Vector(j * 32 + 32, i * 64), direction)
                self.aliens.add(alien)

    def shoot(self):
        shot = self.player.get_shot()
        self.shots.add(shot)

    def draw(self):
        self.game.screen.clear()
        self.player.draw()
        self.shots.draw(self.game.screen.display)
        self.alien_shots.draw(self.game.screen.display)
        self.aliens.draw(self.game.screen.display)
        self.floor.draw()

    def update(self):
        self.player.move()
        self.shots.update()
        self.alien_shots.update()
        self.aliens.update()
        for a in self.aliens:
            shot = a.get_shot()
            if shot:
                self.alien_shots.add(shot)
        self.draw()
        groupcollide(self.shots, self.aliens, True, True, collided=collide_mask)
        if spritecollideany(self.player, self.alien_shots, collided=collide_mask):
            self.game.exit()
        if not self.aliens:
            self.game.exit()

    def run(self):
        self.game.event_loop(figure_moves=self.player.set_direction,
            draw_func=self.update, keymap={K_SPACE: self.shoot}, delay=30)
Exemple #4
0
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)
Exemple #5
0
class PacGame:
    def __init__(self):
        self.game = Game()

        self.level = None
        self.pac = None
        self.ghosts = []
        self.status_box = None

        self.create_level()
        self.create_pac()
        self.create_ghosts()
        self.create_status_box()
        frame = Frame(self.game.screen, Rect(660, 220, 200, 200))
        self.lives = BarDisplay(frame, self.game, 3, 'p')

        self.collided = False
        self.mode = None
        self.update_mode = self.update_ingame

    def create_level(self):
        tmap = TiledMap(self.game)
        level = create_maze(*RANDOM_LEVEL_SIZE)
        self.level = PacLevel(level, tmap)

    def create_pac(self):
        self.pac = Pac(self.game, PAC_START, self.level)
        self.pac.set_direction(RIGHT)

    def create_ghosts(self):
        self.ghosts = []
        for pos in GHOST_POSITIONS:
            self.ghosts.append(Ghost(self.game, pos, self.level))

    def reset_level(self):
        self.pac.sprite.pos = Vector(PAC_START)
        self.create_ghosts()

    def create_status_box(self):
        frame = Frame(self.game.screen, Rect(660, 20, 200, 200))
        data = {
            'score': 0,
            'level': 1,
        }
        self.status_box = DictBox(frame, data)

    def check_collision(self):
        if self.pac.collision(self.ghosts):
            self.update_mode = self.update_die
            self.pac.die()
            self.collided = True

    def update_die(self):
        """finish movements"""
        if self.pac.sprite.finished:
            time.sleep(1)
            self.lives.decrease()
            if self.lives.value == 0:
                self.game.exit()
            else:
                self.reset_level()
                self.game.events.empty_event_queue()
                self.update_mode = self.update_ingame

    def update_level_complete(self):
        """finish movement"""
        if self.pac.sprite.finished:
            time.sleep(1)
            self.game.exit()

    def update_ingame(self):
        self.check_collision()
        if self.pac.eaten:
            self.status_box.data['score'] = self.pac.score
            self.pac.eaten = None
            self.score = self.pac.score
        if self.level.dots_left == 0:
            self.update_mode = self.update_level_complete

    def draw(self):
        self.update_mode()
        self.level.draw()
        self.pac.update()
        self.pac.draw()
        for g in self.ghosts:
            g.update()
            g.draw()
        self.status_box.draw()
        self.check_collision()

    def run(self):
        self.mode = self.update_ingame
        self.game.event_loop(figure_moves=self.pac.set_direction,
                             draw_func=self.draw)
Exemple #6
0
class SnakeGame:
    def __init__(self):
        self.game = Game()

        self.level = None
        self.snake = None
        self.status_box = None
        self.events = None
        self.score = 0

        self.create_level()
        self.create_snake()
        self.create_status_box()

        self.update_mode = self.update_ingame
        self.move_delay = MOVE_DELAY
        self.delay = MOVE_DELAY

    def create_snake(self):
        start_pos = (5, 5)
        self.snake = SnakeSprite(self.game, start_pos, self.level)
        self.snake.set_direction(RIGHT)

    def create_level(self):
        tmap = TiledMap(self.game)
        self.level = SnakeLevel(LEVEL, tmap)
        self.level.place_random_fruit()

    def create_status_box(self):
        frame = Frame(self.game.screen, Rect(660, 20, 200, 200))
        self.status_box = DictBox(frame, {'score': 0})

    def update_finish_moves(self):
        """finish movements before Game Over"""
        if not self.snake.is_moving():
            pygame.display.update()
            time.sleep(1)
            self.game.exit()

    def update_ingame(self):
        self.delay -= 1
        if self.delay <= 0:
            self.delay = self.move_delay
            if not EASY:
                self.snake.move_forward()
        if self.snake.eaten and not self.snake.is_moving():
            self.level.remove_fruit(self.snake.head.pos)
            self.level.place_random_fruit()
            self.status_box.data['score'] += 100
            self.snake.eaten = None
        if self.snake.crashed:
            self.update_mode = self.update_finish_moves
            self.score = self.status_box.data['score']

    def update(self):
        self.update_mode()
        self.snake.move()

    def draw(self):
        self.update()
        self.level.draw()
        self.snake.draw()
        self.status_box.draw()

    def run(self):
        self.game.event_loop(figure_moves=self.snake.set_direction,
                             draw_func=self.draw)
Exemple #7
0
class SnakeGame:

    def __init__(self):
        self.game = Game()

        self.level = None
        self.snake = None
        self.status_box = None
        self.events = None
        self.score = 0

        self.create_level()
        self.create_snake()
        self.create_status_box()

        self.update_mode = self.update_ingame
        self.move_delay = MOVE_DELAY
        self.delay = MOVE_DELAY

    def create_snake(self):
        start_pos = (5, 5)
        self.snake = SnakeSprite(self.game, start_pos, self.level)
        self.snake.set_direction(RIGHT)

    def create_level(self):
        tmap = TiledMap(self.game)
        self.level = SnakeLevel(LEVEL, tmap)
        self.level.place_random_fruit()

    def create_status_box(self):
        frame = Frame(self.game.screen, Rect(660, 20, 200, 200))
        self.status_box = DictBox(frame, {'score': 0})

    def update_finish_moves(self):
        """finish movements before Game Over"""
        if not self.snake.is_moving():
            pygame.display.update()
            time.sleep(1)
            self.game.exit()

    def update_ingame(self):
        self.delay -= 1
        if self.delay <= 0:
            self.delay = self.move_delay
            if not EASY:
                self.snake.move_forward()
        if self.snake.eaten and not self.snake.is_moving():
            self.level.remove_fruit(self.snake.head.pos)
            self.level.place_random_fruit()
            self.status_box.data['score'] += 100
            self.snake.eaten = None
        if self.snake.crashed:
            self.update_mode = self.update_finish_moves
            self.score = self.status_box.data['score']

    def update(self):
        self.update_mode()
        self.snake.move()

    def draw(self):
        self.update()
        self.level.draw()
        self.snake.draw()
        self.status_box.draw()

    def run(self):
        self.game.event_loop(figure_moves=self.snake.set_direction, draw_func=self.draw)
Exemple #8
0
class PacGame:

    def __init__(self):
        self.game = Game()

        self.level = None
        self.pac = None
        self.ghosts = []
        self.status_box = None

        self.create_level()
        self.create_pac()
        self.create_ghosts()
        self.create_status_box()
        frame = Frame(self.game.screen, Rect(660, 220, 200, 200))
        self.lives = BarDisplay(frame, self.game, 3, 'p')

        self.collided = False
        self.mode = None
        self.update_mode = self.update_ingame

    def create_level(self):
        tmap = TiledMap(self.game)
        level = create_maze(*RANDOM_LEVEL_SIZE)
        self.level = PacLevel(level, tmap)

    def create_pac(self):
        self.pac = Pac(self.game, PAC_START, self.level)
        self.pac.set_direction(RIGHT)

    def create_ghosts(self):
        self.ghosts = []
        for pos in GHOST_POSITIONS:
            self.ghosts.append(Ghost(self.game, pos, self.level))

    def reset_level(self):
        self.pac.sprite.pos = Vector(PAC_START)
        self.create_ghosts()

    def create_status_box(self):
        frame = Frame(self.game.screen, Rect(660, 20, 200, 200))
        data = {
            'score': 0,
            'level': 1,
        }
        self.status_box = DictBox(frame, data)

    def check_collision(self):
        if self.pac.collision(self.ghosts):
            self.update_mode = self.update_die
            self.pac.die()
            self.collided = True

    def update_die(self):
        """finish movements"""
        if self.pac.sprite.finished:
            time.sleep(1)
            self.lives.decrease()
            if self.lives.value == 0:
                self.game.exit()
            else:
                self.reset_level()
                self.game.events.empty_event_queue()
                self.update_mode = self.update_ingame

    def update_level_complete(self):
        """finish movement"""
        if self.pac.sprite.finished:
            time.sleep(1)
            self.game.exit()

    def update_ingame(self):
        self.check_collision()
        if self.pac.eaten:
            self.status_box.data['score'] = self.pac.score
            self.pac.eaten = None
            self.score = self.pac.score
        if self.level.dots_left == 0:
            self.update_mode = self.update_level_complete

    def draw(self):
        self.update_mode()
        self.level.draw()
        self.pac.update()
        self.pac.draw()
        for g in self.ghosts:
            g.update()
            g.draw()
        self.status_box.draw()
        self.check_collision()

    def run(self):
        self.mode = self.update_ingame
        self.game.event_loop(figure_moves=self.pac.set_direction,
            draw_func=self.draw)