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 ...
随机推荐
- ios 本地存储文件夹的使用注意
文件夹 tmp 属于临时文件夹,不需要自己删除,系统会在应用退出后清空 文件夹 Library 下面的子文件 Caches 也是用来存储的,,但是Library 基本上不会被清除,但是在内存不足的 ...
- 【Shared Server Mode】测试调整shared_servers参数对数据库的影响
本文来源于:secooler 的 <[Shared Server Mode]测试调整shared_servers参数对数据库的影响> 关于Shared Server模式的配置方法请参见文 ...
- ES6 必须要用的数组Filter() 方法,不要再自己循环遍历了!!!
1,来一个最简单最常用的栗子: 获得年龄为9岁的孩子 1 let arr = [ 2 { 3 name:'小明', 4 sex:0, 5 age:9 6 }, 7 { 8 name:'小红', 9 s ...
- python之通过thread来实现多进程
代码如下: import threading, time class Test1(threading.Thread): def __init__(self, name): super().__init ...
- linux 下安装vscode
下载安装包 https://code.visualstudio.com/docs/?dv=linux64_deb (注意是deb包) sudo dpkg -i code_1.18.1-15108573 ...
- Microsoft Visual Studio Community 2017 修改新建项目的默认位置
IDE: Microsoft Visual Studio Community 2017 15.5.2 通过修改默认的设置,在下一次新建项目时,就可以节省一些不必要的操作. 菜单:工具 > 选项, ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
- CHENGDU1-Python编程语言和PEP8规范
CHENGDU1-Python编程语言和PEP8规范 PEP8规范6条? 答:PEP8规范说白了就是一种规范,可以遵守,也可以不遵守,遵守PEP8可以让代码的可读性更高. 代码编排:---缩进,4个空 ...
- VS2015 生成事件 命令参数
来源:https://stackoverflow.com/questions/11001822/copy-files-from-one-project-to-another-using-post-bu ...
- Oozie如何和安装部署
1.Oozie的简单介绍: .Oozie是一个工作流引擎服务器,用于运行hadoop map/reduce和hive等任务工作流,同时Oozie还是一个Java web程序,运行在Java Servl ...