76 python - 打飞机案例(显示敌机)
2023-09-11 14:15:43 时间
#coding=utf-8
import pygame
from pygame.locals import *
'''
显示敌人飞机
'''
class HeroPlane(object):
def __init__(self,screen):
#设置飞机默认的位置
self.x = 230
self.y = 600
#设置要显示内容的窗口
self.screen = screen
self.imageName = "./feiji/hero.gif"
self.image = pygame.image.load(self.imageName).convert()
#用来存储英雄飞机发射的所有子弹
self.bulletList = []
def display(self):
self.screen.blit(self.image,(self.x,self.y))
for bullet in self.bulletList:
bullet.display()
bullet.move()
#修改所有子弹的位置
# for bullet in self.bulletList:
# bullet.y -= 2
def moveLeft(self):
self.x -= 10
def moveRight(self):
self.x += 10
def sheBullet(self):
newBullet = Bullet(self.x,self.y,self.screen)
self.bulletList.append(newBullet)
class Bullet(object):
def __init__(self,x,y,screen):
self.x = x+40
self.y = y-20
self.screen = screen
self.image = pygame.image.load("./feiji/bullet-3.gif").convert()
def move(self):
self.y -= 2
def display(self):
self.screen.blit(self.image,(self.x,self.y))
class EnemyPlane(object):
def __init__(self,screen):
#设置飞机默认的位置
self.x = 0
self.y = 0
#设置要显示内容的窗口
self.screen = screen
self.imageName = "./feiji/enemy-1.gif"
self.image = pygame.image.load(self.imageName).convert()
#用来存储敌人飞机发射的所有子弹
self.bulletList = []
def display(self):
self.screen.blit(self.image,(self.x,self.y))
if __name__ == "__main__":
#1. 创建一个窗口,用来显示内容
screen = pygame.display.set_mode((480,890),0,32)
#2. 创建一个和窗口大小的图片,用来充当背景
background = pygame.image.load("./feiji/background.png").convert()
#3. 创建一个飞机对象
heroPlane = HeroPlane(screen)
#4. 创建一个敌人飞机
enemyPlane = EnemyPlane(screen)
#3. 把背景图片放到窗口中显示
while True:
screen.blit(background,(0,0))
heroPlane.display()
enemyPlane.display()
#判断是否是点击了退出按钮
for event in pygame.event.get():
# print(event.type)
if event.type == QUIT:
print("exit")
exit()
elif event.type == KEYDOWN:
if event.key == K_a or event.key == K_LEFT:
print('left')
heroPlane.moveLeft()
#控制飞机让其向左移动
elif event.key == K_d or event.key == K_RIGHT:
print('right')
heroPlane.moveRight()
elif event.key == K_SPACE:
print("space")
heroPlane.sheBullet()
pygame.display.update()
相关文章
- python 火车票爬取代码
- 【Python五篇慢慢弹(3)】函数修行知python
- python: 安装DeOldify库:黑白图片上色(Python 3.7.15)
- Python之pandas:特征工程中数据类型(object/category/bool/int32/int64/float64)的简介、数据类型转换四大方法、案例应用之详细攻略
- Python语言学习:Python语言学习之面向对象编程OO(继承&封装&多态)/类&方法/装饰器的简介、案例应用之详细攻略
- Python:python语言中与时间有关的库函数简介、安装、使用方法(获取当前时间/计算程序块前后运行时间/模型训练时间或耗费时间)之详细攻略
- Python:利用python编程将上海十六区,2020年5月份房价实时地图(数据来源房天下)进行柱状图、热图可视化
- Python编程语言学习:python中浅复制/深复制(或浅拷贝/深拷贝)的简介、案例应用注意事项之详细攻略
- Python编程语言学习:python中与数字相关的函数(取整等)、案例应用之详细攻略
- Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统
- Python之多线程:python多线程设计之同时执行多个函数命令详细攻略
- Python IDE之Pydev: 基于Eclipse搭建python的编译环境(Eclipse+pydev)简介、安装、使用技巧方法集合之详细攻略
- Python语言学习之图表可视化:python语言中可视化工具包的简介、安装、使用方法、经典案例之详细攻略
- Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)—命令提示符cmd的几种方法
- 100天精通Python(数据分析篇)——第65天:Pandas聚合操作与案例
- Python每日一练——第16天:字符串分析
- Python从入门到爬虫案例实现~
- python小案例——采集财经数据
- 【LeetCode Python实现】386. 字典序排数(中等)
- Python爬虫:PyExecJS在python中运行javascript代码
- Python: yield, python 实现tail -f
- Python开发案例之用Python子进程关闭Excel自动化中的弹窗
- 【异常】前端ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- opencv-python图像处理之让你的照片变旧
- Python开发入门到精通之11个案例讲透 Python 函数参数