装备东西: 搭建好python环境, 四张图片,(背景图片,炮弹图片,僵尸图片,豌豆图片),就ok了  没有安装pygame的需要进行安装  pip install pygame

  参考视频

# 植物大战僵尸的练习 使用 pygame  模块进行开发

import pygame
from pygame.locals import *
import random # 1.创建窗口
# 2.显示豌豆
# 3.通过键盘控制豌豆上下移动
WIDTH = 1200
HEIGHT = 600 # 创建豌豆对象
class Peas: def __init__(self):
self.image_source = pygame.image.load('./res/peas.png')
self.image_new = pygame.transform.scale(self.image_source, (100, 100))
self.image_rect = self.image_new.get_rect()
# 初始化位置
self.image_rect.top = 280
self.image_rect.left = 60
# 设置豌豆是否上下移动
self.is_move_up = False
self.is_move_down = False
# 是否发射炮弹
self.is_shout = False def dispaly(self):
""" 豌豆显示在页面上"""
mode.blit(self.image_new, self.image_rect) def move_up(self):
if self.image_rect.top > 90:
self.image_rect.move_ip(0, -6) def move_down(self):
if self.image_rect.bottom < 600:
self.image_rect.move_ip(0, 6) def shout_bullet(self):
# 创建一个炮弹对象
bullet = Bullet(self);
# 保存创建好的炮弹对象
Bullet.bullet_list.append(bullet) # 炮弹对象
class Bullet:
bullet_list = [] # 创建一个类对象
interval = 0 # 炮弹的间隔 def __init__(self,peas):
self.image_source = pygame.image.load('./res/bullet.png')
self.image_new = pygame.transform.scale(self.image_source, (50, 50))
self.image_rect = self.image_new.get_rect()
# 初始化位置(和豌豆有关系)
self.image_rect.top = peas.image_rect.top
self.image_rect.left = peas.image_rect.right def display(self):
mode.blit(self.image_new, self.image_rect) def move(self):
self.image_rect.move_ip(10, 0)
# 如果炮弹越界移除炮弹
if self.image_rect.left > WIDTH - 100:
Bullet.bullet_list.remove(self) # 如果炮弹碰撞到僵尸,僵尸消失
for item in Zombie.zombie_list:
if self.image_rect.colliderect(item.image_rect): # 碰撞机制
Bullet.bullet_list.remove(self)
Zombie.zombie_list.remove(item)
break # 僵尸的出现和移动
class Zombie:
# 保存多个僵尸
zombie_list = []
interval = 0 # 僵尸创建频率 def __init__(self):
self.image_source = pygame.image.load('./res/zombie.png')
self.image_new = pygame.transform.scale(self.image_source, (150, 150))
self.image_rect = self.image_new.get_rect()
# 初始化位置
self.image_rect.top = random.randint(100,HEIGHT - 100)
self.image_rect.left = WIDTH - 100 def display(self):
mode.blit(self.image_new, self.image_rect) def move(self):
self.image_rect.move_ip(-3, 0)
# 如果炮弹越界移除炮弹
if self.image_rect.left < 20:
Zombie.zombie_list.remove(self) # 如果炮弹碰撞到僵尸,僵尸消失
for item in Bullet.bullet_list:
if self.image_rect.colliderect(item.image_rect):
Bullet.bullet_list.remove(item)
Zombie.zombie_list.remove(self)
break def key_control():
'''时间监听'''
# 对事件进行处理 它监听的是一个列表
for item in pygame.event.get():
# 事件类型进行判断
if item.type == QUIT:
pygame.quit()
exit()
# 按下键盘的事件判断
if item.type == KEYDOWN: # 也就是说,只要键盘向下,一直是True
# 判断具体的键
if item.key == K_UP:
print("向上移动")
# 移动
peas.is_move_up = True
elif item.key == K_DOWN:
print("向下移动")
peas.is_move_down = True
elif item.key == K_SPACE:
print("空格键按下")
peas.is_shout = True
elif item.type == KEYUP:
# 判断具体的键
if item.key == K_UP:
print("向上移动")
peas.is_move_up = False
elif item.key == K_DOWN:
print("向下移动")
peas.is_move_down = False
elif item.key == K_SPACE:
print("空格键松开")
peas.is_shout = False if __name__ == '__main__':
# 显示窗体
mode = pygame.display.set_mode((WIDTH, HEIGHT))
# 为了使图片完全覆盖窗口,涉及到一个坐标,就是从左上角开始
# 加载图片
bg = pygame.image.load('./res/bg.jpg')
# 改变图片大小 生成新的图片
bg = pygame.transform.scale(bg,(WIDTH,HEIGHT))
# 获取图片的位置和大小
bg_rect = bg.get_rect() # 创建一个时钟,优化速度效果
clock = pygame.time.Clock()
# 创建豌豆对象
peas = Peas();
# 为了方式程序结束,窗口消失,需要写一个死循环
while True:
# 设置背景颜色(颜色的填充)
mode.fill((0, 0, 0))
# 图片和窗口的关联
mode.blit(bg, bg_rect)
# 调用豌豆的显示方法
# 显示豌豆
peas.dispaly()
# 事件的调用
key_control() if peas.is_move_up:
peas.move_up() if peas.is_move_down:
peas.move_down() # 发射炮弹
Bullet.interval += 1
if peas.is_shout and Bullet.interval >= 20:
Bullet.interval = 0
peas.shout_bullet() # 显示所有的炮弹
for bullet in Bullet.bullet_list:
bullet.display()
bullet.move() # 创建僵尸对象
Zombie.interval += 1
if Zombie.interval >= 20:
Zombie.interval = 0
zombie = Zombie();
Zombie.zombie_list.append(zombie) # 显示所有的僵尸
for zombie in Zombie.zombie_list:
zombie.display()
zombie.move() # 动画变化的帧频率
clock.tick(60)
# 显示图片
pygame.display.update()

植物大战僵尸游戏的开发(python)的更多相关文章

  1. Python 开发植物大战僵尸游戏

    作者:楷楷 链接:https://segmentfault.com/a/1190000019418065 开发思路 完整项目地址: https://github.com/371854496/pygam ...

  2. space defender,太空版植物大战僵尸 游戏基本框架的设计

  3. 用 Python 实现植物大战僵尸代码!

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: marble_xu GitHub地址:https://github ...

  4. Python 植物大战僵尸代码实现: 图片加载和显示切换

    游戏介绍以前很火的植物大战僵尸游戏, 本想在网上找个python版本游戏学习下,无奈没有发现比较完整的,那就自己来写一个把.图片资源是从github上下载的,因为图片资源有限,只能实现几种植物和僵尸. ...

  5. 植物大战僵尸的代码如何使用python来实现

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:程序IT圈 PS:如有需要Python学习资料的小伙伴可以加点击下方链 ...

  6. 植物大战僵尸作弊器源代码(MFC版)

    控制版使用不太方便,此MFC版与控制台版内容一样.具体可以参考前面.此处只附源代码,不加以说明.......... 头文件 // jsMFCDlg.h : 头文件 // #pragma once // ...

  7. 使用Python对植物大战僵尸学习研究

    根据上一篇 使用Python读写游戏1 中,使用Python win32库,对一款游戏进行了读内存 操作. 今天来写一下对内存进行写的操作 正文 要进行32位的读写,首先了解一下要用到的几个函数,通过 ...

  8. 植物大战僵尸中文第二版和年度版 游戏分析及delphi源码

    00413184 |. E8 77E30100   |CALL PlantsVs.00431500                  ; 地上的物品00413189 |. 8D7424 10     ...

  9. Python游戏引擎开发(七):绘制矢量图

    今天来完毕绘制矢量图形. 没有读过前几章的同学,请先阅读前几章: Python游戏引擎开发(一):序 Python游戏引擎开发(二):创建窗体以及重绘界面 Python游戏引擎开发(三):显示图片 P ...

随机推荐

  1. [ZJOI 2008] 骑士

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1040 [算法] 首先 , 题目中互相讨厌的关系构成了一棵基环森林 用拓扑排序找出环 ...

  2. 【转】 JUnit单元测试--IntelliJ IDEA

    原文地址:https://blog.csdn.net/weixin_38104426/article/details/74388375 使用idea IDE 进行单元测试,首先需要安装JUnit 插件 ...

  3. 用 SDL2 在屏幕上打印文本

    打印完图片,是时候打印文字了.这里引用了SDL的字体扩展库,SDL2_ttf.lib,需要包含相应的头文件. 环境:SDL2 + VC++2015 下面的代码将在窗口打印一段文字,并对相应的操作做出响 ...

  4. windows上搭建php环境

    在Windows 7下进行PHP环境搭建,首先需要下载PHP代码包和Apache与Mysql的安装软件包. PHP版本:php-5.3.2-Win32-VC6-x86,VC9是专门为IIS定制的,VC ...

  5. Table View Programming Guide for iOS---(七)---Managing Selections

    Managing Selections 管理选择 When users tap a row of a table view, usually something happens as a result ...

  6. Windows 下openssl安装与配置

    编译thirift失败 网上方法很多,大部分是针对32位机的,自己的电脑因为是win7,64位,摸索了很久才安装成功. 环境 WIN7, 64位, vs2005 下载ActivePerl 配置过程中需 ...

  7. 更换过Ubuntu之后经常性卡死,原因有待细究

    如题: 卡死时间: 2019-5-22-14:45 再次卡死,这次绝对不是看视频的原因了,具体什么原因还是不知道,不过我觉得就是显卡的问题,和搜索出来的问题差不多,安装了一些东西,看看行不行吧 sud ...

  8. [洛谷P4185] [USACO18JAN]MooTube

    题目链接: 传送门 题意: 给定一颗N个节点的树,定义两点距离为他们之间路径中边权最小值. Q次询问K,V,询问到V距离>=K的点有多少(不含V) 呃呃呃呃考试的时候直奔了T3,结果公式推挂了( ...

  9. django 相关问题

    和数据库的连接 session的实现 django app开发步骤 python环境准备 数据库安装 model定义 url mapping定义 view定义 template定义 如何查看数据库里的 ...

  10. 微服务dubbo面试题

    dubbo的工作原理? dubbo支持的序列化协议? dubbo的负载均衡和高可用策略?动态代理策略? dubbo的SPI思想? 如何基于dubbo进行服务治理.服务降级.失败重试以及超时重试? du ...