python植物大战僵尸十一之太阳花摆放

简介: python植物大战僵尸十一之太阳花摆放
import pygame
from pygame.locals import *
import sys
from Bullet import Bullet
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("植物大战僵尸")
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/TwinSunflower.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()
score = '500'
myfont = pygame.font.SysFont('arial', 20)
txtImg = myfont.render(score, True, (0, 0, 0))
peashooter = Peashooter()
# sunFlower = SunFlower()
wallNut = WallNut()
# zombie = Zombie()
spriteList = pygame.sprite.Group()
sunFlowerList = pygame.sprite.Group()
spriteList.add(peashooter)
# spriteList.add(sunFlower)
spriteList.add(wallNut)
# spriteList.add(zombie)
sunList = pygame.sprite.Group()
zombieList = pygame.sprite.Group()
index = 0
clock = pygame.time.Clock()
GENERATOR_SUN_EVENT = pygame.USEREVENT + 1
pygame.time.set_timer(GENERATOR_SUN_EVENT, 5000)
GENERATOR_ZOMBIE_EVENT = pygame.USEREVENT + 2
pygame.time.set_timer(GENERATOR_ZOMBIE_EVENT, 5000)
choose = 0
while True:
    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:
                for sunFlower in sunFlowerList:
                    sun = Sun(sunFlower.rect)
                    sunList.add(sun)
        if event.type == GENERATOR_ZOMBIE_EVENT:
            zombie = Zombie()
            zombieList.add(zombie)
        if event.type == MOUSEBUTTONDOWN:
            mouse_pressed = pygame.mouse.get_pressed()
            # 判断是否按下的事鼠标左键
            if mouse_pressed[0]:
                (x, y) = pygame.mouse.get_pos()
                # 判断鼠标是否点中了某个卡片
                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()
                        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))
                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))
    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(wallNut.images[0], (x, y))
    if choose == 3:
        screen.blit(peashooter.images[0], (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)
    for zombie in zombieList:
        headStr = '刘无敌'
        yourfont = pygame.font.SysFont('simsunnsimsun', 30)
        headpic = yourfont.render(headStr, True, (0, 0, 0))
        screen.blit(headpic, (zombie.rect.left + 60, zombie.rect.top - 20))
    index += 1
    pygame.display.update()

'''太阳花'''
import pygame
class SunFlower(pygame.sprite.Sprite):
    def __init__(self):
        super(SunFlower, self).__init__()
        self.image = pygame.image.load('material/images/SunFlower_00.png').convert_alpha()
        self.images = [pygame.image.load('material/images/SunFlower_{:02d}.png'.format(i)).convert_alpha() for i in
                       range(0, 13)]
        self.rect = self.images[0].get_rect()
        # self.rect.top = 380
        # self.rect.left = 250
    def update(self, *args):
        self.image = self.images[args[0] % len(self.images)]


目录
相关文章
|
2月前
|
定位技术 Python
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
840 1
|
17天前
|
Python
【python绘图库turtle实战】使用python绘图库turtle绘制:太阳花、彩虹线与小黄人【含完整源码】
【python绘图库turtle实战】使用python绘图库turtle绘制:太阳花、彩虹线与小黄人【含完整源码】
|
8月前
|
存储 JSON 搜索推荐
Python植物大战僵尸源码分享
Python植物大战僵尸源码分享
286 0
|
Python
python&pygame植物大战僵尸后续课程及项目资源
python&pygame植物大战僵尸后续课程及项目资源
138 0
|
开发框架 Python
手把手教你使用python开发植物大战僵尸游戏
手把手教你使用python开发植物大战僵尸游戏
232 0
|
Python
python植物大战僵尸二十六之打包游戏为exe可执行文件
python植物大战僵尸二十六之打包游戏为exe可执行文件
190 0
|
8天前
|
机器学习/深度学习 人工智能 前端开发
Python中的模块化编程
【6月更文挑战第17天】Python模块化编程与软件架构设计的关键在于拆分任务到独立模块,提高代码的可维护性、可重用性和可扩展性。例如,学生管理系统可分解为录入、查询和删除模块。MVC和MVVM架构模式有助于组织代码,而微服务和函数式编程将在未来发展中扮演重要角色。通过示例代码,读者能学习如何实现这些概念,提升项目开发效率和质量。
155 57
|
15天前
|
测试技术 虚拟化 云计算
GitHub高赞!速通Python编程基础手册,被玩出花了!
随着云时代的来临,Python 语言越来越被程序开发人员喜欢和使用,因为其不仅简单易学,而且还有丰富的第三方程序库和相应完善的管理工具。 从命令行脚本程序到 GUI程序,从图形技术到科学计算,从软件开发到自动化测试,从云计算到虚拟化,所有这些领域都有 Python 的身影。 今天给小伙伴们分享的这份手册采用以任务为导向的编写模式,全面地介绍了 Python 编程基础及其相关知识的应用,讲解了如何利用 Python 的知识解决部分实际问题。
GitHub高赞!速通Python编程基础手册,被玩出花了!
|
4天前
|
数据挖掘 数据处理 Python
Python编程入门:从基础到实践
【6月更文挑战第26天】这篇文章引导读者逐步学习Python编程,从基础语法如变量、数据类型(整数、浮点数、字符串)到条件语句、循环(if/for/while),再到函数定义和模块导入。通过实例展示了Python在文本处理、数据分析(使用pandas)和Web开发(使用Flask)的应用。学习Python能为初学者开启更广阔的技术领域,如面向对象编程、并发和网络编程等。
|
2天前
|
设计模式 程序员 测试技术
老程序员分享:Python数据模型及Pythonic编程
老程序员分享:Python数据模型及Pythonic编程
13 1