Arcade实例应用(一)

Arcade实例应用(一)

今天我们来学习一下如何用arcade画土星

打开一个窗口

arcade的功能都是体现在一个独特的窗口上的,所以我们最开始需要先打开一个窗口。

from arcade import open_window as window
from arcade import set_background_color as sbc
from arcade import start_render as start
from arcade import draw_triangle_filled as tri
from arcade import draw_rectangle_filled as rect
from arcade import draw_ellipse_filled as ellipse
from arcade import csscolor as css
from arcade import draw_arc_outline as arc
from arcade import draw_polygon_filled as pol
from arcade import draw_circle_filled as circle
from arcade import draw_line as line
from arcade import draw_text as text
from arcade import finish_render as finish
from arcade import run
from arcade import color
from time import sleep as sl
from random import randint as rt
height = 620
width = 980
window(width,height, "Your best choice for window desktop")

我自己还from import as了一些arcade里面的函数,这样方便我们在接下来的程序调用(原来的函数名字太长了)

原函数arcade.open_window(height,width, window_name),height就是打开的窗口的高度,width就是打开的窗口的宽度,window_name就是打开的窗口的名字,这个名字会出现在左上角。因为这个window_name是一个string类型,所以说填什么都可以。

开始画图

仅仅打开一个窗口只是提供了一个画图的地方,想要画图,我们还需要arcade.start_render()和arcade.finish_render()

arcade.start_render()
#这里填程序主体
arcade.finish_render()

这两个函数就如同turtle库里面的begin_fill()和end_fill()一个作用,不过arcade是必须写的,turtle则不是。

背景构造

对于背景的构造,我的想法是纯黑色+白色的星星

sbc(color.BLACK)
start()
x = 0
#随机在创建的窗口上画729颗星星
while(x<729):
    circle(rt(0,width-1),rt(0,height-1),1,color.WHITE)
    x = x + 1

先设置背景的颜色为黑色,然后使用random函数(详见:0基础学python---random库和random库实例应用),在创建的窗口上随机选择坐标进行圆的绘制。本来想过import sympy的库,然后利用sympy.oo去达到一个无限大的用处,反过来就可以得到直径无限小的圆了。但是经过实验,1除以无限大会是0,这样不但会什么都不出现,还不报错。这样会造成非常大的困扰。最后经过实验,1的直径其实就可以了,因为整个的长度和宽度都是以百做单位的,所以即使半径是1,也没有什么大碍。

效果如下:

虽然说是无序且随机的,但是不得不说比有序的要好看多了。

土星构造

对于土星的构造,我想的是利用一个圆去达到星球的效果。

circle(175,175,151,color.OLIVE)

原函数是arcade.draw_circle_filled(x坐标,y坐标,半径,颜色)。这里顺便说一下,arcade里面的函数填写颜色的部分的时候只能用有关颜色的函数,直接填颜色会报错。本来是想使用那种土黄但是偏橘的颜色去画土星,可惜arcade好像不支持rgb。

效果如下:

虽然说这个圆很丑,但是这个是最接近真实的土星的颜色了。虽然土星本身不是单一的颜色,但是由于我本身并没有很多时间,就只能这样了。如果想要完美还原土星本来的颜色,那么少说需要一个大神级别(技术高还有很多肝)的程序员和一个精通调色的美术家。

土星圆环制造

图形圆环一看就是一个标准的椭圆(从某个视角来看,并不是指本来的样子),这个就非常好画了。

y = 0
x = 0
#draw a arc
while(x<50 and y<50):
    a = rt(1,11)
    if(a<6):
        arc(150,170,700-3*x,200-y,color.BLACK, -180,180)
        y +=1
        x +=1
    else:
        arc(150,170,700-3*x,200-y,color.BRONZE_YELLOW, -180,180)
        y +=1
        x +=1

通过一个循环,不断地画越来越小的椭圆,这样就可以构造出土星圆环看上去两头宽,中间窄的效果。我这里用的是3:1,有可能有更好的比例。因为本身土星的圆环颜色不是单一的颜色,所以我通过randint函数随机去选择是黄色还是黑色。

效果如下:

但是这里就有问题了,如果是这样画圆环的话,那么圆环就不是围绕着土星,而是给人感觉立起来的。所以,这里我把圆环拆成两部分,先画遮挡住的一部分,再画在前面的一部分,这样土星的圆环就可以绕着土星了。

y = 0
x = 0
#draw a half of a arc
while(x<50 and y<50):
    a = rt(1,11)
    if(a<6):
        arc(150,170,700-3*x,200-y,color.BLACK, 0,180)
        y +=1
        x +=1
    else:
        arc(150,170,700-3*x,200-y,color.BRONZE_YELLOW, 0,180)
        y +=1
        x +=1
#draw a circle
circle(175,175,151,color.OLIVE)

y = 0
x = 0
#draw a arc
while(x<50 and y<50):
    a = rt(1,11)
    if(a<6):
        arc(150,170,700-3*x,200-y,color.BLACK, -180,0)
        y +=1
        x +=1
    else:
        arc(150,170,700-3*x,200-y,color.BRONZE_YELLOW, -180,0)
        y +=1
        x +=1

两段相似的程序,只不过在arcade.draw_arc_outline里面,我们可以选择角度,去控制展现的部分。这里我选择了一切为二。

效果如下:

虽然土星圆环的边缘肉眼可见的没有对齐,但是这是因为randint函数的不可控性,可以多试几次,说不定哪次就好看了。

点缀

对于点缀方面,我本来有两个方案,一个是画一个航天飞船或者空间站,另一个是画一个歼星大炮。但是试了之后,发现果然学编程的不适合画画,于是选择了画一个卫星(事实上土星没有人造卫星)。

#draw a rectangle
rect(200,350,10,10,color.SILVER,)
#draw a triangle
tri(205,350,210,355,210,345,color.SILVER)
#draw a rectangle
rect(223,350,25,14,color.SILVER)
#draw a triangle
tri(195,350,190,355,190,345,color.SILVER)
#draw a rectangle
rect(177,350,25,14,color.SILVER)
#text
text("Python huahua", (width-100),10,color.YELLOW)
text("2021", (width-50),0,color.YELLOW)
#draw a line
line(width-50,25,(width-1),25,color.YELLOW)

这里我用了arcade.draw_rectangle_filled(), arcade.draw_triangle_filled(),draw_line()和draw_text()。这些函数虽然说都不是很难,但是计算起来还是挺烦人的。有一点需要注意就是text是不能显示中文的,会以乱码的形式出现。

总代码:

from arcade import open_window as window
from arcade import set_background_color as sbc
from arcade import start_render as start
from arcade import draw_triangle_filled as tri
from arcade import draw_rectangle_filled as rect
from arcade import draw_ellipse_filled as ellipse
from arcade import csscolor as css
from arcade import draw_arc_outline as arc
from arcade import draw_polygon_filled as pol
from arcade import draw_circle_filled as circle
from arcade import draw_line as line
from arcade import draw_text as text
from arcade import finish_render as finish
from arcade import run
from arcade import color
from time import sleep as sl
from random import randint as rt
height = 620
width = 980
window(width,height, "Your best choice for window desktop")
sbc(color.BLACK)
start()
x = 0
#randomly draw 729 circles
while(x<729):
    circle(rt(0,width-1),rt(0,height-1),1,color.WHITE)
    x = x + 1

y = 0
x = 0
#draw a half of a arc
while(x<50 and y<50):
    a = rt(1,11)
    if(a<6):
        arc(150,170,700-3*x,200-y,color.BLACK, 0,180)
        y +=1
        x +=1
    else:
        arc(150,170,700-3*x,200-y,color.BRONZE_YELLOW, 0,180)
        y +=1
        x +=1
#draw a circle
circle(175,175,151,color.OLIVE)

y = 0
x = 0
#draw a arc
while(x<50 and y<50):
    a = rt(1,11)
    if(a<6):
        arc(150,170,700-3*x,200-y,color.BLACK, -180,0)
        y +=1
        x +=1
    else:
        arc(150,170,700-3*x,200-y,color.BRONZE_YELLOW, -180,0)
        y +=1
        x +=1

#draw a rectangle
rect(200,350,10,10,color.SILVER,)
#draw a triangle
tri(205,350,210,355,210,345,color.SILVER)
#draw a rectangle
rect(223,350,25,14,color.SILVER)
#draw a triangle
tri(195,350,190,355,190,345,color.SILVER)
#draw a rectangle
rect(177,350,25,14,color.SILVER)
#text
text("Python huahua", (width-100),10,color.YELLOW)
text("2021", (width-50),0,color.YELLOW)
#draw a line
line(width-50,25,(width-1),25,color.YELLOW)
finish()
run()

总效果图:

tips:

土星环是太阳系行星的行星环中最突出与明显的一个,环中有不计其数的小颗粒,其大小从微米到米都有,轨道成丛集的绕着土星运转。环中的颗粒主要成分都是水冰,还有一些尘埃和其它的化学物质。

虽然环的反射能够增加土星的亮度,但从地球仅凭裸眼还是看不见环。在1610年,当望远镜第一次指向天空之际,伽利略虽然未能清楚的看出环的本质,但他还是成为观察土星环的第一个人。在1655年,惠更斯成为第一个描述环是环绕土星的盘状的人。

虽然许多人都认为土星环是由许多微细的小环累积而成的(这个观念可以回溯至拉普拉斯),并有少数真实的空隙。更正确的想法是这些环是有着同心但是在密度和著亮度上有着极值的圆环盘。在丛集的尺度上,圆环之间有许多空洞的空间。

土星环真正的样子。厉害的程序员们可以试试,但是我没有那个时间。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A Python 萌新花花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值