|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
import random
import sys
WIDTH, HEIGHT = 1000, 600
WINDOW_RECT = pygame.rect.Rect(0, 0, WIDTH, HEIGHT)
TEXT_COLOR = (255, 255, 255)
class wu:
def __init__(self):
self.rect = pygame.rect.Rect(-10, -10, 1, 1)
class wu2:
def __init__(self, rect):
self.rect = rect
class kapai:
'''卡牌类'''
def __init__(self, name, yangguang):
self.name = name
self.yang = yangguang
self.img = pygame.image.load('imgs/KaPian.png')
self.img = pygame.transform.rotozoom(self.img, 0, 0.5)
self.font = pygame.font.SysFont('Arial', 12)
self.text = self.font.render(str(self.yang), True, (0, 0, 0))
self.images = {
'豌豆射手': pygame.image.load('imgs/zhiwu/wandousheshou/1.png'),
'向日葵': pygame.image.load('imgs/zhiwu/xiangrikui/1.png'),
'坚果墙': pygame.image.load('imgs/zhiwu/jianguoqiang/1.png')
}
self.rect = self.img.get_rect()
def show_kapian(self, x, y):
self.rect = self.img.get_rect()
self.image = self.images[self.name]
self.image = pygame.transform.rotozoom(self.image, 0, 0.5)
self.image_rect = self.image.get_rect()
self.image_rect.x = x
self.image_rect.y = y
self.rect.center = self.image_rect.center
self.image_rect.y -= 3
self.text_rect = self.text.get_rect()
self.text_rect.centerx = self.rect.centerx - 5
self.text_rect.centery = self.rect.centery + 27
Main.window.blit(self.img, self.rect)
Main.window.blit(self.image, self.image_rect)
Main.window.blit(self.text, self.text_rect)
return self.rect
def show_zhiwu(self, x, y):
self.image = self.images[self.name]
rect = self.image.get_rect()
rect.centerx = x
rect.centery = y
Main.window.blit(self.image, rect)
class zhiwu:
'''植物基类'''
def __init__(self):
pass
# 种植的方法
def zhongzhi(self):
pass
class WanDou_sheshou(zhiwu):
'''豌豆射手类'''
name = '豌豆射手'
yangguang = 100
imgs = [pygame.image.load('imgs/zhiwu/wandousheshou/' + str(i) + '.png') for i in range(1, 13 + 1)]
def __init__(self, lu):
self.xiabiao = 0
self.q = 1
self.rect = self.imgs[self.xiabiao].get_rect()
self.lu = lu
self.xue = 140
def show(self, x, y):
self.rect.x = x
self.rect.y = y
Main.window.blit(self.imgs[self.xiabiao], self.rect)
self.q += 1
if self.q % 5 == 0:
self.xiabiao += 1
if self.xiabiao == len(self.imgs):
self.xiabiao = 0
self.q = 1
class xiangrikui(zhiwu):
'''向日葵类'''
name = '向日葵'
yangguang = 50
imgs = [pygame.image.load('imgs/zhiwu/xiangrikui/' + str(i) + '.png') for i in range(1, 18 + 1)]
def __init__(self, lu):
self.xiabiao = 0
self.q = 1
self.rect = self.imgs[self.xiabiao].get_rect()
self.lu = lu
self.xue = 140
def show(self, x, y):
self.rect.x = x
self.rect.y = y
Main.window.blit(self.imgs[self.xiabiao], self.rect)
self.q += 1
if self.q % 5 == 0:
self.xiabiao += 1
if self.xiabiao == len(self.imgs):
self.xiabiao = 0
self.q = 1
class jianguo(zhiwu):
'''坚果墙类'''
name = '坚果墙'
yangguang = 50
imgs = [pygame.image.load('imgs/zhiwu/jianguoqiang/' + str(i) + '.png') for i in range(1, 16 + 1)]
def __init__(self, lu):
self.xiabiao = 0
self.q = 1
self.rect = self.imgs[self.xiabiao].get_rect()
self.lu = lu
self.xue = 2000
def show(self, x, y):
self.rect.x = x
self.rect.y = y
Main.window.blit(self.imgs[self.xiabiao], self.rect)
self.q += 1
if self.q % 5 == 0:
self.xiabiao += 1
if self.xiabiao == len(self.imgs):
self.xiabiao = 0
self.q = 1
class jiangshi:
'''僵尸基类'''
def __init__(self):
pass
# 移动方法
def move(self):
pass
class PuTong(jiangshi):
'''普通僵尸类'''
def __init__(self, lu):
self.zhanshi = [pygame.image.load('imgs/zhanshi/pujiang/' + str(i) + '.png') for i in range(1, 11 + 1)]
self.imgs = [pygame.image.load('imgs/jiangshi/pujiang/' + str(i) + '.png') for i in range(1, 22 + 1)]
self.kenshi_img = [pygame.image.load('imgs/jiangshi/pujiang/kenshi/' + str(i) + '.png') for i in range(1, 21 + 1)]
self.xia = 0
self.xiabiao = 0
self.ken = 0
self.r = 1
self.r2 = 1
self.rect = self.imgs[self.xiabiao].get_rect()
self.name = '普通僵尸'
self.lu = lu
self.xue = 100
self.gongji = 20
def go(self, x, y):
self.rect = self.imgs[self.xiabiao].get_rect()
self.rect.left = x
self.rect.bottom = y
Main.window.blit(self.imgs[self.xiabiao], self.rect)
self.r += 1
if self.r % 7 == 0:
self.r = 1
self.xiabiao += 1
if self.xiabiao == 21:
self.xiabiao = 0
def kenshi(self, x, y):
self.rect = self.kenshi_img[self.ken].get_rect()
self.rect.left = x
self.rect.bottom = y
Main.window.blit(self.kenshi_img[self.ken], self.rect)
self.r2 += 1
if self.r2 % 7 == 0:
self.r2 = 1
self.ken += 1
if self.ken == 20:
self.ken = 0
def showzhanShi(self, x, y, zan):
rect = self.zhanshi[self.xia].get_rect()
rect.centerx = x
rect.centery = y
Main.window.blit(self.zhanshi[self.xia], rect)
if zan % 5 == 0:
self.xia += 1
if self.xia == 10:
self.xia = 0
class Main:
'''主类'''
def __init__(self):
pygame.init()
# 设置长宽
Main.window = pygame.display.set_mode((WIDTH, HEIGHT))
# 设置标题
pygame.display.set_caption('植物大战僵尸Python版 - 元豪')
# 加载音乐
self.bgsound = pygame.mixer.Sound('music/bg.mp3')
self.startsound = pygame.mixer.Sound('music/start.mp3')
# 储存卡片的列表
self.kapian = []
# 加载背景图集
self.bgs = [
pygame.image.load('imgs/bg1.jpg'),
pygame.image.load('imgs/bg2.jpg')
]
# 加载小推车
self.tuiche = pygame.image.load('imgs/tuiche.png')
# 小推车的坐标
self.tuichepos = [[90, 90], [90, 185], [90, 285], [90, 385], [90, 480]]
# 加载卡槽栏
self.kacao = pygame.image.load('imgs/KaCao.png')
self.kacao_rect = self.kacao.get_rect()
# 加载选择栏
self.xuanze = pygame.image.load('imgs/XuanZe.png')
self.xuan_rect = self.xuanze.get_rect()
# 加载好、准备、开始的图片和音乐
self.hao_img = pygame.image.load('imgs/hao.png')
self.hao_img_rect = self.hao_img.get_rect()
self.hao_img_rect.center = WINDOW_RECT.center
self.zhunbei_img = pygame.image.load('imgs/zhunbei.png')
self.kaishi_img = pygame.image.load('imgs/kaishi.png')
self.zhunbei_sound = pygame.mixer.Sound('music/ZhunBei.ogg')
# 初始化僵尸的坐标
self.jiangshi_init_pos = [[1000, 175], [1000, 270], [1000, 380], [1000, 455], [1000, 565]]
# 僵尸的列表
self.jiangshiList = []
# 僵尸坐标
self.jiangshi_pos = []
# 刷僵尸计时器
self.jiangshi_time = 1
# 检测5条路是否有僵尸
self.ifLu = [False, False, False, False, False]
# 设置背景下标
self.bgindex = 0
# 设置背景图
self.bg = self.bgs[self.bgindex]
# 获取背景图的矩形
self.bg_rect = self.bg.get_rect()
# 游戏是否开始
self.open = False
# 是否选择完毕
self.go = False
# 是否可以放置植物
self.ifstart = False
# 是否已经选上了植物
self.zhongzhi = False
# 初始化选上植物的坐标
self.yix, self.yiy = -100, -100
# 如果已选择,记录种植的植物
self.yixuanze = None
# 初始化阳光
self.yangguang = 50000
# 设置背景x, y坐标
self.bgx, self.bgy = 0, 0
# 设置卡槽x, y坐标
self.kax, self.kay = 15, -self.kacao_rect.height
# 设置选择栏x, y坐标
self.xuanx, self.xuany = -self.xuan_rect.width, HEIGHT - self.xuan_rect.height
# 鼠标点击后创建一个小矩形
self.ju = wu()
# 储存卡片矩形的列表
self.ka_rect_list = []
# 储存已选择卡片的列表
self.yi_kapian = []
# 储存已选择卡片的矩形的列表
self.yi_kapian_rect = []
# 初始化第一个已选择卡片的坐标
self.kapos_x = 105
self.kapos_y = 25
# 指定已选择卡片坐标的列表
self.yi_pos = []
for i in range(6):
self.yi_pos.append([self.kapos_x, self.kapos_y])
self.kapos_x += 60
# 设置展示僵尸的数量
self.zhanshi_num = random.randint(5, 9)
# 设置展示僵尸的x, y坐标
self.zhanshi = [[random.randint(700, 860), random.randint(80, 500)] for i in range(self.zhanshi_num)]
# 设置展示僵尸的列表
self.zhanshi_list = [PuTong(0) for i in range(self.zhanshi_num)]
# 已经种下的植物
self.yizhong = []
# 鼠标按下的坐标
self.moupos = []
# 已经种下的植物的坐标
self.yipos = []
# 以下属于暂用变量
self.numder = 0
self.ci = 1
self.ci2 = 0
self.r = 40
self.r2 = 152
self.o = []
self.xiabiao = 0
self.zan = 0
self.y = 0
self.b = 0
self.g = None
# 啃食频率
self.t = 1
# 初始化卡牌
self.ka = [
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang)
]
# 开始
def start(self):
self.bgsound.play(-1)
# 游戏主循环
while True:
# 设置循环次数
pygame.time.Clock().tick(60)
self.zan += 1
# 如果游戏还没有开始
# 则放置开始界面
if not self.open:
self.showOpen()
self.numder += 1
if self.numder in [i for i in range(10, 41)]:
if self.numder == 40:
self.numder = 0
text = self.showFont('Please press the space bar to start')
self.font_rect = text.get_rect()
self.window.blit(text, (WIDTH / 2 - self.font_rect.width / 2, 570))
else:
# 放置开始界面
if self.ci > 0:
# 只运行一次,让背景音乐暂停,让准备音乐播放
self.ci -= 1
self.bgsound.stop()
self.startsound.play(-1)
# 展示背景
self.showBg(self.bgx, self.bgy)
if self.go:
# 如果游戏已经开始
# 放置卡槽
self.showYiXuan()
if self.b == 0:
self.b = 1
for j in range(len(self.yi_kapian)):
self.yi_kapian_rect.append(wu2(self.yi_kapian[zxsq-anti-bbcode-j].show_kapian(self.yi_pos[zxsq-anti-bbcode-j][zxsq-anti-bbcode-0], self.yi_pos[zxsq-anti-bbcode-j][zxsq-anti-bbcode-1])))
for j in range(len(self.yi_kapian)):
# 循环遍历已选择的列表
# 让他们在合适位置放置
self.yi_kapian[zxsq-anti-bbcode-j].show_kapian(self.yi_pos[zxsq-anti-bbcode-j][zxsq-anti-bbcode-0], self.yi_pos[zxsq-anti-bbcode-j][zxsq-anti-bbcode-1])
if self.bgx < -100:
# 将背景重新右移
# 直到 >= -100
self.bgx += 5
else:
# 背景已经移动完毕
if self.y <= 60:
self.window.blit(self.hao_img, self.hao_img_rect)
self.y += 1
elif self.y <= 120:
self.window.blit(self.zhunbei_img, self.hao_img_rect)
self.y += 1
elif self.y <= 180:
self.window.blit(self.kaishi_img, self.hao_img_rect)
self.y += 1
else:
for i in range(5):
self.window.blit(self.tuiche, (self.tuichepos[zxsq-anti-bbcode-i][zxsq-anti-bbcode-0], self.tuichepos[zxsq-anti-bbcode-i][zxsq-anti-bbcode-1]))
self.ifstart = True
self.for_show_yi()
if self.yixuanze:
self.g = self.yixuanze.show_zhiwu(self.yix, self.yiy)
# print(self.jiangshi_time)
self.jiangshi_time += 1
# 900 15秒
if self.jiangshi_time % 900 == 0:
self.jiangshi_time = 1
lu = random.randint(0, 4)
self.jiangshi_pos.append(self.jiangshi_init_pos[zxsq-anti-bbcode-lu].copy())
self.jiangshiList.append(PuTong(lu + 1))
self.showJiangShi()
if self.y in [1, 60, 120]:
self.zhunbei_sound.play()
else:
if self.bgx > -(self.bg_rect.width - WIDTH):
self.bgx -= 5
else:
self.for_showZhanshi(self.zan)
self.window.blit(self.xuanze, (self.xuanx, self.xuany))
self.showYiXuan()
if self.kay < 0:
self.kay += 3
else:
if self.xuanx < 0:
self.xuanx += 10
else:
if self.ci2 < 1:
self.ci2 += 1
for i in range(len(self.ka)):
self.o.append([self.r, self.r2])
self.r += 60
if self.r == 40 + 60 * 7:
self.r = 40
self.r2 += 92
self.ka_rect_list.append(wu2(self.ka[zxsq-anti-bbcode-i].show_kapian(self.o[zxsq-anti-bbcode-i][zxsq-anti-bbcode-0], self.o[zxsq-anti-bbcode-i][zxsq-anti-bbcode-1])))
for i in range(len(self.ka)):
self.ka[zxsq-anti-bbcode-i].show_kapian(self.o[zxsq-anti-bbcode-i][zxsq-anti-bbcode-0], self.o[zxsq-anti-bbcode-i][zxsq-anti-bbcode-1])
if self.yi_kapian:
for j in range(len(self.yi_kapian)):
self.yi_kapian[zxsq-anti-bbcode-j].show_kapian(self.yi_pos[zxsq-anti-bbcode-j][zxsq-anti-bbcode-0], self.yi_pos[zxsq-anti-bbcode-j][zxsq-anti-bbcode-1])
# 获取事件
for event in pygame.event.get():
# 退出
if event.type == pygame.QUIT:
self.end()
# 获取键盘按下事件
if event.type == pygame.KEYDOWN:
# 按下Esc键
if event.key == pygame.K_ESCAPE:
self.end()
if event.key == pygame.K_SPACE:
if not self.open:
self.open = True
# 获取鼠标点击事件
if event.type == pygame.MOUSEBUTTONDOWN:
if self.zhongzhi:
if self.yixuanze.yang <= self.yangguang:
self.moupos.append(event.pos)
t, t2 = self.ifx_and_y(self.moupos[-1][zxsq-anti-bbcode-0], self.moupos[-1][zxsq-anti-bbcode-1])
if [t, t2] not in self.yipos:
self.yipos.append([t, t2])
print('已种植', self.yixuanze.name)
if t2 == 90:
lu = 1
elif t2 == 185:
lu = 2
elif t2 == 285:
lu = 3
elif t2 == 385:
lu = 4
elif t2 == 480:
lu = 5
if '豌豆射手' == self.yixuanze.name:
self.yizhong.append(WanDou_sheshou(lu))
elif '向日葵' == self.yixuanze.name:
self.yizhong.append(xiangrikui(lu))
elif '坚果墙' == self.yixuanze.name:
self.yizhong.append(jianguo(lu))
self.yangguang -= self.yixuanze.yang
self.yix, self.yiy = -100, -100
self.zhongzhi = False
self.yixuanze = None
elif self.ifstart:
pos_x, pos_y = event.pos
self.ju.rect = pygame.rect.Rect(pos_x, pos_y, 1, 1)
for i in range(len(self.yi_kapian_rect)):
if pygame.sprite.collide_rect(self.ju, self.yi_kapian_rect[zxsq-anti-bbcode-i]):
print('选择卡片', self.yi_kapian[zxsq-anti-bbcode-i].name)
self.zhongzhi = True
self.yixuanze = self.yi_kapian[zxsq-anti-bbcode-i]
break
elif self.open:
x, y = event.pos
self.ju.rect = pygame.rect.Rect(x, y, 1, 1)
if self.ka_rect_list:
for i in self.ka_rect_list:
if len(self.yi_kapian) < 6:
if pygame.sprite.collide_rect(self.ju, i):
self.ka_rect_list.pop()
for j in range(len(self.ka_rect_list)):
if self.ka_rect_list[zxsq-anti-bbcode-j] == i:
break
else:
j = -1
self.yi_kapian.append(self.ka.pop(j))
# print(self.yi_kapian)
# print(len(self.yi_kapian))
else:
self.go = True
# 获取鼠标弹起事件
if event.type == pygame.MOUSEBUTTONUP:
pass
# 获取鼠标移动事件
if event.type == pygame.MOUSEMOTION:
if self.zhongzhi:
self.yix, self.yiy = event.pos
# 刷新界面
pygame.display.update()
# 判断植物是否僵尸与碰撞
def JiangshiAndZhiwu(self, jiangshi):
for i in range(len(self.yizhong)):
if self.yizhong[zxsq-anti-bbcode-i].lu == jiangshi.lu:
if pygame.sprite.collide_rect(self.yizhong[zxsq-anti-bbcode-i], jiangshi):
self.t += 1
if self.t == 5:
self.yizhong[zxsq-anti-bbcode-i].xue -= jiangshi.gongji
print(self.yizhong[zxsq-anti-bbcode-i].xue)
self.t = 1
return True
# 遍历僵尸列表展示僵尸
def showJiangShi(self):
for i in range(len(self.jiangshiList)):
if not self.JiangshiAndZhiwu(self.jiangshiList[zxsq-anti-bbcode-i]):
self.jiangshiList[zxsq-anti-bbcode-i].go(self.jiangshi_pos[zxsq-anti-bbcode-i][zxsq-anti-bbcode-0], self.jiangshi_pos[zxsq-anti-bbcode-i][zxsq-anti-bbcode-1])
self.jiangshi_pos[zxsq-anti-bbcode-i][zxsq-anti-bbcode-0] -= 0.5
if self.jiangshi_pos[zxsq-anti-bbcode-i][zxsq-anti-bbcode-0] == -100:
self.jiangshi_pos.pop(i)
self.jiangshiList.pop(i)
break
else:
self.jiangshiList[zxsq-anti-bbcode-i].kenshi(self.jiangshi_pos[zxsq-anti-bbcode-i][zxsq-anti-bbcode-0], self.jiangshi_pos[zxsq-anti-bbcode-i][zxsq-anti-bbcode-1])
# 判断植物要放下的位置
def ifx_and_y(self, x, y):
if x <= 245:
newx = 160
elif x <= 320:
newx = 245
elif x <= 400:
newx = 320
elif x <= 475:
newx = 400
elif x <= 565:
newx = 475
elif x <= 650:
newx = 565
elif x <= 725:
newx = 650
elif x <= 800:
newx = 725
else:
newx = 810
if y <= 180:
newy = 90
elif y <= 270:
newy = 185
elif y <= 375:
newy = 285
elif y <= 460:
newy = 385
else:
newy = 480
return (newx, newy)
# 遍历已经种下的植物的列表
def for_show_yi(self):
for i in range(len(self.yizhong)):
if self.yizhong[zxsq-anti-bbcode-i].xue > 0:
self.yizhong[zxsq-anti-bbcode-i].show(self.yipos[zxsq-anti-bbcode-i][zxsq-anti-bbcode-0], self.yipos[zxsq-anti-bbcode-i][zxsq-anti-bbcode-1])
o = []
p = []
for i in range(len(self.yizhong)):
if self.yizhong[zxsq-anti-bbcode-i].xue < 0:
pass
else:
o.append(self.yizhong[zxsq-anti-bbcode-i])
p.append(self.yipos[zxsq-anti-bbcode-i])
self.yizhong = o
self.yipos = p
# 展示已选择卡牌
def showYiXuan(self):
self.window.blit(self.kacao, (self.kax, self.kay))
font = pygame.font.SysFont('Arial', 15)
text = font.render(str(self.yangguang), True, (0, 0, 0))
text_rect = text.get_rect()
text_rect.y = self.kay + 65
text_rect.x = self.kax + 20
self.window.blit(text, text_rect)
# 循环遍历展示僵尸列表并展示
def for_showZhanshi(self, zan):
for zhanshi in self.zhanshi_list:
zhanshi.showzhanShi(self.zhanshi[self.zhanshi_list.index(zhanshi)][zxsq-anti-bbcode-0], self.zhanshi[self.zhanshi_list.index(zhanshi)][zxsq-anti-bbcode-1], zan)
# 展示背景
def showBg(self, x, y):
self.window.blit(self.bg, (x, y))
# 展示开始界面
def showOpen(self):
self.img = pygame.image.load('imgs/startBg.jpg')
self.window.blit(self.img, (0, 0))
# 展示文字
def showFont(self, text):
font = pygame.font.SysFont('Arial', 15)
text2 = font.render(text, True, TEXT_COLOR)
return text2
# 结束
def end(self):
pygame.quit()
sys.exit()
if __name__ == '__main__':
Main().start()
|
|