-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_mouse_input.py
107 lines (74 loc) · 2.67 KB
/
12_mouse_input.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
105
106
107
"""
Registering mouse clicks and drawing objects.
Left Mouse Button: Single "splat".
Right Mouse Button: Hold it for multi-splat.
Kris Pritchard / @krp
"""
import pyxel
import random
class Splat:
"""Create a splat on the screen."""
def __init__(self, x, y, color):
# Create the splat at position (x, y)
self.x = x
self.y = y
# Give the splat a random radius
self.radius = random.randint(1, 10)
self.color = color
def draw(self):
# Draw our splat
pyxel.circ(self.x, self.y, self.radius, self.color)
# Slowly decrease our radius every 30 frames.
if pyxel.frame_count % 30 == 0 and self.radius > 0:
self.radius -= 1
class Crosshair:
"""Draws a crosshair at the mouse position."""
def __init__(self):
self.x = 0
self.y = 0
self.color = 11
def update(self, x, y):
self.x = x
self.y = y
def draw(self):
# East line
pyxel.line(self.x + 5, self.y, self.x + 10, self.y, self.color)
# West line
pyxel.line(self.x - 5, self.y, self.x - 10, self.y, self.color)
# North line
pyxel.line(self.x, self.y - 5, self.x, self.y - 10, self.color)
# South line
pyxel.line(self.x, self.y + 5, self.x, self.y + 10, self.color)
class App:
def __init__(self):
pyxel.init(256, 256, fps=60)
# Hide the mouse cursor
pyxel.mouse(False)
# Create a list of "splats"
self.splats = []
# Create the crosshair.
self.crosshair = Crosshair()
# Run the update/draw callback functions
pyxel.run(self.update, self.draw)
def update(self):
# Update the crosshair position
self.crosshair.update(pyxel.mouse_x, pyxel.mouse_y)
# Create a copy of our splat list but remove any small splats.
self.splats = [splat for splat in self.splats if splat.radius >= 1]
# If the user left-clicked, create a splat at the mouse cursor position.
if pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
self.splats.append(Splat(pyxel.mouse_x, pyxel.mouse_y, 8))
# If the user right-clicked, create a bunch of splats at the mouse cursor position.
if pyxel.btn(pyxel.MOUSE_BUTTON_RIGHT):
self.splats.append(Splat(pyxel.mouse_x, pyxel.mouse_y, 6))
def draw(self):
pyxel.cls(0)
# Draw the crosshair
self.crosshair.draw()
# Display our splats.
for splat in self.splats:
splat.draw()
# Display the help text.
pyxel.text(40, 120, f'Try both left click and hold right mouse button.', 7)
if __name__ == "__main__":
App()