效果图

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-飞机大战的更多相关文章

  1. Python飞机大战实例有感——pygame如何实现“切歌”以及多曲重奏?

    目录 pygame如何实现"切歌"以及多曲重奏? 一.pygame实现切歌 初始化路径 尝试一 尝试二 尝试三 成功 总结 二.如何在python多线程顺序执行的情况下实现音乐和音 ...

  2. python飞机大战

    '''新手刚学python,仿着老师敲的代码.1.敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹.2.正在研究怎么写计分.3.也参考了不少大佬的代码,但也仅仅只是参考了.加油!''' import ...

  3. python 飞机大战 实例

    飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...

  4. python飞机大战简单实现

    小游戏飞机大战的简单代码实现: # 定义敌机类 class Enemy: def restart(self): # 重置敌机的位置和速度 self.x = random.randint(50, 400 ...

  5. python飞机大战代码

    import pygame from pygame.locals import * from pygame.sprite import Sprite import random import time ...

  6. 小甲鱼python基础教程飞机大战源码及素材

    百度了半天小甲鱼python飞机大战的源码和素材,搜出一堆不知道是什么玩意儿的玩意儿. 最终还是自己对着视频一行行代码敲出来. 需要的同学点下面的链接自取. 下载

  7. Python小游戏之 - 飞机大战美女 !

    用Python写的"飞机大战美女"小游戏 源代码如下: # coding=utf-8 import os import random import pygame # 用一个常量来存 ...

  8. 一、利用Python编写飞机大战游戏-面向对象设计思想

    相信大家看到过网上很多关于飞机大战的项目,但是对其中的模块方法,以及使用和游戏工作原理都不了解,看的也是一脸懵逼,根本看不下去.下面我做个详细讲解,在做此游戏需要用到pygame模块,所以这一章先进行 ...

  9. Python版飞机大战

    前面学了java用java写了飞机大战这次学完python基础后写了个python版的飞机大战,有兴趣的可以看下. 父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的 ...

  10. Python之游戏开发-飞机大战

    Python之游戏开发-飞机大战 想要代码文件,可以加我微信:nickchen121 #!/usr/bin/env python # coding: utf-8 import pygame impor ...

随机推荐

  1. [原著]java或者Js 代码逻辑来处理 突破 oracle sql “IN”长度的极限的问题

    注:本文出自:博主自己研究验证可行   [原著]java或者Js  代码逻辑来处理  突破 oracle  sql "IN"长度的极限的问题    在很多的时候 使用 select ...

  2. Confluence 6 使用 CSS 样式化 Confluence 的介绍

    这个页面对 Confluence 通过修改 CSS 来改变外观和感觉的情况进行了说明. 层叠样式表(Cascading Style Sheets (CSS))是对 Web 页面进行样式化的工业化标准. ...

  3. Git- 命令及使用

    关于Git相关介绍这里就不介绍了,可转<Git- 简介>或者查看官网信息.这篇整理一下git相关的命令.   1) 远程仓库相关命令 克隆下载仓库:$ git clone git://gi ...

  4. 用json获取拉钩网的信息

    class LaoGo(object): def __init__(self): self.url="http://www.lagou.com/lbs/getAllCitySearchLab ...

  5. 分块入门(根据hzwer的博客。。)(右端点是r不是n。。)

    1.区间更新单点查询 #include <bits/stdc++.h> using namespace std; #define ll long long #define maxn 100 ...

  6. python字符编码和文件处理

    一.了解字符编码的知识储备 1.文本编辑器存取文件的原理(nodepad++,python,word) 打开编辑器就打开了启动了一个进程,是在内存中的,所以,用编辑器编写的内容也都是存放于内存中的,断 ...

  7. 部署MySQL5.7时的权限问题

    本周部署MySQL5.7的时候遇到这样的问题,在初始化的时候,总是失败,并且报错: 2019-01-09T09:47:13.957685Z 0 [ERROR] InnoDB: Operating sy ...

  8. 论文阅读笔记三十四:DSSD: Deconvolutiona lSingle Shot Detector(CVPR2017)

    论文源址:https://arxiv.org/abs/1701.06659 开源代码:https://github.com/MTCloudVision/mxnet-dssd 摘要 DSSD主要是向目标 ...

  9. POJ 1002 487-3279(字典树/map映射)

    487-3279 Time Limit: 2000MS        Memory Limit: 65536K Total Submissions: 309257        Accepted: 5 ...

  10. Django中间件 及 form 实现用户登陆

    Django中间件 及 form 实现用户登陆 Form 验证 密码调用md5 加密存储 form.add_error("字段名", "错误信息") 自定义错误 ...