-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
102 lines (77 loc) · 3.25 KB
/
draw.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
import pygame
import sys
pygame.init()
width, height = 800, 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("DTT")
rows = 20
columns = 20
active = True
boxes = []
drawn = False
counter = 0
def draw_grid(screen, width, height):
"""
This draws the grid lines
First For loop is for the vertical lines
second For loop is for the horizontal lines
"""
for x in range(0, width, 40):
pygame.draw.line(screen, (0, 0, 0), (x, 0), (x, height))
for y in range(0, height, 40):
pygame.draw.line(screen, (0, 0, 0), (0, y), (width, y))
def draw_boxes(screen, width, height):
"""
This draws the individual boxes
It also appends the individual boxes from top to bottom going towards the right
"""
for i in range(rows):
for j in range(columns):
pygame.draw.rect(screen, (0, 255, 0), (i * 40, j * 40, 40, 40))
boxes.append((screen, [0, 255, 0], (i * 40, j * 40, 40, 40)))
return boxes
def hand_draw(screen, width, height):
"""
This changes the color of the clicked box to red
"""
for box in boxes:
if box[1] == [255, 0, 0]:
pygame.draw.rect(screen, (255, 0, 0), box[2])
while active:
cursor = pygame.mouse.get_pos()
if drawn == False: # Stops the green boxes from drawing again
draw_boxes(screen, width, height)
drawn = True
hand_draw(screen, width, height)
draw_grid(screen, width, height)
for event in pygame.event.get():
if event.type == pygame.QUIT:
active = False
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
char_hold = {}
if event.key == pygame.K_SPACE:
with open("grid.txt", "w", encoding="utf-8") as f:
for i, box in enumerate(boxes):
if i < 20: # This is basically creating the outline of the dictionary
if box[1] == [255, 0, 0]: # If the box is red, then we will add it to the dictionary as an " ▢ "
char_hold[i] = "▢"
else:
char_hold[i] = "▥" # If the box is green, then we will add it to the dictionary as an " ▥ "
else:
if counter == 20: # This is resetting the counter so then values over 20 can be added to the dictionary
counter = 0
if box[1] == [255, 0, 0]: # This is now adding to the dictionary instead of setting it
char_hold[counter] += "▢"
else:
char_hold[counter] += "▥"
counter += 1
for i in char_hold:
f.write((char_hold[i]) + "\n") # prints each key on separate lines.
if event.type == pygame.MOUSEBUTTONDOWN:
for box in boxes:
if pygame.Rect(box[2]).collidepoint(cursor): # box[2] are the coordinates of the box
box[1][0] = 255 # turns the green box into red
box[1][1] = 0 # Removes the green value
pygame.display.update()