笔记-python飞机大战

python版的飞机大战,有兴趣的可以看下。

父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的喜欢的图片,敌机可以分为敌机和奖励,enemy为普通敌人的父类,award为奖励敌机的父类。
在这里插入图片描述

飞机大战(一)

各个类的基本属性
在这里插入图片描述

飞机大战(二)

主类的大概逻辑
在这里插入图片描述

具体的代码:
settings配置

import pygame class Settings(object):    """设置常用的属性"""     def __init__(self):        self.bgImage = pygame.image.load('img/background.png')  # 背景图         self.bgImageWidth = self.bgImage.get_rect()[2]  # 背景图宽        self.bgImageHeight = self.bgImage.get_rect()[3]  # 背景图高        self.start=pygame.image.load("img/start.png")        self.pause=pygame.image.load("img/pause.png")        self.gameover=pygame.image.load("img/gameover.png")        self.heroImages = ["img/hero.gif",                           "img/hero1.png", "img/hero2.png"]  # 英雄机图片        self.airImage = pygame.image.load("img/enemy0.png") # airplane的图片        self.beeImage = pygame.image.load("img/bee.png") # bee的图片        self.heroBullet=pygame.image.load("img/bullet.png")# 英雄机的子弹

飞行物类

import abc  class FlyingObject(object):    """飞行物类,基类"""     def __init__(self, screen, x, y, image):        self.screen = screen        self.x = x        self.y = y        self.width = image.get_rect()[2]        self.height = image.get_rect()[3]        self.image = image     @abc.abstractmethod    def outOfBounds(self):        """检查是否越界"""        pass     @abc.abstractmethod    def step(self):        """飞行物移动一步"""        pass     def shootBy(self, bullet):        """检查当前飞行物是否被子弹bullet(x,y)击中"""        x1 = self.x        x2 = self.x + self.width        y1 = self.y        y2 = self.y + self.height        x = bullet.x        y = bullet.y        return x > x1 and x < x2 and y > y1 and y < y2     def blitme(self):        """打印飞行物"""        self.screen.blit(self.image, (self.x, self.y)) 

英雄机

from flyingObject import FlyingObjectfrom bullet import Bulletimport pygame  class Hero(FlyingObject):    """英雄机"""    index = 2  # 标志位    def __init__(self, screen, images):         # self.screen = screen                self.images = images  # 英雄级图片数组,为Surface实例        image = pygame.image.load(images[0])        x = screen.get_rect().centerx        y = screen.get_rect().bottom        super(Hero,self).__init__(screen,x,y,image)        self.life = 3  # 生命值为3        self.doubleFire = 0  # 初始火力值为0     def isDoubleFire(self):        """获取双倍火力"""        return self.doubleFire     def setDoubleFire(self):        """设置双倍火力"""        self.doubleFire = 40     def addDoubleFire(self):        """增加火力值"""        self.doubleFire += 100    def clearDoubleFire(self):        """清空火力值"""        self.doubleFire=0     def addLife(self):        """增命"""        self.life += 1        def sublife(self):        """减命"""        self.life-=1       def getLife(self):        """获取生命值"""        return self.life    def reLife(self):        self.life=3        self.clearDoubleFire()      def outOfBounds(self):        return False     def step(self):        """动态显示飞机"""        if(len(self.images) > 0):            Hero.index += 1            Hero.index %= len(self.images)            self.image = pygame.image.load(self.images[int(Hero.index)])  # 切换图片     def move(self, x, y):        self.x = x - self.width / 2        self.y = y - self.height / 2     def shoot(self,image):        """英雄机射击"""        xStep=int(self.width/4-5)        yStep=20        if self.doubleFire>=100:            heroBullet=[Bullet(self.screen,image,self.x+1*xStep,self.y-yStep),Bullet(self.screen,image,self.x+2*xStep,self.y-yStep),Bullet(self.screen,image,self.x+3*xStep,self.y-yStep)]            self.doubleFire-=3            return heroBullet        elif self.doubleFire<100 and self.doubleFire > 0:            heroBullet=[Bullet(self.screen,image,self.x+1*xStep,self.y-yStep),Bullet(self.screen,image,self.x+3*xStep,self.y-yStep)]            self.doubleFire-=2            return heroBullet        else:            heroBullet=[Bullet(self.screen,image,self.x+2*xStep,self.y-yStep)]            return heroBullet     def hit(self,other):        """英雄机和其他飞机"""        x1=other.x-self.width/2        x2=other.x+self.width/2+other.width        y1=other.y-self.height/2        y2=other.y+self.height/2+other.height        x=self.x+self.width/2        y=self.y+self.height        return x>x1 and x<x2 and y>y1 and y<y2

enemys

import abc class Enemy(object):    """敌人,敌人有分数"""    @abc.abstractmethod    def getScore(self):        """获得分数"""        pass

award

import abc  class Award(object):    """奖励"""    DOUBLE_FIRE = 0    LIFE = 1     @abc.abstractmethod    def getType(self):        """获得奖励类型"""        pass  if __name__ == '__main__':     print(Award.DOUBLE_FIRE) 

airplane

import randomfrom flyingObject import FlyingObjectfrom enemy import Enemy  class Airplane(FlyingObject, Enemy):    """普通敌机"""     def __init__(self, screen, image):         x = random.randint(0, screen.get_rect()[2] - 50)        y = -40        super(Airplane, self).__init__(screen, x, y, image)     def getScore(self):        """获得的分数"""        return 5     def outOfBounds(self):        """是否越界"""         return self.y < 715     def step(self):        """移动"""        self.y += 3  # 移动步数 

Bee

import randomfrom flyingObject import FlyingObjectfrom award import Award  class Bee(FlyingObject, Award):     def __init__(self, screen, image):        x = random.randint(0, screen.get_rect()[2] - 60)        y = -50        super(Bee, self).__init__(screen, x, y, image)        self.awardType = random.randint(0, 1)        self.index = True     def outOfBounds(self):        """是否越界"""        return self.y < 715     def step(self):        """移动"""        if self.x + self.width > 480:            self.index = False        if self.index == True:            self.x += 3        else:            self.x -= 3        self.y += 3  # 移动步数     def getType(self):        return self.awardType  

主类

import pygameimport sysimport randomfrom setting import Settingsfrom hero import H

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/731426.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

鸿蒙正在跨越“生态繁荣阈值”

作者 | 曾响铃 文 | 响铃说 先讲一个故事。 一个朋友曾经做了一个本地互联网装修平台&#xff0c;一边是装修服务的提供者——各工种工人等&#xff0c;一边是有装修需求的业主。这个平台要做独立生态&#xff0c;绕过旧有的装修公司渠道垄断&#xff0c;直接提供服务&#…

凯迪仕霸榜全渠道TOP1 全域曝光100亿

618年中狂欢盛典&#xff0c;已正式落下帷幕。智能锁行业领头羊凯迪仕&#xff0c;凭借过硬的科技产品力和品牌势能&#xff0c;在全域流量加持以及传奇大师K70新品强势曝光之下&#xff0c;霸榜天猫、京东、抖音各平台&#xff0c;稳居各类型榜单榜首&#xff0c;继续以行业领…

Redis-事务-基本操作-在执行阶段出错不会回滚

文章目录 1、Redis事务控制命令2、Redis事务错误处理3、Redis事务错误处理&#xff0c;在执行阶段出错不会回滚 1、Redis事务控制命令 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> multi OK 127.0.0.1:6379(TX)> set a1 v1 QUEUED 127.0.0.1:6379(TX)>…

华为---配置OSPF的认证(三)

9.3 配置OSPF的认证 9.3.1 原理概述 OSPF支持报文验证功能&#xff0c;只有通过验证的报文才能接收&#xff0c;否则将不能正常建立邻居关系。OSPF协议支持两种认证方式——区域认证和链路认证。使用区域认证时&#xff0c;一个区域中所有的路由器在该区域下的认证模式和口令…

韩顺平0基础学java——第28天

p569-591 坦克大战&#xff01;&#xff08;绘图监听事件线程文件处理&#xff09; 绘图 绘图原理 Component类提供了两个和绘图相关最重要的方法: 1. paint(Graphics g)绘制组件的外观 2. repaint()刷新组件的外观。 当组件第一次在屏幕显示的时候,程序会自动的调用paint()…

容器之按钮盒构件演示

代码; #include <gtk-2.0/gtk/gtk.h> #include <glib-2.0/glib.h> #include <gtk-2.0/gdk/gdkkeysyms.h> #include <stdio.h>int main(int argc, char *argv[]) {gtk_init(&argc, &argv);GtkWidget *window;window gtk_window_new(GTK_WINDO…

北大推出全新机器人多模态大模型!面向通用和机器人场景的高效推理和操作

为了赋予机器人端到端的推理和操纵能力&#xff0c;本文创新性地将视觉编码器与高效的状态空间语言模型集成&#xff0c;构建了全新的 RoboMamba 多模态大模型&#xff0c;使其具备视觉常识任务和机器人相关任务的推理能力&#xff0c;并都取得了先进的性能表现。同时&#xff…

【秋招刷题打卡】Day01-自定义排序

Day01-自定排序 前言 给大家推荐一下咱们的 陪伴打卡小屋 知识星球啦&#xff0c;详细介绍 >笔试刷题陪伴小屋-打卡赢价值丰厚奖励 < ⏰小屋将在每日上午发放打卡题目&#xff0c;包括&#xff1a; 一道该算法的模版题 (主要以力扣&#xff0c;牛客&#xff0c;acwin…

20240621在飞凌的OK3588-C开发板的Buildroot系统中集成i2ctool工具

20240621在飞凌的OK3588-C开发板中打开i2ctool工具 2024/6/21 17:44 默认继承的i2c工具&#xff1a; rootrk3588-buildroot:/# rootrk3588-buildroot:/# i2c i2c-stub-from-dump i2cdump i2cset i2cdetect i2cget i2ctransfer rootrk3588-…

CVPR 2024盛况空前,上海科技大学夺得最佳学生论文奖,惊艳全场

CVPR 2024盛况空前&#xff01;上海科技大学夺得最佳学生论文奖&#xff0c;惊艳全场&#xff01; 会议之眼 快讯 2024 年 CVPR &#xff08;Computer Vision and Pattern Recogntion Conference) 即国际计算机视觉与模式识别会议&#xff0c;于6月17日至21日正在美国西雅图召…

邀请函 | 桥田智能出席AMTS展会 家族新成员正式发布

作为国际汽车制造技术与装备及材料专业展览会&#xff0c;AMTS将于2024年7月3-5日在上海新国际博览中心举行。本届展会以【向“新”而行 “智”领未来】为主题&#xff0c;聚焦汽车及新能源全产业链&#xff0c;围绕“车身工程、部件工程、新能源三电工程及未来汽车开发”等技…

Linux (centos7.9)上部署 NFS(Network File System)服务

NFS&#xff08;Network File System&#xff09;是一种网络文件系统协议&#xff0c;允许不同计算机之间通过网络共享文件和目录。NFS 最初由 Sun Microsystems 在 1984 年开发&#xff0c;现已成为许多 Unix 和类 Unix 系统&#xff08;包括 Linux&#xff09;上的标准文件系…

好大夫在线医生数据医生姓名科室荣誉等202406

好大夫在线医生数据医生姓名科室荣誉等 2024年6月数据&#xff0c;总共934027条数据&#xff0c;可导出为excel,csv等 好大夫在线2024-06月数据 包含简介信息&#xff01; 可见样例数据 样例数据_医生2.xlsx https://www.alipan.com/s/DBEW9MgHEPP 点击链接保存&#xff0…

【Python】成功解决TypeError: missing 1 required positional argument

【Python】成功解决TypeError: missing 1 required positional argument 下滑即可查看博客内容 &#x1f308; 欢迎莅临我的个人主页 &#x1f448;这里是我静心耕耘深度学习领域、真诚分享知识与智慧的小天地&#xff01;&#x1f387; &#x1f393; 博主简介&#xff1…

4种方法轻松掌握怎么调节电脑屏幕亮度的技巧!

在使用电脑的过程中&#xff0c;调节屏幕亮度是一项常见的操作&#xff0c;它可以帮助我们适应不同的环境光线&#xff0c;减轻眼睛的疲劳&#xff0c;提高使用体验。 然而&#xff0c;对于一些新手用户来说&#xff0c;怎么调节电脑屏幕亮度可能会感到困惑。本文将介绍四种简…

RocketMQ的安装和原理

.RocketMQ的安装 一.RocketMQ安装 1.1.下载RocketMQ 下载地址&#xff1a;http://rocketmq.apache.org/release_notes/release-notes-4.2.0/ 下载后解压 Bin : 可执行文件目录 config&#xff1a;配置文件目录 Lib : 依赖库&#xff0c;一堆Jar包 1.2.配置ROCKETMQ_HOME…

AI时代:硬件狂欢,软件落寞 华为开发者大会2024

内容提要 分析师表示&#xff0c;目前AI行业大多数的支出都流向用于训练大模型的硬件或云基础设备。相较之下&#xff0c;软件应用商们在AI时代显得停滞不前。尽管软件应用商们十分热衷于构建AI工具&#xff0c;然而其收入状况却并不乐观。 文章正文 AI浪潮之下&#xff0c;英…

Day 29:1600. 王位继承顺序

Leetcode1600. 王位继承顺序 一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点&#xff0c;这个家庭里有人出生也有人死亡。 这个王国有一个明确规定的王位继承顺序&#xff0c;第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) &#xff0c;给…

校园疫情防控健康打卡系统

摘 要 自疫情出现以来&#xff0c;全世界人民的生命安全和健康都面临着严重威胁。高校是我国培养人才的重要基地&#xff0c;其安全和稳定影响着社会的发展和进步。因此&#xff0c;各高校高度重视疫情防控工作&#xff0c;并在校园疫情防控中引入了健康打卡系统。本论文主要研…

NodeJs实现对本地 mysql 数据库的增删改查

写在前面 今天我们接着写nodejs对数据库的操作&#xff0c;今天实现简单的增删改查&#xff0c;读之前请先移步到这里NodeJs 连接本地 mySql 数据库获取数据,避免后续一些代码出险阅读断层。 安装 nodemon npm install nodemon因为 nodejs 的服务是本地启动&#xff0c;避免后…