python大飞机项目简介_python 打飞机项目 ( 基类封装 )

项目代码 | plane

main.py 主程序文件,启动文件

# -*- coding:utf-8 -*-

import pygame, time

from Plane import Plane

from HeroPlane import HeroPlane

from Screen import Screen

from pygame.locals import *

def key_control(plane_temp):

# 获取事件,比如按键等

for event in pygame.event.get():

# 判断是否是点击了退出按钮

if event.type == QUIT:

print("exit")

exit()

# 判断是否是按下了键

elif event.type == KEYDOWN:

# 检测按键是否是a或者left

if event.key == K_a or event.key == K_LEFT:

print('left')

plane_temp.move_left()

# 检测按键是否是d或者right

elif event.key == K_d or event.key == K_RIGHT:

print('right')

plane_temp.move_right()

# 检测按键是否是空格键

elif event.key == K_SPACE:

print('space')

plane_temp.fire()

def main():

screen = pygame.display.set_mode((480, 852), 0, 32)

# 创建窗口对象

screen_temp = Screen(screen)

# 创建一个飞机对象

plane = Plane(screen)

# 创建敌机对象

enemyPlane = HeroPlane(screen)

while True:

screen_temp.display() # 显示窗口

plane.display(plane.bullet_list) # 显示飞机

enemyPlane.display(enemyPlane.bullet_list) # 敌机显示

enemyPlane.move() # 敌机移动

enemyPlane.fire() # 敌机开火

key_control(plane) # 键盘事件监听

pygame.display.update() # 更新窗口

time.sleep(0.01) # 延时0.01秒,防止程序内存溢出

if __name__ == '__main__':

main()

Base.py 基类

# -*- coding:utf-8 -*-

import pygame

class Base(object):

# 背景图片

image = None

def __init__(self, screen_temp, x, y, image_path):

self.x = x

self.y = y

self.screen = screen_temp

self.image_load(image_path)

# 飞机赋值图片对象

def image_load(self, image_path):

self.image = pygame.image.load(image_path)

BasePlane.py 飞机基类

# -*- coding:utf-8 -*-

from Base import Base

class BasePlane(Base):

def __init__(self, screen_temp, x, y, image_path):

Base.__init__(self, screen_temp, x, y, image_path)

# 显示飞机

def display(self, bullet_list):

self.screen.blit(self.image, (self.x, self.y)) # 显示飞机

for bullet in bullet_list: # 循环取出子弹对象

# 判断子弹是否越界

if bullet.judge():

bullet_list.remove(bullet) # 如果子弹越界就删除子弹

bullet.display() # 显示子弹

bullet.move() # 子弹移动步长

Plane.py 飞机对象

# -*- coding:utf-8 -*-

from Bullet import Bullet

from BasePlane import BasePlane

class Plane(BasePlane):

# 储存子弹对象

bullet_list = []

def __init__(self, screen_temp):

BasePlane.__init__(self, screen_temp, 210, 700, "./resource/hero1.png")

# 飞机向左移动偏移量

def move_left(self):

self.x -= 10

# 飞机向右移动偏移量

def move_right(self):

self.x += 10

# 将飞机创建的子弹对象存储进 bullet_list 列表中

def fire(self):

self.bullet_list.append(Bullet(self.screen, self.x, self.y))

print(self.bullet_list)

HeroPlane.py 敌机对象

# -*- coding:utf-8 -*-

import random

from BasePlane import BasePlane

from EnemyBullet import EnemyBullet

class HeroPlane(BasePlane):

# 定义一个类属性用来保存

direction = 'right'

# 储存子弹对象

bullet_list = []

def __init__(self, screen_temp):

BasePlane.__init__(self, screen_temp, 0, 0, "./resource/enemy-1.gif")

# 敌机移动轨迹

def move(self):

# 敌机创建的子弹默认向右移动

if self.direction == 'right':

self.x += 5 # 每次向右移动增加 5px 的步长

elif self.direction == 'left': # 向左移动

self.x -= 5 # 每次向左移动减少 5px 的步长

# 当敌机向右移动到了边界就向左移动

if self.x > 480 - 50: # 480 是界面总宽度; 50 是飞机宽度. 所以敌机移动的距离应该是界面宽度-敌机宽度 ( 移动距离 = 界面宽度 - 敌机宽度 )

self.direction = 'left'

elif self.x <= 0: # 当敌机移动到了最左边就会继续向右移动

self.direction = 'right'

# 开火

def fire(self):

random_temp = random.randint(1, 100) # 随机生成 1 - 100的随机数

if (random_temp == 20) or (random_temp == 78): # 随机数概率

self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) # 创建敌机子弹对象

BaseBullet.py 子弹基类

# -*- coding:utf-8 -*-

from Base import Base

class BaseBullet(Base):

def __init__(self, screen_temp, x, y, image_path):

Base.__init__(self, screen_temp, x, y, image_path)

# 子弹背景

def display(self):

self.screen.blit(self.image, (self.x, self.y))

Bullet.py 子弹对象

# -*- coding:utf-8 -*-

from BaseBullet import BaseBullet

class Bullet(BaseBullet):

def __init__(self, screen_temp, x, y):

BaseBullet.__init__(self, screen_temp, x + 40, y - 20, "./resource/bullet.png")

# 子弹步长

def move(self):

self.y -= 10

# 判断子弹y轴是否已经越界

def judge(self):

if self.y < 0:

return True

else:

return False

EnemyBullet.py 敌机子弹对象

# -*- coding:utf-8 -*-

from BaseBullet import BaseBullet

class EnemyBullet(BaseBullet):

def __init__(self, screen_temp, x, y):

BaseBullet.__init__(self, screen_temp, x + 30, y + 30, "./resource/bullet-1.gif")

# 子弹移动步长

def move(self):

self.y += 20

# 判断子弹y轴是否已经越界

def judge(self):

if self.y > 890: # 890 界面总高度

return True

else:

return False

Screen.py 窗口对象

# -*- coding:utf-8 -*-

from Base import Base

class Screen(Base):

def __init__(self, screen_temp):

Base.__init__(self, screen_temp, 0, 0, "./resource/background.png")

def display(self):

self.screen.blit(self.image, (self.x, self.y))

python开发等相关IT技术群:887934385 提供资料,部分相关源码 共同探讨

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的抽象基类(Abstract Base Class,简称ABC)是一种特殊的类,它定义了一组抽象方法,这些方法必须在子类中实现。ABC的一个主要用途是为了检查子类是否符合某个接口的规范。 在ABC中,可以使用`__subclasshook__`方法来判断一个类是否是该ABC的虚拟子类。具体来说,如果一个类没有直接继承该ABC,但是它的基类中有`__subclasshook__`方法,而且该方法返回True,则该类就被视为该ABC的虚拟子类。 下面是一个例子,我们定义了一个ABC `MyABC`,其中定义了一个抽象方法`my_method`。然后定义了一个普通类`MyClass`,它继承自`object`,并且实现了`my_method`方法。最后,我们在`MyClass`中定义了`__subclasshook__`方法,它判断一个类是否实现了`my_method`方法。 ```python from abc import ABC, abstractmethod class MyABC(ABC): @abstractmethod def my_method(self): pass class MyClass(object): def my_method(self): print("MyClass.my_method() is called") @classmethod def __subclasshook__(cls, C): if cls is MyABC: if hasattr(C, 'my_method'): return True return NotImplemented print(issubclass(MyClass, MyABC)) # True ``` 在上面的代码中,我们使用`issubclass`函数来检查`MyClass`是否是`MyABC`的子类。由于`MyClass`实现了`my_method`方法,而且定义了`__subclasshook__`方法,因此`issubclass(MyClass, MyABC)`返回True。 接下来我们实现一个自己的虚拟子类。我们定义一个普通类`MyClass2`,它没有直接继承自`MyABC`,但是它的基类中定义了`__subclasshook__`方法,该方法判断一个类是否实现了`my_method`方法。然后我们通过`register`方法将`MyClass2`注册为`MyABC`的虚拟子类。 ```python class MyClass2(object): def my_method(self): print("MyClass2.my_method() is called") @classmethod def __subclasshook__(cls, C): if cls is MyABC: if hasattr(C, 'my_method'): return True return NotImplemented MyABC.register(MyClass2) print(issubclass(MyClass2, MyABC)) # True ``` 在上面的代码中,我们使用`register`方法将`MyClass2`注册为`MyABC`的虚拟子类。然后我们再次使用`issubclass`函数来检查`MyClass2`是否是`MyABC`的子类。由于`MyClass2`实现了`my_method`方法,并且已经被注册为`MyABC`的虚拟子类,因此`issubclass(MyClass2, MyABC)`返回True。 总之,`__subclasshook__`方法是ABC中的一个重要方法,它可以让我们方便地判断一个类是否符合某个接口的规范。同时,我们也可以通过`register`方法将一个普通类注册为ABC的虚拟子类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值