From: adrianna Date: Sat, 10 Sep 2016 12:19:50 +0000 (+0200) Subject: approximate circles by polygons in pymunk X-Git-Tag: tabakrolletjie-v1.0.0~109^2 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=e584696d6d991979ee25d614c7e9b66290b345ee;p=tabakrolletjie.git approximate circles by polygons in pymunk --- diff --git a/tabakrolletjie/obstacles.py b/tabakrolletjie/obstacles.py index 9adb803..7096424 100644 --- a/tabakrolletjie/obstacles.py +++ b/tabakrolletjie/obstacles.py @@ -6,6 +6,7 @@ import pymunk import pymunk.pygame_util import pygame.draw import pygame.surface +from pygame.math import Vector2 from .constants import (SCREEN_SIZE, OBSTACLE_CATEGORY) from .loader import loader @@ -85,8 +86,14 @@ class Shrub(BaseObstacle): def __init__(self, shrublets): super(Shrub, self).__init__() - for [x, y, r] in shrublets: - self.shapes.append(pymunk.Circle(self.body, r, offset=(x, y))) + for (x, y, r) in shrublets: + vec = Vector2(0, int(r)) + STEPS = 18 + vertices = [vec.rotate(angle) + (x, y) for angle in range(0, 360, 360/STEPS)] + vertices = [(v.x, v.y) for v in vertices] + + self.shapes.append(pymunk.Poly(self.body, vertices)) + self.shrublets = shrublets self._image = None def get_image(self): @@ -94,10 +101,10 @@ class Shrub(BaseObstacle): self._image = pygame.surface.Surface(SCREEN_SIZE).convert_alpha() self._image.fill((0,0,0,0)) - for shape in self.shapes: - centre = pymunk.pygame_util.to_pygame(shape.offset, self._image) - pygame.draw.circle(self._image, (255, 255, 255), centre, int(shape.radius)) - + for (x, y, r) in self.shrublets: + centre = pymunk.pygame_util.to_pygame((x, y), self._image) + pygame.draw.circle(self._image, (255, 255, 255), centre, r) + shrub_texture = loader.load_image("textures", "shrub.png").convert_alpha() self._image.blit(shrub_texture, (0, 0), None, pgl.BLEND_RGBA_MULT)