-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.py
57 lines (49 loc) · 1.77 KB
/
cell.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
# Modules:
import pygame
from random import randint
from json import load
with open("preferences.json", "r") as preferences_json:
preferences = load(preferences_json)["cell"]
class Cell():
# Default variables for the objects:
pos = None
window = None
color = tuple(
rgb for rgb in preferences["color"].values()
)
radius = preferences["radius"]
def define_pos(self, actual_pos=None, how_far = 5):
'''
Arguments:
`actual_pos`: a tuple that contains two values
representing a position 2D (x, y) or None (by default);
`how_far`: how many pixel far from the initial
position can be retuned, (5 pixels by default).
Returns: other (x, y) position tuple, 0 to `how_far` far
from the initial (x, y) position, or a default value of
(300, 300) if the `actual_pos` was None.
'''
if isinstance(actual_pos, tuple):
x_actual = actual_pos[0]
y_actual = actual_pos[1]
x = randint(x_actual-how_far, x_actual+how_far)
y = randint(y_actual-how_far, y_actual+how_far)
xy = (x, y)
else:
xy = x, y = (300,300)
return(xy)
def draw(self, window):
'''
Given: a pygame.window() object.
Draw the cell in the screen with the attributes:
color, pos and radius defined above.
'''
if window is not None:
for _ in range(5):
self.pos = self.define_pos((self.pos))
cell = pygame.draw.circle(
window,
self.color,
self.pos,
self.radius
)