class Laser: dmg = 0 speed = 10 direction = 0 pos = Vector2D(0, 0) hitbox = None texture = Texture("─" * 10, fore_color=colorama.Fore.RED, style=colorama.Style.BRIGHT) #constructor übergibt shaden position sowie richtung des lasers def __init__(self, pos, dmg, dir): self.dmg = dmg self.direction = dir self.pos = pos if (self.direction == 0): self.hitbox = Hitbox(self.pos, Vector2D(10, 2)) else: self.hitbox = Hitbox(self.pos, Vector2D(10, 0)) if (self.dmg >= 5): self.texture.texture = "=" * 10 else: self.texture.texture = "─" * 10 #updated shuss position def update(self): if (self.direction == 0): self.pos.x += self.speed self.hitbox.update_pos(Vector2D(self.pos.x - 10, self.pos.y - 1)) else: self.hitbox.update_pos(Vector2D(self.pos.x, self.pos.y)) self.pos.x -= self.speed
def __init__(self, world, x, y, pmode): self.x, self.y = x, y self.collidable = True self.dangerous = False self.entitytype = "Player" self.active = True self.texture = pyglet.sprite.Sprite(Player.ship, batch=world.batch, group=world.layers[1]) self.world = world self.hitboxes = [ Hitbox(self.x - 104.0, self.y + 48, 64.0, 128.0, self), Hitbox(self.x + 104.0, self.y + 48, 64.0, 128.0, self) ] self.texture.opacity = 0.0 for i in self.hitboxes: i.texture.opacity = 0.0 if pmode == "spaceship": self.sx, self.sy = 0.0, 0.0 self.firing = False self.weapon = 0 self.angle = math.radians(270.0) self.rate = 1.0 self.weapontimer = 0.0
class Block(Entity): def __init__(self, x, y): Entity.__init__(self) self.x = x self.y = y self.collides = True self.img = pygame.image.load("res/pics/box.png") self.hitbox = Hitbox(self.x, self.y, self.img.get_width(), self.img.get_height()) def update(self): self.hitbox.update(self.x, self.y) def draw(self): globals.window.blit(self.img, (self.x, self.y)) def getType(self): return "Block" def collide(self, entity): pass
def attack_event(self, direction, d_x, d_y): ''' Executes player attack ''' self.hero.load_attack_image(direction) hitbox = Hitbox(self, self.hero.x_pos + d_x, self.hero.y_pos + d_y) hitbox.collide_with_enemy() sword_slash_sound()
def test_size_level(): v1 = Vector(-1, -1) v2 = Vector(1, -1) v3 = Vector(1, 1) v4 = Vector(-1, 1) R = Rect(-1, -1, 2, 2) Hb = Hitbox(R) plat1 = SolidPlatform(Hb) Hb2 = Hb.copy() plat2 = SolidPlatform(Hb2) plat2.translate(Vector(3, 2)) gl = GameLevel([plat1, plat2], []) pass
def __init__(self,pos,type): self.pos = pos; if(type in [1,2]): self.texture = Texture(load_texture("obstacles",0 if type == 1 else 1),bg_color = colorama.Back.BLUE,fore_color = colorama.Fore.CYAN); self.hitbox = Hitbox(self.pos,Vector2D(3,11)); if(type in [3,4]): self.texture = Texture(load_texture("obstacles",2 if type == 3 else 3),bg_color = colorama.Back.BLUE,fore_color = colorama.Fore.CYAN); self.hitbox = Hitbox(self.pos,Vector2D(2,2)); if(type in [5,6]): self.texture = Texture(load_texture("obstacles",4 if type == 5 else 5),bg_color = colorama.Back.BLUE,fore_color = colorama.Fore.CYAN); self.hitbox = Hitbox(self.pos,Vector2D(6,2)); if(type in [7,8]): self.texture = Texture(load_texture("obstacles",6 if type == 7 else 7),bg_color = colorama.Back.BLUE,fore_color = colorama.Fore.CYAN,transparent = False); self.hitbox = Hitbox(self.pos,Vector2D(7,24));
def test_copy(): r = Rect(-1, -1, 2, 2) Hb = Hitbox(r) t = CollideTransformable() t.set_hit_box(Hb) t.set_collide(True) t2 = t.copy() t.set_collide(False) assert t2.get_collide() Hb2 = t2.get_hit_box() assert Hb2.get_ctrbl() == t2 t2.translate(Vector(2, 0)) assert Hb2.get_world_rect() == Rect(1, -1, 2, 2) assert Hb.get_world_rect() == Rect(-1, -1, 2, 2)
def __init__(self, pos, dmg, dir): self.dmg = dmg self.direction = dir self.pos = pos if (self.direction == 0): self.hitbox = Hitbox(self.pos, Vector2D(10, 2)) else: self.hitbox = Hitbox(self.pos, Vector2D(10, 0)) if (self.dmg >= 5): self.texture.texture = "=" * 10 else: self.texture.texture = "─" * 10
def test_hitbox4(): T1 = CollideTransformable() T2 = CollideTransformable() R1 = Rect(-1, -1, 2, 2) R2 = Rect(-1, -1, 2, 2) Hb1 = Hitbox(R1) Hb2 = Hitbox(R2) Hb1.link(T1) Hb2.link(T2) T1.translate(Vector(1, 1)) T2.translate(Vector(3, 1)) T2.set_speed(Vector(-1, 0)) v = Hb2.remove_collide(Hb1) T2.translate(v) assert not (Hb1.collide(Hb2))
def test_center(): r = Rect(0, 0, 2, 2) Hb = Hitbox(r) t = CollideTransformable() t.set_hit_box(Hb) assert t.get_hit_box().get_world_rect() == Rect(0, 0, 2, 2) assert t.get_position() == Vector(1, 1)
def __init__(self, pos, path): self.path = [] self.laser = [] self.path = get_path(path) self.pos = pos self.texture = Texture(load_texture("entity", 6), fore_color=colorama.Fore.CYAN) self.hitbox = Hitbox(pos, Vector2D(6, 5))
def __init__(self, x: int, y: int, radius: int, color: Tuple[int, int, int], tank_body_model: str, tank_turret_model: str, width: int, height: int) -> None: self.x = x self.y = y self.radius = radius self.img_width = width self.img_height = height self.color = color self.speed = 3 self.turn_speed = 0.05 self.tank_body_model = tank_body_model self.tank_turret_model = tank_turret_model self.body_rotation = 0 self.turret_rotation = 0 self.hitbox = Hitbox() self.update_hitbox()
def get_hitbox(self): top = self.points[0].y bottom = self.points[0].y left = self.points[0].x right = self.points[0].x for point in self.points[1:]: top = min(top, point.y) bottom = max(bottom, point.y) left = min(left, point.x) right = max(right, point.x) return Hitbox(Vec2d(left, top), right - left, bottom - top)
def test_full_1(): Hb = Hitbox(Rect(0, 0, 2, 2)) mvn = CollideTransformable() mvn.set_hit_box(Hb) mvn.set_rigid_body(True) print(mvn.get_position()) Hb2 = Hitbox(Rect(0, 3, 2, 2)) mvn2 = CollideTransformable() mvn2.set_hit_box(Hb2) mvn2.set_rigid_body(True) print(mvn.get_position()) mvn.set_speed(Vector(0, 4)) assert not (mvn.get_hit_box().collide(mvn2.get_hit_box())) mvn.move(1) assert mvn.get_hit_box().collide(mvn2.get_hit_box())
def __init__(self, x, y): Entity.__init__(self) self.x = x self.y = y self.collides = True self.img = pygame.image.load("res/pics/box.png") self.hitbox = Hitbox(self.x, self.y, self.img.get_width(), self.img.get_height())
class View: pos = None; size = None; hitbox = None; #constructor erstellt standart sicht mit hitbox def __init__(self): self.pos = Vector2D(0,0); self.size = Vector2D(154,50); self.hitbox = Hitbox(Vector2D(self.pos.x+1,self.pos.y+1),Vector2D(self.size.x -2, self.size.y -1)); #updated die position des Sichtfeldes def update(self, speed): self.pos.x += speed; self.hitbox.update_pos(Vector2D(self.pos.x-5,self.pos.y+3)); #löscht alle daten vom sichtfeld def delete(self): self.pos = None; self.size = None; self.hitbox = None;
class Bullet(): def __init__(self, x, y, direction, speed=10): self.collides = True self.x = x self.y = y self.x_v = 0 self.y_v = 0 self.img = pygame.image.load("res/pics/bullet.png") self.hitbox = Hitbox(self.x, self.y, self.img.get_width(), self.img.get_height()) if(direction == "up"): self.y_v = -speed elif(direction == "down"): self.y_v = speed elif(direction == "right"): self.x_v = speed elif(direction == "left"): self.x_v = -speed def update(self): self.x += self.x_v self.y += self.y_v self.hitbox.update(self.x, self.y) if(self.x < 0): self.kill() if(self.y < 0): self.kill() if(self.x > globals.width): self.kill() if(self.y > globals.height): self.kill() def draw(self): globals.window.blit(self.img, (self.x, self.y)) def collide(self, entity): pass def kill(self): globals.entities.remove(self)
def __init__(self, context, x, y, r, density, color): self.mass = density * pi * r**2 PhysicsBody.__init__(self, context, x, y, imrotatable=True) self.r = r self.color = color self.hitbox = Hitbox(self.pos - (self.r, self.r), self.r * 2, self.r * 2) self.shape = "circle" self.Surface = pygame.Surface( (self.px2m(self.r * 2), self.px2m(self.r * 2))) self.Surface.fill((255, 255, 255)) pygame.draw.circle(self.Surface, (0, 0, 0), self.px2m_tuple(self.r, self.r), self.px2m(self.r)) self.Surface = self.Surface.convert()
def test_pos_in_camera(): pygame.init() fen = pygame.display.set_mode((500, 500), 0) S = CollideTransformable() R = Rect(-1, -1, 2, 2) Hb = Hitbox(R) S.set_hit_box(Hb) C = Camera() C.set_position(Vector(-1, -1)) C.set_dimension(Vector(4, 4)) C.set_fen(fen) pos_vect = S.get_pos_camera(C.get_distorsion(), S.get_hit_box()) assert pos_vect == (0, 0, 250, 250) S = CollideTransformable() R = Rect(-2, -2, 2, 2) Hb = Hitbox(R) S.set_hit_box(Hb) C = Camera() C.set_position(Vector(-1, -1)) C.set_dimension(Vector(4, 4)) C.set_fen(fen) pos_vect = S.get_pos_camera(C.get_distorsion(), S.get_hit_box()) assert pos_vect == (-125, -125, 250, 250) S = CollideTransformable() R = Rect(1, 1, 2, 2) Hb = Hitbox(R) S.set_hit_box(Hb) C = Camera() C.set_position(Vector(-1, -1)) C.set_dimension(Vector(4, 4)) C.set_fen(fen) pos_vect = S.get_pos_camera(C.get_distorsion(), S.get_hit_box()) assert pos_vect == (250, 250, 250, 250)
def __init__(self, x, y, direction, speed=10): self.collides = True self.x = x self.y = y self.x_v = 0 self.y_v = 0 self.img = pygame.image.load("res/pics/bullet.png") self.hitbox = Hitbox(self.x, self.y, self.img.get_width(), self.img.get_height()) if(direction == "up"): self.y_v = -speed elif(direction == "down"): self.y_v = speed elif(direction == "right"): self.x_v = speed elif(direction == "left"): self.x_v = -speed
class HitboxController(): def __init__(self, logger): self.logger = logger self.hitbox = Hitbox() def show_root(self): add_dir('Games', PATH_GAMES) add_dir('Channels', PATH_CHANNELS) add_dir('Following', PATH_FOLLOWING) xbmcplugin.endOfDirectory(handle=PLUGINHANDLE) def show_games(self): for game in self.hitbox.get_games(): add_dir(game['category_name'], '?category=' + game['category_id'], self.hitbox.STATIC_URL + game['category_logo_large']) xbmcplugin.endOfDirectory(handle=PLUGINHANDLE)
def __init__(self, ai_settings, screen): super(Alien, self).__init__() self.screen = screen self.ai_settings = ai_settings self.image = pygame.image.load('Assets/BitmapV/Enemy.bmp') self.rect = self.image.get_rect() self.ratio = ai_settings.screen_height / 1080 self.rect.width = self.rect.width * self.ratio self.rect.height = self.rect.height * self.ratio self.image = pygame.transform.scale( self.image, (self.rect.width, self.rect.height)) self.speed = ai_settings.alien_speed self.rect.y = -1000 self.rect.x = randint(0, ai_settings.screen_width - self.rect.width) self.active = False self.hitbox = Hitbox(self)
def __init__(self, player, window_size): """ :param player: Which player the frog is :param window_size: the size of the window """ super().__init__() self.hitbox = Hitbox() self.player = player self.stunned = False self.time_of_stun = 0 self.swirl = pygame.image.load("swirl.png").convert_alpha() if player == 1: self.image = pygame.image.load("frog1.png").convert_alpha() self.size = self.image.get_size() self.x = window_size[0]/2 - self.size[0]/2 self.y = window_size[1] - self.size[1] self.mouth = (self.x + self.size[0]/2, self.y) self.lily = pygame.image.load("lily1.png") self.lily_size = self.lily.get_size() self.lily_x = window_size[0]/2 - self.size[0]/2 - 15 self.lily_y = window_size[1] - self.size[1] - 50 self.rect = self.image.get_rect() self.rect.x = self.x self.rect.y = self.y else: self.image = pygame.image.load("frog2.png").convert_alpha() self.size = self.image.get_size() self.x = window_size[0]/2 - self.size[0]/2 self.y = 0 self.mouth = (self.x + self.size[0]/2, self.size[1]) self.lily = pygame.image.load("lily1.png") self.lily = pygame.transform.rotate(self.lily, 180) self.lily_size = self.lily.get_size() self.lily_x = window_size[0]/2 - self.size[0]/2 - 15 self.lily_y = -15 self.rect = self.image.get_rect() self.rect.x = self.x self.rect.y = self.y
def test_physics_high_fall(): R = Rect(0, 0, 10, 16) Hb = Hitbox(R) plat = SolidPlatform(Hb) def pos(t): return 0 gl = GameLevel([plat], pos) gl.player.set_position(0, -100) gravity = Gravity(50) gl.player.add_force(gravity) #Au cas où la gravité soit nulle dans Gl timeout = 1000 gl.opti_step = 10 gl.optimise_data() while not (gl.player.get_hit_box().collide( plat.get_hit_box())) and timeout > 0: print(gl.player.get_hit_box()) gl.physics_step(0.01, gl.get_objects_opti()) timeout -= 1 assert timeout > 0 assert gl.player.get_position().y <= 0
def __init__(self, x, y): Entity.__init__(self) self.x = x self.y = y self.oldx = x self.oldy = y self.collides = True self.img_body = pygame.image.load("res/pics/body.png") self.hitbox = Hitbox(self.x, self.y, self.img_body.get_width(), self.img_body.get_height()) self.up = 0 self.down = 0 self.left = 0 self.right = 0 self.max_direction_accel = 5 self.direction_accel = 0.2 self.cooldown = 30 self.cooldown_timer = 0 self.is_clamp = True
def test_hitbox1(): T = CollideTransformable() R = Rect(-1, -1, 2, 2) Hb = Hitbox(R) Hb.link(T) T.translate(Vector(2, 0)) assert Hb.get_ctrbl().get_position() == Vector(2, 0) R = Rect(-1, -1, 2, 2) Hb = Hitbox(R) Hb.link(T) T.rotate(np.pi / 2) #Isn't supposed to affect hit box (removed in Physics 4.0) assert T.get_position() == Vector(2, 0) T.scale(2, 3) assert Hb.get_world_rect() == Rect(0, -3, 4, 6) R = Rect(0, 0, 2, 2) Hb = Hitbox(R) T = CollideTransformable() Hb.link(T) T.set_position(2, 0) assert Hb.get_world_rect() == Rect(2, 0, 2, 2) T.scale(4, 3) assert Hb.get_world_rect() == Rect(2, 0, 8, 6)
def __init__(self, logger): self.logger = logger self.hitbox = Hitbox()
def test_hitbox2(): T1 = CollideTransformable() T2 = CollideTransformable() R1 = Rect(-1, -1, 2, 2) R2 = Rect(-1, -1, 2, 2) Hb1 = Hitbox(R1) Hb2 = Hitbox(R2) Hb1.link(T1) Hb2.link(T2) T1.translate(Vector(0.5, 0.5)) assert Hb1.collide(Hb2) assert Hb2.collide(Hb1) T1.translate(Vector(-0.5, -0.5)) T2.translate(Vector(2, 0)) assert Hb1.collide(Hb1) assert Hb2.collide(Hb2) T2.translate(Vector(0.01, 0)) assert not (Hb1.collide(Hb2)) assert not (Hb2.collide(Hb1))
def get_hitbox(self): x, y = self.position return Hitbox((x - self.radius, y - self.radius), (x + self.radius, y + self.radius), ident=self.ident)
def ball_hitbox(cls, position, radius): return Hitbox((position.x - radius, position.y - radius), (position.x + radius, position.y + radius))
from hypothesis import given from hypothesis.strategies import integers, lists from hitbox import Hitbox pygame.init() fen = pygame.display.set_mode((500, 500),0) #Coordinates for the platform x = 0 y = 10 #Let's start by creating the polygon for hit_boxes p = Rect(-1,-1,2,2) #Creates the polygon corresponding to the given sequence -> it's a rectangle hb = Hitbox(p) #Now let's build the platform associated to this polygon and move it to our coordinates plat = SolidPlatform(hb) plat.set_sps(None) plat.translate(Vector(x,y)) #To create an other platform with the same hit box it easy: plat2 = plat.copy() plat3 = plat.copy() plat4 = plat.copy() plat2.translate(Vector(0.5,-5)) plat3.translate(Vector(0.1,-5)) plat4.translate(Vector(0.3,-15))
def __init__(self): """ Constructor function """ # Call the parent's constructor pygame.sprite.Sprite.__init__(self) sprite_sheet = SpriteSheet( os.path.join("ressources", "images", "player", "sprite-sheet.png")) # Load all the idle images right image = sprite_sheet.get_image(0, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(50, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(100, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(150, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(0, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(50, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(100, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(150, 0, 50, 37) self.idle_frames_r.append(image) # Load all the idle images left image = sprite_sheet.get_image(0, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(50, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(100, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(150, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(0, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(50, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(100, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(150, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) # Load all the right facing images image = sprite_sheet.get_image(0, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(50, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(100, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(150, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(200, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(250, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(300, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(350, 37, 50, 37) self.walking_frames_r.append(image) # Load all the right facing images, then flip them to face left. image = sprite_sheet.get_image(0, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(50, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(100, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(150, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(200, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(250, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(300, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(350, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) """for i in range(8): self.idle_frames_l[i].scroll(-16,-5) self.idle_frames_r[i].scroll(-16,-5) self.walking_frames_l[i].scroll(-16,-5) self.walking_frames_r[i].scroll(-16,-5)""" # Set the image the player starts with self.image = self.walking_frames_r[0] # Set a referance to the image rect. self.rect = self.image.get_rect() #rect size: 50,37 #self.rect.size = (20,30) self.diff_x = 15 self.diff_y = 3 self.hitbox = Hitbox(20, 31, self.rect.x + self.diff_x, self.rect.y + self.diff_y) #self.hitbox_rect = pygame.Rect(self.rect.x+self.diff_x,self.rect.y+self.diff_y,20,31) #difference: 15,3 self.hitbox_rect = self.hitbox.get_rect()
class Player(Entity): def __init__(self, x, y): Entity.__init__(self) self.x = x self.y = y self.oldx = x self.oldy = y self.collides = True self.img_body = pygame.image.load("res/pics/body.png") self.hitbox = Hitbox(self.x, self.y, self.img_body.get_width(), self.img_body.get_height()) self.up = 0 self.down = 0 self.left = 0 self.right = 0 self.max_direction_accel = 5 self.direction_accel = 0.2 self.cooldown = 30 self.cooldown_timer = 0 self.is_clamp = True def update(self): self.oldx = self.x self.oldy = self.y if(globals.inputs.k_k_down): self.is_clamp = not self.is_clamp if(globals.inputs.isKeyDown("w")): self.up += self.direction_accel else: self.up -= self.direction_accel if(globals.inputs.isKeyDown("a")): self.left += self.direction_accel else: self.left -= self.direction_accel if(globals.inputs.isKeyDown("s")): self.down += self.direction_accel else: self.down -= self.direction_accel if(globals.inputs.isKeyDown("d")): self.right += self.direction_accel else: self.right -= self.direction_accel if(self.up > self.max_direction_accel): self.up = self.max_direction_accel if(self.down > self.max_direction_accel): self.down = self.max_direction_accel if(self.left > self.max_direction_accel): self.left = self.max_direction_accel if(self.right > self.max_direction_accel): self.right = self.max_direction_accel if(self.up < 0): self.up = 0 if(self.down < 0): self.down = 0 if(self.left < 0): self.left = 0 if(self.right < 0): self.right = 0 total_speed = self.up + self.down + self.right + self.left if(total_speed > 0): globals.entities.append(Particle(self.x + (self.img_body.get_width() / 2), self.y + (self.img_body.get_height() / 2))) self.y -= self.up self.y += self.down self.x -= self.left self.x += self.right if(self.is_clamp): self.clamp(self.img_body) self.hitbox.update(self.x, self.y) if(self.cooldown_timer > 0): self.cooldown_timer -= 1 else: if(globals.inputs.isKeyDown("right")): globals.entities.append(Bullet(self.x, self.y, "right")) elif(globals.inputs.isKeyDown("left")): globals.entities.append(Bullet(self.x, self.y, "left")) elif(globals.inputs.isKeyDown("up")): globals.entities.append(Bullet(self.x, self.y, "up")) elif(globals.inputs.isKeyDown("down")): globals.entities.append(Bullet(self.x, self.y, "down")) self.cooldown_timer = self.cooldown def draw(self): globals.window.blit(self.img_body, (self.x, self.y)) def getType(self): return "Player" def collide(self, entity): pass def getCenter(self): x = (self.x - self.hitbox.width) / 2 y = (self.y - self.hitbox.height) / 2 return (x, y)
def __init__(self): self.pos = Vector2D(0,0); self.size = Vector2D(154,50); self.hitbox = Hitbox(Vector2D(self.pos.x+1,self.pos.y+1),Vector2D(self.size.x -2, self.size.y -1));
def get_hitbox(self): return Hitbox( (self.position[0], self.position[1]), (self.position[0] + self.width, self.position[1] + self.height), self.ident)
class Player(pygame.sprite.Sprite): """ This class represents the bar at the bottom that the player controls. """ # -- Attributes # Set speed vector of player change_x = 0 change_y = 0 # This holds all the images for the animated walk left/right of our player idle_frames_l = [] idle_frames_r = [] walking_frames_l = [] walking_frames_r = [] # Frame to show frame = 0 frame_speed = 12 # What direction is the player facing? direction = "R" # List of sprites we can bump against level = None # -- Methods def __init__(self): """ Constructor function """ # Call the parent's constructor pygame.sprite.Sprite.__init__(self) sprite_sheet = SpriteSheet( os.path.join("ressources", "images", "player", "sprite-sheet.png")) # Load all the idle images right image = sprite_sheet.get_image(0, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(50, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(100, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(150, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(0, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(50, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(100, 0, 50, 37) self.idle_frames_r.append(image) image = sprite_sheet.get_image(150, 0, 50, 37) self.idle_frames_r.append(image) # Load all the idle images left image = sprite_sheet.get_image(0, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(50, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(100, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(150, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(0, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(50, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(100, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) image = sprite_sheet.get_image(150, 0, 50, 37) image = pygame.transform.flip(image, True, False) self.idle_frames_l.append(image) # Load all the right facing images image = sprite_sheet.get_image(0, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(50, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(100, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(150, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(200, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(250, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(300, 37, 50, 37) self.walking_frames_r.append(image) image = sprite_sheet.get_image(350, 37, 50, 37) self.walking_frames_r.append(image) # Load all the right facing images, then flip them to face left. image = sprite_sheet.get_image(0, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(50, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(100, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(150, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(200, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(250, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(300, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) image = sprite_sheet.get_image(350, 37, 50, 37) image = pygame.transform.flip(image, True, False) self.walking_frames_l.append(image) """for i in range(8): self.idle_frames_l[i].scroll(-16,-5) self.idle_frames_r[i].scroll(-16,-5) self.walking_frames_l[i].scroll(-16,-5) self.walking_frames_r[i].scroll(-16,-5)""" # Set the image the player starts with self.image = self.walking_frames_r[0] # Set a referance to the image rect. self.rect = self.image.get_rect() #rect size: 50,37 #self.rect.size = (20,30) self.diff_x = 15 self.diff_y = 3 self.hitbox = Hitbox(20, 31, self.rect.x + self.diff_x, self.rect.y + self.diff_y) #self.hitbox_rect = pygame.Rect(self.rect.x+self.diff_x,self.rect.y+self.diff_y,20,31) #difference: 15,3 self.hitbox_rect = self.hitbox.get_rect() def update(self): """ Move the player. """ self.hitbox_rect = self.hitbox.get_rect() # Gravity self.calc_grav() # Move left/right #self.rect.x += self.change_x self.hitbox_rect.x += self.change_x """pos = self.hitbox_rect.x + self.level.world_shift if self.direction == "R": frame = (pos // 30) % len(self.walking_frames_r) self.image = self.walking_frames_r[frame] else: frame = (pos // 30) % len(self.walking_frames_l) self.image = self.walking_frames_l[frame]""" self.frame = self.frame + 1 if self.frame >= 8 * 8: self.frame = 0 if self.change_x != 0: if self.direction == "R": self.image = self.walking_frames_r[self.frame // self.frame_speed] else: self.image = self.walking_frames_l[self.frame // self.frame_speed] else: if self.direction == "R": self.image = self.idle_frames_r[self.frame // self.frame_speed] else: self.image = self.idle_frames_l[self.frame // self.frame_speed] # See if we hit anything self.hitbox.update_pos(self.hitbox_rect.x, self.hitbox_rect.y) block_hit_list = pygame.sprite.spritecollide(self.hitbox, self.level.platform_list, False) for block in block_hit_list: # If we are moving right, # set our right side to the left side of the item we hit if self.change_x > 0: #self.rect.right = block.rect.left-self.diff_x self.hitbox_rect.right = block.rect.left elif self.change_x < 0: # Otherwise if we are moving left, do the opposite. #self.rect.left = block.rect.right+self.diff_x self.hitbox_rect.left = block.rect.right # Move up/down #self.rect.y += self.change_y self.hitbox_rect.y += self.change_y # Check and see if we hit anything self.hitbox.update_pos(self.hitbox_rect.x, self.hitbox_rect.y) block_hit_list = pygame.sprite.spritecollide(self.hitbox, self.level.platform_list, False) for block in block_hit_list: # Reset our position based on the top/bottom of the object. if self.change_y > 0: #self.rect.bottom = block.rect.top-self.diff_y self.hitbox_rect.bottom = block.rect.top elif self.change_y < 0: #self.rect.top = block.rect.bottom+self.diff_y self.hitbox_rect.top = block.rect.bottom # Stop our vertical movement self.change_y = 0 if isinstance(block, MovingPlatform): #self.rect.x += block.change_x self.hitbox_rect.x += block.change_x # Update image self.rect.x = self.hitbox_rect.x - self.diff_x self.rect.y = self.hitbox_rect.y - self.diff_y self.hitbox.update_pos(self.hitbox_rect.x, self.hitbox_rect.y) def calc_grav(self): """ Calculate effect of gravity. """ if self.change_y == 0: self.change_y = 1 else: self.change_y += .35 # See if we are on the ground. if self.hitbox_rect.y >= constants.SCREEN_HEIGHT - self.hitbox_rect.height and self.change_y >= 0: self.change_y = 0 self.hitbox_rect.y = constants.SCREEN_HEIGHT - self.hitbox_rect.height def jump(self): """ Called when user hits 'jump' button. """ # move down a bit and see if there is a platform below us. # Move down 2 pixels because it doesn't work well if we only move down 1 # when working with a platform moving down. self.hitbox_rect.y += 2 platform_hit_list = pygame.sprite.spritecollide( self.hitbox, self.level.platform_list, False) self.hitbox_rect.y -= 2 # If it is ok to jump, set our speed upwards if len(platform_hit_list ) > 0 or self.hitbox_rect.bottom >= constants.SCREEN_HEIGHT: self.change_y = -10 # Player-controlled movement: def go_left(self): """ Called when the user hits the left arrow. """ self.change_x = -6 self.direction = "L" def go_right(self): """ Called when the user hits the right arrow. """ self.change_x = 6 self.direction = "R" def stop(self): """ Called when the user lets off the keyboard. """ self.change_x = 0 def set_hitbox(self): self.hitbox.update_pos(self.rect.x + self.diff_x, self.rect.y + self.diff_y)
class Frog(pygame.sprite.Sprite): def __init__(self, player, window_size): """ :param player: Which player the frog is :param window_size: the size of the window """ super().__init__() self.hitbox = Hitbox() self.player = player self.stunned = False self.time_of_stun = 0 self.swirl = pygame.image.load("swirl.png").convert_alpha() if player == 1: self.image = pygame.image.load("frog1.png").convert_alpha() self.size = self.image.get_size() self.x = window_size[0]/2 - self.size[0]/2 self.y = window_size[1] - self.size[1] self.mouth = (self.x + self.size[0]/2, self.y) self.lily = pygame.image.load("lily1.png") self.lily_size = self.lily.get_size() self.lily_x = window_size[0]/2 - self.size[0]/2 - 15 self.lily_y = window_size[1] - self.size[1] - 50 self.rect = self.image.get_rect() self.rect.x = self.x self.rect.y = self.y else: self.image = pygame.image.load("frog2.png").convert_alpha() self.size = self.image.get_size() self.x = window_size[0]/2 - self.size[0]/2 self.y = 0 self.mouth = (self.x + self.size[0]/2, self.size[1]) self.lily = pygame.image.load("lily1.png") self.lily = pygame.transform.rotate(self.lily, 180) self.lily_size = self.lily.get_size() self.lily_x = window_size[0]/2 - self.size[0]/2 - 15 self.lily_y = -15 self.rect = self.image.get_rect() self.rect.x = self.x self.rect.y = self.y def draw(self, screen): """ :param screen: the surface object :return: void """ screen.blit(self.lily, (self.lily_x, self.lily_y)) screen.blit(self.image, (self.x, self.y)) if self.stunned and self.player == 1: screen.blit(self.swirl, (self.x + 35, self.y)) elif self.stunned and self.player == 2: screen.blit(self.swirl, (self.x + 35, self.y + 40)) def fire_tongue(self, screen, power, x, y, fly_sprites, dfly_sprites): """ :param screen: the surface object :param power: the power of the tongue shot :param x: the x position of the ending position :param y: the y position of the ending position :param fly_sprites: fly sprite group :param dfly_sprites: dragonfly sprite group :return: a list of collisions, and a boolean to determine whether or not it was a dragonfly, which is prioritized This function makes a calculation between starting position and ending positions to determine how far the tongue will be launched depending on the power of the charge """ power /= 3.4 tongue_color = (212, 144, 198) if self.player == 1: start = (self.x + self.size[0]/2, self.y) dist = (power * (start[0] - x), power * (y - start[1])) pygame.draw.line(screen, tongue_color, start, ((start[0] - dist[0]), (start[1] + dist[1])), 20) self.hitbox.draw(screen, int(start[0] - dist[0]), int(start[1] + dist[1])) dfly_hit_list = pygame.sprite.spritecollide(self.hitbox, dfly_sprites, True) if len(dfly_hit_list) > 0: return dfly_hit_list, True else: fly_hit_list = pygame.sprite.spritecollide(self.hitbox, fly_sprites, True) return fly_hit_list, False else: start = (self.x + self.size[0]/2, self.size[1]) dist = (power * (start[0] - x), power * (y - start[1])) pygame.draw.line(screen, tongue_color, start, ((start[0] - dist[0]), (start[1] + dist[1])), 20) pygame.draw.circle(screen, tongue_color, (int(start[0] - dist[0]), int(start[1] + dist[1])), 30, 0) self.hitbox.draw(screen, int(start[0] - dist[0]), int(start[1] + dist[1])) dfly_hit_list = pygame.sprite.spritecollide(self.hitbox, dfly_sprites, True) if len(dfly_hit_list) > 0: return dfly_hit_list, True else: fly_hit_list = pygame.sprite.spritecollide(self.hitbox, fly_sprites, True) return fly_hit_list, False
def test_hitbox3(): T1 = CollideTransformable() T2 = CollideTransformable() R1 = Rect(-1, -1, 2, 2) R2 = Rect(-1, -1, 2, 2) Hb1 = Hitbox(R1) Hb2 = Hitbox(R2) Hb1.link(T1) Hb2.link(T2) T1.translate(Vector(1, 1)) T2.translate(Vector(2.5, 1)) T1.set_speed(Vector(1, 0)) print(Hb1.get_world_rect()) print(Hb2.get_world_rect()) v = Hb1.remove_collide(Hb2) print(v) T1.translate(v) assert not (Hb1.collide(Hb2))
import matplotlib as plt import profile import pygame import time pygame.init() fen = pygame.display.set_mode((500, 500), 0) fen.set_alpha(None) plat = [] for i in range(100): size = 100 plat.append(SolidPlatform(Hitbox(Rect(-10 + i * size, 0, size - 20, 24)))) def pos_player(t): return t * 200 gl = GameLevel(plat, pos_player) gl.load_camera(fen) gl.optimise_data() gl.get_camera().set_dimension(Vector(200, 150)) def launch(): for i in range(1, 300):
class Tank: def __init__(self, x: int, y: int, radius: int, color: Tuple[int, int, int], tank_body_model: str, tank_turret_model: str, width: int, height: int) -> None: self.x = x self.y = y self.radius = radius self.img_width = width self.img_height = height self.color = color self.speed = 3 self.turn_speed = 0.05 self.tank_body_model = tank_body_model self.tank_turret_model = tank_turret_model self.body_rotation = 0 self.turret_rotation = 0 self.hitbox = Hitbox() self.update_hitbox() def draw(self, window: pygame.display) -> None: pygame.draw.circle(window, self.color, (self.x, self.y), self.radius) pygame.draw.line(window, self.color, (self.x, self.y), (self.x + 15 * cos(self.turret_rotation), self.y + 15 * sin(self.turret_rotation)), 3) def move(self, keys) -> None: if keys[pygame.K_w]: self.x += self.speed * cos(self.body_rotation) self.y -= self.speed * sin(self.body_rotation) if keys[pygame.K_d]: self.body_rotation -= self.turn_speed if self.body_rotation < 0: self.body_rotation = self.body_rotation + 2 * pi self.body_rotation = self.body_rotation % (2 * pi) if keys[pygame.K_a]: self.body_rotation += self.turn_speed if self.body_rotation < 0: self.body_rotation = self.body_rotation + 2 * pi self.body_rotation = self.body_rotation % (2 * pi) self.update_hitbox() def update_hitbox(self): rotation = abs(self.body_rotation - 2 * pi) half_width = self.img_width // 2 half_height = self.img_height // 2 self.hitbox.update_location( self.rotate_point(-half_width, half_height, rotation), self.rotate_point(half_width, half_height, rotation), self.rotate_point(half_width, -half_height, rotation), self.rotate_point(-half_width, -half_height, rotation)) def rotate_point(self, x: int, y: int, rotation: float) -> Tuple[int, int]: return int(x * cos(rotation) - y * sin(rotation) + self.x), \ int(y * cos(rotation) + x * sin(rotation) + self.y) def detect_hit(self, point_1) -> bool: return self.hitbox.detect_hit_bullet(point_1) def update_tank_body(self, x: str) -> None: self.tank_body_model = x def update_turret_rotation(self, mouse_pos: Tuple[int, int]) -> None: self.turret_rotation = -atan2(mouse_pos[1], mouse_pos[0])