How to use the pygame.Rect function in pygame

To help you get started, we’ve selected a few pygame examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MyreMylar / pygame_gui / tests / test_core / test_ui_element.py View on Github external
def test_set_dimensions(self, _init_pygame, default_ui_manager):
        test_container = UIContainer(relative_rect=pygame.Rect(10, 10, 300, 300),
                                     manager=default_ui_manager)

        element_1 = UIElement(relative_rect=pygame.Rect(30, 30, 50, 50),
                              manager=default_ui_manager,
                              container=test_container,
                              starting_height=0,
                              layer_thickness=1,
                              anchors={'left': 'left',
                                       'right': 'left',
                                       'top': 'top',
                                       'bottom': 'top'})
        assert element_1.relative_right_margin is None
        assert element_1.relative_bottom_margin is None

        element_1.set_dimensions((20, 20))
        assert element_1.relative_rect.topleft == (30, 30)
        assert element_1.relative_rect.size == (20, 20)
        assert element_1.relative_rect.bottomright == (50, 50)
        assert element_1.rect.topleft == (40, 40)
github MyreMylar / pygame_gui / tests / test_core / test_ui_container.py View on Github external
def test_recalculate_container_layer_thickness(self, _init_pygame,
                                                   default_ui_manager: IUIManagerInterface,
                                                   _display_surface_return_none):
        container = UIContainer(pygame.Rect(100, 100, 200, 200), manager=default_ui_manager)
        UIButton(relative_rect=pygame.Rect(0, 0, 50, 50), text="",
                 manager=default_ui_manager,
                 container=container)

        container.recalculate_container_layer_thickness()

        assert container.layer_thickness == 2
github MyreMylar / pygame_gui / tests / test_elements / test_ui_label.py View on Github external
def test_set_position(self, _init_pygame, default_ui_manager,
                          _display_surface_return_none):
        test_container = UIContainer(relative_rect=pygame.Rect(100, 100, 300, 60),
                                     manager=default_ui_manager)
        label = UILabel(relative_rect=pygame.Rect(100, 100, 150, 30),
                        text="Test Label",
                        container=test_container,
                        manager=default_ui_manager)

        label.set_position(pygame.math.Vector2(150.0, 30.0))

        assert label.relative_rect.topleft == (50, -70)
github ActiveState / neuroblast / actors.py View on Github external
self.cooldown = 0.1
        self.canfire = True
        self.bulcount = 0
        self.brain = brain
        ## Generate the sprite image from spritesheet
        ssrect = pygame.Rect((96,192,96,192))
        global spritesheet
        self.image.blit(spritesheet,(0,0),ssrect)
        self.image.convert()
        self.image.set_colorkey(self.image.get_at((0, 0)))
        self.rect.center = (self.x, self.y)
        self.spawntime = pygame.time.get_ticks()
        self.deadcb = self.amdead
        self.idleAnim = SpriteSequence("idle",spritesheet,pygame.Rect(96,192,96,192),7,1,0,0.1,True,None)
        self.hitAnim = SpriteSequence("hit",spritesheet,pygame.Rect(96,480,96,96),8,1,0,0.1,False,None)
        self.blowAnim = SpriteSequence("blow",spritesheet,pygame.Rect(96,384,96,96),8,1,0,0.1,False,self.onAnimComplete)
        self.idleAnim.play()
github dgomes / iia-ia-bomberman / viewer.py View on Github external
def update_sprite(self, pos=None):
        if not pos:
            pos = self.x, self.y
        else:
            pos = scale(pos)
        self.rect = pygame.Rect(pos + CHAR_SIZE)
        self.image.fill((0, 0, 230))
        self.image.blit(*self.sprite)
        # self.image = pygame.transform.scale(self.image, scale((1, 1)))
        self.x, self.y = pos
github oscarcx123 / MagicTower-Python / lib / winbase.py View on Github external
def init_wind(self, w, h, dir):
        x, y = 0, 0  # self.pos[0], self.pos[1]
        scale_list = [
            (Rect(0, 0, 128, 128), (w - 4, h - 4), Rect(x + 2, y + 2, w - 4, h - 4)),  # back
            (Rect(144, 0, 32, 16), (w - 32, 16), Rect(x + 16, y, w - 32, 16)),  # top
            (Rect(128, 16, 16, 32), (16, h - 32), Rect(x, y + 16, 16, h - 32)),  # left
            (Rect(176, 16, 16, 32), (16, h - 32), Rect(x + w - 16, y + 16, 16, h - 32)),  # right
            (Rect(144, 48, 32, 16), (w - 32, 16), Rect(x + 16, y + h - 16, w - 32, 16)),  # bottom
            (Rect(128, 0, 16, 16), Rect(x, y, 16, 16)),  # top left
            (Rect(176, 0, 16, 16), Rect(x + w - 16, y, 16, 16)),  # top right
            (Rect(128, 48, 16, 16), Rect(x, y + h - 16, 16, 16)),  # bottom left
            (Rect(176, 48, 16, 16), Rect(x + w - 16, y + h - 16, 16, 16)),  # bottom right
        ]
        src_dir = {"up": (Rect(128, 96, 32, 32), Rect(x + int(w / 2), y + h - 3, 32, 32)),
                   "down": (Rect(160, 96, 32, 32), Rect(x + int(w / 2), y - 29, 32, 32))
                   }
        if dir is not None:
            scale_list.append(src_dir[dir])

        blit_list = [self.trans_image(fmt) for fmt in scale_list]
        ret = Surface([w, h])
        for l1, l2 in blit_list:
            ret.blit(l1, l2)
        # ret.blits(blit_list)
        return ret
github strin / curriculum-deep-RL / pyrl / tasks / pyale / games / london / object.py View on Github external
def __init__(self, data, startAtLeft, yPos):
		pygame.sprite.Sprite.__init__(self)
		self.add(data.bombers)
		self.add(data.bulletproofEntities)
		self.add(data.superbombableEntities)

		self.imageL = data.loadImage('assets/enemies/bomber.png')
		self.imageR = pygame.transform.flip(self.imageL, 1, 0)

		self.droppedBomb = False
		self.timeTillBombPrimed = random.uniform(Bomber.minTimeToDropBomb, Bomber.maxTimeToDropBomb)
		self.lastBombDropTime = time.time()

		self.rect = self.imageR.get_rect()
		self.targetingRect = pygame.Rect((0, 0), (6, 10)) # BOMB SIZE

		self.smoke = SmokeSpawner(data, self.rect.topleft, 10)

		if startAtLeft:
			self.direction = 'right'
			self.image = self.imageR
			self.rect.topright = (0, yPos)
		else:
			self.direction = 'left'
			self.image = self.imageL
			self.rect.topleft = (data.WINDOWWIDTH, yPos)
		self.coords = list(self.rect.topleft) # for more accurate positioning using floats

		sound.play('swoosh', 0.3)
github yenchenlin / DeepLearningFlappyBird / game / wrapped_flappy_bird.py View on Github external
pi = player['index']
    player['w'] = IMAGES['player'][0].get_width()
    player['h'] = IMAGES['player'][0].get_height()

    # if player crashes into ground
    if player['y'] + player['h'] >= BASEY - 1:
        return True
    else:

        playerRect = pygame.Rect(player['x'], player['y'],
                      player['w'], player['h'])

        for uPipe, lPipe in zip(upperPipes, lowerPipes):
            # upper and lower pipe rects
            uPipeRect = pygame.Rect(uPipe['x'], uPipe['y'], PIPE_WIDTH, PIPE_HEIGHT)
            lPipeRect = pygame.Rect(lPipe['x'], lPipe['y'], PIPE_WIDTH, PIPE_HEIGHT)

            # player and upper/lower pipe hitmasks
            pHitMask = HITMASKS['player'][pi]
            uHitmask = HITMASKS['pipe'][0]
            lHitmask = HITMASKS['pipe'][1]

            # if bird collided with upipe or lpipe
            uCollide = pixelCollision(playerRect, uPipeRect, pHitMask, uHitmask)
            lCollide = pixelCollision(playerRect, lPipeRect, pHitMask, lHitmask)

            if uCollide or lCollide:
                return True

    return False
github MyreMylar / pygame_gui / pygame_gui / elements / ui_drop_down_menu.py View on Github external
disabled_background_colour = self.ui_theme.get_colour_or_gradient('disabled_dark_bg',
                                                                          self.combined_element_ids)
        if disabled_background_colour != self.disabled_background_colour:
            self.disabled_background_colour = disabled_background_colour
            has_any_changed = True

        disabled_border_colour = self.ui_theme.get_colour_or_gradient('disabled_border',
                                                                      self.combined_element_ids)
        if disabled_border_colour != self.disabled_border_colour:
            self.disabled_border_colour = disabled_border_colour
            has_any_changed = True

        if has_any_changed:
            border_and_shadow = self.border_width + self.shadow_width
            self.background_rect = pygame.Rect(self.relative_rect.x + border_and_shadow,
                                               self.relative_rect.y + border_and_shadow,
                                               self.relative_rect.width - (2 * border_and_shadow),
                                               self.relative_rect.height - (2 * border_and_shadow))

            for state in self.menu_states:
                self.menu_states[state].expand_direction = self.expand_direction

            self.rebuild()
github tingbot / tingbot-python / tingbot / graphics.py View on Github external
"""
        Draws a rectangle.

        Args:
            xy (tuple): The position (x, y) to draw the rectangle, as measured from the top-left.
            size (tuple): The size (width, height) of the rectangle.
            color (tuple or str): The color (r, g, b) or color name.
            align (str): How to align the box relative to `xy`, or relative to the drawing surface
                if `xy` is None. Defaults to 'center'.
        """
        if len(size) != 2:
            raise ValueError('size should be a 2-tuple')

        xy = _topleft_from_aligned_xy(xy, align, size, self.size)

        self._fill(_color(color), pygame.Rect(xy, size))