XMWAI 0.4.4__py3-none-any.whl → 0.4.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of XMWAI might be problematic. Click here for more details.
- XMWAI/snake_core.py +12 -49
- {xmwai-0.4.4.dist-info → xmwai-0.4.5.dist-info}/METADATA +1 -1
- {xmwai-0.4.4.dist-info → xmwai-0.4.5.dist-info}/RECORD +6 -6
- {xmwai-0.4.4.dist-info → xmwai-0.4.5.dist-info}/WHEEL +0 -0
- {xmwai-0.4.4.dist-info → xmwai-0.4.5.dist-info}/licenses/LICENSE.txt +0 -0
- {xmwai-0.4.4.dist-info → xmwai-0.4.5.dist-info}/top_level.txt +0 -0
XMWAI/snake_core.py
CHANGED
|
@@ -8,15 +8,14 @@ from PIL import ImageFont, ImageDraw, Image
|
|
|
8
8
|
import os
|
|
9
9
|
import time
|
|
10
10
|
import importlib.resources as pkg_resources
|
|
11
|
-
|
|
11
|
+
|
|
12
|
+
# 引入包内资源
|
|
13
|
+
import XMWAI.assets as assets
|
|
12
14
|
|
|
13
15
|
|
|
14
|
-
# ---------- 工具函数:从 assets 读取资源 ----------
|
|
15
16
|
def get_asset_path(filename: str) -> str:
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
"""
|
|
19
|
-
with pkg_resources.path(XMWAI.assets, filename) as p:
|
|
17
|
+
"""获取包内资源的路径"""
|
|
18
|
+
with pkg_resources.path(assets, filename) as p:
|
|
20
19
|
return str(p)
|
|
21
20
|
|
|
22
21
|
|
|
@@ -32,6 +31,9 @@ class SnakeGame:
|
|
|
32
31
|
self._foodScores = foodScores if foodScores is not None else [3, 2, 1]
|
|
33
32
|
self.snakeLineWidth = snakeLineWidth
|
|
34
33
|
|
|
34
|
+
# 字体路径(默认取包内字体)
|
|
35
|
+
self.fontPath = fontPath or get_asset_path("微软雅黑.ttf")
|
|
36
|
+
|
|
35
37
|
# 默认资源路径
|
|
36
38
|
if foodPaths is None:
|
|
37
39
|
foodPaths = [get_asset_path("h.png"),
|
|
@@ -43,13 +45,10 @@ class SnakeGame:
|
|
|
43
45
|
obstaclePaths = [get_asset_path("g.png"),
|
|
44
46
|
get_asset_path("l.png"),
|
|
45
47
|
get_asset_path("m.png")]
|
|
46
|
-
if fontPath is None:
|
|
47
|
-
fontPath = get_asset_path("微软雅黑.ttf")
|
|
48
48
|
|
|
49
49
|
self.foodPaths = foodPaths
|
|
50
50
|
self.foodNames = foodNames
|
|
51
51
|
self.obstaclePaths = obstaclePaths
|
|
52
|
-
self.fontPath = fontPath
|
|
53
52
|
|
|
54
53
|
self.cap = None
|
|
55
54
|
self.detector = None
|
|
@@ -155,39 +154,6 @@ class SnakeGame:
|
|
|
155
154
|
cv2.imshow("AI Snake", self.img)
|
|
156
155
|
cv2.waitKey(1)
|
|
157
156
|
|
|
158
|
-
def hand(self):
|
|
159
|
-
if self.cap is None:
|
|
160
|
-
print("请先调用 open_window()")
|
|
161
|
-
return
|
|
162
|
-
if self.detector is None:
|
|
163
|
-
self.detector = HandDetector(detectionCon=0.8, maxHands=1)
|
|
164
|
-
while True:
|
|
165
|
-
success, self.img = self.cap.read()
|
|
166
|
-
if not success:
|
|
167
|
-
break
|
|
168
|
-
self.img = cv2.flip(self.img, 1)
|
|
169
|
-
hands, self.img = self.detector.findHands(self.img, flipType=False)
|
|
170
|
-
cv2.imshow("AI Snake", self.img)
|
|
171
|
-
key = cv2.waitKey(1) & 0xFF
|
|
172
|
-
if hands or key == ord('q'):
|
|
173
|
-
break
|
|
174
|
-
|
|
175
|
-
def display(self):
|
|
176
|
-
if self.img is None:
|
|
177
|
-
self._render_frame(show_food=False)
|
|
178
|
-
self.foodManager.randomFoodLocation(self.obstacleManager)
|
|
179
|
-
self._render_frame(show_food=True)
|
|
180
|
-
self.overlayTexts = [
|
|
181
|
-
(f'玩家分数:{self.snake.score}', (50, 50), 30, (255, 0, 255)),
|
|
182
|
-
(f'倒计时:{self.timer} 秒', (50, 120), 30, (255, 0, 255))
|
|
183
|
-
]
|
|
184
|
-
img_copy = self.img.copy()
|
|
185
|
-
for txt, pos, size, color in self.overlayTexts:
|
|
186
|
-
img_copy = self._putChineseText(img_copy, txt, pos, size, color)
|
|
187
|
-
cv2.imshow("AI Snake", img_copy)
|
|
188
|
-
cv2.waitKey(1)
|
|
189
|
-
|
|
190
|
-
# ---------------- 重置游戏 ----------------
|
|
191
157
|
def reset_game(self):
|
|
192
158
|
self.snake.reset()
|
|
193
159
|
self.snake.headSize = self._snakeHeadSize
|
|
@@ -199,7 +165,6 @@ class SnakeGame:
|
|
|
199
165
|
if self.cap is None or not self.cap.isOpened():
|
|
200
166
|
self.open_window()
|
|
201
167
|
|
|
202
|
-
# ---------------- 游戏结束 ----------------
|
|
203
168
|
def gameover(self, path=None, size=(100, 100)):
|
|
204
169
|
if path is None:
|
|
205
170
|
path = get_asset_path("1.png")
|
|
@@ -229,7 +194,6 @@ class SnakeGame:
|
|
|
229
194
|
cv2.destroyAllWindows()
|
|
230
195
|
break
|
|
231
196
|
|
|
232
|
-
# ---------------- 游戏主循环 ----------------
|
|
233
197
|
def start(self):
|
|
234
198
|
if self.cap is None or not self.cap.isOpened():
|
|
235
199
|
self.open_window()
|
|
@@ -238,7 +202,6 @@ class SnakeGame:
|
|
|
238
202
|
|
|
239
203
|
while True:
|
|
240
204
|
if self.snake.gameOver or self.timer == 0:
|
|
241
|
-
# 游戏结束,进入 gameover 界面
|
|
242
205
|
self.gameover()
|
|
243
206
|
break
|
|
244
207
|
|
|
@@ -259,7 +222,7 @@ class SnakeGame:
|
|
|
259
222
|
cv2.imshow("AI Snake", img_copy)
|
|
260
223
|
|
|
261
224
|
key = cv2.waitKey(1)
|
|
262
|
-
if key == ord('r'):
|
|
225
|
+
if key == ord('r'):
|
|
263
226
|
self.reset_game()
|
|
264
227
|
elif key == ord('q'):
|
|
265
228
|
if self.cap is not None:
|
|
@@ -367,7 +330,7 @@ class SnakeGame:
|
|
|
367
330
|
max_attempts = 100
|
|
368
331
|
for _ in range(max_attempts):
|
|
369
332
|
self.foodPoint = random.randint(
|
|
370
|
-
50,
|
|
333
|
+
50, 670), random.randint(50, 670)
|
|
371
334
|
self.foodIndex = random.randint(0, len(self.foodImages)-1)
|
|
372
335
|
self.hFood, self.wFood, _ = self.foodImages[self.foodIndex].shape
|
|
373
336
|
if obstacleManager:
|
|
@@ -402,8 +365,8 @@ class SnakeGame:
|
|
|
402
365
|
self.obstacles.clear()
|
|
403
366
|
for img in self.obstacleImages:
|
|
404
367
|
h, w, _ = img.shape
|
|
405
|
-
x = random.randint(150,
|
|
406
|
-
y = random.randint(50,
|
|
368
|
+
x = random.randint(150, 600)
|
|
369
|
+
y = random.randint(50, 600)
|
|
407
370
|
dx = random.choice([-5, 5])
|
|
408
371
|
dy = random.choice([-5, 5])
|
|
409
372
|
self.obstacles.append([x, y, w, h, dx, dy, img])
|
|
@@ -3,7 +3,7 @@ XMWAI/bomb_core.py,sha256=h2ZPH3SuoG2L_XOf1dcK8p3lhw5QzhneWl2yMLj1RiU,1819
|
|
|
3
3
|
XMWAI/core.py,sha256=rOXj7FnewSdnzBcFLjpnBtrOTCsvMfiycIcdPDagxho,10012
|
|
4
4
|
XMWAI/idiom_core.py,sha256=yU-VHhqqoutVm6GVikcjL3m9yuB1hUsFBpPYvwY4n5g,1689
|
|
5
5
|
XMWAI/magic_core.py,sha256=Ms4b12PJ8rjsmceg1VUqWCWx2ebvdhLp4sIF6K_Vaok,3491
|
|
6
|
-
XMWAI/snake_core.py,sha256=
|
|
6
|
+
XMWAI/snake_core.py,sha256=oKcjGwEm2MB7Q8gfRo80NRqul20LGiEIe3--4k1-E74,14782
|
|
7
7
|
XMWAI/trial_class.py,sha256=fPsl7BZvhzch2FOIG4azr999kjtoly53Acm3LqL8f98,9724
|
|
8
8
|
XMWAI/web_core.py,sha256=7awPg1kYW3lYrbgylqJvUF3g050bn6H21PgmQ7Kv1wA,10927
|
|
9
9
|
XMWAI/assets/1.png,sha256=eEuKH_M_q3tc_O2bYnuLOsRP-NlJHIbNg0pgrKXEEjw,139720
|
|
@@ -117,8 +117,8 @@ XMWAI/static/images/tomato.png,sha256=FEOEAOdUhW_BDFgTpxOkYc0I5Iu29_gtHb3RIPEej0
|
|
|
117
117
|
XMWAI/templates/burger.html,sha256=vDnxpSW8phetyScySsalScZnFKl3LNpy5lJjKxGXgbI,3320
|
|
118
118
|
XMWAI/templates/nutrition_pie.html,sha256=yJVXI28i-UfvF0xOXGSNLMb8oCJNhh2J3zoRDr5_7DM,5567
|
|
119
119
|
XMWAI/templates/创意菜谱.html,sha256=RcDgH58QLyUJ9A59wobu3wvchGBY1snVsXcZQZam5M0,4805
|
|
120
|
-
xmwai-0.4.
|
|
121
|
-
xmwai-0.4.
|
|
122
|
-
xmwai-0.4.
|
|
123
|
-
xmwai-0.4.
|
|
124
|
-
xmwai-0.4.
|
|
120
|
+
xmwai-0.4.5.dist-info/licenses/LICENSE.txt,sha256=bcaIQMrIhdQ3O-PoZlexjmW6h-wLGvHxh5Oksl4ohtc,1066
|
|
121
|
+
xmwai-0.4.5.dist-info/METADATA,sha256=PBWm8ONw169ZRDJr5VSBWpGwb-XGsjGMrjzwaZs_umw,1227
|
|
122
|
+
xmwai-0.4.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
123
|
+
xmwai-0.4.5.dist-info/top_level.txt,sha256=yvGcDI-sggK5jqd9wz0saipZvk3oIE3hNGHlqUjxf0c,6
|
|
124
|
+
xmwai-0.4.5.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|