python-飞机大战
Python 大战 飞机
2023-09-14 08:59:10 时间
效果图
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的安装
- python实现微信小游戏“飞机大战”
- python多线程详解
- Python报错:pandas.errors.ParserError: Error tokenizing data. C error: Expected 3……
- python监控网页内容变化_使用Python监控文件内容变化代码实例
- 怎么用python打开csv文件_Python文本处理之csv-csv文件怎么打开[通俗易懂]
- python语言一般用于什么_PYthon
- python全局变量赋值_Python全局变量和局部变量[通俗易懂]
- Python抛出异常_python抛出异常的作用
- lambda python表达式_Python的条件表达式和lambda表达式实例
- 用Python做一个久坐提醒小助手
- 【7】python_matplotlib 输出(保存)矢量图方法;画图时图例说明(legend)放到图像外侧;Python_matplotlib图例放在外侧保存时显示不完整问题解决
- 新手学Python可行吗?需要什么基础?(新手学python可行吗?需要什么基础)
- 03.Python Dash网页开发:多页面网站制作
- python-数据库编程-如何在Python中连接到数据库
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(三)
- python-Python与PostgreSQL数据库-PostgreSQL数据库的基本知识(二)
- python-Python与PostgreSQL数据库-处理PostgreSQL查询结果
- Python type()函数:动态创建类
- python arp欺骗伪造网关代码详解编程语言
- python生成指定尺寸的缩略图详解编程语言
- Python测试Sqlite代码详解编程语言
- 用Python简化Linux指令:从繁琐变简单(python运行linux命令)
- Linux系统如何运行Python脚本(linux执行python脚本)
- Python驱动Oracle数据库(python操作oracle)
- 使用这个开源工具来监控 Python 中的变量
- 迭代器设计模式,帮你大幅提升Python性能
- python飞机大战源码 python微信飞机大战
- Python轻松连接SQL Server数据库(python连接sqlserver)
- 在Linux上运行Python脚本的简单指南(linux运行python)
- 利用Neo4j和Python进行无缝图形数据库支持(neo4j python)
- Python操作MySQL数据库的必备模块mysqlpython(mysql_python)
- PHPwebshell检查工具python实现代码
- python字符串split的用法分享