-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclick_the_square.py
62 lines (56 loc) · 1.93 KB
/
click_the_square.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
import pyxel
import random
class App:
def __init__(self):
pyxel.init(160, 120)
self.x = 0
self.y = 0
self.color = 1
self.square_size = 8
self.x_direction = 1
pyxel.mouse(True)
self.won = False
self.dx = random.randint(1, 4)
self.dy = random.randint(1, 4)
self.wins = 0
pyxel.run(self.update, self.draw)
def reset(self):
self.won = False
self.x = 0
self.y = 0
self.color = 1
def update(self):
self.x = self.x + self.dx * self.x_direction #% pyxel.width
if self.x + self.square_size > pyxel.width:
self.x_direction = -1
elif self.x < 0:
self.x_direction = 1
self.y = (self.y + self.dy) % pyxel.height
if pyxel.frame_count % 30 == 0:
self.color = (self.color + 1) % 15
if pyxel.btnp(pyxel.KEY_SPACE):
self.x_direction = self.x_direction * -1
# print(pyxel.mouse_x, pyxel.mouse_y)
if pyxel.btnp(pyxel.MOUSE_LEFT_BUTTON):
mouse_x = pyxel.mouse_x
mouse_y = pyxel.mouse_y
if self.x <= mouse_x <= self.x + self.square_size \
and self.y <= mouse_y <= self.y + self.square_size:
self.won = True
self.wins += 1
if pyxel.btnp(pyxel.KEY_R):
self.reset()
if pyxel.frame_count % random.randint(40, 120):
self.dx = random.randint(1, 4)
self.dy = random.randint(1, 4)
self.square_size = random.randint(5, 15)
# self.x_direction = self.x_direction * -1
def draw(self):
if self.won:
pyxel.cls(0)
pyxel.text(100, 100, 'you won!', 11)
else:
pyxel.cls(0)
pyxel.rect(self.x, self.y, self.square_size, self.square_size, self.color)
pyxel.text(pyxel.width - 50, 10, f'score: {self.wins}', 11)
App()