Exemplo n.º 1
0
 def __init__(self, controller, game_mode=None, game_speed=1, game_level=1):
     super().__init__(controller=controller, game_mode=game_mode)
     self.cursor = Cursor(pos=(0, 0))
     self.wall = Wall(start_line_count=0, auto_line_add=False)
     self.game_objects = [self.cursor, self.wall]
     self.lives = 0
     self.start_game()
Exemplo n.º 2
0
 def restart_game(self):
     self.lives -= 1
     self.start_time = time.time()
     self.turret = Turret((5, 18))
     self.bullets = []
     self.wall = Wall()
     self.game_status = True
     self.game_objects = [self.turret, self.wall]
     self.start_game()
Exemplo n.º 3
0
 def restart_game(self):
     self.game_status = True
     self.lives -= 1
     self.brick = Brick(pos=(12, 5), game_speed=self.game_speed)
     self.wall = Wall(start_line_count=0,
                      auto_line_add=False,
                      direction='down',
                      out_of_screen=True)
     self.game_objects = [self.brick, self.wall]
     self.player = self.brick
     self.start_game()
Exemplo n.º 4
0
    def random(cls, width, height, rock_count, diamond_count):
        grid = cls(width, height)

        for x in range(width):
            for y in range(height):
                if x == 0 or y == 0 or x == width - 1 or y == height - 1:
                    grid.cells[x][y] = Wall(grid, x, y)
                else:
                    grid.cells[x][y] = Ground(grid, x, y)
        grid.cells[1][1] = Hero(grid, 1, 1)

        cell_indexes = [
            (x, y)
            for x in range(1, width - 1)
            for y in range(1, height - 1)
        ]

        cell_indexes.remove((1, 1))

        for _ in range(rock_count):
            x, y = cell_indexes.pop(random.randint(0, len(cell_indexes) - 1))
            grid.cells[x][y] = Rock(grid, x, y)

        for _ in range(diamond_count):
            x, y = cell_indexes.pop(random.randint(0, len(cell_indexes) - 1))
            grid.cells[x][y] = Diamond(grid, x, y)

        return grid
Exemplo n.º 5
0
    def setupWalls(self):
        points = [(1, 0), (-1, 0), (0, 1), (0, -1)]
        w, h = UI.SCREEN_WIDTH / 2 * Wall.a, UI.SCREEN_HEIGHT / 2 * Wall.b

        points = list(map(lambda x: (x[0] * w, x[1] * h), points))
        self.walls = [
            Wall(p,
                 Direction.Horizontal if p[0] == 0.0 else Direction.Vertical)
            for p in points
        ]
Exemplo n.º 6
0
 def __init__(self,
              controller,
              game_mode='traffic',
              game_level=1,
              game_speed=1):
     super().__init__(controller=controller, game_mode=game_mode)
     self.snake = SnakeObj()
     self.player = self.snake
     self.__class__.FRAME = self.fps / game_speed
     print(self.__class__.FRAME)
     self.snake_food = SnakeFood(self.snake)
     self.wall = Wall(start_line_count=0,
                      auto_line_add=False,
                      direction='down',
                      out_of_screen=True,
                      wall_scheme=self.LEVELS[str(game_level)])
     self.game_objects = [self.snake, self.snake_food, self.wall]
     print(self.snake.obj)
     print(self.wall.get_obj())
     self.start_game()
Exemplo n.º 7
0
 def __init__(self,
              controller,
              game_mode='build',
              game_speed=1,
              game_level=1):
     super().__init__(
         controller=controller,
         game_mode=game_mode,
     )
     self.start_time = time.time()
     self.turret = Turret((5, 18))
     self.player = self.turret
     self.bullets = []
     self.wall = Wall(direction='up',
                      out_of_screen=True,
                      line_add_speed=game_speed,
                      start_line_count=2 + game_level)
     self.game_status = True
     self.game_objects = [self.turret, self.wall]
     self.score = 0
     self.start_game()
Exemplo n.º 8
0
 def __init__(self, controller, game_mode, game_speed=1, game_level=2):
     super().__init__(controller, game_mode=game_mode)
     # self.brick = Brick(pos=(12, 0))
     self.game_speed = game_speed
     print(self.game_speed, 'testris speed')
     self.game_objects = None
     self.wall = Wall(
         start_line_count=game_level - 1,
         auto_line_add=False,
         direction='down',
         out_of_screen=True,
     )
     self.brick = None
     self.next_brick = None
     self.next_rotation = None
     # self.add_new_brick()
     # self.game_objects = [self.brick, self.wall]
     self.player = self.brick
     self.lives = None
     # self.add_new_brick()
     self.start_game()
Exemplo n.º 9
0
class DrawObjects(Game):
    def __init__(self, controller, game_mode=None, game_speed=1, game_level=1):
        super().__init__(controller=controller, game_mode=game_mode)
        self.cursor = Cursor(pos=(0, 0))
        self.wall = Wall(start_line_count=0, auto_line_add=False)
        self.game_objects = [self.cursor, self.wall]
        self.lives = 0
        self.start_game()

    # def start_game(self):
    #     super().start_game()

    def run(self):
        if self.game_status:
            self.render(*self.game_objects)
            self.blink_effect(self.cursor.obj)
        else:
            self.end_game()

    def draw(self):
        pos = self.cursor.get_pos()
        if pos not in self.wall.obj:
            self.wall.add_brick(pos)
        else:
            self.wall.drop_brick(pos)

    def game_key_controller(self, key):
        super().game_key_controller(key=key)
        if key == pygame.K_LEFT:
            self.cursor.move(direction='left')
        elif key == pygame.K_RIGHT:
            self.cursor.move(direction='right')
        elif key == pygame.K_UP:
            self.cursor.move(direction='up')
        elif key == pygame.K_DOWN:
            self.cursor.move(direction='down')
        elif key == pygame.K_d:
            self.draw()
        elif key == pygame.K_s:
            print(tuple(self.wall))
        elif key == pygame.K_c:
            self.wall.clean_wall()
Exemplo n.º 10
0
class TurretTetris(Game):
    """modes :
    build, destroy
    """
    max_bullet_count = 3
    SCORE = 1

    def __init__(self,
                 controller,
                 game_mode='build',
                 game_speed=1,
                 game_level=1):
        super().__init__(
            controller=controller,
            game_mode=game_mode,
        )
        self.start_time = time.time()
        self.turret = Turret((5, 18))
        self.player = self.turret
        self.bullets = []
        self.wall = Wall(direction='up',
                         out_of_screen=True,
                         line_add_speed=game_speed,
                         start_line_count=2 + game_level)
        self.game_status = True
        self.game_objects = [self.turret, self.wall]
        self.score = 0
        self.start_game()

    def start_game(self):
        super().start_game()
        """Иницилизация стартового состояния игры"""

    def restart_game(self):
        self.lives -= 1
        self.start_time = time.time()
        self.turret = Turret((5, 18))
        self.bullets = []
        self.wall = Wall()
        self.game_status = True
        self.game_objects = [self.turret, self.wall]
        self.start_game()

    def run(self):
        if self.game_status:
            self.check_bullet_in_screen()
            for bullet in self.bullets:
                bullet.move()
            self.collision()
            self.wall.act()
            if self.game_mode == 'build':
                self.score = self.wall.del_lines_counter
            self.render(*self.game_objects, *self.bullets)
        else:
            self.end_game()

    def create_bullet(self):
        if len(self.bullets) != TurretTetris.max_bullet_count:
            y, x = self.turret.get_position()
            bullet = Bullet((y - 1, x))
            self.bullets.append(bullet)

    def check_bullet_in_screen(self):
        """Удаление пуль вышедших за экран"""
        if self.bullets:
            for bullet_id, bullet in enumerate(self.bullets):
                y, x = bullet.get_pos()
                if y == -1:
                    self.bullets.pop(bullet_id)

    def collision(self):
        if self.wall._get_down() == self.turret.get_position()[0]:
            self.game_status = False
            self.bomb.activate(player=self.player)
            self.game_objects.append(self.bomb)
        """Bullet - Wall collision"""
        for bullet_id, bullet in enumerate(self.bullets):
            if self.array_collision(self.wall, bullet):
                # print(id(bullet), bullet.get_obj(), bullet.number)
                y, x = bullet.get_pos()
                if self.game_mode == 'build':
                    self.wall.add_brick((y + 1, x))
                    self.score = self.wall.del_lines_counter
                else:
                    self.wall.drop_brick((y, x))
                    self.score += 1
                self.bullets.pop(bullet_id)
                # break

        # if self.bullets:
        #     for bullet_id, bullet in enumerate(self.bullets):
        #         y, x = bullet.get_pos()
        #         if (y, x) in self.wall.get_obj():
        #             if self.game_mode == 'build':
        #                 self.wall.add_brick((y + 1, x))
        #             else:
        #                 self.wall.drop_brick((y, x))
        #             self.bullets.pop(bullet_id)
        #             self.score += 1
        #         if y == - 1:
        #             if self.game_mode == 'build':
        #                 self.wall.add_brick((y + 1, x))

    def game_key_controller(self, key):
        super().game_key_controller(key=key)
        if key == pygame.K_UP:
            self.create_bullet()
        elif key == pygame.K_ESCAPE:
            self.controller.chose_game('default')
        else:
            self.turret.move(key)
Exemplo n.º 11
0
class Tetris(Game):
    FRAME = 1
    SCORE = 10

    def __init__(self, controller, game_mode, game_speed=1, game_level=2):
        super().__init__(controller, game_mode=game_mode)
        # self.brick = Brick(pos=(12, 0))
        self.game_speed = game_speed
        print(self.game_speed, 'testris speed')
        self.game_objects = None
        self.wall = Wall(
            start_line_count=game_level - 1,
            auto_line_add=False,
            direction='down',
            out_of_screen=True,
        )
        self.brick = None
        self.next_brick = None
        self.next_rotation = None
        # self.add_new_brick()
        # self.game_objects = [self.brick, self.wall]
        self.player = self.brick
        self.lives = None
        # self.add_new_brick()
        self.start_game()

    def start_game(self):
        super().start_game()
        self.add_new_brick()

    def restart_game(self):
        self.game_status = True
        self.lives -= 1
        self.brick = Brick(pos=(12, 5), game_speed=self.game_speed)
        self.wall = Wall(start_line_count=0,
                         auto_line_add=False,
                         direction='down',
                         out_of_screen=True)
        self.game_objects = [self.brick, self.wall]
        self.player = self.brick
        self.start_game()

    def run(self):
        if self.game_status:
            if not self.pause:
                self.player.auto_move()
                self.collisions()
                self.wall.test_wall_check_lines()
                self.score = self.wall.del_lines_counter * self.SCORE
                self.render(*self.game_objects)
        else:
            self.end_game()

    def collisions(self):
        if self.array_collision(self.brick, self.wall):
            self.player.move_up()
            self.wall.add_array(self.brick.get_obj())
            self.add_new_brick()

    def add_new_brick(self):
        # y = r.randint(0, 3)
        # x = r.randint(1,8)
        # shape = self.next_brick
        if not self.next_brick:
            self.next_brick = r.choice(Brick.shapes)
            self.next_rotation = r.choice(list(self.next_brick.keys()))
        self.brick = Brick(pos=(0, 5),
                           shape=self.next_brick,
                           rotation=self.next_rotation,
                           game_speed=self.game_speed)
        self.player = self.brick
        self.game_objects = [self.brick, self.wall]
        # if self.array_collision(self.player, self.wall):
        #     self.game_status = False
        #     self.bomb.activate(player=self.player)
        #     self.game_objects.append(self.bomb)
        self.next_brick = r.choice(Brick.shapes)
        self.next_rotation = r.choice(list(self.next_brick.keys()))
        self.lives = {'shape': self.next_brick, 'rotation': self.next_rotation}
        self.get_small_screen_condition()
        if self.wall.get_top() <= 1:
            self.game_status = False
            self.bomb.activate(player=self.player)
            self.game_objects.append(self.bomb)
            self.lives = 0

    def game_key_controller(self, key):
        super().game_key_controller(key=key)

        if key == pygame.K_LEFT:
            self.player.move_left()
            if self.array_collision(self.brick, self.wall):
                self.player.move_right()
            if self.player.out_screen_pos_x_in_obj():
                self.player.move_right()
        elif key == pygame.K_RIGHT:
            self.player.move_right()
            if self.array_collision(self.brick, self.wall):
                self.player.move_left()
            if self.player.out_screen_pos_x_in_obj():
                self.player.move_left()
        # elif key == pygame.K_UP:
        #     self.player.move_up()
        elif key == pygame.K_DOWN:
            self.player.move_down()

        elif key == pygame.K_UP:
            # print('Rotate key')
            self.brick.rotate()
            if self.array_collision(self.brick, self.wall):
                self.brick.rotate_back()
            is_out = self.player.out_screen_pos_x_in_obj()
            if is_out:
                if is_out > 0:
                    self.player.move_left()
                else:
                    self.player.move_right()
                if self.array_collision(self.brick, self.wall):

                    self.brick.move_back()
                    self.brick.rotate_back()
Exemplo n.º 12
0
class Snake(Game):
    """game_modes: traffic, step"""

    level_2_scheme = ((2, 3), (2, 2), (3, 2), (2, 6), (2, 7), (3, 7), (16, 2),
                      (17, 2), (17, 3), (17, 6), (17, 7), (16, 7), (9, 4),
                      (9, 5), (10, 5), (10, 4))
    level_3_scheme = ((2, 4), (2, 5), (3, 5), (3, 4), (9, 0), (10, 0), (10, 1),
                      (9, 1), (9, 8), (9, 9), (10, 9), (10, 8), (17, 5),
                      (16, 5), (16, 4), (17, 4))

    level_4_scheme = ((2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6),
                      (17, 3), (17, 4), (17, 5), (17, 6), (17, 7), (17, 8),
                      (17, 9), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8),
                      (7, 9), (12, 0), (12, 1), (12, 2), (12, 3), (12, 4),
                      (12, 5), (12, 6))

    LEVELS = {
        '1': None,
        '2': level_2_scheme,
        '3': level_3_scheme,
        '4': level_4_scheme,
        '5': None,
        '6': None,
        '7': None,
        '8': None,
        '9': None,
        '10': None,
    }

    FRAME = 30
    SCORE = 1

    def __init__(self,
                 controller,
                 game_mode='traffic',
                 game_level=1,
                 game_speed=1):
        super().__init__(controller=controller, game_mode=game_mode)
        self.snake = SnakeObj()
        self.player = self.snake
        self.__class__.FRAME = self.fps / game_speed
        print(self.__class__.FRAME)
        self.snake_food = SnakeFood(self.snake)
        self.wall = Wall(start_line_count=0,
                         auto_line_add=False,
                         direction='down',
                         out_of_screen=True,
                         wall_scheme=self.LEVELS[str(game_level)])
        self.game_objects = [self.snake, self.snake_food, self.wall]
        print(self.snake.obj)
        print(self.wall.get_obj())
        self.start_game()

    def start_game(self):
        super().start_game()
        """Иницилизация стартового состояния игры"""
        if self.game_mode == 'traffic':
            self.snake.direction = 'UP'
            self.snake.last_direction = 'UP'

    def restart_game(self):
        self.lives -= 1
        self.snake = SnakeObj()
        self.snake_food = SnakeFood(self.snake)
        self.player = self.snake
        self.game_status = True
        self.game_objects = [self.snake, self.snake_food, self.wall]

        self.start_game()

    def run(self):
        """Изменение состояние игры"""
        if self.game_status:
            if not self.pause:
                self.collision()
                if self.game_mode == 'traffic':
                    self.frame -= 1
                    if self.frame <= 0:
                        self.snake.move()
                        self.frame = self.__class__.FRAME
                if self.game_mode == 'step':
                    self.snake.move()
                    self.snake.direction = None
            self.render(*self.game_objects)
            self.blink_effect(self.snake_food.obj, [self.snake.get_pos()])
        else:
            self.end_game()

    def collision(self):
        # snake move in self
        # print(self.snake.get_pos(),self.snake.get_obj()[1:])
        if self.snake.get_pos() in self.snake.get_obj()[1:]:
            self.game_status = False
            self.bomb.activate(player=self.player)
            self.game_objects.append(self.bomb)
        # eat food
        if self.snake.get_pos() == self.snake_food.get_pos():
            self.snake_food.new_food(self.snake)
            self.snake.eat()
            self.score += 1
        # wall
        if self.array_collision(self.snake, self.wall):
            print('WALL COLISION')
            self.game_status = False
            self.bomb.activate(player=self.player)
            self.game_objects.append(self.bomb)

    def game_key_controller(self, key):
        """Меняет флаг направление движения -
        изменение на противоположное не проходит"""
        super().game_key_controller(key=key)
        if key == pygame.K_LEFT:
            if self.snake.last_direction != 'RIGHT':
                self.snake.direction = 'LEFT'
        elif key == pygame.K_RIGHT:
            if self.snake.last_direction != 'LEFT':
                self.snake.direction = 'RIGHT'
        elif key == pygame.K_UP:
            if self.snake.last_direction != 'DOWN':
                self.snake.direction = 'UP'
        elif key == pygame.K_DOWN:
            if self.snake.last_direction != 'UP':
                self.snake.direction = 'DOWN'