效果图

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中关于string的些许问题及解析

    问题一:String 和 StringBuffer 的区别JAVA 平台提供了两个类: String 和 StringBuf fer ,它们可以储存和操作字符串,即包含多个字符的字符数据.这个 Str ...

  2. 通过设置ie的通过跨域访问数据源,来访问本地服务

    1.首先设置通过域访问数据源 设置通过域访问数据源 2.javascript脚本ajax使用本地服务登录(评价,人证的类似)接口 <html> <head> <scrip ...

  3. 为 Confluence 6 配置发送邮件消息

    如何配置 Confluence 向外发送邮件: 进入  > 基本配置(General Configuration) > 邮件服务器(Mail Servers).这里列出了所有当前配置的 S ...

  4. vuex action 与mutations 的区别

    面试没说清楚.这个太丢人回来整理下: 事实上在 vuex 里面 actions 只是一个架构性的概念,并不是必须的,说到底只是一个函数,你在里面想干嘛都可以,只要最后触发 mutation 就行.异步 ...

  5. java 得到目录路径的方法

    得到web项目的根目录路径 System.getProperty("user.dir")// String path = this.getServletContext().getR ...

  6. Java中Super和final关键字以及异常类

    1.final类不能有子类,也就谈不上继承的说法,如果用final修饰成员变量或者局部变量,那成了常量需要制定常量的值. 2.对象的上转型对象,上转型对象不能操作子类新增的成员变量,不能调用子类新增的 ...

  7. tensorflow(3):神经网络优化(ema,regularization)

    1.指数滑动平均 (ema) 描述滑动平均: with tf.control_dependencies([train_step,ema_op]) 将计算滑动平均与 训练过程绑在一起运行 train_o ...

  8. django----图书管理

    待完成 from django.db import models # Create your models here. class Book(models.Model): nid = models.A ...

  9. Jenkins构建后发送邮件

    我们首先安装Jenkins邮件扩展插件“ Email Extension Plugin ”. Jenkins和插件的安装方法见上一篇文章:http://qicheng0211.blog.51cto.c ...

  10. 关于Mybaits10种通用的写法

    用来循环容器的标签forEach,查看例子 foreach元素的属性主要有item,index,collection,open,separator,close. item:集合中元素迭代时的别名, i ...