python-飞机大战
效果图

main.py
import time import pygame from EnemyPlane import EnemyPlane
from HeroPlane import HeroPlane
from KeyControl import key_control def main():
screen = pygame.display.set_mode((515, 960), 0, 32)
background = pygame.image.load("./images/background.png") hero = HeroPlane(screen)
enemy = EnemyPlane(screen) right = 0
down = 0
while True:
screen.blit(background, (0, 0)) hero.display()
hero.move(right, down) enemy.display()
enemy.move()
enemy.fire() pygame.display.update() right, down = key_control(hero, right, down) time.sleep(0.01) if __name__ == "__main__":
main()
Base.py
import pygame # 加载素材
class Base(object):
def __init__(self, screen_temp, x, y, image_path):
self.x = x
self.y = y
self.screen = screen_temp
self.image = pygame.image.load(image_path)
BasePlane.py
from Base import Base # 飞机基类
class BasePlane(Base):
def __init__(self, screen_temp, x, y, image_path):
super().__init__(screen_temp, x, y, image_path)
self.bullet_list = [] # 展示飞机
def display(self):
self.screen.blit(self.image, (self.x, self.y)) # 循环展示子弹
for bullet in self.bullet_list:
bullet.display()
bullet.move()
# 判断子弹是否越界
if bullet.judge():
self.bullet_list.remove(bullet)
BaseBullet.py
from Base import Base # 子弹基类
class BaseBullet(Base): # 展示子弹
def display(self):
self.screen.blit(self.image, (self.x, self.y))
HeroPlane.py
from BasePlane import BasePlane
from HeroBullet import HeroBullet # 英雄飞机类
class HeroPlane(BasePlane):
def __init__(self, screen_temp):
super().__init__(screen_temp, 204, 800, "./images/me.png") def move(self, x_right, y_down):
self.x += x_right
self.y += y_down # 开火
def fire(self):
# 子弹列表创建子弹对象
self.bullet_list.append(HeroBullet(self.screen, self.x, self.y))
EnemyPlane.py
import random from BasePlane import BasePlane
from EnemyBullet import EnemyBullet # 敌机类
class EnemyPlane(BasePlane):
def __init__(self, screen_temp):
super().__init__(screen_temp, 0, 0, "./images/e0.png")
self.direction = "right" # 左右移动
def move(self):
if self.direction == "right":
self.x += 5
elif self.direction == "left":
self.x -= 5 # 改变移动方向
if self.x > 399:
self.direction = "left"
elif self.x < 0:
self.direction = "right" # 开火
def fire(self):
# 子弹列表创建子弹对象
random_num = random.randint(1, 100)
if random_num == 25 or random_num == 75:
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))
HeroBullet.py
from BaseBullet import BaseBullet # 子弹类
class HeroBullet(BaseBullet):
def __init__(self, screen_temp, x, y):
super().__init__(screen_temp, x + 53, y - 20, "./images/pd.png") # 子弹移动
def move(self):
self.y -= 10 # 判断子弹是否越界
def judge(self):
if self.y < 0:
return True
else:
return False
EnemyBullet.py
from BaseBullet import BaseBullet # 敌机子弹
class EnemyBullet(BaseBullet):
def __init__(self, screen_temp, x, y):
super().__init__(screen_temp, x + 53, y + 82, "./images/epd.png") # 子弹移动
def move(self):
self.y += 10 # 判断子弹是否越界
def judge(self):
if self.y > 960:
return True
else:
return False
KeyControl.py
import pygame
from pygame.locals import * # 按键控制
def key_control(hero_temp, right, down):
for event in pygame.event.get():
if event.type == QUIT:
print("exit")
exit()
elif event.type == KEYUP:
print("keyup")
right = 0
down = 0
elif event.type == KEYDOWN:
# 按左键
if event.key == K_a or event.key == K_LEFT:
print('left')
right = -10
# 按右键
elif event.key == K_d or event.key == K_RIGHT:
print('right')
right = 10
# 按上键
elif event.key == K_w or event.key == K_UP:
print('up')
down = -10
# 按下键
elif event.key == K_s or event.key == K_DOWN:
print("down")
down = 10
# 按空格键
elif event.key == K_SPACE:
print('space')
hero_temp.fire()
return right, down
最后在Main.py里运行即可
注意点:
1.py文件名和里面的类名不需要相同,py文件里可以放class,也可以只有一个函数,这点和java非常不一样
2.py文件开头导包的时候必须是 from EnemyPlane import EnemyPlane (导入XXX文件里的某某内容) ,不能只写 import EnemyPlane
python-飞机大战的更多相关文章
- Python飞机大战实例有感——pygame如何实现“切歌”以及多曲重奏?
目录 pygame如何实现"切歌"以及多曲重奏? 一.pygame实现切歌 初始化路径 尝试一 尝试二 尝试三 成功 总结 二.如何在python多线程顺序执行的情况下实现音乐和音 ...
- python飞机大战
'''新手刚学python,仿着老师敲的代码.1.敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹.2.正在研究怎么写计分.3.也参考了不少大佬的代码,但也仅仅只是参考了.加油!''' import ...
- python 飞机大战 实例
飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...
- python飞机大战简单实现
小游戏飞机大战的简单代码实现: # 定义敌机类 class Enemy: def restart(self): # 重置敌机的位置和速度 self.x = random.randint(50, 400 ...
- python飞机大战代码
import pygame from pygame.locals import * from pygame.sprite import Sprite import random import time ...
- 小甲鱼python基础教程飞机大战源码及素材
百度了半天小甲鱼python飞机大战的源码和素材,搜出一堆不知道是什么玩意儿的玩意儿. 最终还是自己对着视频一行行代码敲出来. 需要的同学点下面的链接自取. 下载
- Python小游戏之 - 飞机大战美女 !
用Python写的"飞机大战美女"小游戏 源代码如下: # coding=utf-8 import os import random import pygame # 用一个常量来存 ...
- 一、利用Python编写飞机大战游戏-面向对象设计思想
相信大家看到过网上很多关于飞机大战的项目,但是对其中的模块方法,以及使用和游戏工作原理都不了解,看的也是一脸懵逼,根本看不下去.下面我做个详细讲解,在做此游戏需要用到pygame模块,所以这一章先进行 ...
- Python版飞机大战
前面学了java用java写了飞机大战这次学完python基础后写了个python版的飞机大战,有兴趣的可以看下. 父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的 ...
- Python之游戏开发-飞机大战
Python之游戏开发-飞机大战 想要代码文件,可以加我微信:nickchen121 #!/usr/bin/env python # coding: utf-8 import pygame impor ...
随机推荐
- 手把手教你React Native 实战之开山篇《一》
先说一下我为什么学习RN 18年3月29号,随着自己内心的欲望和冲动,任务交接了一下,正式离开一家医疗公司.第二天就入职了这之前已经找好的公司,由于自己对代码浓厚的热情,自己终于也不再带团队.正好有充 ...
- Windows Service 2012 R2 下如何建立ftp服务器
1.首先在本地机器上创建一个用户!这些用户是用来登录到FTP的!我的电脑右键->管理->本地用户和组->用户->“右键”新建用户->输入用户名和密码再点创建就行了! 2. ...
- bzoj 1812
什么鬼noip互测题... 这题很显然是树形dp,但设计状态以及转移是个难点 记状态f[i][j][k]表示以i为根节点的子树,离i最近的祖宗节点编号为j放了虫洞(伐木场?),i的子树内放了k个伐木场 ...
- 性能测试四十八:Jenkins+Ant+Jmeter系统部署
工作步骤: 1.开发提交代码SVN/Git 2.代码编译.打war包 3.上传war包到服务器 4.重启web服务器 5.开始测试/自动化测试 6.发测试结果 Jenkins工作: 1.开发提交代码G ...
- C++ Primer 笔记——命名空间
1.我们既可以用 using 声明整个空间,也可以声明部分名字. using namespace std; using std::cout; 2.头文件不应包含 using 声明,因为头文件会拷贝到所 ...
- js中匿名函数和回调函数
匿名函数: 通过这种方式定义的函数:(没有名字的函数) 作用:当它不被赋值给变量单独使用的时候 1.将匿名函数作为参数传递给其他函数 2.定义某个匿名函数来执行某些一次性任务 var f = func ...
- CORS跨域
一:简介 为什么会出现跨域问题? 受同源策略影响,不同域名之间不可以进行访问.同源策略(Same-Origin Policy).所谓的 同源 是指域名.协议.端口号 相同.不同的客户端脚本(JavaS ...
- IDEAL 中配置Tomcat的内存值
-server -XX:PermSize=2048M -XX:MaxPermSize=4096m
- [转] JavaScript 之 ArrayBuffer
JS里的ArrayBuffer 还记得某个晚上在做 canvas 像素级操作,发现存储像素的数据格式并不是Array类型,而是ArrayBuffer,心想这是什么鬼?后来查了一些资料,发现自己这半年来 ...
- [转] HTML5 Blob与ArrayBuffer、TypeArray和字符串String之间转换
1.将String字符串转换成Blob对象 //将字符串 转换成 Blob 对象 var blob = new Blob(["Hello World!"], { type: 'te ...