-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
s269_pygame.py
131 lines (105 loc) · 3.86 KB
/
s269_pygame.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME = "s268" # 20180923
import pygame # install Pygame https://www.pygame.org
from random import choice
from random import randint
gliphs = [lambda x, y, s: rect(x, y, s, s),
lambda x, y, s: ellipse(x, y, s, s),
lambda x, y, s: triangle(x - s, y, x - s, y + s, x, y + s),
lambda x, y, s: triangle(x + s, y, x + s, y - s, x, y - s),
]
def setup():
size(1200, 600)
def draw():
background(0)
grid(300, 300., 4, 150, ensamble, 5) # ensamble of 5 , on grid also order=5
grid(width/4.*3, height/2., 4, 150, ensamble, 5) # ensamble of 5 , on grid also order=5
noLoop()
def ensamble(ex, ey, order):
for _ in range(order):
order, spacing, side = randint(3, 5), 14, 7
x, y = randint(-5, 4) * side, randint(-5, 4) * side
grid(ex+x, ey+y, order, spacing, choice(gliphs), side)
def grid(x, y, order, spacing, function, *args):
xo, yo = (x - order * spacing / 2, y - order * spacing / 2)
for i in range(order):
gx = spacing/2 + i * spacing
for j in range(order):
gy = spacing/2 + j * spacing
function(xo + gx, yo + gy, *args)
# Now stuff to make it easier to port from Processing to Pygame
def triangle(x0, y0, x1, y1, x2, y2):
# This draws a triangle using the polygon command
pygame.draw.polygon(screen, current_fill,
[[x0, y0], [x1, y1], [x2, y2]], 0)
if _stroke_weight:
pygame.draw.polygon(screen, current_stroke,
[[x0, y0], [x1, y1], [x2, y2]], _stroke_weight)
def rect(x, y, w, h):
pygame.draw.rect(screen, current_fill, [x, y, w, h], 0)
if _stroke_weight:
pygame.draw.rect(screen, current_stroke, [x, y, w, h], _stroke_weight)
def ellipse(x, y, w, h):
pygame.draw.ellipse(screen, current_fill, [x, y, w, h], 0)
if _stroke_weight:
pygame.draw.ellipse(screen, current_stroke, [x, y, w, h], _stroke_weight)
def background(r, g=None, b=None):
if g and b:
screen.fill((r, g, b))
else:
screen.fill((r, r, r))
def fill(r, g=None, b=None):
global current_fill
if g and b:
current_fill = (r, g, b)
else:
current_fill = (r, r, r)
def noStroke():
global _stroke_weight
_stroke_weight = 0
def size(w, h):
global width, height
width, height = w, h
def noLoop():
global _pause
_pause = True
def loop():
global _pause
_pause = False
def run():
global width, height, screen, current_fill, current_stroke, _stroke_weight, _pause
# Initialize the game engine
pygame.init()
current_fill = (255, 255, 255)
current_stroke = (0, 0, 0)
_stroke_weight = 1
width, height = 100, 100
_done = False
_pause = False
setup()
# Set the height and width of the screen
screen = pygame.display.set_mode([width, height])
pygame.display.set_caption(SKETCH_NAME)
clock = pygame.time.Clock()
draw()
# Loop until the user clicks the close button.
while not _done:
# This limits the while loop to a max of 10 times per second.
# Leave this out and we will use all CPU we can.
clock.tick(10)
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
_done = True # Flag that we are _done so we exit this loop
if event.type == pygame.KEYDOWN:
_pause = not _pause
# All drawing code happens after the for loop and but
# inside the main while _done==False loop.
# Draw on screen
if not _pause:
draw()
# Go ahead and update the screen with what we've drawn.
# This MUST happen after all the other drawing commands.
pygame.display.flip()
# Be IDLE friendly
pygame.quit()
run()