python来写打飞机
准备用python写一个打飞机的游戏,相信能够写完这个项目,我对python的学习应该也算可以了。
首先,我们要使用python的一个pygame的库来写,这个库的安装比较简单就是普通的pip安装就可以了。
简单的介绍一下这个库的使用,当然,是代码的方式:
import pygame
import time
def main():
#1. 创建窗口(无背景,大小与背景一样)
screen = pygame.display.set_mode((480,852),0,32)
#2. 创建一个背景图片()
background = pygame.image.load("./img/background.png")
while True:#不停的刷新感觉好像一直都在一样
screen.blit(background, (0,0))
pygame.display.update()
time.sleep(0.01)#防cpu使用率过高
创建窗口并贴上背景
import pygame
import time
from pygame.locals import *#按键的左右控制 def main():
#1. 创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2. 创建一个背景图片
background = pygame.image.load("./img/background.png")
#3. 创建一个飞机图片
hero = pygame.image.load("./img/hero1.png")
x = 210
y = 700
while True:#左上的位置是(0,0),下面坐标就是放置的位置
screen.blit(background, (0,0))
screen.blit(hero, (x, y))
pygame.display.update()
# x+=1#让飞机动态动起来
# y-=1 # 获取事件,比如按键等
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')
x -= 5
# 检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
x += 5
# 检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
time.sleep(0.01)#防cpu使用率过高
造飞机并移动
有了飞机以后我们会多次操作游戏里的对象(fps的刷新导致图像的动态操作),所以我们把他封装成对象来操作更为方便。
# -*- coding:utf-8 -*- import pygame
from pygame.locals import *
import time class HeroPlane():#定义飞机类
def __init__(self, screen_temp):#实例化飞机对象
self.x = 210
self.y = 700
self.screen = screen_temp
self.image = pygame.image.load("./img/hero1.png")
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() def move_left(self):
self.x -= 5 def move_right(self):
self.x += 5
def fire(self):#开火技能
self.bullet_list.append(Bullet(self.screen, self.x, self.y)) class Bullet(object):#每一个子弹对象
def __init__(self, screen_temp, x, y):
self.x = x+40
self.y = y-20
self.screen = screen_temp
self.image = pygame.image.load("./img/bullet.png") def display(self):
self.screen.blit(self.image, (self.x, self.y)) def move(self):
self.y-=20 def key_control(hero_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')
hero_temp.move_left()
#检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()
#检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
hero_temp.fire()#开火 def main():
#1. 创建窗口
screen = pygame.display.set_mode((480,852),0,32) #2. 创建一个背景图片
background = pygame.image.load("./img/background.png") #3. 创建一个飞机对象
hero = HeroPlane(screen) while True:
screen.blit(background, (0,0))
hero.display()
pygame.display.update()
key_control(hero)
time.sleep(0.01) main()
有了飞机还要有枪

有枪有炮,这个时候我们需要一个敌人来锤一波。
# -*- coding:utf-8 -*- import pygame
from pygame.locals import *
import time
import random class HeroPlane():#定义飞机类
def __init__(self, screen_temp):#实例化飞机对象
self.x = 210
self.y = 700
self.screen = screen_temp
self.image = pygame.image.load("./img/hero1.png")
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) def move_left(self):
self.x -= 5 def move_right(self):
self.x += 5
def fire(self):#开火技能
self.bullet_list.append(Bullet(self.screen, self.x, self.y)) class Bullet(object):#每一个子弹对象
def __init__(self, screen_temp, x, y):
self.x = x+40
self.y = y-20
self.screen = screen_temp
self.image = pygame.image.load("./img/bullet.png") def display(self):
self.screen.blit(self.image, (self.x, self.y)) def move(self):
self.y-=20
def judge(self):
if self.y<0:#超过屏幕就删除
return True
else:
return False class EnemyPlane():
"""敌机的类和我们的一样"""
def __init__(self, screen_temp):
self.x = 0
self.y = 0
self.screen = screen_temp
self.image = pygame.image.load("./img/enemy0.png")
self.direction = "right" # 用来存储飞机默认的显示方向
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) def move(self): if self.direction == "right":
self.x += 5
elif self.direction == "left":
self.x -= 5 if self.x > 480 - 50:
self.direction = "left"
elif self.x < 0:
self.direction = "right" def fire(self):
random_num = random.randint(1,100)
if random_num == 8 or random_num == 20:
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) class EnemyBullet(object):#敌机的子弹
def __init__(self, screen_temp, x, y):
self.x = x+25
self.y = y+40
self.screen = screen_temp
self.image = pygame.image.load("./img/bullet1.png") def display(self):
self.screen.blit(self.image, (self.x, self.y)) def move(self):
self.y+=5 def judge(self):
if self.y>852:
return True
else:
return False def key_control(hero_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')
hero_temp.move_left()
#检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()
#检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
hero_temp.fire()#开火 def main():
#1. 创建窗口
screen = pygame.display.set_mode((480,852),0,32) #2. 创建一个背景图片
background = pygame.image.load("./img/background.png") #3. 创建一个飞机对象
hero = HeroPlane(screen) #4. 创建一个敌机
enemy = EnemyPlane(screen) while True:
screen.blit(background, (0,0))
hero.display()
enemy.display()
enemy.move()#调用敌机的移动方法
enemy.fire() # 敌机开火
pygame.display.update()
key_control(hero)
time.sleep(0.01) main()
制造一个敌机来锤
真的,代码超多的,我们来简化一点,用面向对象的继承,组合来解决这个问题。
# -*- coding:utf-8 -*-
import pygame
from pygame.locals import *
import time
import random class Base():#所有对象都要初始化位置
def __init__(self, screen_temp, x, y, image_name):
self.x = x
self.y = y
self.screen = screen_temp
self.image = pygame.image.load(image_name) class BasePlane(Base):#所有飞机特性抽象出来
def __init__(self, screen_temp, x, y, image_name):
super().__init__(screen_temp, x, y, image_name)
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) class HeroPlane(BasePlane):
def __init__(self, screen_temp):
super().__init__(screen_temp, 210, 700, "./img/hero1.png") def move_left(self):
self.x -= 5 def move_right(self):
self.x += 5 def fire(self):
self.bullet_list.append(Bullet(self.screen, self.x, self.y)) class EnemyPlane(BasePlane):
"""敌机的类""" def __init__(self, screen_temp):
super().__init__(screen_temp, 0, 0, "./img/enemy0.png")
self.direction = "right" # 用来存储飞机默认的显示方向 def move(self): if self.direction == "right":
self.x += 5
elif self.direction == "left":
self.x -= 5 if self.x > 480 - 50:
self.direction = "left"
elif self.x < 0:
self.direction = "right" def fire(self):
random_num = random.randint(1, 100)
if random_num == 8 or random_num == 20:
self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) class BaseBullet(Base):
def display(self):
self.screen.blit(self.image, (self.x, self.y)) class Bullet(BaseBullet):
def __init__(self, screen_temp, x, y):
super().__init__(screen_temp, x + 40, y - 20, "./img/bullet.png") def move(self):
self.y -= 20 def judge(self):
if self.y < 0:
return True
else:
return False class EnemyBullet(BaseBullet):
def __init__(self, screen_temp, x, y):
super().__init__(screen_temp, x + 25, y + 40, "./img/bullet1.png") def move(self):
self.y += 5 def judge(self):
if self.y > 852:
return True
else:
return False #后面就不需要改动了
def key_control(hero_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')
hero_temp.move_left()
# 检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()
# 检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
hero_temp.fire() def main():
# 1. 创建窗口
screen = pygame.display.set_mode((480, 852), 0, 32) # 2. 创建一个背景图片
background = pygame.image.load("./img/background.png") # 3. 创建一个飞机对象
hero = HeroPlane(screen) # 4. 创建一个敌机
enemy = EnemyPlane(screen) while True:
screen.blit(background, (0, 0))
hero.display()
enemy.display()
enemy.move() # 调用敌机的移动方法
enemy.fire() # 敌机开火
pygame.display.update()
key_control(hero)
time.sleep(0.01) if __name__ == "__main__":
main()
也没简化多少的版本
然后,然后游戏就完成了。
PlayPlane/
|-- bin/
| |-- main.py 程序运行主体程序
|-- config/
| |-- settings.py 程序配置(例如: 游戏背景音乐的加载等)
|-- material 程序素材放置(打飞机游戏素材放置)
|-- ...
|-- src/ 程序主体模块存放
| |-- __init__.py
| |-- bullet.py 我方飞机发射子弹实现代码存放
| |-- enemy.py 敌方飞机实现代码存放
| |-- plane.py 我方飞机实现代码存放
|-- manage.py 程序启动文件
|-- README.md
可以说是非常的暴力,非常的简单。
这个因为今天翻到了刚开始学python的时候买的一本《从入门到实践的书》,最初也想写一个打飞机游戏一定很有意思,然后经过这么长的时间的学习,现在对python的代码也有了一定的认知,前段时间看到github上一位老哥做的打飞机,今天做了一下,发现挺简单的。希望不忘初心,再接再厉。时间有限后面我就不写了,那位飞机老哥的打飞机地址:
https://github.com/triaquae/jerkoff
晚上还要看视频,溜了溜了。
python来写打飞机的更多相关文章
- python 之路,200行Python代码写了个打飞机游戏!
早就知道pygame模块,就是没怎么深入研究过,恰逢这周未没约到妹子,只能自己在家玩自己啦,一时兴起,花了几个小时写了个打飞机程序. 很有意思,跟大家分享下. 先看一下项目结构 "" ...
- Python小游戏之 - 飞机大战美女 !
用Python写的"飞机大战美女"小游戏 源代码如下: # coding=utf-8 import os import random import pygame # 用一个常量来存 ...
- Python小游戏之 - 飞机大战 !
用Python写的"飞机大战"小游戏 源代码如下: # coding=utf-8 import random import os import pygame # 用一个常量来存储屏 ...
- Python - 动手写个ORM
Python - 动手写个ORM 任务: 模拟简单的ORM - Object Relational Mapping 为model添加create方法 代码很简单,直接上 字段类型类 class Fie ...
- python中写shell(转)
python中写shell,亲测可用,转自stackoverflow To run a bash script, copy from stackoverflow def run_script(scri ...
- Python urllib2写爬虫时候每次request open以后一定要关闭
最近用python urllib2写一个爬虫工具,碰到运行一会程序后就会出现scoket connection peer reset错误.经过多次试验发现原来是在每次request open以后没有及 ...
- (转)Python新手写出漂亮的爬虫代码2——从json获取信息
https://blog.csdn.net/weixin_36604953/article/details/78592943 Python新手写出漂亮的爬虫代码2——从json获取信息好久没有写关于爬 ...
- (转)Python新手写出漂亮的爬虫代码1——从html获取信息
https://blog.csdn.net/weixin_36604953/article/details/78156605 Python新手写出漂亮的爬虫代码1初到大数据学习圈子的同学可能对爬虫都有 ...
- 原生canvas写的飞机游戏
一个原生canvas写的飞机游戏,实用性不大,主要用于熟悉canvas的一些熟悉用法. 项目地址:https://github.com/BothEyes1993/canvas_game
随机推荐
- 1.QT开发第一个程序
Ubuntu16.04安装QT5.8.0:http://www.cnblogs.com/dotnetcrazy/p/6725945.html QT5.8支持中文输入法(附带老版本的解决+不理想的情况解 ...
- Windows 7样式地址栏(Address Bar)控件实现
介绍 从Vista开始,地址栏就有了很大的改变,不知道大家有什么感觉,笔者觉得很方便,同时又兼容之前的功能,是个很不错的创新.不过,微软并不打算把这一很酷的功能提供给广大的开发人员. 本文提供了一个简 ...
- Kill 进程
动态杀各种进程,谨慎操作:事例 status='sleeping' --AUTHOR KiNg --DATE 2016-05-30 DECLARE @SPID INT ...
- 分布式监控系统Zabbix3.2添加自动发现磁盘IO并注册监控
zabbix并没有给我们提供这么一个模板来完成在Linux中磁盘IO的监控,所以我们需要自己来创建一个,在此还是在Linux OS中添加. 由于一台服务器中磁盘众多,如果只一两台可以手动添加,但服务 ...
- thinkphp->add方法错误
$group_id=$model->add($add); 以上这句代码如果执行成功,返回它存储的id,但是,会有一种情况一直返回1. 代码完全没有问题,检查数据库发现有两个主键id,删除一个就O ...
- 又趟一个坑,IO卡信号DI和DO的信号类型
工控IO卡可以感应到各种电信号,传感器的状态变化. DI信号包括数字开关信号(ture,false\0,1),光信号的变化(上升沿,下降沿). DO信号包括脉宽和数字开关信号(ture,false\0 ...
- C#调用windows api 实现打印机控制
using System; using System.Text; using System.Runtime.InteropServices; using System.Security; using ...
- JDK1.8中HashMap实现
JDK1.8中的HashMap实现跟JDK1.7中的实现有很大差别.下面分析JDK1.8中的实现,主要看put和get方法. 构造方法的时候并没有初始化,而是在第一次put的时候初始化 putVal方 ...
- CentOS7下安装Docker-Compose
Docker-Compose是一个部署多个容器的简单但是非常必要的工具. 安装Docker-Compose之前,请先安装 python-pip 安装 python-pip 1.首先检查linux有没有 ...
- 2018年1月 常用的linux命令
项目中经常用到的Linux命令 (注意:linux命令要小写哦!) (1).ls 显示当前目录下的文件 (2).vi vim 进入编辑器,可以选择你要编辑的文档,一般我们将项目打包成jar包来 ...