-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaint.py
104 lines (102 loc) · 2.9 KB
/
paint.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
#CC written by Joost Markerink (March 2020) http://joostmarkerink.nl
import pew
import time
pew.init()
screen = pew.Pix()
drawing = pew.Pix()
cursorx = 3
cursory = 4
prevkeys = pew.keys()
paint = 1
editing = True
willEdit = False
xbuttontime = 0
eventTime = 0
isClear = True
splash = [
0b11111111,
0b10000001,
0b10111101,
0b10100101,
0b10111101,
0b10100001,
0b10000001,
0b11111111
]
for y in range(8):
for x in range(8):
screen.pixel(x,y,(splash[y] >> (7 - x)) & 1)
pew.show(screen)
pew.tick(2.4)
def fill(sx,sy, fill_value):
orig_value = drawing.pixel(sx, sy)
stack = set(((sx, sy),))
if fill_value == orig_value:
return
while stack:
x, y = stack.pop()
if drawing.pixel(x, y) == orig_value:
drawing.pixel(x, y, fill_value)
if x > 0:
stack.add((x - 1, y))
if x < 7:
stack.add((x + 1, y))
if y > 0:
stack.add((x, y - 1))
if y < 7:
stack.add((x, y + 1))
while True:
keys = pew.keys()
tm = time.monotonic()
needUpdate=False
if keys != prevkeys:
needUpdate = True
eventTime = tm
if keys > 0 and editing == False:
willEdit = True
needUpdate=True
elif keys == 0 and willEdit:
editing = True
willEdit = False
elif editing:
if keys:
if keys & pew.K_LEFT and (prevkeys & pew.K_LEFT) == 0:
cursorx -= 1
if keys & pew.K_RIGHT and (prevkeys & pew.K_RIGHT) == 0:
cursorx += 1
if keys & pew.K_UP and (prevkeys & pew.K_UP) == 0:
cursory -= 1
if keys & pew.K_DOWN and (prevkeys & pew.K_DOWN) == 0:
cursory += 1
cx = cursorx % 8
cy = cursory % 8
if keys & pew.K_O:
if (prevkeys & pew.K_O) == 0:
if drawing.pixel(cx, cy) == 1:
paint = 0
else:
paint = 1
isClear = False
drawing.pixel(cx, cy, paint)
if keys & pew.K_X:
if (prevkeys & pew.K_X) == 0:
if drawing.pixel(cx, cy) == 0:
fill(cx, cy, 1)
isClear = False
else:
fill(cx, cy, 0)
elif keys == 0 and editing and isClear == False and (tm - eventTime) > 3:
isClear = True
for y in range(8):
for x in range(8):
if drawing.pixel(x,y) > 0:
isClear = False
break
editing = isClear
needUpdate = True
prevkeys = keys
if needUpdate:
screen.blit(drawing,0,0)
if editing or willEdit:
screen.pixel(cursorx % 8, cursory % 8, 3)
pew.show(screen)