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
# 可点击对象类
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'), 50, 550, '', 0, 0)
lConeheadZombie_card = Button(pygame.image.load('Cards/card_cone_zombie.png'), 100, 550, '', 0, 0)
lBucketheadZombie_card = Button(pygame.image.load('Cards/card_bucket_zombie.png'), 150, 550, '', 0, 0)
lxueren_card = Button(pygame.image.load('Cards/xueren.png'), 200, 550, '', 0, 0)
lxiaogui_card = Button(pygame.image.load('Cards/xiaogui.png'), 250, 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 Scene(object):
def __init__(self):
self.scene = 1
self.loading_cont = 147
def show(self):
global timer, win1, win2, win3, occupy1, occupy2, occupy3, lzombielist
global lsun
# 默认场景
if self.scene == 1:
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
else:
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:
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))
screen.blit(
font.render('温馨提示:随着时间的增加阳光也会增加,同时击杀敌人也可获取阳光', True, (255, 255, 255)),
(325, 545))
pygame.draw.rect(screen, (0, 183, 0), (615, 505, self.loading_cont, 10), 5, 2)
self.loading_cont += 1
else:
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))
if not occupy1:
关卡1.draw()
else:
关卡11.draw()
if not occupy2:
关卡2.draw()
else:
关卡22.draw()
if not occupy3:
关卡3.draw()
else:
关卡33.draw()
返回.draw()
elif self.scene == 3:
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))
screen.blit(
font.render('温馨提示:选中僵尸后鼠标左键生成僵尸,鼠标右键取消选中', True, (255, 255, 255)),
(325, 545))
pygame.draw.rect(screen, (0, 183, 0), (615, 505, self.loading_cont, 10), 5, 2)
self.loading_cont += 1
lsun = 2000
else:
lsun += 0.01
screen.fill((0, 0, 0))
screen.blit(BATTLE_BAC, (0, 0))
if 7 * lcont < 146:
suanfa()
if timer <= 600:
timer += 1
else:
rzombielist.append(Zombie(0, 0, True, 0))
timer = 0
else:
win1 = True
occupy1 = True
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.