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 ...
随机推荐
- est是基于less的样式工具库
网址:http://ecomfe.github.io/est/ 此处HTML中如果直接引用less就会报错! est 如果直接引用less和js 是会报错的 样式规范:https://www.zybu ...
- ORACLE in与exists语句的区别
select * from A where id in(select id from B) 以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与 ...
- Confluence 6 查看内容索引概要
内容索引,通常也被称为查找索引,这个索引被用来在 Confluence 中支持查找.这个索引同时也被其他的一些功能使用,例如在归档邮件中构建邮件主题,View Space Activity 的特性和将 ...
- js 获取当前的网址
http://www.xcx.cc/index.php/home/index/ind?idf=12321var $cur_url=window.location.href; //获取全部的网址var ...
- Android 自动化测试框架
Android常用的自动化测试工具框架: Monkey,MonkeyRunner,UIAutomator,Robotium,Appium,Monkey Talk...... 但这些工具框架都是什么呢有 ...
- Java编制至今总结和学习报告
日期:2018.8.19 星期日 博客期:006 说个事,本来想把博客园做一个交流平台的,可是交流度有点少...嗯...我看我还是把这个平台当作经验传授平台和自己的作品发布平台吧!Java的知识详解, ...
- Op-level的快速算法
十岁的小男孩 本文为终端移植的一个小章节. 目录 引言 FFT Conv2d (7x7, 9x9) Winograd Conv2d (3x3, 5x5) 引言 本节针对CNN进行加速计算的,主要有以下 ...
- sqlmap实例文档
sqlmap 手册参数整理文档 1.--data sqlmap -u "http://www.target.com/vuln.php" --data="id=1" ...
- bitset用法详解
参见此博客: https://www.cnblogs.com/magisk/p/8809922.html
- Appium Desired Capabilities
Appium Desired Capabilities Desired Capabilities 是由 keys 和 values 组成的 JSON 对象. 举个简单例子: { "platf ...