Skip to content

Commit

Permalink
Move examples out of package, new loading scheme without imports from…
Browse files Browse the repository at this point in the history
… directory
  • Loading branch information
sseemayer committed Jun 30, 2013
1 parent 940e5f8 commit 1114b75
Show file tree
Hide file tree
Showing 13 changed files with 266 additions and 270 deletions.
15 changes: 8 additions & 7 deletions py2d/examples/Bezier.py → examples/Bezier.py
Expand Up @@ -3,13 +3,14 @@

from py2d.Bezier import *
from py2d.Math import *
import py2d.examples.Main

SELECTION_DISTANCE = 20

class Cubic(py2d.examples.Main.Example):
from examples import Example

class Cubic(Example):
"""Cubic Bezier curve sample
Draw around end and control points of the bezier curve.
Key mappings:
Expand Down Expand Up @@ -39,7 +40,7 @@ def update(self, time_elapsed):
pass

def render(self):

pygame.draw.line(self.runner.screen, 0x006600, self.p1.as_tuple(), self.c1.as_tuple())
pygame.draw.line(self.runner.screen, 0x006600, self.p2.as_tuple(), self.c2.as_tuple())

Expand Down Expand Up @@ -74,9 +75,9 @@ def mouse_move(self, pos, rel, buttons):
self.sel_point.x, self.sel_point.y = pos


class Quadratic(py2d.examples.Main.Example):
class Quadratic(Example):
"""Quadratic Bezier curve sample
Draw around end and control points of the bezier curve.
Key mappings:
Expand Down Expand Up @@ -104,7 +105,7 @@ def update(self, time_elapsed):
pass

def render(self):

pygame.draw.line(self.runner.screen, 0x006600, self.p1.as_tuple(), self.c.as_tuple())
pygame.draw.line(self.runner.screen, 0x006600, self.p2.as_tuple(), self.c.as_tuple())

Expand Down
8 changes: 4 additions & 4 deletions py2d/examples/Draw.py → examples/Draw.py
Expand Up @@ -2,11 +2,11 @@
from pygame.locals import *

from py2d.Math import *
import py2d.examples.Main
from examples import Example

class Draw(py2d.examples.Main.Example):
class Draw(Example):
"""Polygon Drawing Sample
Draw a polygon outline with the mouse.
Key mappings:
Expand All @@ -29,7 +29,7 @@ def update(self, time_elapsed):
self.runner.keys[K_BACKSPACE] = False

def render(self):

if len(self.poly) > 1:
pygame.draw.lines(self.runner.screen, 0xff0000, True, self.poly.as_tuple_list())
elif self.poly.points:
Expand Down
12 changes: 6 additions & 6 deletions py2d/examples/FOV.py → examples/FOV.py
Expand Up @@ -3,9 +3,9 @@

from py2d.FOV import *
from py2d.Math import *
import py2d.examples.Main
from examples import Example, SCREEN_WIDTH, SCREEN_HEIGHT

class FOV(py2d.examples.Main.Example):
class FOV(Example):
"""Field of View Example
Move your mouse and watch how the obstacles affect the field of view
Expand All @@ -16,13 +16,13 @@ class FOV(py2d.examples.Main.Example):
def __init__(self, runner):
self.runner = runner
self.title = "Field of View"
center = Vector( py2d.examples.Main.SCREEN_WIDTH / 2, py2d.examples.Main.SCREEN_HEIGHT / 2)

center = Vector( SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)

## create circle of boxes around center
box = [Vector(-15, -2), Vector(15,-2), Vector(15,2), Vector(-15,2), Vector(-15,-2)]
self.obstructors = [ [ Transform.move( center.x, center.y ) * Transform.rotate(phi) * Transform.move(100,0) * p for p in box ] for phi in [i * 2.0 * math.pi / 16 for i in range(16)]]

#self.obstructors = [ [ Vector(100,100), Vector(220, 100), Vector(220,120), Vector(100,120), Vector(100,100) ] ]

self.eye = center
Expand Down Expand Up @@ -52,7 +52,7 @@ def render(self):

pygame.draw.ellipse(self.runner.screen, 0x000000, pygame.Rect(self.eye.x - 2, self.eye.y - 2, 4,4))

def mouse_move(self, pos, rel, buttons):
def mouse_move(self, pos, rel, buttons):
# add 0.01 to avoid colinearity
self.eye = Vector(pos[0]+0.01, pos[1]+0.01)

Expand Down
37 changes: 19 additions & 18 deletions py2d/examples/Logo.py → examples/Logo.py
@@ -1,10 +1,11 @@
import pygame

from examples import Example, SCREEN_WIDTH, SCREEN_HEIGHT
from pygame.locals import *

from py2d.Math import *
import py2d.examples.Main

class Logo(py2d.examples.Main.Example):
class Logo(Example):
"""Logo Sample
You should see a bouncing, rotating Py2D logo that is made up of polygons. Spiffy!
Expand All @@ -28,31 +29,31 @@ def __init__(self, runner):
self.colors = [0xFF0000, 0xFFCC00, 0x00FF00, 0xFFFF00, 0xFF00FF, 0x00FFFF]

def update(self, time_elapsed):
self.time = (self.time + time_elapsed)

self.time = (self.time + time_elapsed)
scale = abs( math.sin(self.time * 0.001 ) * 8 ) + 1
rot = math.sin(self.time * 0.0003) * (math.pi / 6)

# first transform logo to be centered at 0,0. then rotate, scale, finally position at center of screen
self.transform = Transform.move(py2d.examples.Main.SCREEN_WIDTH / 2, py2d.examples.Main.SCREEN_HEIGHT / 2) * Transform.scale(scale,scale) * Transform.rotate(rot) * Transform.move(-self.w/2, -self.h/2)
self.transform = Transform.move(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) * Transform.scale(scale,scale) * Transform.rotate(rot) * Transform.move(-self.w/2, -self.h/2)

def render(self):
for polys, color in zip(PY2D_LOGO, self.colors):
for poly in polys:
points = [(p.x, p.y) for p in [ self.transform * v for v in poly.points ]]
pygame.draw.lines(self.runner.screen, color, True, points)

class Opaque(py2d.examples.Main.Example):
class Opaque(Example):
"""Logo (Convex decomposition) sample
You should see a bouncing, rotating Py2D logo that is made up of polygons. Spiffy!
This demo also shows you how to use Transform objects to modify your poly points plus how to decompose concave polygons with holes into convex parts.
Key mappings:
F: Toggle polygon fill
"""

def __init__(self, runner):
Expand All @@ -75,14 +76,14 @@ def __init__(self, runner):
self.fill = False

def update(self, time_elapsed):
self.time = (self.time + time_elapsed)

self.time = (self.time + time_elapsed)
scale = abs( math.sin(self.time * 0.001 ) * 8 ) + 1
rot = math.sin(self.time * 0.0003) * (math.pi / 6)

# first transform logo to be centered at 0,0. then rotate, scale, finally position at center of screen
self.transform = Transform.move(py2d.examples.Main.SCREEN_WIDTH / 2, py2d.examples.Main.SCREEN_HEIGHT / 2) * Transform.scale(scale,scale) * Transform.rotate(rot) * Transform.move(-self.w/2, -self.h/2)
self.transform = Transform.move(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2) * Transform.scale(scale,scale) * Transform.rotate(rot) * Transform.move(-self.w/2, -self.h/2)

if self.runner.keys[K_f]:
self.runner.keys[K_f] = False
self.fill = not self.fill
Expand All @@ -94,19 +95,19 @@ def render(self):
pygame.draw.polygon(self.runner.screen, color, points, 0 if self.fill else 1)

PY2D_LOGO = [

[ # P
Polygon.from_pointlist([Vector(0.002860, 27.997820),Vector(0.002860, -0.002180),Vector(10.842860, -0.002180),Vector(13.272860, 1.517820),Vector(15.122860, 3.677820),Vector(16.322860, 6.367820),Vector(16.722870, 9.477820),Vector(16.312850, 12.637820),Vector(15.082850, 15.557820),Vector(13.052850, 17.947820),Vector(10.242850, 19.517820),Vector(5.402850, 19.517820),Vector(5.402850, 27.997820)]),
Polygon.from_pointlist([Vector(5.402850, 4.797820),Vector(5.402850, 14.797820),Vector(8.962850, 14.797820),Vector(10.612850, 12.577820),Vector(11.162860, 9.517820),Vector(10.562860, 6.717820),Vector(9.202860, 4.797820),Vector(5.402860, 4.797820)])
],
Polygon.from_pointlist([Vector(0.002860, 27.997820),Vector(0.002860, -0.002180),Vector(10.842860, -0.002180),Vector(13.272860, 1.517820),Vector(15.122860, 3.677820),Vector(16.322860, 6.367820),Vector(16.722870, 9.477820),Vector(16.312850, 12.637820),Vector(15.082850, 15.557820),Vector(13.052850, 17.947820),Vector(10.242850, 19.517820),Vector(5.402850, 19.517820),Vector(5.402850, 27.997820)]),
Polygon.from_pointlist([Vector(5.402850, 4.797820),Vector(5.402850, 14.797820),Vector(8.962850, 14.797820),Vector(10.612850, 12.577820),Vector(11.162860, 9.517820),Vector(10.562860, 6.717820),Vector(9.202860, 4.797820),Vector(5.402860, 4.797820)])
],
[ # y
Polygon.from_pointlist([Vector(18.972850, 16.757820),Vector(18.972850, 7.557820),Vector(24.292850, 7.557820),Vector(24.292850, 16.797820),Vector(24.642850, 19.987820),Vector(25.692850, 22.597820),Vector(26.872850, 22.597820),Vector(28.052850, 22.597820),Vector(29.042840, 20.007820),Vector(29.372850, 16.797820),Vector(29.372850, 7.557820),Vector(34.692850, 7.557820),Vector(34.692850, 16.477820),Vector(33.892850, 21.757820),Vector(31.372850, 26.917820),Vector(28.692850, 31.277820),Vector(27.172850, 33.117820),Vector(25.092850, 33.997820),Vector(18.772850, 33.997820),Vector(18.772850, 29.797820),Vector(23.092850, 29.797820),Vector(24.492850, 28.957820),Vector(25.652850, 27.237820),Vector(25.892850, 26.797820),Vector(23.132850, 26.797820),Vector(20.092850, 22.597820),Vector(18.972850, 16.757820)])
],
[ # 2
Polygon.from_pointlist([Vector(37.002850, 23.997820),Vector(44.962850, 5.077820),Vector(43.978940, 4.797820),Vector(37.842840, 4.797820),Vector(37.842840, -0.002180),Vector(46.482840, -0.002180),Vector(48.871540, 1.657820),Vector(49.949240, 4.319720),Vector(48.162840, 11.117820),Vector(42.762840, 23.357820),Vector(49.762840, 23.357820),Vector(49.762840, 27.997820),Vector(37.002840, 27.997820)])
] ,
[ # D
Polygon.from_pointlist([Vector(52.812840, 27.997820),Vector(52.812840, -0.002180),Vector(64.412840, -0.002180),Vector(68.672840, 5.937820),Vector(70.092840, 13.677820),Vector(68.762840, 21.557820),Vector(64.772840, 27.997820)]),
Polygon.from_pointlist([Vector(58.252850, 23.357820),Vector(61.852850, 23.357820),Vector(63.612850, 19.277820),Vector(64.292850, 13.957820),Vector(63.702840, 8.527820),Vector(61.932840, 4.797820),Vector(58.252840, 4.797820),Vector(58.252840, 23.357820)])
Polygon.from_pointlist([Vector(52.812840, 27.997820),Vector(52.812840, -0.002180),Vector(64.412840, -0.002180),Vector(68.672840, 5.937820),Vector(70.092840, 13.677820),Vector(68.762840, 21.557820),Vector(64.772840, 27.997820)]),
Polygon.from_pointlist([Vector(58.252850, 23.357820),Vector(61.852850, 23.357820),Vector(63.612850, 19.277820),Vector(64.292850, 13.957820),Vector(63.702840, 8.527820),Vector(61.932840, 4.797820),Vector(58.252840, 4.797820),Vector(58.252840, 23.357820)])
]
]
58 changes: 29 additions & 29 deletions py2d/examples/Math.py → examples/Math.py
Expand Up @@ -2,21 +2,21 @@
from pygame.locals import *

from py2d.Math import *
import py2d.examples.Main
from examples import Example

class Decompose(py2d.examples.Main.Example):
class Decompose(Example):
"""Convex Decomposition Sample
Draw a polygon and holes and observe its convex decomposition.
The currently active polygon is colored white. You can switch active polygons with the number keys 0-9.
The polygons are numbered as follows:
0 The Main Polygon (color: green)
1-9 Holes in the Main polygon (color: red)
The result of the decomposition will be shown in yellow.
Key mappings:
0-9: Switch active polygon
Expand All @@ -33,7 +33,7 @@ def __init__(self, runner):

self.polys = [Polygon() for i in range(10)]
self.active_poly = 0

self.decomp = []

self.debug = False
Expand All @@ -44,9 +44,9 @@ def __init__(self, runner):

def update(self, time_elapsed):
if self.runner.keys[K_BACKSPACE]:

self.runner.keys[K_BACKSPACE] = False

if self.polys[self.active_poly].points: del(self.polys[self.active_poly].points[-1])


Expand All @@ -57,7 +57,7 @@ def update(self, time_elapsed):
if self.runner.keys[key]:
self.runner.keys[key] = False
self.active_poly = i

if self.runner.keys[K_d]:
self.runner.keys[K_d] = False
self.debug = not self.debug
Expand All @@ -67,10 +67,10 @@ def update(self, time_elapsed):
self.fill = not self.fill

def render(self):


self.draw_poly(self.polys[0], 0x00ff00, False)

for h in self.polys[1:]:
self.draw_poly(h, 0xff0000, False)

Expand All @@ -85,8 +85,8 @@ def draw_poly(self, poly, color, fill):
if len(poly) > 1:
if fill and len(poly) > 2:
pygame.draw.polygon(self.runner.screen, color, poly.as_tuple_list())


pygame.draw.lines(self.runner.screen, color, True, poly.as_tuple_list())
elif poly.points:
pygame.draw.circle(self.runner.screen, color, poly.points[0].as_tuple(), 2)
Expand All @@ -103,7 +103,7 @@ def mouse_down(self, pos, button):
def update_decomp(self):
self.debug_points = []
if len(self.polys[0]) > 2:

holes = [h for h in self.polys[1:] if len(h) > 2]

def debug_point(p,c,t):
Expand All @@ -114,9 +114,9 @@ def debug_point(p,c,t):
self.decomp = []


class Offset(py2d.examples.Main.Example):
class Offset(Example):
"""Polygon Offset Sample
Draw a polygon outline with the mouse. Py2D will calculate offset polygons.
Key mappings:
Expand All @@ -136,7 +136,7 @@ def __init__(self, runner):
self.runner = runner
self.title = "Polygon Offset"
self.poly = Polygon()

self.update_offset()

self.amount = 10
Expand Down Expand Up @@ -185,7 +185,7 @@ def update(self, time_elapsed):
self.update_offset()

def render(self):

for p in self.grow:
self.draw_poly(p, 0x00ff00)

Expand Down Expand Up @@ -218,7 +218,7 @@ def mouse_down(self, pos, button):
def update_offset(self):
self.debug_points = []


def debug_point(color):
return lambda p, c, t: self.debug_points.append((p,color,t))

Expand All @@ -230,16 +230,16 @@ def debug_point(color):
self.grow = []


class Boolean(py2d.examples.Main.Example):
class Boolean(Example):
"""Boolean Operations sample
Draw polygons A and B and observe their intersections, unions and differences.
The currently active polygon is colored white. You can switch active polygons with the SPACE BAR.
If not active, the polygon A will be colored red. Polygon B will be colored green.
The result of the boolean operation will be shown in yellow.
Key mappings:
SPACE BAR: Toggle active polygon
Expand Down Expand Up @@ -275,9 +275,9 @@ def __init__(self, runner):

def update(self, time_elapsed):
if self.runner.keys[K_BACKSPACE]:

self.runner.keys[K_BACKSPACE] = False

if self.active_poly:
if self.poly_a.points: del(self.poly_a.points[-1])
else:
Expand Down Expand Up @@ -309,10 +309,10 @@ def update(self, time_elapsed):
self.fill = not self.fill

def render(self):

a_color = 0xffffff if self.active_poly else 0xff0000
b_color = 0xffffff if not self.active_poly else 0x00ff00

self.draw_poly(self.poly_a, a_color)
self.draw_poly(self.poly_b, b_color)

Expand All @@ -324,8 +324,8 @@ def draw_poly(self, poly, color):
if len(poly) > 1:
if self.fill and len(poly) > 2:
pygame.draw.polygon(self.runner.screen, color, poly.as_tuple_list())


pygame.draw.lines(self.runner.screen, color, True, poly.as_tuple_list())
elif poly.points:
pygame.draw.circle(self.runner.screen, color, poly.points[0].as_tuple(), 2)
Expand Down

0 comments on commit 1114b75

Please sign in to comment.