python植物大战僵尸二十之僵尸吃豌豆射手

简介: 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("植物大战僵尸")
#初始化音乐模块
pygame.mixer.init()
#加载音乐
pygame.mixer.music.load("material/music/02 - Crazy Dave (Intro Theme).mp3")
# 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)
    ##如果没有音乐流则选择播放
    if not pygame.mixer.music.get_busy():
        pygame.mixer.music.play()
    # 启动消息队列,获取消息并处理
    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 zombie in zombieList:
        for peashooter in peashooterList:
            if pygame.sprite.collide_mask(zombie, peashooter):
                zombie.isMeetWallNut = True
                peashooter.zombies.add(zombie)
    for zombie in flagZombieList:
        for peashooter in peashooterList:
            if pygame.sprite.collide_mask(zombie, peashooter):
                zombie.isMeetWallNut = True
                peashooter.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 Peashooter(pygame.sprite.Sprite):
    def __init__(self):
        super(Peashooter,self).__init__()
        self.image = pygame.image.load('material/images/Peashooter_00.png').convert_alpha()
        self.images = [pygame.image.load('material/images/Peashooter_{:02d}.png'.format(i)).convert_alpha() for i in
                       range(0, 13)]
        self.rect = self.images[0].get_rect()
        self.rect.top = 80
        self.rect.left = 250
        self.zombies = set()
        self.energy = 3 * 15
    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()
        self.image = self.images[args[0] % len(self.images)]


目录
相关文章
|
6月前
|
定位技术 Python
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
1436 1
|
存储 JSON 搜索推荐
Python植物大战僵尸源码分享
Python植物大战僵尸源码分享
424 0
|
Python
python&pygame植物大战僵尸后续课程及项目资源
python&pygame植物大战僵尸后续课程及项目资源
161 0
|
开发框架 Python
手把手教你使用python开发植物大战僵尸游戏
手把手教你使用python开发植物大战僵尸游戏
256 0
|
3天前
|
机器学习/深度学习 人工智能 TensorFlow
人工智能浪潮下的自我修养:从Python编程入门到深度学习实践
【10月更文挑战第39天】本文旨在为初学者提供一条清晰的道路,从Python基础语法的掌握到深度学习领域的探索。我们将通过简明扼要的语言和实际代码示例,引导读者逐步构建起对人工智能技术的理解和应用能力。文章不仅涵盖Python编程的基础,还将深入探讨深度学习的核心概念、工具和实战技巧,帮助读者在AI的浪潮中找到自己的位置。
|
3天前
|
机器学习/深度学习 数据挖掘 Python
Python编程入门——从零开始构建你的第一个程序
【10月更文挑战第39天】本文将带你走进Python的世界,通过简单易懂的语言和实际的代码示例,让你快速掌握Python的基础语法。无论你是编程新手还是想学习新语言的老手,这篇文章都能为你提供有价值的信息。我们将从变量、数据类型、控制结构等基本概念入手,逐步过渡到函数、模块等高级特性,最后通过一个综合示例来巩固所学知识。让我们一起开启Python编程之旅吧!
|
3天前
|
存储 Python
Python编程入门:打造你的第一个程序
【10月更文挑战第39天】在数字时代的浪潮中,掌握编程技能如同掌握了一门新时代的语言。本文将引导你步入Python编程的奇妙世界,从零基础出发,一步步构建你的第一个程序。我们将探索编程的基本概念,通过简单示例理解变量、数据类型和控制结构,最终实现一个简单的猜数字游戏。这不仅是一段代码的旅程,更是逻辑思维和问题解决能力的锻炼之旅。准备好了吗?让我们开始吧!
|
5天前
|
设计模式 算法 搜索推荐
Python编程中的设计模式:优雅解决复杂问题的钥匙####
本文将探讨Python编程中几种核心设计模式的应用实例与优势,不涉及具体代码示例,而是聚焦于每种模式背后的设计理念、适用场景及其如何促进代码的可维护性和扩展性。通过理解这些设计模式,开发者可以更加高效地构建软件系统,实现代码复用,提升项目质量。 ####
|
4天前
|
机器学习/深度学习 存储 算法
探索Python编程:从基础到高级应用
【10月更文挑战第38天】本文旨在引导读者从Python的基础知识出发,逐渐深入到高级编程概念。通过简明的语言和实际代码示例,我们将一起探索这门语言的魅力和潜力,理解它如何帮助解决现实问题,并启发我们思考编程在现代社会中的作用和意义。
|
5天前
|
机器学习/深度学习 数据挖掘 开发者
Python编程入门:理解基础语法与编写第一个程序
【10月更文挑战第37天】本文旨在为初学者提供Python编程的初步了解,通过简明的语言和直观的例子,引导读者掌握Python的基础语法,并完成一个简单的程序。我们将从变量、数据类型到控制结构,逐步展开讲解,确保即使是编程新手也能轻松跟上。文章末尾附有完整代码示例,供读者参考和实践。