-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
72 lines (59 loc) · 2.03 KB
/
example.py
File metadata and controls
72 lines (59 loc) · 2.03 KB
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
from data import *
# 初始化游戏
jie = Jie()
# 创建一个场景
my_chang = Chang(jie)
# 创建一个固定的方块
class FixedBlock(Role):
def __init__(self, chang):
super().__init__(chang)
self.name = "fixed_block"
self.flag = "block"
self.pos = Ve2(200, 200) # 方块的位置
# 添加渲染模块
self.add_model(FixedBlockRender(self))
# 固定方块的渲染模块
class FixedBlockRender(Render):
def __init__(self, role):
super().__init__(role)
# 加载一个简单的图像
self.image = pygame.Surface((50, 50))
self.image.fill((0, 255, 0)) # 绿色方块
# 创建一个角色
class MyRole(Role):
def __init__(self, chang):
super().__init__(chang)
self.name = "test_role"
self.flag = "test"
self.pos = Ve2(400, 300) # 角色初始位置
# 添加渲染模块
self.add_model(MyRender(self))
# 绑定摄像头
self.chang.camera.set_father(self)
def update(self):
"""更新角色位置,实现移动功能"""
super().update()
# 实现简单的移动逻辑
if self.chang.is_key_pressed(pygame.K_w): # 上
self.pos -= (0, 5)
if self.chang.is_key_pressed(pygame.K_s): # 下
self.pos += (0, 5)
if self.chang.is_key_pressed(pygame.K_a): # 左
self.pos -= (5, 0)
if self.chang.is_key_pressed(pygame.K_d): # 右
self.pos += (5, 0)
# 更新摄像头位置
self.chang.camera.pos = self.pos
# 创建一个渲染模块
class MyRender(Render):
def __init__(self, role):
super().__init__(role)
# 加载一个简单的图像
self.image = pygame.Surface((50, 50))
self.image.fill((255, 0, 0)) # 红色方块
# 启动游戏
FixedBlock(my_chang) # 添加固定方块
MyRole(my_chang)
my_chang.draw_order = ["test", "block", "camera"]
jie.change_chang(my_chang)
jie.start()