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()
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")
text = '1000'
myfont = pygame.font.SysFont('arial', 20)
txtImg = myfont.render(text, True, (0, 0, 0))
peashooter = Peashooter()
sunFlower = SunFlower()
wallNut = WallNut()
zombie = Zombie()
spriteList = pygame.sprite.Group()
spriteList.add(peashooter)
spriteList.add(sunFlower)
spriteList.add(wallNut)
spriteList.add(zombie)
sunList = pygame.sprite.Group()
#定义生成太阳的事件
GENERATORSUNEVNET = pygame.USEREVENT + 1
pygame.time.set_timer(GENERATORSUNEVNET, 2000)
index = 0
choose = 0
clock = pygame.time.Clock()
while True:
    if index > 100: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")
score = '100'
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()
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, 2000)
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:
            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:
                    choose = 1
                elif 380 < x <= 430 and 10 <= y <= 80:
                    choose = 2
                elif 430 < x <= 480 and 10 <= y <= 80:
                    choose = 3
                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))
    print(choose)
    # 根据选中的卡片,将对应的植物图片,显示在当前鼠标的右下角,跟随鼠标移动
    (x, y) = pygame.mouse.get_pos()
    if choose == 1:
        screen.blit(sunFlower.images[0], (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)
    spriteList.update(index)
    spriteList.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()


目录
相关文章
|
7月前
|
定位技术 Python
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
1644 1
|
1月前
|
JSON 开发工具 git
基于Python和pygame的植物大战僵尸游戏设计源码
本项目是基于Python和pygame开发的植物大战僵尸游戏,包含125个文件,如PNG图像、Python源码等,提供丰富的游戏开发学习素材。游戏设计源码可从提供的链接下载。关键词:Python游戏开发、pygame、植物大战僵尸、源码分享。
|
7月前
|
算法 数据可视化 定位技术
【python实操】如何通过简单拖拽直接开发软件,避免手写界面布局
【python实操】如何通过简单拖拽直接开发软件,避免手写界面布局
128 0
|
存储 JSON 搜索推荐
Python植物大战僵尸源码分享
Python植物大战僵尸源码分享
457 0
|
存储 网络协议 小程序
我的小工具,用C和python实现远程读卡器,远程读写消费卡片
我的小工具,用C和python实现远程读卡器,远程读写消费卡片
我的小工具,用C和python实现远程读卡器,远程读写消费卡片
|
Python
python&pygame植物大战僵尸后续课程及项目资源
python&pygame植物大战僵尸后续课程及项目资源
174 0
|
开发框架 Python
手把手教你使用python开发植物大战僵尸游戏
手把手教你使用python开发植物大战僵尸游戏
265 0
|
Python
python植物大战僵尸二十六之打包游戏为exe可执行文件
python植物大战僵尸二十六之打包游戏为exe可执行文件
228 0
|
23天前
|
人工智能 数据可视化 数据挖掘
探索Python编程:从基础到高级
在这篇文章中,我们将一起深入探索Python编程的世界。无论你是初学者还是有经验的程序员,都可以从中获得新的知识和技能。我们将从Python的基础语法开始,然后逐步过渡到更复杂的主题,如面向对象编程、异常处理和模块使用。最后,我们将通过一些实际的代码示例,来展示如何应用这些知识解决实际问题。让我们一起开启Python编程的旅程吧!
|
22天前
|
存储 数据采集 人工智能
Python编程入门:从零基础到实战应用
本文是一篇面向初学者的Python编程教程,旨在帮助读者从零开始学习Python编程语言。文章首先介绍了Python的基本概念和特点,然后通过一个简单的例子展示了如何编写Python代码。接下来,文章详细介绍了Python的数据类型、变量、运算符、控制结构、函数等基本语法知识。最后,文章通过一个实战项目——制作一个简单的计算器程序,帮助读者巩固所学知识并提高编程技能。