1 说明
=====
1.1 火箭发射嫦娥五号已经正在月球上“挖土”了,培养自己和孩子的计算机知识和科学素养刻不容缓![笑哭]
1.2 我就用python的turtle来画火箭发射。
1.3 效果图
1.3 牵涉到的知识
=============
1.3.1 python-turtle画火箭=rocket和尾部的火焰。
1.3.2 截图png的图片修改透明背景,用PIL,生成png,本机直接再修改为gif,适合做动画使用。
1.3.3 turtle图片的gif的动画设计编程。
图片来自今日头条
2 画火箭和火焰
============
2.1 rocket-draw0.py
===============
2.1.1 效果图
rocket0.png图片
2.1.2 代码:
from turtle import *
import math
title('rocket0')
shape("turtle")
color("blue")
speed(1)
pu()
goto(0,300)
pd()
#画头
def head():
begin_fill()
fillcolor('red')
left(120)
forward(100)
left(120)
forward(100)
left(120)
forward(100)
end_fill()
#画体
def body():
begin_fill()
fillcolor('pink')
right(90)
forward(300)
right(90)
forward(100)
right(90)
forward(300)
right(90)
forward(100)
right(90)
forward(300)
end_fill()
#画底
def bottom():
begin_fill()
fillcolor('green')
pu()
goto(0,0)
pd()
left(45)
forward(70)
right(135)
forward(200)
right(135)
forward(70)
end_fill()
#画火箭体左侧耳朵
def ear_left():
begin_fill()
fillcolor('blue')
pu()
goto(-100,200)
left(180)
pd()
fd(70)
left(135)
fd(50)
end_fill()
#画火箭体右侧耳朵
def ear_right():
pu()
goto(0,200)
begin_fill()
fillcolor('blue')
right(45)
pd()
fd(70)
right(135)
fd(50)
right(90)
fd(50)
end_fill()
def main():
head()
body()
bottom()
ear_left()
ear_right()
ht()
done()
main()
2.2 rocket-draw1.py
==============
2.2.1 效果图
rocket1.png图片
2.2.2 注意与代码相同部分折叠起来
2.2.3 代码:
from turtle import *
import math
title('rocket1')
shape("turtle")
color("blue")
speed(1)
pu()
goto(0,300)
pd()
'''
#与上面相同部分,暂时折叠和省略
#画头
def head():
#画体
def body():
#画底
def bottom():
#画火箭体左侧耳朵
def ear_left():
#画火箭体右侧耳朵
def ear_right():
'''
#黄色内火焰
def huangse():
pencolor('yellow')
pu()
goto(-50,-50)
left(180)
pd()
for i in range(20):
fd(10)
pensize(i 30)
for i in range(10):
fd(10)
pensize(40-i*2)
pencolor('black')
penup()
home()
pendown()
def main():
head()
body()
bottom()
ear_left()
ear_right()
huangse()
ht()
done()
main()
2.3 rocket-draw2.py
==============
2.3.1 效果图
rocket2.png图片
2.3.2 同上面,代码
from turtle import *
import math
title('rocket2')
shape("turtle")
color("blue")
speed(1)
pu()
goto(0,300)
pd()
'''
#相同部分已经折叠和省略
#画头
def head():
#画体
def body():
#画底
def bottom():
#画火箭体左侧耳朵
def ear_left():
#画火箭体右侧耳朵
def ear_right():
'''
def waiyan():
pu()
goto(0,-45)
pd()
fillcolor("#FF4500")
pencolor("#FF4500")
pensize(5)
begin_fill()
circle(50,-180)
for i in range(4):
right(35-7*i)
fd(100 10*i)
left(155 3*i)
fd(40 i**2)
seth(270)
seth(228)
penup()
goto(0,-45)
pendown()
seth(0)
for i in range(4):
right(75 7*i)
fd(100 10*i)
right(155 3*i)
fd(40 i**2)
seth(0)
seth(42)
goto(0,-45)
end_fill()
pencolor('white')
penup()
home()
pendown()
def main():
head()
body()
bottom()
ear_left()
ear_right()
waiyan()
ht()
done()
main()
3 图片背景透明
============
3.1 代码
import PIL.Image as Image
# 以第一个像素为准,相同色改为透明
def transparent_back(img):
img = img.convert('RGBA')
L, H = img.size
color_0 = img.getpixel((0,0))
for h in range(H):
for l in range(L):
dot = (l,h)
color_1 = img.getpixel(dot)
if color_1 == color_0:
color_1 = color_1[:-1] (0,)
img.putpixel(dot,color_1)
return img
if __name__ == '__main__':
#3张图片逐个修改
img=Image.open('/home/xgj/Desktop/rocket-fly/rocket2.png')
img=transparent_back(img)
img.save('/home/xgj/Desktop/rocket-fly/rocket22.png')
3.2 将图片复制后重新命名:
rocket00.png改为rocketfly0.gif
rocket11.png改为rocketfly1.gif
rocket22.png改为rocketfly2.gif
3.3 文件夹下图
4 火箭发射
========
4.1 rocket-fly.py代码
#在该文件夹打开终端,输入
#python3.8 rocket-fly.py
#第1步:导入模块
from turtle import *
from time import time
import sys
import glob
#第2步:窗口的初始化设置
#窗口的标题名
title('rocket fly in sky')
#背景颜色
bgcolor("black")
#输出文字颜色和内容等设置
pu()
pencolor("white")
goto(-500,400)
pd()
write("操作说明:\n按空格键开始和发射!", align="left", font=("楷体", 16, "bold"))
#地平线位置
ground_line =-132
#第3步:动画设置
#读取动画
for f in glob.glob("*.gif"):
addshape(f)
#定义动画
def GIFTurtle(fname):
t = Turtle(fname ".gif")
t.speed(0)
t.up()
return t
#动画,初始化第一张图
rocket = GIFTurtle("rocketfly0")
#第4步:游戏类
class game:
state = "end"
best = 0
game = Game()
#定义游戏开始
def start_game(game):
game.hit_t, game.hit_y = 0, 0
game.state = "alive"
game.start_time = time()
update_game(game)
#计算y坐标轴,可变
def compute_y(t, game):
return game.hit_y - 100 * (t - game.hit_t) * (t - game.hit_t - 1)
#游戏更新
def update_game(game):
if game.state == "dead":
game.state = "end"
return
t = time() - game.start_time
rocket_y = compute_y(t, game)
if rocket_y <= ground_line:
rocket_y = ground_line
game.state = "dead"
rocket.shape("rocketfly%d.gif" % abs(int(t * 4) % 4 - 1))
#说明是垂直上下运动
rocket.goto(0, rocket_y)
update()
ontimer(lambda: update_game(game), 10)
#第5步:起飞和发射
def fly(game=game):
if game.state == "end":
start_game(game)
return
t = time() - game.start_time
rocket_y = compute_y(t, game)
if rocket_y > ground_line:
game.hit_t, game.hit_y = t, rocket_y
#第6步:最后设置
#总起和调用
#按空格键==space起飞
onkey(fly, "space")
listen()
mainloop()
sys.exit(1)
4.2 效果图
在游戏中学习python编程,分享处理。
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved