pygameP 0.0.1__tar.gz

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.
pygamep-0.0.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 pygameP contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
pygamep-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,405 @@
1
+ Metadata-Version: 2.4
2
+ Name: pygameP
3
+ Version: 0.0.1
4
+ Summary: Pygame Plus - 高级游戏开发框架:着色器、物理引擎、场景管理
5
+ Author: pygameP contributors
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/pygameP/pygameP
8
+ Keywords: pygame,game,shader,physics,scene
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.8
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Games/Entertainment
19
+ Classifier: Topic :: Multimedia :: Graphics
20
+ Requires-Python: >=3.8
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: pygame>=2.0.0
24
+ Requires-Dist: PyOpenGL>=3.1.0
25
+ Provides-Extra: dev
26
+ Requires-Dist: pytest>=7.0; extra == "dev"
27
+ Dynamic: license-file
28
+
29
+ # pygameP
30
+
31
+ **Pygame Plus** — 一个面向 Pygame 的高级扩展框架,提供 GLSL 着色器特效、性能优化工具、JSON 场景管理、简单物理引擎和扩展输入设备支持。
32
+
33
+ ## 安装
34
+
35
+ ```bash
36
+ pip install pygameP
37
+ ```
38
+
39
+ 依赖项会自动安装:
40
+ - `pygame >= 2.0.0`
41
+ - `PyOpenGL >= 3.1.0`(用于着色器支持)
42
+
43
+ ## 核心特性
44
+
45
+ | 模块 | 功能 |
46
+ |------|------|
47
+ | **shaders** | GLSL 着色器系统 + 内置特效(灰度/模糊/反转/亮度/脉冲/波浪) |
48
+ | **performance** | 对象池、空间哈希、帧率监控、批处理渲染 |
49
+ | **scene** | `.pgstage` JSON 场景文件,支持多场景切换 |
50
+ | **physics** | 简单刚体物理(重力、碰撞、射线检测) |
51
+ | **input** | 键盘、鼠标、手柄、多点触摸统一输入管理 |
52
+
53
+ ---
54
+
55
+ ## 快速开始
56
+
57
+ ### 1. GLSL 着色器特效
58
+
59
+ 支持从 `.glsl` 文件加载或从代码字符串创建着色器。可作用于整个屏幕或单个精灵。
60
+
61
+ ```python
62
+ from pygameP import Shader, ShaderEffect, BuiltInEffects
63
+
64
+ # 使用内置特效
65
+ grayscale = BuiltInEffects.grayscale()
66
+ blur = BuiltInEffects.blur(radius=3.0)
67
+ invert = BuiltInEffects.invert()
68
+ pulse = BuiltInEffects.pulse(speed=2.0)
69
+ wave = BuiltInEffects.wave(amplitude=0.05, frequency=10.0)
70
+
71
+ # 应用到整个屏幕
72
+ grayscale.apply(screen)
73
+
74
+ # 应用到单个精灵
75
+ pulse.apply(my_sprite)
76
+
77
+ # 自定义 GLSL 代码
78
+ my_shader = Shader(fragment_source="""
79
+ #version 330 core
80
+ in vec2 v_texcoord;
81
+ out vec4 frag_color;
82
+ uniform sampler2D u_texture;
83
+ uniform float u_time;
84
+ void main() {
85
+ vec4 c = texture(u_texture, v_texcoord);
86
+ frag_color = vec4(c.r, c.g * abs(sin(u_time)), c.b, c.a);
87
+ }
88
+ """)
89
+
90
+ # 从文件加载
91
+ custom = Shader(
92
+ vertex_file="assets/shaders/default.vert",
93
+ fragment_file="assets/shaders/plasma.frag"
94
+ )
95
+ ```
96
+
97
+ ### 2. 性能优化
98
+
99
+ ```python
100
+ from pygameP import ObjectPool, SpatialHash, FPSMonitor, BatchRenderer
101
+
102
+ # 对象池 - 减少子弹/粒子的创建开销
103
+ bullet_pool = ObjectPool(lambda: Bullet(), initial_size=200)
104
+ bullet = bullet_pool.acquire() # 从池中获取
105
+ # ... 使用后 ...
106
+ bullet_pool.release(bullet) # 归还
107
+
108
+ # 空间哈希 - 快速碰撞检测
109
+ spatial = SpatialHash(cell_size=64)
110
+ spatial.insert(enemy, enemy.rect)
111
+ nearby = spatial.query(player.rect) # 只检测附近对象
112
+
113
+ # FPS 监控 + 自适应质量
114
+ fps_monitor = FPSMonitor(target_fps=60)
115
+ while running:
116
+ dt = fps_monitor.tick()
117
+ if fps_monitor.should_reduce_quality():
118
+ reduce_particle_count()
119
+
120
+ # 批处理渲染 - 合并绘制调用
121
+ batch = BatchRenderer(screen)
122
+ for sprite in sprites:
123
+ batch.blit(sprite.image, sprite.rect.topleft)
124
+ batch.render() # 一次性执行
125
+ ```
126
+
127
+ ### 3. 场景管理(.pgstage)
128
+
129
+ `.pgstage` 是 pygameP 的自定义场景文件格式,基于 JSON。
130
+
131
+ > **场景编辑器下载:** `.pgstage` 文件由 **[Objector Coder](https://tomlct2015.github.io/Objector-Coder/#download)** 可视化编辑器创建和管理,pygameP 库只负责加载和运行场景。
132
+ > 下载地址:https://tomlct2015.github.io/Objector-Coder/#download
133
+
134
+ ```json
135
+ {
136
+ "name": "ExampleLevel",
137
+ "width": 1600,
138
+ "height": 1200,
139
+ "background_color": [20, 25, 40],
140
+ "camera_x": 0,
141
+ "camera_y": 0,
142
+ "properties": {
143
+ "music": "assets/music/level1.ogg",
144
+ "difficulty": "normal"
145
+ },
146
+ "entities": [
147
+ {
148
+ "type": "Player",
149
+ "x": 100,
150
+ "y": 800,
151
+ "layer": 5,
152
+ "hp": 100,
153
+ "tag": "player"
154
+ },
155
+ {
156
+ "type": "Enemy",
157
+ "x": 500,
158
+ "y": 800,
159
+ "layer": 4,
160
+ "hp": 50,
161
+ "ai": "patrol"
162
+ }
163
+ ]
164
+ }
165
+ ```
166
+
167
+ Python 中使用:
168
+
169
+ ```python
170
+ from pygameP import Scene, SceneManager, SceneEntity
171
+
172
+ # 创建场景
173
+ scene = Scene("Level1", width=1600, height=1200)
174
+ scene.background_color = (20, 25, 40)
175
+ scene.add_entity(SceneEntity(100, 800, hp=100, tag="player"))
176
+
177
+ # 保存为 .pgstage 文件
178
+ scene.save("levels/level1.pgstage")
179
+
180
+ # 加载场景
181
+ loaded = Scene.load("levels/level1.pgstage")
182
+
183
+ # 多场景管理(带过渡效果)
184
+ manager = SceneManager()
185
+ manager.add_scene("menu", Scene("Menu"))
186
+ manager.add_scene("game", Scene.load("levels/level1.pgstage"))
187
+ manager.switch_to("game", transition_duration=0.5)
188
+
189
+ # 游戏主循环
190
+ while running:
191
+ dt = clock.tick(60) / 1000.0
192
+ manager.update(dt)
193
+ manager.draw(screen)
194
+ ```
195
+
196
+ ### 4. 简单物理引擎
197
+
198
+ ```python
199
+ from pygameP import RigidBody, PhysicsWorld, BoxCollider, CircleCollider
200
+
201
+ # 创建物理世界
202
+ world = PhysicsWorld(gravity=980.0)
203
+
204
+ # 动态刚体(受重力影响)
205
+ player = RigidBody(x=100, y=0, mass=1.0, collider=BoxCollider(32, 64))
206
+ player.restitution = 0.3 # 弹性
207
+ player.friction = 0.2 # 摩擦
208
+ world.add_body(player)
209
+
210
+ # 静态刚体(地面、平台)
211
+ ground = RigidBody(x=0, y=600, mass=0, collider=BoxCollider(800, 40))
212
+ world.add_body(ground)
213
+
214
+ # 施加力或冲量
215
+ player.apply_force(500, 0) # 持续力
216
+ player.apply_impulse(0, -300) # 跳跃(瞬间)
217
+
218
+ # 每帧更新
219
+ world.update(dt)
220
+
221
+ # 碰撞回调
222
+ def on_hit(body1, body2):
223
+ print("碰撞!")
224
+ world.on_collision = on_hit
225
+
226
+ # 射线检测
227
+ hit = world.raycast((100, 300), (1, 0), max_distance=500)
228
+ if hit:
229
+ body, distance, point = hit
230
+ print(f"命中 {body},距离 {distance}")
231
+ ```
232
+
233
+ ### 5. 扩展输入设备
234
+
235
+ ```python
236
+ from pygameP import InputManager
237
+
238
+ input_mgr = InputManager()
239
+
240
+ # 映射动作到多种输入
241
+ input_mgr.map_action("jump", "keyboard", pygame.K_SPACE)
242
+ input_mgr.map_action("jump", "gamepad", (0, 0)) # 手柄 0 的按钮 0
243
+
244
+ input_mgr.map_action("shoot", "mouse", 1) # 鼠标左键
245
+ input_mgr.map_action("move_right", "gamepad", (0, "axis_0"))
246
+
247
+ # 游戏主循环
248
+ while running:
249
+ events = pygame.event.get()
250
+ input_mgr.update(events)
251
+
252
+ if input_mgr.is_action_just_pressed("jump"):
253
+ player.jump()
254
+ if input_mgr.is_action_pressed("move_right"):
255
+ player.move_right()
256
+
257
+ # 模拟输入(手柄摇杆)
258
+ move_x = input_mgr.get_action_value("move_right")
259
+
260
+ # 直接访问手柄
261
+ pad = input_mgr.get_gamepad(0)
262
+ if pad:
263
+ left_stick = pad.get_left_stick()
264
+ pad.rumble(0.5, 0.5, 100) # 震动
265
+
266
+ # 触摸输入
267
+ if input_mgr.touch.is_touching():
268
+ pos = input_mgr.touch.get_touch_position()
269
+ ```
270
+
271
+ 支持的输入设备:
272
+ - **键盘** — 按键按下/释放/刚刚按下
273
+ - **鼠标** — 位置、相对运动、滚轮、按钮状态
274
+ - **手柄** — 按钮、摇杆轴、方向帽、震动(rumble)
275
+ - **触摸** — 多点触控、触摸开始/结束/移动
276
+
277
+ ---
278
+
279
+ ## API 参考
280
+
281
+ ### `Shader`
282
+ - `Shader(vertex_source, fragment_source, vertex_file, fragment_file)` — 创建着色器
283
+ - `shader.use()` / `shader.stop()` — 启用/停用
284
+ - `shader.set_uniform(name, value)` — 设置 uniform(支持 float/int/vec2/vec3/vec4)
285
+ - `shader.apply_to_surface(surface)` — 应用于整个屏幕
286
+ - `shader.apply_to_sprite(sprite)` — 应用于单个精灵
287
+
288
+ ### `BuiltInEffects`(内置特效)
289
+ - `grayscale()` — 灰度
290
+ - `blur(radius)` — 模糊
291
+ - `invert()` — 颜色反转
292
+ - `brightness(amount)` — 亮度
293
+ - `pulse(speed)` — 脉冲发光
294
+ - `wave(amplitude, frequency)` — 波浪扭曲
295
+
296
+ ### `ObjectPool`
297
+ - `acquire()` — 获取对象
298
+ - `release(obj)` — 归还对象
299
+ - `release_all()` — 归还所有
300
+ - `resize(n)` — 调整池大小
301
+
302
+ ### `SpatialHash`
303
+ - `insert(obj, rect)` — 插入对象
304
+ - `query(rect)` — 查询区域内对象
305
+ - `query_nearby(obj, rect)` — 查询附近(排除自己)
306
+ - `remove(obj)` / `clear()`
307
+
308
+ ### `Scene` / `SceneManager`
309
+ - `scene.save(path)` — 保存为 `.pgstage`
310
+ - `Scene.load(path)` — 加载 `.pgstage`
311
+ - `manager.switch_to(name, transition_duration)` — 切换场景
312
+
313
+ ### `PhysicsWorld`
314
+ - `add_body(body)` / `remove_body(body)`
315
+ - `update(dt)` — 更新物理(自动处理重力和碰撞)
316
+ - `raycast(start, direction, max_distance)` — 射线检测
317
+
318
+ ### `InputManager`
319
+ - `update(events)` — 更新所有输入设备
320
+ - `is_action_pressed(action)` — 动作是否被按住
321
+ - `is_action_just_pressed(action)` — 动作是否刚刚按下
322
+ - `get_action_value(action)` — 获取模拟值(-1 到 1)
323
+ - `map_action(action, device, binding)` — 映射动作
324
+
325
+ ---
326
+
327
+ ## 完整示例
328
+
329
+ ```python
330
+ import pygame
331
+ from pygameP import (
332
+ SceneManager, Scene, SceneEntity,
333
+ PhysicsWorld, RigidBody, BoxCollider,
334
+ InputManager, FPSMonitor, BuiltInEffects
335
+ )
336
+
337
+ pygame.init()
338
+ screen = pygame.display.set_mode((800, 600))
339
+ clock = pygame.time.Clock()
340
+
341
+ # 场景
342
+ manager = SceneManager()
343
+ game = Scene("Game", 800, 600)
344
+ game.background_color = (30, 40, 60)
345
+ manager.add_scene("game", game)
346
+ manager.switch_to("game")
347
+
348
+ # 物理
349
+ world = PhysicsWorld(gravity=980.0)
350
+ player = RigidBody(400, 100, mass=1.0, collider=BoxCollider(32, 32))
351
+ world.add_body(player)
352
+ ground = RigidBody(0, 550, mass=0, collider=BoxCollider(800, 50))
353
+ world.add_body(ground)
354
+
355
+ # 输入
356
+ input_mgr = InputManager()
357
+ input_mgr.map_action("jump", "keyboard", pygame.K_SPACE)
358
+ input_mgr.map_action("left", "keyboard", pygame.K_LEFT)
359
+ input_mgr.map_action("right", "keyboard", pygame.K_RIGHT)
360
+
361
+ # 性能监控
362
+ fps = FPSMonitor(target_fps=60)
363
+
364
+ # 着色器
365
+ effect = BuiltInEffects.pulse(speed=2.0)
366
+
367
+ running = True
368
+ while running:
369
+ dt = clock.tick(60) / 1000.0
370
+ fps.tick()
371
+
372
+ events = pygame.event.get()
373
+ input_mgr.update(events)
374
+
375
+ for event in events:
376
+ if event.type == pygame.QUIT:
377
+ running = False
378
+
379
+ # 输入
380
+ if input_mgr.is_action_just_pressed("jump") and player.grounded:
381
+ player.apply_impulse(0, -500)
382
+ if input_mgr.is_action_pressed("left"):
383
+ player.apply_force(-300, 0)
384
+ if input_mgr.is_action_pressed("right"):
385
+ player.apply_force(300, 0)
386
+
387
+ # 更新
388
+ world.update(dt)
389
+ manager.update(dt)
390
+
391
+ # 绘制
392
+ manager.draw(screen)
393
+ pygame.draw.rect(screen, (255, 200, 100),
394
+ (player.x, player.y, 32, 32))
395
+ pygame.draw.rect(screen, (100, 100, 100),
396
+ (ground.x, ground.y, 800, 50))
397
+
398
+ pygame.display.flip()
399
+
400
+ pygame.quit()
401
+ ```
402
+
403
+ ## 许可证
404
+
405
+ MIT License