-
Notifications
You must be signed in to change notification settings - Fork 790
Expand file tree
/
Copy pathArray backed grids.py
More file actions
72 lines (57 loc) · 1.99 KB
/
Array backed grids.py
File metadata and controls
72 lines (57 loc) · 1.99 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
import arcade
width = 20
height = 20
margin = 5
ROW_COUNT = 10
COLUMN_COUNT = 10
SCREEN_WIDTH = width * COLUMN_COUNT + margin * (COLUMN_COUNT + 1)
SCREEN_HEIGHT = width * ROW_COUNT + margin * (ROW_COUNT + 1)
class MyGame(arcade.Window):
"""
Main application class.
"""
def __init__(self, width, height):
super().__init__(width, height)
# --- Create grid of numbers
# Create an empty list
self.grid = []
# Loop for each row
for row in range(ROW_COUNT):
# For each row, create a list that will
# represent an entire row
self.grid.append([])
# Loop for each column
for column in range(COLUMN_COUNT):
# Add a the number zero to the current row
self.grid[row].append(0)
arcade.set_background_color(arcade.color.BLACK)
def on_draw(self):
"""
Render the screen.
"""
arcade.start_render()
for row in range(ROW_COUNT):
for column in range(COLUMN_COUNT):
color = arcade.color.WHITE
if self.grid[row][column] == 1:
color = arcade.color.GREEN
x = (margin + width) * column + margin + width // 2
y = (margin + width) * row + margin + width // 2
arcade.draw_rectangle_filled(x, y, width, height, color)
def on_mouse_press(self, x, y, button, key_modifiers):
"""
Called when the user presses a mouse button.
"""
column = x // (width + margin)
row = y // (height + margin)
print(f"Click coordinates: ( {x}, {y}. Grid Coordinates: ({row}, {column})")
if row < ROW_COUNT and column < COLUMN_COUNT:
if self.grid[row][column] == 0:
self.grid[row][column] = 1
else:
self.grid[row][column] = 0
def main():
window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT)
arcade.run()
if __name__ == "__main__":
main()