forked from LeoEkky/OpenAI-Codex-Code-Generation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython and P5.js.py
More file actions
180 lines (133 loc) · 3.55 KB
/
Copy pathPython and P5.js.py
File metadata and controls
180 lines (133 loc) · 3.55 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Python and P5.js
from _typeshed import HasFileno
import math
import random
from typing import runtime_checkable
from pyp5js import *
# sketch's global variables
# the number of balls
n = 10
# the balls' positions
x = []
y = []
# the balls' velocities
vx = []
vy = []
# the balls' colors
colors = []
# the balls' sizes
sizes = []
width, height = 600, 600
def noLoop():
"""
Stops the animation loop.
"""
global loop
loop = False
print("noLoop")
def createCanvas():
"""
Creates a P5.js canvas.
"""
global canvas, sketch
canvas = document.createElement('canvas')
canvas.setAttribute('width', '600')
canvas.setAttribute('height', '600')
sketch = document.getElementById('sketch')
sketch.appendChild(canvas)
sketch.style.display = 'block'
noLoop()
print("hello")
def color(r, g, b):
"""
Returns the P5.js color format from RGB.
"""
return f'rgba({r}, {g}, {b}, 1.0)'
def setup():
"""
Setup function.
"""
global x, y, vx, vy, colors, sizes
createCanvas(600, 600)
# create the balls' positions
for i in range(n):
x.append(random.randint(0, width))
y.append(random.randint(0, height))
# create the balls' velocities
for i in range(n):
vx.append(random.randint(-2, 2))
vy.append(random.randint(-2, 2))
# create the balls' colors
for i in range(n):
colors.append(color(random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255)))
# create the balls' sizes
for i in range(n):
sizes.append(random.randint(10, 50))
def image(img, x, y):
"""
Displays an image at a given position.
"""
global canvas, sketch
# get the context
ctx = canvas.getContext('2d')
# draw the image
ctx.drawImage(img, x, y)
def createGraphics():
"""
Creates a P5.js canvas.
"""
global canvas, sketch
window = html.window
document = window.document
canvas = document.createElement('canvas')
canvas.setAttribute('width', '600')
canvas.setAttribute('height', '600')
sketch = document.getElementById('sketch')
sketch.appendChild(canvas)
sketch.style.display = 'block'
noLoop()
def ellipse(x, y, w, h, c):
"""
Draws an ellipse at a given position with a given size and color.
"""
global canvas, sketch
# get the context
ctx = canvas.getContext('2d')
# set the color
ctx.fillStyle = c
# draw the ellipse
ctx.beginPath()
ctx.ellipse(x, y, w / 2, h / 2, 0, 0, 2 * math.pi)
ctx.fill()
def background(r, g, b):
"""
Sets the background color.
"""
background = createGraphics(width, height)
background.beginDraw()
background.background(r, g, b)
background.endDraw()
image(background, 0, 0)
def draw():
"""
Draw function.
"""
global x, y, vx, vy, colors, sizes
background(255)
# move the balls
for i in range(n):
x[i] += vx[i]
y[i] += vy[i]
# check if the balls hit the walls
if x[i] > width or x[i] < 0:
vx[i] = -vx[i]
if y[i] > height or y[i] < 0:
vy[i] = -vy[i]
# draw the balls
ellipse(x[i], y[i], sizes[i], sizes[i], colors[i])
# start the sketch
if __name__ == '__main__':
createGraphics()
runtime_checkable()