python植物大战僵尸十九之僵尸吃坚果

import pygame
from pygame.locals import *
import sys
import time
import random

from Bullet import Bullet
from FlagZombie import FlagZombie
from Peashooter import Peashooter
from Sun import Sun
from SunFlower import SunFlower
from WallNut import WallNut

# 初始化pygame
from Zombie import Zombie

pygame.init()

# for font in pygame.font.get_fonts():
#     print(font)

size = (1200, 600)

# 设置屏幕宽高
screen = pygame.display.set_mode(size)
# 设置屏幕标题
pygame.display.set_caption("植物大战僵尸")

# logo = pygame.image.load('material/images/logo.jpg').convert_alpha()
# pygame.display.set_icon(logo)

backgroundImg = pygame.image.load('material/images/background1.jpg').convert_alpha()
sunbackImg = pygame.image.load('material/images/SeedBank.png').convert_alpha()
flower_seed = pygame.image.load("material/images/Sunflower.gif")
wallNut_seed = pygame.image.load("material/images/WallNut.gif")
peashooter_seed = pygame.image.load("material/images/Peashooter.gif")
sunFlowerImg = pygame.image.load('material/images/SunFlower_00.png').convert_alpha()
wallNutImg = pygame.image.load('material/images/WallNut_00.png').convert_alpha()
peashooterImg = pygame.image.load('material/images/Peashooter_00.png').convert_alpha()

score = '500'
myfont = pygame.font.SysFont('arial', 20)
txtImg = myfont.render(score, True, (0, 0, 0))

nameList = ['刘无敌 ', '刘国臣 ', '邸家兴 ', '陶倩', '大汉 ', '张新杰', '李彪 ', '纪勇博', ' 张榕', '楠姐 ', '天昊 ']

peashooterList = pygame.sprite.Group()
sunFlowerList = pygame.sprite.Group()
wallNutList = pygame.sprite.Group()
bulletList = pygame.sprite.Group()

sunList = pygame.sprite.Group()
zombieList = pygame.sprite.Group()
flagZombieList = pygame.sprite.Group()

index = 0
clock = pygame.time.Clock()

GENERATOR_SUN_EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(GENERATOR_SUN_EVENT, 100)

GENERATOR_ZOMBIE_EVENT = pygame.USEREVENT + 2
pygame.time.set_timer(GENERATOR_ZOMBIE_EVENT, 5000)

GENERATOR_PEASHOOTER_EVENT = pygame.USEREVENT + 3
pygame.time.set_timer(GENERATOR_PEASHOOTER_EVENT, 2000)

GENERATOR_FLAGZOMBIE_EVENT = pygame.USEREVENT + 4
pygame.time.set_timer(GENERATOR_FLAGZOMBIE_EVENT, 10000)

choose = 0

while True:

    # print(time.time())
    clock.tick(15)
    # 启动消息队列,获取消息并处理
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == GENERATOR_SUN_EVENT:
            # 当前是否有太阳花对象,有几个太阳花对象,就生成几个太阳
            if len(sunFlowerList) > 0:
                timeNow = time.time()
                for sunFlower in sunFlowerList:
                    if timeNow - sunFlower.lasttime >= 5:
                        sunFlower.lasttime = timeNow
                        sun = Sun(sunFlower.rect)
                        sunList.add(sun)

        if event.type == GENERATOR_PEASHOOTER_EVENT:
            # 当前是否有太阳花对象,有几个太阳花对象,就生成几个太阳
            if len(peashooterList) > 0:
                for peashooter in peashooterList:
                    bullet = Bullet(peashooter.rect, size)
                    bulletList.add(bullet)

        if event.type == GENERATOR_ZOMBIE_EVENT:
            randomIndex = random.randrange(0, len(nameList))
            nameOfZombie = nameList[randomIndex]
            zombie = Zombie(nameOfZombie)
            zombieList.add(zombie)

        if event.type == GENERATOR_FLAGZOMBIE_EVENT:
            flagZombie = FlagZombie()
            flagZombieList.add(flagZombie)

        if event.type == MOUSEBUTTONDOWN:
            mouse_pressed = pygame.mouse.get_pressed()
            # 判断是否按下的事鼠标左键
            if mouse_pressed[0]:
                (x, y) = pygame.mouse.get_pos()
                # print(x, y)

                # 判断鼠标是否点中了某个卡片
                if 330 <= x <= 380 and 10 <= y <= 80 and int(score) >= 50:
                    choose = 1
                elif 380 < x <= 430 and 10 <= y <= 80 and int(score) >= 50:
                    choose = 2
                elif 430 < x <= 480 and 10 <= y <= 80 and int(score) >= 100:
                    choose = 3
                elif 250 < x < 1200 and 70 < y < 600:
                    if choose == 1:
                        sunFlower = SunFlower(time.time())
                        sunFlower.rect.top = y
                        sunFlower.rect.left = x
                        sunFlowerList.add(sunFlower)
                        choose = 0

                        # 扣去太阳花相应的分数
                        score = int(score)
                        score -= 50
                        myfont = pygame.font.SysFont('arial', 20)
                        txtImg = myfont.render(str(score), True, (0, 0, 0))
                    if choose == 2:
                        wallNut = WallNut()
                        wallNut.rect.top = y
                        wallNut.rect.left = x
                        wallNutList.add(wallNut)
                        choose = 0

                        # 扣去太阳花相应的分数
                        score = int(score)
                        score -= 50
                        myfont = pygame.font.SysFont('arial', 20)
                        txtImg = myfont.render(str(score), True, (0, 0, 0))
                    if choose == 3:
                        peashooter = Peashooter()
                        peashooter.rect.top = y
                        peashooter.rect.left = x
                        peashooterList.add(peashooter)
                        choose = 0

                        # 扣去太阳花相应的分数
                        score = int(score)
                        score -= 100
                        myfont = pygame.font.SysFont('arial', 20)
                        txtImg = myfont.render(str(score), True, (0, 0, 0))

                for sun in sunList:
                    if sun.rect.collidepoint((x, y)):
                        # sunList.remove(sun)
                        sun.is_click = True
                        score = int(score) + 50
                        myfont = pygame.font.SysFont('arial', 20)
                        txtImg = myfont.render(str(score), True, (0, 0, 0))

    # 如果坚果和僵尸冲突:
    # 1.僵尸要停住,动画要变成吃坚果
    # 2.坚果要掉血(n个僵尸同时吃,要掉n个血)
    # 3.坚果血没后,要kill掉自己
    # 4.坚果吃完后,僵尸要继续向前走
    # 5.僵尸死后,坚果的僵尸集合中要移除该僵尸
    for zombie in zombieList:
        for wallNut in wallNutList:
            if pygame.sprite.collide_mask(zombie, wallNut):
                zombie.isMeetWallNut = True
                wallNut.zombies.add(zombie)

    for zombie in flagZombieList:
        for wallNut in wallNutList:
            if pygame.sprite.collide_mask(zombie, wallNut):
                zombie.isMeetWallNut = True
                wallNut.zombies.add(zombie)


    for bullet in bulletList:
        for zombie in zombieList:
            if pygame.sprite.collide_mask(bullet, zombie):
                zombie.energy -= 1
                bulletList.remove(bullet)

    for bullet in bulletList:
        for flagZombie in flagZombieList:
            if pygame.sprite.collide_mask(bullet, flagZombie):
                flagZombie.energy -= 1
                bulletList.remove(bullet)


    screen.blit(backgroundImg, (0, 0))
    screen.blit(sunbackImg, (250, 0))
    screen.blit(txtImg, (270, 60))

    screen.blit(flower_seed, (330, 10))
    screen.blit(wallNut_seed, (380, 10))
    screen.blit(peashooter_seed, (430, 10))

    # 根据选中的卡片,将对应的植物图片,显示在当前鼠标的右下角,跟随鼠标移动
    (x, y) = pygame.mouse.get_pos()

    if choose == 1:
        screen.blit(sunFlowerImg, (x, y))
    if choose == 2:
        screen.blit(wallNutImg, (x, y))
    if choose == 3:
        screen.blit(peashooterImg, (x, y))

    # if index % 10 == 0:
    #     bullet = Bullet(peashooter.rect, size)
    #     spriteList.add(bullet)

    sunFlowerList.update(index)
    sunFlowerList.draw(screen)
    sunList.update(index)
    sunList.draw(screen)
    zombieList.update(index)
    zombieList.draw(screen)
    wallNutList.update(index)
    wallNutList.draw(screen)
    peashooterList.update(index)
    peashooterList.draw(screen)
    bulletList.update(index)
    bulletList.draw(screen)
    flagZombieList.update(index)
    flagZombieList.draw(screen)

    for zombie in zombieList:
        yourfont = pygame.font.SysFont('simsunnsimsun', 30)
        headpic = yourfont.render(zombie.name, True, (255, 0, 0))
        screen.blit(headpic, (zombie.rect.left + 60, zombie.rect.top - 20))

    index += 1

    pygame.display.update()

'''坚果'''
import pygame


class WallNut(pygame.sprite.Sprite):
    def __init__(self):
        super(WallNut, self).__init__()
        self.image = pygame.image.load('material/images/WallNut_00.png').convert_alpha()
        self.images = [pygame.image.load('material/images/WallNut_{:02d}.png'.format(i)).convert_alpha() for i in
                       range(0, 13)]
        self.crackedImgs = [pygame.transform.smoothscale(pygame.image.load("material/images/Wallnut_body.png").convert_alpha(),
                                                         (self.image.get_rect().width, self.image.get_rect().height)),
                            pygame.transform.smoothscale(
                                pygame.image.load("material/images/Wallnut_cracked1.png").convert_alpha(),
                                (self.image.get_rect().width, self.image.get_rect().height)),
                            pygame.transform.smoothscale(
                                pygame.image.load("material/images/Wallnut_cracked2.png").convert_alpha(),
                                (self.image.get_rect().width, self.image.get_rect().height))]
        self.rect = self.images[0].get_rect()

        self.zombies = set()
        self.energy = 8 * 15
        # self.rect.top = 280
        # self.rect.left = 250

    def update(self, *args):
        for zoobie in self.zombies:
            if not zoobie.isAlive:
                continue
            self.energy -= 1
            if self.energy <= 0:
                for zoobie in self.zombies:
                    zoobie.isMeetWallNut = False
                self.kill()

        if self.energy == 8 * 15:
            self.image = self.images[args[0] % len(self.images)]
        elif 6*15 <= self.energy < 8 * 15:
            self.image = self.crackedImgs[0]
        elif 3 * 15 <= self.energy < 6 * 15:
            self.image = self.crackedImgs[1]
        else:
            self.image = self.crackedImgs[2]
'''僵尸'''
import pygame
import random


class Zombie(pygame.sprite.Sprite):
    def __init__(self, name):
        super(Zombie, self).__init__()
        self.image = pygame.image.load('material/images/Zombie_0.png').convert_alpha()
        self.images = [pygame.image.load('material/images/Zombie_{}.png'.format(i)).convert_alpha() for i in
                       range(0, 22)]
        self.dieimages = [pygame.image.load('material/images/ZombieDie_{}.png'.format(i)).convert_alpha() for i in
                          range(0, 10)]
        self.attackimages = [pygame.image.load('material/images/ZombieAttack_{}.png'.format(i)).convert_alpha() for i in
                          range(0, 21)]
        self.rect = self.images[0].get_rect()
        self.rect.top = 25 + random.randrange(0, 4) * 125
        # print(self.rect.top)
        self.rect.left = 1000
        self.speed = 2
        self.name = name
        self.energy = 6
        self.dietimes = 0
        self.isMeetWallNut = False #僵尸是否遇到了坚果
        self.isAlive = True

    def update(self, *args):
        if self.energy > 0:
            if self.isMeetWallNut:
                self.image = self.attackimages[args[0] % len(self.attackimages)]
            else:
                self.image = self.images[args[0] % len(self.images)]

            if self.rect.left > 250 and not self.isMeetWallNut:
                self.rect.left -= self.speed
        else:
            if self.dietimes > 9:
                if self.dietimes > 24:
                    self.kill()
                else:
                    self.isAlive = False
                    self.dietimes += 1
            else:
                self.image = self.dieimages[self.dietimes]
                self.dietimes += 1


'''旗帜僵尸'''
import pygame
import random

class FlagZombie(pygame.sprite.Sprite):
    def __init__(self):
        super(FlagZombie, self).__init__()
        self.image = pygame.image.load('material/images/FlagZombie_0.png').convert_alpha()
        self.images = [pygame.image.load('material/images/FlagZombie_{}.png'.format(i)).convert_alpha() for i in
                       range(0, 12)]
        self.dieimages = [pygame.image.load('material/images/ZombieDie_{}.png'.format(i)).convert_alpha() for i in
                          range(0, 10)]
        self.attackimages = [pygame.image.load('material/images/FlagZombieAttack_{}.png'.format(i)).convert_alpha() for i in
                             range(0, 11)]
        self.rect = self.images[0].get_rect()
        self.rect.top = 25 + random.randrange(0, 4) * 125
        self.rect.left = 1000
        self.speed = 4
        self.energy = 6
        self.dietimes = 0
        self.isMeetWallNut = False  # 僵尸是否遇到了坚果
        self.isAlive = True

    def update(self, *args):
        if self.energy > 0:
            if self.isMeetWallNut:
                self.image = self.attackimages[args[0] % len(self.attackimages)]
            else:
                self.image = self.images[args[0] % len(self.images)]

            if self.rect.left > 250 and not self.isMeetWallNut:
                self.rect.left -= self.speed
        else:
            if self.dietimes > 9:
                if self.dietimes > 24:
                    self.kill()
                else:
                    self.isAlive = False
                    self.dietimes += 1
            else:
                self.image = self.dieimages[self.dietimes]
                self.dietimes += 1



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,723评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,485评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,998评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,323评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,355评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,079评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,389评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,019评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,519评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,971评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,100评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,738评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,293评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,289评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,517评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,547评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,834评论 2 345

推荐阅读更多精彩内容