def test_collide(self): class Test: def __init__(self, rect, hitmask): self.rect = rect self.hitmask = hitmask rect2 = Rectangle(2, 2, (1, 2)) rect1 = Rectangle(2, 2, (1, 1)) sprite_a = Test(rect1, [[True, True], [True, True]]) sprite_b = Test(rect2, [[True, True], [True, True]]) rect2 = Rectangle(2, 2, (1, 15)) self.assertEqual(collide(sprite_b, sprite_a), True) sprite_b = Test(rect2, [[True, True], [True, True]]) self.assertEqual(collide(sprite_b, sprite_a), False)
def recompute_angle(self): self.time += 1 # modulates gravity scaling = 2000 / (self.swing_length**2) first_d_d_theta = -sin(radians(self.theta)) * scaling mid_d_theta = self.d_theta + first_d_d_theta mid_theta = self.theta + (self.d_theta + mid_d_theta) / 2.0 mid_d_d_theta = -sin(radians(mid_theta)) * scaling mid_d_theta = self.d_theta + (first_d_d_theta + mid_d_d_theta) / 2 mid_theta = self.theta + (self.d_theta + mid_d_theta) / 2 mid_d_d_theta = -sin(radians(mid_theta)) * scaling last_d_theta = mid_d_theta + mid_d_d_theta last_theta = mid_theta + (mid_d_theta + last_d_theta) / 2.0 last_d_d_theta = -sin(radians(last_theta)) * scaling last_d_theta = mid_d_theta + (mid_d_d_theta + last_d_d_theta) / 2.0 last_theta = mid_theta + (mid_d_theta + last_d_theta) / 2.0 self.d_theta = last_d_theta self.theta = last_theta self.rect = Rectangle( 1, 1, (int(self.pivot[0] - self.swing_length * sin(radians(self.theta))), int(self.pivot[1] + self.swing_length * cos(radians(self.theta)))))
def __init__(self, colour, width, height, x, y, tag=TAG_GROUND): self.width, self.height = width, height self.x, self.y = x, y self.tag = tag self.colour = colour self.rect = Rectangle(width, height, Vector(x + width / 2, y + height / 2))
def __init__(self, width, height, window_width, window_height): self.state = Rectangle(width, height, Vector(0 + width // 2, 0 + height // 2)) self.half_width = window_width // 2 self.half_height = window_height // 2 self.window_width = window_width self.window_height = window_height
def setup_menu_camera(self): # this is the menu bar self.menu = Rectangle(WIDTH // 4, HEIGHT, (WIDTH - WIDTH // 4 + (WIDTH // 4) // 2, HEIGHT // 2)) self.camera = Camera(LevelDesign.GAME_MEASURES[0], LevelDesign.GAME_MEASURES[1], WIDTH - WIDTH // 4, HEIGHT)
def __init__(self, angle, swing_length, pivot): self.theta = angle self.d_theta = 0 self.swing_length = swing_length self.pivot = pivot self.time = 0 self.rect = Rectangle( 1, 1, (int(self.pivot[0] - self.swing_length * cos(radians(self.theta))), int(self.pivot[1] + self.swing_length * sin(radians(self.theta)))))
def functionality(self, target): """defines how the camera will move, also makes sure it doesn't go outside level space """ left, top = target[0], target[1] left = self.half_width - left top = self.half_height - top left = min(0, left) left = max((self.window_width - self.state.width), left) top = max((self.window_height - self.state.height), top) top = min(0, top) return Rectangle( self.state.width, self.state.height, Vector(left + self.state.width // 2, top + self.state.height // 2))
def __init__(self, **options): """ Options: x, y, font, color, restricted, maxlength, prompt """ self.options = Config( options, ['x', '0'], ['y', '0'], ['font', 'pygame.font.Font(None, 32)'], ['color', '(0,0,0)'], [ 'restricted', '\'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM' 'NOPQRSTUVWXYZ0123456789!"#$%&\\\'()' '*+,-./:;<=>?@[\]^_`{|}~\'' ], ['maxlength', '-1'], ['prompt', '\'\'']) self.x = self.options.x self.y = self.options.y self.font = self.options.font self.color = self.options.color self.restricted = self.options.restricted self.maxlength = self.options.maxlength self.prompt = self.options.prompt self.value = '' self.shifted = False self.is_focused = False self.rect = Rectangle(150, 32, Vector(self.x + 75, self.y + 16))
def get_containing_rectangle(self): width = self.width + self.text_box.width height = max(self.height, self.text_box.height) return Rectangle(width, height, self.position + Vector(self.text_box.width / 2, 0))
def get_containing_rectangle(self): width = max(self.width, self.text_box.width) height = max(self.height, self.puck.radius * 2) + \ self.text_box.height return Rectangle(width, height, self.position + Vector(0, -self.text_box.height / 2))
from pygame.math import Vector2 as Vector from motions import Motion from ragdoll import HumanRagdoll from basicshapes import Rectangle pygame.init() screen = pygame.display.set_mode((1000, 1000)) clock = pygame.time.Clock() ragdoll = HumanRagdoll("Batman") ragdoll.move(Vector((500, 500))) motion = Motion(ragdoll) ground = Rectangle(1000, 70, Vector(500, 1000)) # for body_part in list(ragdoll.body_parts.values())[::-1]: # control.left_button_selectable.append(body_part) anchor = Vector(0, 0) # def cursor_controll(body, anchor): # body.pull_on_anchor(anchor, cursor_location - anchor) # anchor = cursor_location ragdoll_velosity = Vector(0, 0) cursor_left_button_is_down = False cursor_selected_body = None current_frame = 0 current_part = None
def test_get_hitmask(self): rect = Rectangle(2, 2, (1, 1)) image = pygame.Surface((2, 2)) self.assertEqual(get_hitmask(rect, image, 0), [[True, True], [True, True]])