import json
import pygame
import random
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((1400, 600))
font = pygame.font.Font("C:/Windows/Fonts/STXINWEI.TTF", 25)
# 设置游戏标题
pygame.display.set_caption('僵尸大战僵尸')
# 图片资源
MAIN_BAC = pygame.image.load('Background/MainUI.png')
LOADING_BAC = pygame.image.load('Background/loading.png')
CHOICE_BAC = pygame.image.load('Background/LostcityNoteGroup.png')
BATTLE_BAC = pygame.image.load('Background/Background_0.png')
# 阳光
lsun = 100
rsun = 100
# 随机生成分路
random_y = [80, 160, 240, 320, 400]
# 指定生成分路
my = 0
# 游戏结束判断计数
rcont = 0
lcont = 0
# 建立left僵尸列表数组
lzombielist = []
lzombiecun = 0
# 建立right僵尸列表数组
rzombielist = []
rzombiecun = 0
# 五路僵尸字典
lzombiedict = {0: [], 1: [], 2: [], 3: [], 4: []} # 创建字典,键为纵坐标位置,值为列表
rzombiedict = {0: [], 1: [], 2: [], 3: [], 4: []}
reated = {0: [], 1: [], 2: [], 3: [], 4: []}
leated = {0: [], 1: [], 2: [], 3: [], 4: []}
rdied = {0: [], 1: [], 2: [], 3: [], 4: []}
ldied = {0: [], 1: [], 2: [], 3: [], 4: []}
rlose = {0: [], 1: [], 2: [], 3: [], 4: []}
llose = {0: [], 1: [], 2: [], 3: [], 4: []}
for i in range(5):
reated[i] = [False] * 1000
leated[i] = [False] * 1000
rdied[i] = [False] * 1000
ldied[i] = [False] * 1000
rlose[i] = [False] * 1000
llose[i] = [False] * 1000
timer = 0
time = 9000
# 可点击对象类
class Button(object):
def __init__(self, image, x, y, word, w, h):
self.image = image
self.x = x
self.y = y
self.h = h
self.w = w
self.button = None
self.button_rect = None
self.word = word
def draw(self):
self.button = self.image
self.button_rect = self.button.get_rect()
self.button_rect.center = (self.x, self.y)
screen.blit(self.button, self.button_rect)
screen.blit(font.render(f'{self.word}', True, (255, 255, 255)), (self.x - self.w, self.y - self.h))
# 卡片初始化
lZombie_card = Button(pygame.image.load('Cards/card_normal_zombie.png'), 2250, 550, '', 0, 0)
lConeheadZombie_card = Button(pygame.image.load('Cards/card_cone_zombie.png'), 2100, 550, '', 0, 0)
lBucketheadZombie_card = Button(pygame.image.load('Cards/card_bucket_zombie.png'), 2150, 550, '', 0, 0)
lxueren_card = Button(pygame.image.load('Cards/xueren.png'), 2200, 550, '', 0, 0)
lxiaogui_card = Button(pygame.image.load('Cards/xiaogui.png'), 2250, 550, '', 0, 0)
lZombie_create = False
lConeheadZombie_create = False
lBucketheadZombie_create = False
lxueren_create = False
lxiaogui_create = False
win1 = False
win2 = False
win3 = False
occupy1 = False
occupy2 = False
occupy3 = False
单人对战 = Button(pygame.image.load('UI/button.png'), 685, 400, '单人对战', 46, 10)
关卡1 = Button(pygame.image.load('Background/flag1.png'), 845, 477, '', 46, 10)
关卡11 = Button(pygame.image.load('Background/flag2.png'), 845, 477, '', 46, 10)
关卡2 = Button(pygame.image.load('Background/flag1.png'), 925, 377, '', 46, 10)
关卡3 = Button(pygame.image.load('Background/flag1.png'), 975, 277, '', 46, 10)
关卡22 = Button(pygame.image.load('Background/flag2.png'), 925, 377, '', 46, 10)
关卡33 = Button(pygame.image.load('Background/flag2.png'), 975, 277, '', 46, 10)
返回 = Button(pygame.image.load('UI/button.png'), 1300, 20, '返回', 46, 10)
# 存储类
class Save:
def __init__(self):
# 加载保存的游戏数据
self.load()
def load(self):
# 从 JSON 文件中读取数据并转换为 Python 对象
with open("save_data.json", "r") as file:
data_dict = json.load(file)
# 将 Python 对象的属性赋值给 Save 类实例的属性
self.coin = data_dict["coin"]
self.diamond = data_dict["diamond"]
self.gate_level = data_dict["gate_level"]
self.zombie_unlock = data_dict["zombie_unlock"]
self.zombie_level = data_dict["zombie_level"]
def save(self):
# 将 Save 类实例的属性转换为字典
data_dict = {
"coin": self.coin,
"diamond": self.diamond,
"gate_level": self.gate_level,
"zombie_unlock": self.zombie_unlock,
"zombie_level": self.zombie_level
}
# 将字典数据写入 JSON 文件
with open("save_data.json", "w") as file:
json.dump(data_dict, file)
# 创建 Save 类的实例
save_data = Save()
# 场景类
class Scene(object):
def __init__(self):
# 场景代号默认为1
self.choice = False
self.scene = 1
# 比较场景是否发生变化
self.old_scene = 1
# 加载计数器
self.loading_cont = 147
# 加载界面判定
def turn(self):
global lsun
# 界面发生变化且不是加载界面
if self.scene != self.old_scene and self.scene != 0:
# 计数器重新计数
self.loading_cont = 0
self.old_scene = self.scene
self.scene = 0
# 进入加载界面
elif self.scene == 0:
if not self.loading_cont >= 146:
screen.fill((0, 0, 0))
screen.blit(LOADING_BAC, (0, 0))
screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (610, 500))
screen.blit(font.render('loading...', True, (0, 0, 0)), (625, 445))
pygame.draw.rect(screen, (0, 183, 0), (615, 505, self.loading_cont, 10), 5, 2)
self.loading_cont += 1
# 关卡初始化
if self.old_scene == 2:
screen.blit(font.render('点击卡片拖动到指定分路左键生成,右键取消选中', True, (255, 255, 255)),
(475, 545))
elif self.old_scene == 3:
screen.blit(font.render('阳光可以自动生成,也可以通过打败敌人获取', True, (255, 255, 255)),
(475, 545))
lsun = 2000
elif self.old_scene == 4:
screen.blit(font.render('攻破敌人的防线,才是获胜的关键', True, (255, 255, 255)),
(475, 545))
lsun = 3000
elif self.old_scene == 5:
screen.blit(font.render('后期可以获得更加强力的僵尸助战,但是敌人也会变强', True, (255, 255, 255)),
(475, 545))
lsun = 4000
# 加载完毕回到目标界面
else:
self.scene = self.old_scene
# 战斗界面
def battle(self, num):
global lsun
global timer
global time
global lcont
#
if not self.choice:
# 全局按钮事件惹的祸
lZombie_card.draw()
lConeheadZombie_card.draw()
lBucketheadZombie_card.draw()
lxueren_card.draw()
lxiaogui_card.draw()
ready_ui.show()
else:
win = [False] * 10
# 阳光自然生成速度
lsun += 0.01
screen.fill((0, 0, 0))
screen.blit(BATTLE_BAC, (0, 0))
# 关卡敌人出现设计
# 攻城战
if num == 0 or num == 1:
if 7 * lcont < 6:
suanfa()
if timer <= 600 - num * 100:
timer += 1
else:
if num == 0:
rzombielist.append(Zombie(0, 0, True, 0))
elif num == 1:
rzombielist.append(Zombie(1, 0, True, 0))
timer = 0
# 战败
elif 7 * rcont > 147:
self.scene = 2
else:
# 胜利结算
win[num] = True
if win[num]:
save_data.coin += 10 + 10 * num
win[num] = False
# 解锁新关卡
if save_data.gate_level < num + 2:
save_data.gate_level = num + 2
self.scene = 2
lcont = 0
# 去除上局残余(隐藏法)
for i in lzombielist:
i.y = 1000
for i in rzombielist:
i.y = 1000
# 守城战
if num == 2 or num == 3:
screen.blit(font.render(f'请抵挡敌方:{int(time / 30)}秒', True, (255, 255, 255)), (600, 100))
if 7 * lcont < 6 and time >= 0:
suanfa()
if timer <= 600 - num * 100:
timer += 1
time -= 1
else:
if num == 2:
rzombielist.append(Zombie(2, 0, True, 0))
elif num == 3:
rzombielist.append(Zombie(3, 0, True, 0))
timer = 0
# 战败
elif 7 * rcont > 147:
self.scene = 2
else:
# 胜利结算
win[num] = True
if win[num]:
save_data.coin += 10 + 10 * num
win[num] = False
# 解锁新关卡
if save_data.gate_level < num + 2:
save_data.gate_level = num + 2
self.scene = 2
# 去除上局残余(隐藏法)
for i in lzombielist:
i.y = 1000
# 阳光显示
screen.blit(pygame.image.load(r'C:\Users\Administrator\Desktop\Python\僵尸大战僵尸\UI\sun_bg.png'),
(0, 0))
screen.blit(font.render(str(int(lsun)), True, (0, 0, 0)), (40, 5))
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
lZombie_card.draw()
lConeheadZombie_card.draw()
lBucketheadZombie_card.draw()
lxueren_card.draw()
lxiaogui_card.draw()
# 基地血条
screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (0, 40))
pygame.draw.rect(screen, (0, 183, 0), (5, 45, 146 - 7 * rcont, 10), 5, 2)
screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (1245, 40))
pygame.draw.rect(screen, (0, 183, 0), (1250, 45, 146 - 7 * lcont, 10), 5, 2)
def show(self):
global timer, win1, win2, win3, occupy1, occupy2, occupy3, lzombielist
global lsun
# 默认场景
self.turn()
if self.scene == 1:
pygame.mixer.init()
pygame.mixer.music.load("Audio/Music/bgm5.mp3")
pygame.mixer.music.play(-1)
screen.fill((0, 0, 0))
screen.blit(MAIN_BAC, (0, 0))
单人对战.draw()
# 关卡选择界面
elif self.scene == 2:
pygame.mixer.init()
pygame.mixer.music.load("Audio/Music/bgm2.mp3")
pygame.mixer.music.play(-1)
screen.fill((0, 0, 0))
screen.blit(CHOICE_BAC, (0, 0))
UI.choice = False
# 棋子显示状态
if save_data.gate_level < 2:
关卡1.draw()
else:
关卡1.draw()
关卡11.draw()
if save_data.gate_level < 3:
关卡2.draw()
else:
关卡2.draw()
关卡22.draw()
if save_data.gate_level < 4:
关卡3.draw()
else:
关卡3.draw()
关卡33.draw()
返回.draw()
elif self.scene == 3:
self.battle(0)
elif self.scene == 4:
self.battle(1)
elif self.scene == 5:
self.battle(2)
elif self.scene == 6:
self.battle(3)
# 准备界面
elif self.scene == 7:
ready_ui.show()
UI = Scene()
FlagMeter_frames = []
FlagMeter_currentFrame = 0
for i in range(37):
FlagMeter_frame = pygame.image.load(f'FlagMeter/FlagMeter_{i}.png').convert_alpha()
FlagMeter_frames.append(FlagMeter_frame)
leftwin = pygame.image.load('UI/leftwin.png')
rightwin = pygame.image.load('UI/rightwin.png')
# 僵尸类
class Zombie(pygame.sprite.Sprite):
def __init__(self, kind, camp, ai, ny):
super().__init__()
# 阵营分配
self.camp = camp
self.ai = ai
self.kind = kind
if camp == 0:
self.x = random.randint(1300, 1600) # 默认出生位置
else:
self.x = random.randint(-300, 0)
if not self.ai:
self.y = ny
else:
self.y = random.choice(random_y)
# 僵尸种类
if self.kind == 0: # 普通僵尸
self.speed = 0.5 # 基础速度
self.HP = 500
self.image = 0 # 普通僵尸对应的图片组
elif self.kind == 1: # 路障僵尸(更快)
self.speed = 1 #
self.HP = 600
self.image = 1
elif self.kind == 2: # 铁桶僵尸(更肉)
self.speed = 0.5 #
self.HP = 1200
self.image = 2
elif self.kind == 3: # 雪人僵尸(更大)
self.speed = 0.3 #
self.HP = 2000
self.image = 3
elif self.kind == 4: # 小鬼僵尸(不容易被发现)
self.speed = 1 #
self.HP = 300
self.image = 4
# 进入房间的僵尸数量计数一个僵尸只记一次的标志位
self.cont_added = False
# 两种帧
self.frame = None
self.frames = []
self.currentFrame = 0
self.frame1 = None
self.frames1 = []
self.currentFrame1 = 0
if self.camp == 0:
if self.image == 0:
for i in range(1, 23):
self.frame = pygame.image.load(f'Zombies/Zombie/Zombie_{i}.png').convert_alpha()
self.frames.append(self.frame)
elif self.image == 1:
for i in range(1, 22):
self.frame = pygame.image.load(f'Zombies/ConeheadZombie/ConeheadZombie_{i}.png').convert_alpha()
self.frames.append(self.frame)
elif self.image == 2:
for i in range(1, 16):
self.frame = pygame.image.load(f'Zombies/BucketheadZombie/BucketheadZombie_{i}.png').convert_alpha()
self.frames.append(self.frame)
elif self.image == 3:
for i in range(0, 30):
self.frame = pygame.image.load(f'xueren/walk/Armature_newAnimation_{i}.png').convert_alpha()
self.frames.append(self.frame)
elif self.image == 4:
for i in range(0, 34):
self.frame = pygame.image.load(f'小鬼/walk/Armature_walk_{i}.png').convert_alpha()
self.frames.append(self.frame)
elif self.camp == 1:
if self.image == 0:
for i in range(1, 23):
self.frame = pygame.image.load(f'Zombies/Zombie/Zombie_{i}.png').convert_alpha()
self.frame = pygame.transform.flip(self.frame, True, False)
self.frames.append(self.frame)
elif self.image == 1:
for i in range(1, 22):
self.frame = pygame.image.load(f'Zombies/ConeheadZombie/ConeheadZombie_{i}.png').convert_alpha()
self.frame = pygame.transform.flip(self.frame, True, False)
self.frames.append(self.frame)
elif self.image == 2:
for i in range(1, 16):
self.frame = pygame.image.load(f'Zombies/BucketheadZombie/BucketheadZombie_{i}.png').convert_alpha()
self.frame = pygame.transform.flip(self.frame, True, False)
self.frames.append(self.frame)
elif self.image == 3:
for i in range(0, 30):
self.frame = pygame.image.load(f'xueren/walk/Armature_newAnimation_{i}.png').convert_alpha()
self.frame = pygame.transform.flip(self.frame, True, False)
self.frames.append(self.frame)
elif self.image == 4:
for i in range(0, 34):
self.frame = pygame.image.load(f'小鬼/walk/Armature_walk_{i}.png').convert_alpha()
self.frame = pygame.transform.flip(self.frame, True, False)
self.frames.append(self.frame)
if self.camp == 0:
if self.image == 0:
for i in range(1, 22):
self.frame1 = pygame.image.load(f'Zombies/Zombie/ZombieAttack_{i}.png').convert_alpha()
self.frames1.append(self.frame1)
elif self.image == 1:
for i in range(1, 12):
self.frame1 = pygame.image.load(
f'Zombies/ConeheadZombie/ConeheadZombieAttack_{i}.png').convert_alpha()
self.frames1.append(self.frame1)
elif self.image == 2:
for i in range(1, 12):
self.frame1 = pygame.image.load(
f'Zombies/BucketheadZombie/BucketheadZombieAttack_{i}.png').convert_alpha()
self.frames1.append(self.frame1)
elif self.image == 3:
for i in range(0, 14):
self.frame1 = pygame.image.load(
f'xueren/attack/Armature_newAnimation_1_{i}.png').convert_alpha()
self.frames1.append(self.frame1)
elif self.image == 4:
for i in range(0, 20):
self.frame1 = pygame.image.load(
f'小鬼/attack/Armature_attack_{i}.png').convert_alpha()
self.frames1.append(self.frame1)
elif self.camp == 1:
if self.image == 0:
for i in range(1, 22):
self.frame1 = pygame.image.load(f'Zombies/Zombie/ZombieAttack_{i}.png').convert_alpha()
self.frame1 = pygame.transform.flip(self.frame1, True, False)
self.frames1.append(self.frame1)
elif self.image == 1:
for i in range(1, 12):
self.frame1 = pygame.image.load(
f'Zombies/ConeheadZombie/ConeheadZombieAttack_{i}.png').convert_alpha()
self.frame1 = pygame.transform.flip(self.frame1, True, False)
self.frames1.append(self.frame1)
elif self.image == 2:
for i in range(1, 12):
self.frame1 = pygame.image.load(
f'Zombies/BucketheadZombie/BucketheadZombieAttack_{i}.png').convert_alpha()
self.frame1 = pygame.transform.flip(self.frame1, True, False)
self.frames1.append(self.frame1)
elif self.image == 3:
for i in range(0, 14):
self.frame1 = pygame.image.load(
f'xueren/attack/Armature_newAnimation_1_{i}.png').convert_alpha()
self.frame1 = pygame.transform.flip(self.frame1, True, False)
self.frames1.append(self.frame1)
elif self.image == 4:
for i in range(0, 20):
self.frame1 = pygame.image.load(
f'小鬼/attack/Armature_attack_{i}.png').convert_alpha()
self.frame1 = pygame.transform.flip(self.frame1, True, False)
self.frames1.append(self.frame1)
# 僵尸移动
def walking(self):
if self.camp == 0:
if self.kind == 3:
self.x = self.x - self.speed
screen.blit(self.frames[self.currentFrame],
(self.x, self.y - 50))
# 更新当前帧
self.currentFrame = (self.currentFrame + 1) % len(self.frames)
elif self.kind == 4:
self.x = self.x - self.speed
screen.blit(self.frames[self.currentFrame],
(self.x, self.y + 50))
# 更新当前帧
self.currentFrame = (self.currentFrame + 1) % len(self.frames)
else:
self.x = self.x - self.speed
screen.blit(self.frames[self.currentFrame],
(self.x, self.y))
# 更新当前帧
self.currentFrame = (self.currentFrame + 1) % len(self.frames)
else:
if self.kind == 3:
self.x = self.x + self.speed
screen.blit(self.frames[self.currentFrame],
(self.x, self.y - 50))
# 更新当前帧
self.currentFrame = (self.currentFrame + 1) % len(self.frames)
elif self.kind == 4:
self.x = self.x + self.speed
screen.blit(self.frames[self.currentFrame],
(self.x, self.y + 50))
# 更新当前帧
self.currentFrame = (self.currentFrame + 1) % len(self.frames)
else:
self.x = self.x + self.speed
screen.blit(self.frames[self.currentFrame],
(self.x, self.y))
# 更新当前帧
self.currentFrame = (self.currentFrame + 1) % len(self.frames)
# 血条
if self.camp == 0:
if self.kind == 0:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 10, 10))
elif self.kind == 1:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 100, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 100, self.y, self.HP / 12, 10))
elif self.kind == 2:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 24, 10))
elif self.camp == 1:
if self.kind == 0:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 10, 10))
elif self.kind == 1:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 12, 10))
elif self.kind == 2:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 24, 10))
def eating(self):
if self.kind == 3:
screen.blit(self.frames1[self.currentFrame1],
(self.x, self.y - 50))
# 更新当前帧
self.currentFrame1 = (self.currentFrame1 + 1) % len(self.frames1)
elif self.kind == 4:
screen.blit(self.frames[self.currentFrame],
(self.x, self.y + 50))
# 更新当前帧
self.currentFrame = (self.currentFrame + 1) % len(self.frames)
else:
screen.blit(self.frames1[self.currentFrame1],
(self.x, self.y))
# 更新当前帧
self.currentFrame1 = (self.currentFrame1 + 1) % len(self.frames1)
if self.camp == 0:
if self.kind == 0:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 10, 10))
elif self.kind == 1:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 100, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 100, self.y, self.HP / 12, 10))
elif self.kind == 2:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 24, 10))
elif self.camp == 1:
if self.kind == 0:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 10, 10))
elif self.kind == 1:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 12, 10))
elif self.kind == 2:
pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 24, 10))
def game(self):
global rcont
global lcont
if self.y < 1000:
if self.camp == 0:
if not self.cont_added:
rcont = rcont + 1
self.cont_added = True
elif self.camp == 1:
if not self.cont_added:
lcont = lcont + 1
self.cont_added = True
# 准备界面类
class ReadyUi:
def __init__(self):
# 背景
self.bac = pygame.image.load('Background/Background_0.png')
# 选择栏
self.selected = pygame.image.load('Background/sun_count.png')
# 卡片组框
self.deck = pygame.image.load('Background/SeedChooser_Background.png')
# 卡片组
self.cards_frames = [pygame.image.load('Cards/card_normal_zombie.png'),
pygame.image.load('Cards/card_cone_zombie.png'),
pygame.image.load('Cards/card_bucket_zombie.png'),
pygame.image.load('Cards/xueren.png'),
pygame.image.load('Cards/xiaogui.png')]
# 通用按钮
self.button_image = pygame.image.load('UI/button.png')
self.back_image = pygame.image.load('UI/button.png')
# 卡片创建
self.card = []
for i in range(len(self.cards_frames)):
self.card.append(Button(self.cards_frames[i], 490 + 50 * i, 180, "", 0, 0))
# 确认按钮
self.ok = Button(self.button_image, 700, 450, "确认", 20, 10)
# 取消按钮
self.back = Button(self.button_image, 600, 450, "返回", 20, 10)
self.is_ok = False
# 逻辑标志位
self.choice = [False] * len(self.card)
self.ack = [False] * len(self.card)
# 卡槽上限 默认
self.card_max = 3
self.card_num = 0
self.occupy = [False] * self.card_max
self.card_new_num = 0
# 显示动画
self.play = [False] * self.card_max
self.cartoon = []
# 匹配动画
for j in range(len(self.card)):
self.cartoon.append(Zombie(j, 1, False, 0))
self.cartoon[j].x = 100
for i in range(3):
self.cartoon[j].y = 50 * i + 1
def show(self):
if not UI.choice:
# 背景
screen.blit(self.bac, (0, 0))
# 已选中窗口
screen.blit(self.selected, (450, 10))
# 全部卡牌栏
screen.blit(self.deck, (440, 100))
# 卡片
for i in self.card:
i.draw()
# 确认按钮
self.ok.draw()
self.back.draw()
self.event()
def event(self):
mouse_x, mouse_y = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
break
elif event.type == pygame.MOUSEBUTTONDOWN:
for i in range(len(self.card)):
if self.card[i].button_rect.collidepoint(pygame.mouse.get_pos()):
if event.button == 1 and not self.choice[i]: # 左键点击后实现拖动
self.choice[i] = True
elif event.button == 1 and self.choice[i]: # 左键点击指定区域实现选定
if 450 <= mouse_x <= 890 and 12 <= mouse_y <= 85 and self.card_num < self.card_max:
self.card_num += 1
for j in range(self.card_max):
if self.card_num == j + 1:
self.card[i].x = 553 + 50 * j
self.card[i].y = 55
if i == 0:
lZombie_card.x = 50 + j * 50
elif i == 1:
lConeheadZombie_card.x = 50 + j * 50
elif i == 2:
lBucketheadZombie_card.x = 50 + j * 50
elif i == 3:
lxueren_card.x = 50 + j * 50
elif i == 4:
lxiaogui_card.x = 50 + j * 50
self.choice[i] = False
self.ack[i] = True
# 取消选中
elif not (450 <= mouse_x <= 890 and 12 <= mouse_y <= 85):
self.choice[i] = False
self.ack[i] = False
self.card[i].x = 490 + 50 * i
self.card[i].y = 180
# 右键取消已经选中的卡片
elif event.button == 3 and self.ack[i]:
self.choice[i] = False
self.ack[i] = False
self.card[i].x = 490 + 50 * i
self.card[i].y = 180
self.card_new_num = self.card_num
self.card_num -= 1
if self.card_new_num > self.card_num:
for j in range(self.card_max):
if self.card_new_num < j + 2:
self.play[j] = False
elif event.button == 3: # 拖拽时右键取消
self.choice[i] = False
self.ack[i] = False
self.card[i].x = 490 + 50 * i
self.card[i].y = 180
# 确认按钮事件
if self.ok.button_rect.collidepoint(pygame.mouse.get_pos()):
if event.button == 1: # 左键点击
UI.choice = True
# 卡片拖拽效果
for i in range(len(self.card)):
if self.choice[i]:
self.card[i].x = mouse_x
self.card[i].y = mouse_y
# 显示出场动画
for j in range(len(self.ack)):
for i in range(len(self.card)):
if self.card_num == j + 1 and self.ack[i]:
self.play[j] = True
if self.ack[j]:
self.cartoon[j].walking()
self.cartoon[j].x = 100
self.cartoon[j].y = 50 * j + 1
else:
pass
# 确认是否结束选卡
if self.is_ok:
pass
ready_ui = ReadyUi()
lcreate0 = False
lcreate1 = False
lcreate2 = False
# 按钮执行函数
def left_create0():
global lzombielist
global lzombiecun
global lsun
global y
if lsun >= 50:
# 创建新的left普通僵尸对象
lzombielist.append(Zombie(0, 1, False, my))
lsun = lsun - 50
# 存在僵尸状态置为1
if lzombiecun != 1:
lzombiecun = 1
def right_create0():
global rzombielist
global rzombiecun
global rsun
if rsun >= 50:
# 创建新的left普通僵尸对象
rzombielist.append(Zombie(0, 0, False, my))
rsun = rsun - 50
# 存在僵尸状态置为1
if rzombiecun != 1:
rzombiecun = 1
def left_create1():
global lzombielist
global lzombiecun
global lsun
if lsun >= 75:
# 创建新的left普通僵尸对象
lzombielist.append(Zombie(1, 1, False, my))
lsun = lsun - 75
# 存在僵尸状态置为1
if lzombiecun != 1:
lzombiecun = 1
def right_create1():
global rzombielist
global rzombiecun
global rsun
if rsun >= 75:
# 创建新的left普通僵尸对象
rzombielist.append(Zombie(1, 0, False, my))
rsun = rsun - 75
# 存在僵尸状态置为1
if rzombiecun != 1:
rzombiecun = 1
def left_create2():
global lzombielist
global lzombiecun
global lsun
if lsun >= 125:
# 创建新的left普通僵尸对象
lzombielist.append(Zombie(2, 1, False, my))
lsun = lsun - 125
# 存在僵尸状态置为1
if lzombiecun != 1:
lzombiecun = 1
def right_create2():
global rzombielist
global rzombiecun
global rsun
if rsun >= 125:
# 创建新的left普通僵尸对象
rzombielist.append(Zombie(2, 0, False, my))
rsun = rsun - 125
# 存在僵尸状态置为1
if rzombiecun != 1:
rzombiecun = 1
def left_create3():
global lzombielist
global lzombiecun
global lsun
if lsun >= 200:
lzombielist.append(Zombie(3, 1, False, my))
lsun = lsun - 200
if lzombiecun != 1:
lzombiecun = 1
def right_create3():
global rzombielist
global rzombiecun
global rsun
if rsun >= 200:
rzombielist.append(Zombie(3, 0, False, my))
rsun = rsun - 200
if rzombiecun != 1:
rzombiecun = 1
def left_create4():
global lzombielist
global lzombiecun
global lsun
if lsun >= 50:
lzombielist.append(Zombie(4, 1, False, my))
lsun = lsun - 50
if lzombiecun != 1:
lzombiecun = 1
def right_create4():
global rzombielist
global rzombiecun
global rsun
if rsun >= 50:
rzombielist.append(Zombie(4, 0, False, my))
rsun = rsun - 50
if rzombiecun != 1:
rzombiecun = 1
def suanfa():
#
global lsun, lcont
global rsun
for i in lzombielist:
if (i.y - 80) // 80 in lzombiedict:
if i not in lzombiedict[(i.y - 80) // 80]:
lzombiedict[(i.y - 80) // 80].append(i)
for i in rzombielist:
if (i.y - 80) // 80 in rzombiedict:
if i not in rzombiedict[(i.y - 80) // 80]:
rzombiedict[(i.y - 80) // 80].append(i)
# 总算法
for k in range(5):
for i in range(len(rzombiedict[k])):
if not reated[k][i] and not rdied[k][i]:
rzombiedict[k][i].walking()
if rzombiedict[k][i].x <= -100:
rzombiedict[k][i].game()
for i in range(len(lzombiedict[k])):
if not leated[k][i] and not ldied[k][i]:
lzombiedict[k][i].walking()
if lzombiedict[k][i].x >= 1400:
lzombiedict[k][i].game()
for i in range(len(rzombiedict[k])):
if rdied[k][i]:
rzombiedict[k][i].y = 1000
break
for i in range(len(lzombiedict[k])):
if ldied[k][i]:
lzombiedict[k][i].y = 1000
break
for j in range(len(rzombiedict[k])):
for i in range(len(lzombiedict[k])):
# 特殊僵尸新距离判定
# 小鬼僵尸
if rzombiedict[k][j].kind == 4 or lzombiedict[k][i].kind == 4:
# 与小鬼僵尸距离为50
if abs(rzombiedict[k][j].x - lzombiedict[k][i].x) <= 50 and not (
rzombiedict[k][j].kind == 3 or lzombiedict[k][i].kind == 3):
# 存在巨型僵尸时触发小鬼被动
reated[k][j] = True
leated[k][i] = True
# 双方血量都大于0
if rzombiedict[k][j].HP > 0 and lzombiedict[k][i].HP > 0:
rzombiedict[k][j].eating()
lzombiedict[k][i].eating()
rzombiedict[k][j].HP -= 10
lzombiedict[k][i].HP -= 10
# 其中一方取胜
elif rzombiedict[k][j].HP <= 0 < lzombiedict[k][i].HP:
leated[k][i] = False
reated[k][j] = False
rdied[k][j] = True
elif lzombiedict[k][i].HP <= 0 < rzombiedict[k][j].HP:
leated[k][i] = False
ldied[k][i] = True
reated[k][j] = False
elif rzombiedict[k][j].HP <= 0 and rlose[k][j] == False:
reated[k][j] = False
rdied[k][j] = True
elif lzombiedict[k][i].HP <= 0 and llose[k][i] == False:
leated[k][i] = False
ldied[k][i] = True
# 雪人僵尸
elif rzombiedict[k][j].kind == 3 or lzombiedict[k][i].kind == 3:
# 与雪人僵尸距离为30
if abs(rzombiedict[k][j].x - lzombiedict[k][i].x) <= 30:
reated[k][j] = True
leated[k][i] = True
# 双方血量都大于0
if rzombiedict[k][j].HP > 0 and lzombiedict[k][i].HP > 0:
rzombiedict[k][j].eating()
lzombiedict[k][i].eating()
rzombiedict[k][j].HP -= 10
lzombiedict[k][i].HP -= 10
# 其中一方取胜
elif rzombiedict[k][j].HP <= 0 < lzombiedict[k][i].HP:
leated[k][i] = False
reated[k][j] = False
rdied[k][j] = True
elif lzombiedict[k][i].HP <= 0 < rzombiedict[k][j].HP:
leated[k][i] = False
ldied[k][i] = True
reated[k][j] = False
elif rzombiedict[k][j].HP <= 0 and rlose[k][j] == False:
reated[k][j] = False
rdied[k][j] = True
elif lzombiedict[k][i].HP <= 0 and llose[k][i] == False:
leated[k][i] = False
ldied[k][i] = True
else:
# 僵尸x相等
if abs(rzombiedict[k][j].x - lzombiedict[k][i].x) <= 2:
# 吃标志置为1
reated[k][j] = True
leated[k][i] = True
# 双方血量都大于0
if rzombiedict[k][j].HP > 0 and lzombiedict[k][i].HP > 0:
rzombiedict[k][j].eating()
lzombiedict[k][i].eating()
rzombiedict[k][j].HP -= 10
lzombiedict[k][i].HP -= 10
# 其中一方取胜
elif rzombiedict[k][j].HP <= 0 < lzombiedict[k][i].HP:
leated[k][i] = False
reated[k][j] = False
rdied[k][j] = True
elif lzombiedict[k][i].HP <= 0 < rzombiedict[k][j].HP:
leated[k][i] = False
ldied[k][i] = True
reated[k][j] = False
elif rzombiedict[k][j].HP <= 0 and rlose[k][j] == False:
reated[k][j] = False
rdied[k][j] = True
elif lzombiedict[k][i].HP <= 0 and llose[k][i] == False:
leated[k][i] = False
ldied[k][i] = True
"""# 测试!!!!!!!!!!!!!!!!!!!
for i in range(5):
lzombielist.append(Zombie(3, 1, True, 0))
rzombielist.append(Zombie(4, 0, True, 0))"""
# 主函数
def main():
global lZombie_card, lZombie_create, win1, win2, win3, UI, lsun, rsun, lxueren_create, lxiaogui_create
global lConeheadZombie_card, lConeheadZombie_create
global lBucketheadZombie_card, lBucketheadZombie_create
global my
run = True
clock = pygame.time.Clock()
while run:
save_data.save()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if UI.scene == 1:
if event.type == pygame.MOUSEBUTTONDOWN:
if 单人对战.button_rect.collidepoint(pygame.mouse.get_pos()):
UI.scene = 2
elif UI.scene == 2:
if event.type == pygame.MOUSEBUTTONDOWN:
if 返回.button_rect.collidepoint(pygame.mouse.get_pos()):
UI.scene = 1
elif 关卡1.button_rect.collidepoint(pygame.mouse.get_pos()) and save_data.gate_level <= 1:
UI.scene = 3
elif 关卡11.button_rect.collidepoint(pygame.mouse.get_pos()):
UI.scene = 3
elif 关卡2.button_rect.collidepoint(pygame.mouse.get_pos()) and save_data.gate_level <= 2:
UI.scene = 4
elif 关卡22.button_rect.collidepoint(pygame.mouse.get_pos()):
UI.scene = 4
elif 关卡3.button_rect.collidepoint(pygame.mouse.get_pos()) and save_data.gate_level <= 3:
UI.scene = 5
elif 关卡33.button_rect.collidepoint(pygame.mouse.get_pos()):
UI.scene = 5
elif UI.scene == 3 or UI.scene == 4 or UI.scene == 5 and UI.choice:
if event.type == pygame.MOUSEBUTTONDOWN:
if lZombie_card.button_rect.collidepoint(pygame.mouse.get_pos()):
lZombie_create = True
elif lConeheadZombie_card.button_rect.collidepoint(pygame.mouse.get_pos()):
lConeheadZombie_create = True
elif lBucketheadZombie_card.button_rect.collidepoint(pygame.mouse.get_pos()):
lBucketheadZombie_create = True
elif lxueren_card.button_rect.collidepoint(pygame.mouse.get_pos()):
lxueren_create = True
elif lxiaogui_card.button_rect.collidepoint(pygame.mouse.get_pos()):
lxiaogui_create = True
UI.show()
if lZombie_create:
lZombie_card_image = pygame.transform.flip(pygame.image.load('Zombies/Zombie/Zombie_1.png'), True,
False)
lZombie_card_image.set_alpha(128) # 设置透明度为50%(255为不透明,0为完全透明)
lZombie_card_image_rect = lZombie_card_image.get_rect()
lZombie_card_image_rect.center = pygame.mouse.get_pos() # 将图像中心设置为鼠标位置
screen.blit(lZombie_card_image, lZombie_card_image_rect)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # 左键点击
mouse_x, mouse_y = pygame.mouse.get_pos()
if 160 < mouse_y <= 240:
my = 80
elif 240 < mouse_y <= 320:
my = 160
elif 320 < mouse_y <= 400:
my = 240
elif 400 < mouse_y <= 480:
my = 320
elif 480 < mouse_y <= 560:
my = 400
left_create0()
lZombie_create = False
elif event.button == 3: # 右键点击
lZombie_create = False
elif lConeheadZombie_create:
lConeheadZombie_card_image = pygame.transform.flip(
pygame.image.load('Zombies/ConeheadZombie/ConeheadZombie_1.png'), True,
False)
lConeheadZombie_card_image.set_alpha(128) # 设置透明度为50%(255为不透明,0为完全透明)
lConeheadZombie_card_image_rect = lConeheadZombie_card_image.get_rect()
lConeheadZombie_card_image_rect.center = pygame.mouse.get_pos() # 将图像中心设置为鼠标位置
screen.blit(lConeheadZombie_card_image, lConeheadZombie_card_image_rect)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # 左键点击
mouse_x, mouse_y = pygame.mouse.get_pos()
if 160 < mouse_y <= 240:
my = 80
elif 240 < mouse_y <= 320:
my = 160
elif 320 < mouse_y <= 400:
my = 240
elif 400 < mouse_y <= 480:
my = 320
elif 480 < mouse_y <= 560:
my = 400
left_create1()
lConeheadZombie_create = False
elif event.button == 3: # 右键点击
lConeheadZombie_create = False
elif lBucketheadZombie_create:
lBucketheadZombie_card_image = pygame.transform.flip(
pygame.image.load('Zombies/BucketheadZombie/BucketheadZombie_1.png'), True,
False)
lBucketheadZombie_card_image.set_alpha(128) # 设置透明度为50%(255为不透明,0为完全透明)
lBucketheadZombie_card_image_rect = lBucketheadZombie_card_image.get_rect()
lBucketheadZombie_card_image_rect.center = pygame.mouse.get_pos() # 将图像中心设置为鼠标位置
screen.blit(lBucketheadZombie_card_image, lBucketheadZombie_card_image_rect)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # 左键点击
mouse_x, mouse_y = pygame.mouse.get_pos()
if 160 < mouse_y <= 240:
my = 80
elif 240 < mouse_y <= 320:
my = 160
elif 320 < mouse_y <= 400:
my = 240
elif 400 < mouse_y <= 480:
my = 320
elif 480 < mouse_y <= 560:
my = 400
left_create2()
lBucketheadZombie_create = False
elif event.button == 3: # 右键点击
lBucketheadZombie_create = False
elif lxueren_create:
lxueren_card_image = pygame.transform.flip(
pygame.image.load('xueren/walk/Armature_newAnimation_0.png'), True,
False)
lxueren_card_image.set_alpha(128) # 设置透明度为50%(255为不透明,0为完全透明)
lxueren_card_image_rect = lxueren_card_image.get_rect()
lxueren_card_image_rect.center = pygame.mouse.get_pos() # 将图像中心设置为鼠标位置
screen.blit(lxueren_card_image, lxueren_card_image_rect)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # 左键点击
mouse_x, mouse_y = pygame.mouse.get_pos()
if 160 < mouse_y <= 240:
my = 80
elif 240 < mouse_y <= 320:
my = 160
elif 320 < mouse_y <= 400:
my = 240
elif 400 < mouse_y <= 480:
my = 320
elif 480 < mouse_y <= 560:
my = 400
left_create3()
lxueren_create = False
elif event.button == 3: # 右键点击
lxueren_create = False
elif lxiaogui_create:
lxiaogui_card_image = pygame.transform.flip(
pygame.image.load('小鬼/walk/Armature_attack_00.png'), True,
False)
lxiaogui_card_image.set_alpha(128) # 设置透明度为50%(255为不透明,0为完全透明)
lxiaogui_card_image_rect = lxiaogui_card_image.get_rect()
lxiaogui_card_image_rect.center = pygame.mouse.get_pos() # 将图像中心设置为鼠标位置
screen.blit(lxiaogui_card_image, lxiaogui_card_image_rect)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # 左键点击
mouse_x, mouse_y = pygame.mouse.get_pos()
if 160 < mouse_y <= 240:
my = 80
elif 240 < mouse_y <= 320:
my = 160
elif 320 < mouse_y <= 400:
my = 240
elif 400 < mouse_y <= 480:
my = 320
elif 480 < mouse_y <= 560:
my = 400
left_create4()
lxiaogui_create = False
elif event.button == 3: # 右键点击
lxiaogui_create = False
for i in range(5):
for j in range(len(rzombiedict[i])):
if -100 < rzombiedict[i][j].HP <= 0 and not rlose[i][j]:
rlose[i][j] = True
if rlose[i][j]:
if rzombiedict[i][j].kind == 0:
lsun += 25
elif rzombiedict[i][j].kind == 1:
lsun += 35
elif rzombiedict[i][j].kind == 2:
lsun += 50
rzombiedict[i][j].HP = -100
rlose[i][j] = False
for j in range(len(lzombiedict[i])):
if -100 < lzombiedict[i][j].HP <= 0 and not llose[i][j]:
llose[i][j] = True
if llose[i][j]:
if lzombiedict[i][j].kind == 0:
rsun += 25
elif lzombiedict[i][j].kind == 1:
rsun += 35
elif lzombiedict[i][j].kind == 2:
rsun += 50
lzombiedict[i][j].HP = -100
llose[i][j] = False
pygame.display.update()
clock.tick(30)
pygame.quit()
if __name__ == "__main__":
main()
图片文件我也分享在资源中