def update(self): self.position = Vector2(self.rect.center) self.grid_position = pos_to_grid(self.position) if self.speed != Vector2(0, 0): self.current_frame += 1 if self.current_frame % self.frame_time == 0: self.index = (self.index + 1) % len( self.frames[self.direction]) self.image = self.frames[self.direction][self.index] self.rect = self.rect.move(self.speed.x, self.speed.y) self.sprite_rect = self.sprite_rect.move(self.speed.x, self.speed.y) if self.rect.right > SCREEN_WIDTH - self.width: self.rect.left = self.width self.sprite_rect.left = self.width elif self.rect.left < self.width: self.rect.right = SCREEN_WIDTH - self.width self.sprite_rect.right = SCREEN_WIDTH - self.width elif self.rect.top < self.height: self.rect.bottom = SCREEN_HEIGHT - self.height self.sprite_rect.bottom = SCREEN_HEIGHT - self.height elif self.rect.bottom > SCREEN_HEIGHT - self.height: self.rect.top = self.height self.sprite_rect.top = self.height else: self.index = 0 self.image = self.frames[self.direction][self.index] pass
def init_from_file(self): data = load('config/{}'.format(self.filename)) self.name = data["name"] self.health = data["health"] self.magic = data["magic"] anim = load(data["animation"]) self.width = anim["width"] self.height = anim["height"] self.frames = [] spritesheet = pygame.image.load(PATH + anim["spritesheet"]) for d in anim["directions"]: Y = self.height * d tmp = [] for i in range(anim["num_frames"]): X = i * self.width surf = pygame.Surface((self.width, self.height)) surf.blit(spritesheet, (0, 0), (X, Y, self.width, self.height)) surf.set_colorkey(tuple(anim["colorkey"])) tmp.append(surf) self.frames.append(tmp) self.frame_time = anim["frame_time"] self.image = self.frames[0][0] self.sprite_rect = self.frames[0][0].get_bounding_rect().move(128, 380) t = (self.sprite_rect.midleft[0] + 3, self.sprite_rect.midleft[1] + 8) self.rect = pygame.Rect( t, (self.sprite_rect.w - 7, int(self.sprite_rect.h / 4) - 1)) self.inventory = data["inventory"] self.mask = pygame.mask.from_surface(self.image) self.position = Vector2(self.rect.center) self.grid_position = pos_to_grid(self.position) pass
def draw(self, screen, color, m: Vector2): p = Vector2(0, self.height) p.rotate_degrees(self.frame + 180) p += self.pos + m pygame.draw.line(screen, color, self.pos + m, p, 3) for i in range(self.levels): for j in -1, 1: epos = Vector2((self.levelSize + self.levelIncrease * i) * j, self.height - i * self.spacing - self.levelIncrease * i * 0.5 - self.levelSize) epos.rotate_degrees(self.frame + 180) epos += self.pos + m spos = Vector2(0, self.height - i * self.spacing) spos.rotate_degrees(self.frame + 180) spos += self.pos + m pygame.draw.line(screen, color, spos, epos, 3)
def __call__(self, screen, m, color=pygame.Color('#000000'), *args, **kwargs): pygame.draw.circle( screen, color, Vector2(int(self.pos.x + m.x), int(self.pos.y + m.y)), 20, 3)
def __init__(self): pygame.sprite.Sprite.__init__(self, playerGroup, entitiesGroup) self.speed = Vector2(0, 0) self.index = 0 self.direction = DOWN self.current_frame = 0 self.MoveSpeed = 7 self.level_position = (1, 1) self.level_has_changed = True self.level_name = "overworld" self.init_from_file()
def __init__(self, filename, tilemap, id_): pygame.sprite.Sprite.__init__(self, entitiesGroup) self.tilemap = tilemap self.speed = Vector2(0, 0) self.index = 0 self.direction = DOWN self.current_frame = 0 self.MoveSpeed = 7 self.filename = filename if "yaml" in filename else filename + ".yaml" self.id = id_ self.path = None self.init_from_file()
def bezier(a: Vector2, b: Vector2, c: Vector2, d: Vector2, j: int): r = [] for i in range(j): t = 1 / j * i r.append( Vector2( #(1-t)**3*a.x + 3 * (1 - t)**2*t*b.x + 3 * (1 - t)**2*t*c.x + t**3*d.x, pow(1 - t, 3) * a.x + 3 * pow(1 - t, 2) * t * b.x + 3 * (1 - t) * pow(t, 2) * c.x + pow(t, 3) * d.x, #(1 - t) ** 3 * a.y + 3 * (1 - t) ** 2 * t * b.y + (3 * (1 - t) * t) ** 2 * c.y + t ** 3 * d.y, pow(1 - t, 3) * a.y + 3 * pow(1 - t, 2) * t * b.y + 3 * (1 - t) * pow(t, 2) * c.y + pow(t, 3) * d.y)) return r
def load(): global car, lines, trees, coins, portal lines = [] trees = [] coins = [] # ----- Player ----- # car = Ball(level['ball']['x'], level['ball']['y'], 10, space, 'img/player.png', config['collid']['body']) car.body.angular_velocity = 0 # ----- Ground ----- # for l in level['lines']: i = [] for j in l: i.append(Vector2(j[0], j[1])) l = bezier.bezier(i[0], i[1], i[2], i[3], 100) line = pymunk.Body(0, 0, body_type=pymunk.Body.STATIC) space.add(line) line.position = (0, 0) lineb = bezier.BezierCollider(line, l, 3) lineb.add(space) lines.append(lineb) # ----- Trees ----- # for i in level['trees']: trees.append(Firtree(Vector2(i[0], i[1]), increase=1, move=5, frame=i[2])) # ----- Coins ----- # for i in level['coins']: c = Coin(Vector2(i[0], i[1]), config['collid']['coin'], frame=random.randint(-5, 5)) c.add(space) coins.append(c) # ----- Portal ----- # portal = Portal(Vector2(level["portal"]["pos"][0], level["portal"]["pos"][1]), config['collid']['portal']) portal.add(space)
def __init__(self, x: int, y: int, radius: int, space: pymunk.space, img, collid): wheel_color = 52, 219, 119 mass = 100 moment = pymunk.moment_for_circle(mass, 0, radius) wheel1_b = pymunk.Body(mass, moment) wheel1_s = pymunk.Circle(wheel1_b, radius) wheel1_s.friction = 10 wheel1_s.color = wheel_color wheel1_b.position = Vector2(x, y) wheel1_s.collision_type = collid space.add(wheel1_b, wheel1_s) self.body = wheel1_b self.collider = wheel1_s self.space = space self.img = pygame.image.load(img) self.img = pygame.transform.scale(self.img, (20, 20)) self.imagerect = self.img.get_rect()
def handle_keypress(self, event): speed = [0, 0] blocking_grid_collision = pygame.sprite.spritecollide( self, gridEntitiesGroupBlocking, False) collision_sides = get_collision_side(self, blocking_grid_collision) if event.type == KEYDOWN: if event.key == K_UP and UP not in collision_sides: self.direction = UP speed[1] -= self.MoveSpeed elif event.key == K_DOWN and DOWN not in collision_sides: self.direction = DOWN speed[1] += self.MoveSpeed elif event.key == K_LEFT and LEFT not in collision_sides: self.direction = LEFT speed[0] -= self.MoveSpeed elif event.key == K_RIGHT and RIGHT not in collision_sides: self.direction = RIGHT speed[0] += self.MoveSpeed self.speed = Vector2(tuple(speed)) pass
def update(self): if self.speed != Vector2(0, 0): self.current_frame += 1 if self.current_frame % self.frame_time == 0: self.index = (self.index + 1) % len( self.frames[self.direction]) self.image = self.frames[self.direction][self.index] self.rect = self.rect.move(self.speed.x, self.speed.y) self.sprite_rect = self.sprite_rect.move(self.speed.x, self.speed.y) if self.rect.right > SCREEN_WIDTH: self.rect.left = self.width self.sprite_rect.left = self.width self.level_position = (self.level_position[0] + 1, self.level_position[1]) self.level_has_changed = True elif self.rect.left < 0: self.rect.right = SCREEN_WIDTH - self.width self.sprite_rect.right = SCREEN_WIDTH - self.width self.level_position = (self.level_position[0] - 1, self.level_position[1]) self.level_has_changed = True elif self.rect.top < 0: self.rect.bottom = SCREEN_HEIGHT - self.height self.sprite_rect.bottom = SCREEN_HEIGHT - self.height self.level_position = (self.level_position[0], self.level_position[1] - 1) self.level_has_changed = True elif self.rect.bottom > SCREEN_HEIGHT: self.rect.top = self.height self.sprite_rect.top = self.height self.level_position = (self.level_position[0], self.level_position[1] + 1) self.level_has_changed = True else: self.index = 0 self.image = self.frames[self.direction][self.index] pass
def cpoint(a: float, d: float, s: Vector2): return Vector2(s.x + round_d(math.cos(a) * d, 3), s.y + round_d(math.sin(a), 3) * d)
def bezier(a: Vector2, b: Vector2, c: Vector2, d: Vector2, j: int): r = [] for i in range(j): t = 1 / j * i r.append( Vector2( #(1-t)**3*a.x + 3 * (1 - t)**2*t*b.x + 3 * (1 - t)**2*t*c.x + t**3*d.x, pow(1 - t, 3) * a.x + 3 * pow(1 - t, 2) * t * b.x + 3 * (1 - t) * pow(t, 2) * c.x + pow(t, 3) * d.x, #(1 - t) ** 3 * a.y + 3 * (1 - t) ** 2 * t * b.y + (3 * (1 - t) * t) ** 2 * c.y + t ** 3 * d.y, pow(1 - t, 3) * a.y + 3 * pow(1 - t, 2) * t * b.y + 3 * (1 - t) * pow(t, 2) * c.y + pow(t, 3) * d.y)) return r bz = bezier(Vector2(150, 0), Vector2(0, 300), Vector2(300, 300), Vector2(150, 0), 10) print(math.degrees(angle_x(Vector2(0, 0), Vector2(12, 12)))) d = 1 a = 90 l = 10 screen.fill(pygame.Color('#ffffff')) for i in range(len(bz) - 1): print(math.degrees(angle_x(bz[i + 1], bz[i]) - math.radians(a) - math.pi)) pygame.draw.lines(screen, pygame.Color('#000000'), False, [(bz[i].x + 10, bz[i].y + 10), (bz[i + 1].x + 10, bz[i + 1].y + 10)], 3) if i % d == 0:
def flipy(y): """Small hack to convert chipmunk physics to pygame coordinates""" return y # ========================= LEVEL VARS ========================= # lines = [] trees = [] coins = [] portal = '' car = '' cam = Vector2(level['ball']['x'], level['ball']['y']) ls = True # ========================= LEVEL ========================= # def load(): global car, lines, trees, coins, portal lines = [] trees = [] coins = [] # ----- Player ----- # car = Ball(level['ball']['x'], level['ball']['y'], 10, space, 'img/player.png', config['collid']['body']) car.body.angular_velocity = 0 # ----- Ground ----- # for l in level['lines']: i = []