-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstargon.py
64 lines (52 loc) · 2.16 KB
/
stargon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Implementation of Stargon Model, and a related Manager"""
import math
import pygame
import base
class Stargon(base.Model):
"""Represents the concave form of an 'order' sided polygon"""
def __init__(self, radius, order):
# Reference parameters
self.radius = radius
self.order = order
def visual(self):
# Create a panel 2x the radius, and give it a central origin and standard orientation
drawing = base.Panel(
pygame.Surface((self.radius*2, self.radius*2)),
origin=base.Point(self.radius, self.radius), orientation=(1, -1)
)
# Draw each line of the stargon, reaching from a point to the second next point
for i in range(self.order):
drawing.draw_line(
base.radial(self.radius, i*2*math.pi/self.order + math.pi/2),
base.radial(self.radius, (i+2)*2*math.pi/self.order + math.pi/2)
)
# Return the panel
return drawing
class StargonManager(base.Manager):
"""Manager Class for the Stargon Model"""
def update(self, events, keyboard):
"""UP/DOWN arrowkeys change order, Holding LEFT/RIGHT change size"""
# Search for UP/DOWN
for event in events:
# Check KEYDOWN events
if event.type == pygame.KEYDOWN:
# Match against Up and Down constants
if event.key == pygame.K_UP:
self.model.order += 1
elif event.key == pygame.K_DOWN:
self.model.order -= 1
# Search for held LEFT/RIGHT keys
if keyboard[pygame.K_LEFT]:
self.model.radius -= 1
if keyboard[pygame.K_RIGHT]:
self.model.radius += 1
# Refresh the display
image = self.model.visual()
# Find blit coordinate to place in the middle
# Reference both rects to manipulate them
screenRect = self.screen.get_rect()
imageRect = image.surface.get_rect()
# Center imageRect on screenRect
imageRect.center = screenRect.center
# Draw the image, which should be centered
image.display(self.screen, (imageRect.x, imageRect.y))