07-植物大战僵尸

Catalogue
  1. 1. 参考资料

效果图:

图1

代码:

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
210
211
212
213
214
215
216
217
218
219
220
221
import pygame
import random
from pygame.locals import *

WIDTH = 1000
HEIGHT = 500

#豌豆类
class Peas:
def __init__(self):
# 豌豆图片
self.image = pygame.image.load("./res/peas.gif")
self.image_rect = self.image.get_rect()
self.image_rect.top = 200
self.is_move_up = False
self.is_move_down = False
#是否发射炮弹
self.is_shout = False

#显示
def display(self):
# 豌豆显示在页面上
screen.blit(self.image, self.image_rect)


#向上移动
def move_up(self):
if self.image_rect.top > 10:
self.image_rect.move_ip(0,-6)

#豌豆是碰上僵尸
for zombie in Zombie.zombie_list:
if(self.image_rect.colliderect(zombie.image_rect)):
pygame.quit();
exit()

#向下移动
def move_down(self):
if self.image_rect.bottom < 500:
self.image_rect.move_ip(0,6)

#豌豆碰上僵尸
for zombie in Zombie.zombie_list:
if self.image_rect.colliderect(zombie.image_rect):
pygame.quit()
exit()
#发射炮弹
def shout_bullit(self):
#创建炮弹对象
bullet = Bullet(self)
#将炮弹对象添加到集合中
Bullet.bullet_list.append(bullet)

#键盘控制
def key_control():
# 对事件的处理
for event in pygame.event.get():
#对事件的处理
if event.type == QUIT:
quit()
elif event.type == KEYDOWN: #键盘按下
#向上键
if event.key == K_UP:
#向上移动
peas.is_move_up = True
#向下键
elif event.key == K_DOWN:
#向下移动
peas.is_move_down = True
elif event.key == K_SPACE: #空格键
peas.is_shout = True

elif event.type == KEYUP:
#键盘弹起
if event.key == K_UP: #向上键
peas.is_move_up = False
elif event.key == K_DOWN: #向下键
peas.is_move_down = False
elif event.key == K_SPACE: #空格键
peas.is_shout = False


#炮弹对象
class Bullet:
# 所有的炮弹对象
bullet_list = []

# 创建炮弹的间隔时间
interval = 0

def __init__(self,peas):
self.image = pygame.image.load("./res/bullet.gif")
self.image_rect = self.image.get_rect()
self.image_rect.top = peas.image_rect.top
self.image_rect.left = peas.image_rect.right

#炮弹显示
def display(self):
# 炮弹显示在页面上
screen.blit(self.image, self.image_rect)

#炮弹移动
def move(self):
self.image_rect.move_ip(10,0)
#如果炮弹超出范围,则删除
if self.image_rect.left > WIDTH:
Bullet.bullet_list.remove(self)

for zombie in Zombie.zombie_list:
if self.image_rect.colliderect(zombie.image_rect):
Bullet.bullet_list.remove(self)
Zombie.zombie_list.remove(zombie)


#僵尸类
class Zombie:
#存放僵尸对象
zombie_list = []

#存放僵尸创建的时间
interval = 0

def __init__(self):
self.image = pygame.image.load("./res/zombie.gif")
self.image = pygame.transform.scale(self.image, (70, 70))
self.image_rect = self.image.get_rect()
self.image_rect.top = random.randint(10, HEIGHT - 70)
self.image_rect.left = WIDTH

#显示
def display(self):
# 僵尸显示在页面上
screen.blit(self.image, self.image_rect)

# 僵尸移动
def move(self):
self.image_rect.move_ip(-2, 0)
# 如果炮弹超出范围,则删除
if self.image_rect.left < -70:
Zombie.zombie_list.remove(self)

#循环僵尸,查看僵尸是否碰上炮弹
for bullet in Bullet.bullet_list:
#发生碰撞
if self.image_rect.colliderect(bullet.image_rect):
Zombie.zombie_list.remove(self)
Bullet.bullet_list.remove(bullet)

#查看僵尸是否碰上豌豆
if self.image_rect.colliderect(peas.image_rect):
pygame.quit();
exit()

if __name__ == '__main__':
#显示窗体
screen = pygame.display.set_mode(size=(WIDTH,HEIGHT))
pygame.display.set_mode()
#设置背景图片
background_image = pygame.image.load("./res/background.png")

#改变图片大小
scale_background_image = pygame.transform.scale(background_image,(WIDTH,HEIGHT))
#获取图片的位置和大小
scale_background_image_rect = scale_background_image.get_rect()

#定义两个状态,用于判断是否向下,向上移动
peas_is_move_up = False
peas_is_move_down = False

#创建一个时钟,优化运行的速度效果
clock = pygame.time.Clock()

# 创建豌豆对象
peas = Peas()

while True:
#设置背景颜色为黑色
screen.fill((0,0,0))

# 设置窗体的背景图片
screen.blit(scale_background_image, scale_background_image_rect)

# 显示豌豆
peas.display()

# 键盘控制
key_control()

if peas.is_move_up:
peas.move_up()

if peas.is_move_down:
peas.move_down()

#发射炮弹
Bullet.interval += 1
if peas.is_shout and Bullet.interval >= 20:
Bullet.interval = 0
peas.shout_bullit()

#显示所有炮弹
for bullet in Bullet.bullet_list:
bullet.display()
bullet.move()

Zombie.interval += 1
#创建僵尸
if Zombie.interval > 20:
Zombie.interval = 0
zombie = Zombie()
Zombie.zombie_list.append(zombie)

#显示所有僵尸
for zombie in Zombie.zombie_list:
zombie.display()
zombie.move()

#帧频率
clock.tick(60)

pygame.display.update()

参考资料