Esempio n. 1
0
    def __create_items(self):
        self.ground = Drawable('ground.png', WORLD)
        self.ground.position.bottom = HEIGHT

        self.block1 = Drawable('block1.png', WORLD)
        self.block1.position.x = WIDTH + 10
        self.block1.position.bottom = RULER
Esempio n. 2
0
 def __init__(self, wrapper, background_image_path=None):
     self.path_to_images = Tab.path_to_tabs + (
         self.__class__.__name__.lower()
         if self.__class__ != Tab else '') + '/'
     if background_image_path is None:
         background_image_path = self.path_to_images + 'background.gif'
     self.wrapper = wrapper
     self.background = Drawable(background_image_path)
     self.objects = []
Esempio n. 3
0
    def __init__(self):
        super().__init__()
        # Using a deque as a linked list improves performance
        # Due to needing multiple remove() calls
        self.balls: deque[b2Body] = deque()
        self.pocketed_balls: List[Ball] = []
        self.drawables: List[Drawable] = []
        self.pockets = PoolWorld.create_pockets()

        self.to_remove: Set[b2Body] = set()

        self.board: PoolBoard = None
        self.cue_ball: b2Body = None

        self.world = b2World(gravity=(0, 0), doSleep=True)
        self.world.autoClearForces = True
        self.world.contactListener = self

        # Create the pocket fixtures which are sensors
        # The radius is such that a collision only occurs when the center of the ball
        # overlaps with the edge of the pocket
        pocket_fd = b2FixtureDef(shape=b2CircleShape(
            radius=Constants.POCKET_RADIUS - Constants.BALL_RADIUS))
        pocket_fd.isSensor = True
        for pocket in self.pockets:
            body: b2Body = self.world.CreateStaticBody(
                position=pocket.to_tuple(), fixtures=pocket_fd)
            body.userData = PoolData(PoolType.POCKET)
            self.drawables.append(
                Drawable(body,
                         Drawable.BLUE,
                         Drawable.draw_circle,
                         outline_color=Drawable.RED))

        # Create the edges of the pool table
        top_left = self.pockets[0]
        top_middle = self.pockets[1]
        top_right = self.pockets[2]
        bottom_left = self.pockets[3]
        bottom_middle = self.pockets[4]
        bottom_right = self.pockets[5]

        thickness = Constants.POCKET_RADIUS
        self.create_boundary_wall(top_left, top_middle, True)
        self.create_boundary_wall(Point(top_middle.x, top_middle.y + 0.1),
                                  top_right, True)
        self.create_boundary_wall(top_right, bottom_right, False)
        self.create_boundary_wall(
            Point(top_left.x - thickness, top_left.y),
            Point(bottom_left.x - thickness, bottom_left.y), False)
        self.create_boundary_wall(
            Point(bottom_left.x, bottom_left.y + thickness),
            Point(bottom_middle.x, bottom_middle.y + thickness), True)
        self.create_boundary_wall(
            Point(bottom_middle.x, bottom_middle.y + thickness - 0.1),
            Point(bottom_right.x, bottom_right.y + thickness), True)
Esempio n. 4
0
    def __init__(self, e_type, x, y, playWidth):
        self.x = x
        self.y = y
        self.velocity = random.randrange(-3, 3)
        self.playWidth = playWidth
        self.e_type = e_type
        self.dying = False
        if (self.e_type == "rb"):
            self.sprites = RB_SPRITES
            self.sprite = self.sprites[0]
            self.speed = 5
            self.drawable = Drawable(self.sprites, x, y)
            self.health = 2

        if (self.e_type == "wb"):
            self.sprites = WB_DEATH
            self.sprite = self.sprites[0]
            self.speed = 6
            self.drawable = Drawable(self.sprites, x, y)

            self.health = 1

        if (self.e_type == "cb"):
            self.sprites = CB_SPRITES
            self.sprite = self.sprites[0]
            self.speed = 4
            self.drawable = Drawable(self.sprites, x, y)
            self.health = 1

        if (self.e_type == "vb"):
            self.sprites = VB_SPRITES
            self.sprite = self.sprites[0]
            self.speed = 4 + random.uniform(-1.5, 2)
            self.drawable = Drawable(self.sprites, x, y)
            self.drawable.initialize_animation(5, 0)
            self.health = 99
Esempio n. 5
0
    def _convert_notification(self, notif: Notification) -> Drawable:
        text = notif.title
        if notif.message:
            text += f". {notif.message}"
        color = 2

        font = ImageFont.truetype(self.__font_path, self.__font_size)
        w = font.getsize(text)[0]
        image = Image.new('L', (w, self.__matrix.height), 1)
        draw = ImageDraw.Draw(image)
        draw.text((0, 0), text, font=font)
        arr = np.asarray(image)
        arr = np.where(arr, 0, color)

        return Drawable(pixels=arr)
Esempio n. 6
0
 def create_boundary_wall(self, pocket1: Point, pocket2: Point,
                          horizontal: bool):
     vertices = []
     diff = Constants.POCKET_RADIUS + 0.05
     thickness = Constants.POCKET_RADIUS
     if horizontal:
         vertices.append((pocket1.x + diff, pocket1.y))
         vertices.append((pocket2.x - diff, pocket1.y))
         vertices.append((pocket2.x - diff, pocket1.y - thickness))
         vertices.append((pocket1.x + diff, pocket1.y - thickness))
     else:
         vertices.append((pocket1.x, pocket1.y + diff))
         vertices.append((pocket1.x, pocket2.y - diff))
         vertices.append((pocket1.x + thickness, pocket2.y - diff))
         vertices.append((pocket1.x + thickness, pocket1.y + diff))
     vertices.append(vertices[0])
     fixture = b2FixtureDef(shape=b2ChainShape(vertices_chain=vertices))
     body: b2Body = self.world.CreateStaticBody(fixtures=fixture)
     body.userData = PoolData(PoolType.WALL)
     self.drawables.append(
         Drawable(body,
                  Drawable.BROWN,
                  Drawable.draw_rect,
                  outline_color=(25, 14, 16)))
Esempio n. 7
0
 def __init__(self, displayWidth, displayHeight):
     self.drawable = Drawable(PLAYER_SPRITE, (int)(0.5 * displayWidth), (int)(0.8 * displayHeight))
     self.displayWidth = displayWidth
     self.displayHeight = displayHeight
     self.health = MAX_HEALTH
     self.damage_counter = 0
Esempio n. 8
0
def main():
    global points
    global level
    global game_over
    global you_win
    global enemies
    global drawables
    fpsClock = pygame.time.Clock()
    shoot_ticker = 0
    pause_counter = 0
    tick_counter = 0
    current_level_line = 0
    current_level = LEVELS[level - 1]

    Sound.play_loop("level")

    create_GUI()

    for i in range(40):
        bg_tile_sprite = random.choice(BG_SPRITES)
        bg_tile = Drawable(bg_tile_sprite, random.randrange(PLAYWIDTH),
                           random.randrange(DISPLAYHEIGHT))
        bg_tiles.append(bg_tile)

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_SPACE:
                    shoot_ticker = 0

        ##Fill the frame with background color
        DISPLAYSURF.fill(BG_COLOR)

        pygame.draw.rect(DISPLAYSURF, BLACK,
                         ((PLAYWIDTH, 0), (GUIWIDTH, DISPLAYHEIGHT)))

        #draw background tiles
        for bg_tile in bg_tiles:
            bg_tile.rect.y += 15
            if (bg_tile.rect.y > DISPLAYHEIGHT):
                bg_tile.rect.y = -bg_tile.sprite.get_height()
                bg_tile.rect.x = random.randrange(PLAYWIDTH)
            DISPLAYSURF.blit(bg_tile.sprite, bg_tile.rect)

        #detect player movement
        if (not game_over and not you_win):
            keys = pygame.key.get_pressed()
            player.move_player(keys)

            #bullet generation logic
            if (keys[K_SPACE] and shoot_ticker == 0):
                Sound.play_sound("shoot")
                shoot_ticker = SHOOT_TICKER_MAX
                bullet_x = (int)(((player.drawable.rect.x * 2) +
                                  player.drawable.sprite.get_width() -
                                  BULLET_SPRITE[0].get_width()) / 2)
                bullet_y = player.drawable.rect.y - (int)(
                    BULLET_SPRITE[0].get_height() / 2)
                bullet = Drawable(BULLET_SPRITE, bullet_x, bullet_y)
                bullets.append(bullet)

            for bullet in bullets:
                bullet.rect.y -= BULLET_SPEED
                if (bullet.rect.y < 0 - bullet.sprite.get_height()):
                    bullets.remove(bullet)
                else:
                    for enemy in enemies:
                        if (bullet.rect.colliderect(enemy.drawable.rect)):
                            if (bullet in bullets):
                                bullets.remove(bullet)
                            if (enemy.take_damage(1)):
                                if (enemy.e_type == "cb"
                                        or enemy.e_type == "vb"):
                                    Sound.play_sound("smallexplode")
                                    points += 100
                                else:
                                    Sound.play_sound("allydamage")
                                    points -= 100
                                    if (points < 0): points = 0
                DISPLAYSURF.blit(bullet.sprite, bullet.rect)

            #level generation logic
            #level increment logic
            if (current_level_line >= len(current_level) and not drawables):
                tick_counter = -30
                current_level_line = 0
                level += 1
                if (level <= len(LEVELS)):
                    current_level = LEVELS[level - 1]
                    player.heal_player()
                else:
                    current_level = ['']
                    Sound.stop_sound("level")
                    Sound.play_sound("win")
                    you_win = True
            if (tick_counter == TICK_COUNTER_MAX
                    and current_level_line < len(current_level)):
                tick_counter = 0
                if (pause_counter == 0):
                    #parse line
                    line = list(current_level[current_level_line])
                    if (line[0] == 'P'):
                        pause_counter = int(line[1])
                    else:
                        for i in range(len(line)):
                            enemy_type = ""
                            if (line[i] == 'R'):
                                enemy_type = "rb"
                            if (line[i] == 'W'):
                                enemy_type = "wb"
                            if (line[i] == 'C'):
                                enemy_type = "cb"
                            if (line[i] == 'V'):
                                enemy_type = "vb"
                            if (enemy_type != ""):
                                enemy = Enemy(enemy_type,
                                              (int)(PLAYWIDTH /
                                                    (len(line) + 1)) * (i + 1),
                                              -64, PLAYWIDTH)
                                enemy.drawable.rect.x -= (int)(
                                    enemy.drawable.sprite.get_width() / 2)
                                drawables.append(enemy.drawable)
                                enemies.append(enemy)
                    current_level_line += 1
                else:
                    pause_counter -= 1
            else:
                tick_counter += 1

            #enemy collision
            for enemy in enemies:
                enemy.update()
                if (player.drawable.rect.colliderect(enemy.drawable.rect)
                        and not enemy.dying):
                    if (enemy.e_type == "cb"):
                        if (player.damage_counter <= 0):
                            enemy.take_damage(enemy.health)
                            points += 50
                        if (player.take_damage(1)):
                            game_over = True
                    elif (enemy.e_type == "vb"):
                        enemy.take_damage(enemy.health)
                        if (player.take_damage(1)):
                            game_over = True
                    elif (player.damage_counter <= 0):
                        points -= 100
                        if (points < 0): points = 0
                        player.take_damage(0)
                        enemy.take_damage(enemy.health)
                if (enemy.health < 0):
                    drawables.remove(enemy.drawable)
                    enemies.remove(enemy)
                if (enemy.drawable.rect.y > DISPLAYHEIGHT
                        and enemy in enemies):
                    enemies.remove(enemy)
        else:
            keys = pygame.key.get_pressed()
            if (keys[K_SPACE] or keys[K_RETURN] or keys[K_ESCAPE]):
                player.reset_position()
                game_over = False
                you_win = False
                level = 1
                score = 0
                drawables = []
                enemies = []
                main()

        for drawable in drawables:
            if (drawable.rect.y > DISPLAYHEIGHT):
                drawables.remove(drawable)
            else:
                DISPLAYSURF.blit(drawable.sprite, drawable.rect)

        if (player.damage_counter % 2 == 0):
            DISPLAYSURF.blit(player.drawable.sprite, player.drawable.rect)

        if shoot_ticker > 0:
            shoot_ticker -= 1

        update_GUI()
        pygame.display.update()
        fpsClock.tick(FPS)