Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtilemap.py
More file actions
Latest commit
209 lines (175 loc) · 7.87 KB
/
Copy pathtilemap.py
File metadata and controls
209 lines (175 loc) · 7.87 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
importpygame
importpytmx
fromcharacterimportExplosion
classTiledMap:
def__init__(self, filename):
tm=pytmx.load_pygame(filename, pixelalpha=True)
self.width=14*64# tilesize = 64×64, mapsize = 14×14 tiles
self.height=14*64
self.tmxdata=tm
defrender(self, surface):
ti=self.tmxdata.get_tile_image_by_gid
forlayerinself.tmxdata.visible_layers:
ifisinstance(layer, pytmx.TiledTileLayer):
forx, y, gid, inlayer:
tile=ti(gid)
iftile:
surface.blit(tile, (x*self.tmxdata.tilewidth,
y*self.tmxdata.tileheight))
defdraw(self):
temp_surface=pygame.Surface((self.width, self.height))
self.render(temp_surface)
returntemp_surface
classCamera:
def__init__(self, width, height):
self.tracking=pygame.Rect(0, 0, width, height)
self.width=width
self.height=height
self.show=pygame.Surface((width, height))
self.x=0
self.y=0
# track the movement of players to scroll map
defupdate(self, player_list):
iflen(player_list) ==1 :
self.x=-1*player_list[0].x-player_list[0].width+480//2
self.y=-1*player_list[0].y-player_list[0].height+480//2
eliflen(player_list) ==2 :
self.x= (-1*(player_list[0].x)-1*(player_list[1].x))//2-player_list[0].width+480//2
self.y= (-1*(player_list[0].y)-1*(player_list[1].y))//2-player_list[0].height+480//2
# limit scrolling to map size
self.x=min(0, self.x) # left
self.y=min(0, self.y) # top
self.x=max(-415, self.x) # right -(480 - 64)
self.y=max(-415, self.y) # bottom
self.tracking=pygame.Rect(self.x, self.y, self.width, self.height)
classObstacle:
def__init__(self, x, y, width, height):
self.rect=pygame.Rect(x, y, width, height)
self.hit_rect=self.rect
self.x=x
self.y=y
self.rect.x=x
self.rect.y=y
self.hitbox= [x, y, width, height]
defcheckPlayerStoneCollision(self,player, obstacle_list):
player_hb_0=player.hitbox[0]
player_hb_1=player.hitbox[1]
player_hb_2=player.hitbox[2]
player_hb_3=player.hitbox[3]
forobstacleinobstacle_list:
obstacle_hb_0=obstacle.hitbox[0]
obstacle_hb_1=obstacle.hitbox[1]
obstacle_hb_2=obstacle.hitbox[2]
obstacle_hb_3=obstacle.hitbox[3]
ifplayer_hb_1+player_hb_3>obstacle_hb_1andplayer_hb_1<obstacle_hb_1+obstacle_hb_3andplayer_hb_0+player_hb_2>obstacle_hb_0andplayer_hb_0<obstacle_hb_0+obstacle_hb_2:
ifplayer.left==True:
player.x+=player.velx
elifplayer.right==True:
player.x-=player.velx
elifplayer.up==True:
player.y-=player.vely
elifplayer.down==True:
player.y+=player.vely
defcheckEnemyStoneCollision(self, enemy, obstacle_list):
enemy_hb_0=enemy.hitbox[0]
enemy_hb_1=enemy.hitbox[1]
enemy_hb_2=enemy.hitbox[2]
enemy_hb_3=enemy.hitbox[3]
forobstacleinobstacle_list:
obstacle_hb_0=obstacle.hitbox[0]
obstacle_hb_1=obstacle.hitbox[1]
obstacle_hb_2=obstacle.hitbox[2]
obstacle_hb_3=obstacle.hitbox[3]
ifenemy_hb_1+enemy_hb_3>obstacle_hb_1andenemy_hb_1<obstacle_hb_1+obstacle_hb_3andenemy_hb_0+enemy_hb_2>obstacle_hb_0andenemy_hb_0<obstacle_hb_0+obstacle_hb_2:
ifenemy.left==True:
enemy.x+=2
# enemy.y += 1 # bounce-off effect, or the enemy will stuck there
elifenemy.right==True:
enemy.x-=2
# enemy.y -= 1
elifenemy.up==True:
enemy.y-=2
# enemy.x -= 1
elifenemy.down==True:
enemy.y+=2
# enemy.x += 1
classProof:
def__init__(self, x, y, width, height):
self.rect=pygame.Rect(x, y, width, height)
self.hit_rect=self.rect
self.x=x
self.y=y
self.rect.x=x
self.rect.y=y
self.hitbox= [x, y, width, height]
defcheckBulletProofCollision(self,bullet, proof_list):
forproofinproof_list:
proof_hb_0=proof.hitbox[0]
proof_hb_1=proof.hitbox[1]
proof_hb_2=proof.hitbox[2]
proof_hb_3=proof.hitbox[3]
ifbullet.x>proof_hb_0andbullet.x<proof_hb_0+proof_hb_2andbullet.y>proof_hb_1andbullet.y<proof_hb_1+proof_hb_3:
returnTrue
classBlock:
def__init__(self, x, y, width, height):
self.rect=pygame.Rect(x, y, width, height)
self.hit_rect=self.rect
self.x=x
self.y=y
self.rect.x=x
self.rect.y=y
self.hitbox= [x, y, width, height]
defdraw(self, win):
win.blit(pygame.image.load('./materials/block.png'), (self.x, self.y))
defcheckPlayerBlockCollision(self,player, block_list):
player_hb_0=player.hitbox[0]
player_hb_1=player.hitbox[1]
player_hb_2=player.hitbox[2]
player_hb_3=player.hitbox[3]
forblockinblock_list:
block_hb_0=block.hitbox[0]
block_hb_1=block.hitbox[1]
block_hb_2=block.hitbox[2]
block_hb_3=block.hitbox[3]
ifplayer_hb_1+player_hb_3>block_hb_1andplayer_hb_1<block_hb_1+block_hb_3andplayer_hb_0+player_hb_2>block_hb_0andplayer_hb_0<block_hb_0+block_hb_2:
ifplayer.left==True:
player.x+=player.velx
elifplayer.right==True:
player.x-=player.velx
elifplayer.up==True:
player.y+=player.vely
elifplayer.down==True:
player.y-=player.vely
defcheckEnemyBlockCollision(self, enemy, block_list):
enemy_hb_0=enemy.hitbox[0]
enemy_hb_1=enemy.hitbox[1]
enemy_hb_2=enemy.hitbox[2]
enemy_hb_3=enemy.hitbox[3]
forblockinblock_list:
block_hb_0=block.hitbox[0]
block_hb_1=block.hitbox[1]
block_hb_2=block.hitbox[2]
block_hb_3=block.hitbox[3]
ifenemy_hb_1+enemy_hb_3>block_hb_1andenemy_hb_1<block_hb_1+block_hb_3andenemy_hb_0+enemy_hb_2>block_hb_0andenemy_hb_0<block_hb_0+block_hb_2:
ifenemy.left==True:
enemy.x+=1
elifenemy.right==True:
enemy.x-=1
elifenemy.up==True:
enemy.y+=2
elifenemy.down==True:
enemy.y-=2
defcheckBlockBulletCollision(self, bullet, block_list, player):
forblockinblock_list:
block_hb_0=block.hitbox[0]
block_hb_1=block.hitbox[1]
block_hb_2=block.hitbox[2]
block_hb_3=block.hitbox[3]
ifbullet.x>block_hb_0andbullet.x<block_hb_0+block_hb_2andbullet.y>block_hb_1andbullet.y<block_hb_1+block_hb_3:
x=block_hb_0
y=block_hb_1
exp=Explosion(x,y)
player.explode_list.append(exp)
player.bullet_list.pop(player.bullet_list.index(bullet))
block_list.pop(block_list.index(block))
break