forked from kitao/pyxel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_click_game.py
executable file
·131 lines (96 loc) · 3.98 KB
/
06_click_game.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import math
import random
import pyxel
SCREEN_WIDTH = 256
SCREEN_HEIGHT = 256
BUBBLE_MAX_SPEED = 1.8
BUBBLE_INITIAL_COUNT = 50
BUBBLE_EXPLODE_COUNT = 11
class Vec2:
def __init__(self, x, y):
self.x = x
self.y = y
class Bubble:
def __init__(self):
self.r = random.uniform(3, 10)
self.pos = Vec2(
random.uniform(self.r, SCREEN_WIDTH - self.r),
random.uniform(self.r, SCREEN_HEIGHT - self.r),
)
self.vel = Vec2(
random.uniform(-BUBBLE_MAX_SPEED, BUBBLE_MAX_SPEED),
random.uniform(-BUBBLE_MAX_SPEED, BUBBLE_MAX_SPEED),
)
self.color = random.randint(1, 15)
def update(self):
self.pos.x += self.vel.x
self.pos.y += self.vel.y
if self.vel.x < 0 and self.pos.x < self.r:
self.vel.x *= -1
if self.vel.x > 0 and self.pos.x > SCREEN_WIDTH - self.r:
self.vel.x *= -1
if self.vel.y < 0 and self.pos.y < self.r:
self.vel.y *= -1
if self.vel.y > 0 and self.pos.y > SCREEN_HEIGHT - self.r:
self.vel.y *= -1
class App:
def __init__(self):
pyxel.init(SCREEN_WIDTH, SCREEN_HEIGHT, caption="Pyxel Bubbles")
pyxel.mouse(True)
self.is_exploded = False
self.bubbles = [Bubble() for _ in range(BUBBLE_INITIAL_COUNT)]
pyxel.run(self.update, self.draw)
def update(self):
if pyxel.btnp(pyxel.KEY_Q):
pyxel.quit()
bubble_count = len(self.bubbles)
if pyxel.btnp(pyxel.MOUSE_LEFT_BUTTON):
for i in range(bubble_count):
bubble = self.bubbles[i]
dx = bubble.pos.x - pyxel.mouse_x
dy = bubble.pos.y - pyxel.mouse_y
if dx * dx + dy * dy < bubble.r * bubble.r:
self.is_exploded = True
new_r = math.sqrt(bubble.r * bubble.r / BUBBLE_EXPLODE_COUNT)
for j in range(BUBBLE_EXPLODE_COUNT):
angle = math.pi * 2 * j / BUBBLE_EXPLODE_COUNT
new_bubble = Bubble()
new_bubble.r = new_r
new_bubble.pos.x = bubble.pos.x + (bubble.r + new_r) * math.cos(
angle
)
new_bubble.pos.y = bubble.pos.y + (bubble.r + new_r) * math.sin(
angle
)
new_bubble.vel.x = math.cos(angle) * BUBBLE_MAX_SPEED
new_bubble.vel.y = math.sin(angle) * BUBBLE_MAX_SPEED
self.bubbles.append(new_bubble)
del self.bubbles[i]
break
for i in range(bubble_count - 1, -1, -1):
bi = self.bubbles[i]
bi.update()
for j in range(i - 1, -1, -1):
bj = self.bubbles[j]
dx = bi.pos.x - bj.pos.x
dy = bi.pos.y - bj.pos.y
total_r = bi.r + bj.r
if dx * dx + dy * dy < total_r * total_r:
new_bubble = Bubble()
new_bubble.r = math.sqrt(bi.r * bi.r + bj.r * bj.r)
new_bubble.pos.x = (bi.pos.x * bi.r + bj.pos.x * bj.r) / total_r
new_bubble.pos.y = (bi.pos.y * bi.r + bj.pos.y * bj.r) / total_r
new_bubble.vel.x = (bi.vel.x * bi.r + bj.vel.x * bj.r) / total_r
new_bubble.vel.y = (bi.vel.y * bi.r + bj.vel.y * bj.r) / total_r
self.bubbles.append(new_bubble)
del self.bubbles[i]
del self.bubbles[j]
bubble_count -= 1
break
def draw(self):
pyxel.cls(0)
for bubble in self.bubbles:
pyxel.circ(bubble.pos.x, bubble.pos.y, bubble.r, bubble.color)
if not self.is_exploded and pyxel.frame_count % 20 < 10:
pyxel.text(96, 50, "CLICK ON BUBBLE", pyxel.frame_count % 15 + 1)
App()