python 打飞机项目 ( 基类封装 )
项目代码 | plane
# -*- coding:utf-8 -*-
import pygame, time
from Plane import Plane
from HeroPlane import HeroPlane
from Screen import Screen
from pygame.locals import * def key_control(plane_temp):
# 获取事件,比如按键等
for event in pygame.event.get(): # 判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
# 判断是否是按下了键
elif event.type == KEYDOWN:
# 检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
plane_temp.move_left()
# 检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
plane_temp.move_right()
# 检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
plane_temp.fire() def main(): screen = pygame.display.set_mode((480, 852), 0, 32) # 创建窗口对象
screen_temp = Screen(screen) # 创建一个飞机对象
plane = Plane(screen) # 创建敌机对象
enemyPlane = HeroPlane(screen) while True:
screen_temp.display() # 显示窗口
plane.display(plane.bullet_list) # 显示飞机
enemyPlane.display(enemyPlane.bullet_list) # 敌机显示
enemyPlane.move() # 敌机移动
enemyPlane.fire() # 敌机开火
key_control(plane) # 键盘事件监听
pygame.display.update() # 更新窗口
time.sleep(0.01) # 延时0.01秒,防止程序内存溢出 if __name__ == '__main__':
main()
Base.py
基类
# -*- coding:utf-8 -*-
import pygame class Base(object):
# 背景图片
image = None def __init__(self, screen_temp, x, y, image_path):
self.x = x
self.y = y
self.screen = screen_temp
self.image_load(image_path) # 飞机赋值图片对象
def image_load(self, image_path):
self.image = pygame.image.load(image_path)
BasePlane.py
飞机基类
# -*- coding:utf-8 -*-
from Base import Base class BasePlane(Base):
def __init__(self, screen_temp, x, y, image_path):
Base.__init__(self, screen_temp, x, y, image_path) # 显示飞机
def display(self, bullet_list): self.screen.blit(self.image, (self.x, self.y)) # 显示飞机 for bullet in bullet_list: # 循环取出子弹对象
# 判断子弹是否越界
if bullet.judge():
bullet_list.remove(bullet) # 如果子弹越界就删除子弹 bullet.display() # 显示子弹
bullet.move() # 子弹移动步长
Plane.py
飞机对象
# -*- coding:utf-8 -*-
from Bullet import Bullet
from BasePlane import BasePlane class Plane(BasePlane): # 储存子弹对象
bullet_list = [] def __init__(self, screen_temp):
BasePlane.__init__(self, screen_temp, 210, 700, "./resource/hero1.png") # 飞机向左移动偏移量
def move_left(self):
self.x -= 10 # 飞机向右移动偏移量
def move_right(self):
self.x += 10 # 将飞机创建的子弹对象存储进 bullet_list 列表中
def fire(self):
self.bullet_list.append(Bullet(self.screen, self.x, self.y))
print(self.bullet_list)
HeroPlane.py
敌机对象
# -*- coding:utf-8 -*-
import random
from BasePlane import BasePlane
from EnemyBullet import EnemyBullet class HeroPlane(BasePlane):
# 定义一个类属性用来保存
direction = 'right' # 储存子弹对象
bullet_list = [] def __init__(self, screen_temp):
BasePlane.__init__(self, screen_temp, 0, 0, "./resource/enemy-1.gif") # 敌机移动轨迹
def move(self):
# 敌机创建的子弹默认向右移动
if self.direction == 'right':
self.x += 5 # 每次向右移动增加 5px 的步长
elif self.direction == 'left': # 向左移动
self.x -= 5 # 每次向左移动减少 5px 的步长 # 当敌机向右移动到了边界就向左移动
if self.x > 480 - 50: # 480 是界面总宽度; 50 是飞机宽度. 所以敌机移动的距离应该是界面宽度-敌机宽度 ( 移动距离 = 界面宽度 - 敌机宽度 )
self.direction = 'left'
elif self.x <= 0: # 当敌机移动到了最左边就会继续向右移动
self.direction = 'right' # 开火
def fire(self):
random_temp = random.randint(1, 100) # 随机生成 1 - 100的随机数
if (random_temp == 20) or (random_temp == 78): # 随机数概率
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) # 创建敌机子弹对象
BaseBullet.py
子弹基类
# -*- coding:utf-8 -*-
from Base import Base class BaseBullet(Base):
def __init__(self, screen_temp, x, y, image_path):
Base.__init__(self, screen_temp, x, y, image_path) # 子弹背景
def display(self):
self.screen.blit(self.image, (self.x, self.y))
Bullet.py
子弹对象
# -*- coding:utf-8 -*-
from BaseBullet import BaseBullet class Bullet(BaseBullet):
def __init__(self, screen_temp, x, y):
BaseBullet.__init__(self, screen_temp, x + 40, y - 20, "./resource/bullet.png") # 子弹步长
def move(self):
self.y -= 10 # 判断子弹y轴是否已经越界
def judge(self):
if self.y < 0:
return True
else:
return False
EnemyBullet.py
敌机子弹对象
# -*- coding:utf-8 -*-
from BaseBullet import BaseBullet class EnemyBullet(BaseBullet):
def __init__(self, screen_temp, x, y):
BaseBullet.__init__(self, screen_temp, x + 30, y + 30, "./resource/bullet-1.gif") # 子弹移动步长
def move(self):
self.y += 20 # 判断子弹y轴是否已经越界
def judge(self):
if self.y > 890: # 890 界面总高度
return True
else:
return False
Screen.py
窗口对象
# -*- coding:utf-8 -*-
from Base import Base class Screen(Base):
def __init__(self, screen_temp):
Base.__init__(self, screen_temp, 0, 0, "./resource/background.png") def display(self):
self.screen.blit(self.image, (self.x, self.y))
python 打飞机项目 ( 基类封装 )的更多相关文章
- 四、spring集成ibatis进行项目中dao层基类封装
Apache iBatis(现已迁至Google Code下发展,更名为MyBatis)是当前IT项目中使用很广泛的一个半自动ORM框架,区别于Hibernate之类的全自动框架,iBatis对数据库 ...
- salesforce 零基础学习(四十八)自定义列表分页之Pagination基类封装 ※※※
我们知道,salesforce中系统标准列表页面提供了相应的分页功能,如果要使用其分页功能,可以访问http://www.cnblogs.com/zero-zyq/p/5343287.html查看相关 ...
- thinkphp5底层基类封装、内部类函数
记录下thinkphp5自定义底层基类.内部类函数使用笔记 大部分笔记来自tp手册. 底层常用代码的封装 在控制器中基类的起着至关重要的作用,整个项目的代码安全,复杂程度,易读性都要看你项目的基类架构 ...
- Android 开发技巧 - Android 6.0 以上权限大坑和权限检查基类封装
简单介绍 关于运行时权限的说法,早在Google发布android 6.0的时候,大家也听得蛮多的.从用户的角度来讲,用户是受益方,更好的保护用户的意思,而对于开发者来说,无疑增加了工作量. 对于6. ...
- Python高级主题:Python ABC(抽象基类)
#抽象类实例 作用统一规范接口,降低使用复杂度.import abcclass Animal(metaclass = abc.ABCMeta): ##只能被继承,不能实例化,实例化会报错 @abc.a ...
- Python中的抽象基类
1.说在前头 "抽象基类"这个词可能听着比较"深奥",其实"基类"就是"父类","抽象"就是&quo ...
- mongodb基类封装实例
mongodb的基类 1 <?php 2 3 namespace BI\Service\MongoDB; 4 5 use MongoDB\Driver\BulkWrite; 6 use Mong ...
- Python一个简单的数据库类封装
#encoding:utf-8 #name:mod_db.py '''使用方法:1.在主程序中先实例化DB Mysql数据库操作类. 2.使用方法:db=database() db.fet ...
- python 打飞机项目 (实战一)
第一步定义 main 函数: # -*- coding=utf-8 -*- import pygame,time from Plane import Plane from pygame.locals ...
随机推荐
- [ch04-02] 用梯度下降法解决线性回归问题
系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 4.2 梯度下降法 有了上一节的最小二乘法做基准,我们这 ...
- jenkins System error
背景 在使用WAR包安装jenkins后,启动tomcat,显示启动成功,但最后提示信息如下: 04-Dec-2018 03:28:21.563 WARNING [Computer.threadPoo ...
- AWK工具 使用介绍
第6周第5次课(4月27日) 课程内容: 9.6/9.7 awk扩展把这里面的所有练习题做一下http://www.apelearn.com/study_v2/chapter14.html 9.6/9 ...
- 高并发编程-CountDownLatch深入解析
要点解说 CountDownLatch允许一个或者多个线程一直等待,直到一组其它操作执行完成.在使用CountDownLatch时,需要指定一个整数值,此值是线程将要等待的操作数.当某个线程为了要执行 ...
- 如何在DevOps中实施连续测试
在过去的十年中,对软件开发的需求已急剧发展.软件已成为公司获得竞争优势的关键优势,特别是如果您的公司属于SaaS范畴.通过在SDLC中实施瀑布等传统流程,组织现在正在向敏捷过渡,以便以更快的速度在市场 ...
- Python异常处理与上下文管理器
Python异常处理 异常与错误 错误 可以通过IDE或者解释器给出提示的错误opentxt('a.jpg','r') 语法层面没有问题,但是自己代码的逻辑有问题if age>18: print ...
- java 反射借助 asm 获取参数名称最优雅简单的方式
背景说明 最近写反射相关的代码,想获取对应的参数名称,却发现没有特别好的方式. jdk7 及其以前,是无法通过反射获取参数名称的. jdk8 可以获取,但是要求指定 -parameter 启动参数,限 ...
- FIve in a row
Alice and Bob play 5-in-a-row game. They have a playing field of size 10 × 10. In turns they put eit ...
- SPOJ Distanct Substrings(求不同子串的数量)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- CodeForces1000A-Light It Up
B. Light It Up time limit per test 1 second memory limit per test 256 megabytes input standard input ...