Arcade实例应用(三)

Arcade实例应用(三)

今天我们来学习一下如何用arcade画一个会动的星空景象

引入库

from arcade import *
from arcade import color
from arcade import draw_rectangle_filled as rect
import random
import time

一定要注意arcade.color要单独引入,不然会报错。也可以对于一些长的函数进行一个as 的化简。

星空背景

上网上搜了很多图后,我发现了一个星空图的共同点,就是图的上半部分要比下半部分偏亮。这个对于背景来说是一个很重要的条件。因为这样的话我们就不能直接使用arcade.set_background()了。所以我们可以通过一个东西来造成色差。颜色在编程中被赋了值,值域从0到255,分为红色(R),绿色(G),蓝色(B)。对于颜色的表达用很多种,例如直接写名字,或者用(0,0,0)的格式,或者用#FFFFFF的形式。

a = 0
while(a < 599):
    rect(SCREEN_WIDTH / 2, a, SCREEN_WIDTH, 1, (a/10, 0, a/5))
    a = a + 1
a = 0 
#randomly draw 729 circles
while(a<81):
    draw_circle_filled(random.randint(0,SCREEN_WIDTH-1),random.randint(SCREEN_HEIGHT / 3,SCREEN_HEIGHT-1),1,color.WHITE)
    a = a + 1

第一个while的用处就是从最底下到上面,不断的画一条宽度为一的类似于线的长方形。

流星

既然是画一个星空,那么就需要画一些流星。流星的图案可以简单的看为一个球,后面有着一个三角形。

draw_circle_filled(self, indi, 3, color.LIGHT_SKY_BLUE)
draw_triangle_filled((self-1), (indi+1), (self+1), (indi-1), (self+90), (indi+75), color.LIGHT_SKY_BLUE)
draw_circle_filled(self,indi,2, color.WHITE)
draw_triangle_filled((self-0.8),(indi+0.8),(self+0.8),(indi-0.8),(self+60),(indi+50),color.WHITE)

在白色的流星外面多加一个蓝色的流星,会显得更好看。

路面

a = 0
while(a < 200):
    rect(SCREEN_WIDTH / 2, a, (SCREEN_WIDTH - 6*a), 1, color.DARK_GRAY)
    a = a + 1
a = 0
while(a < 200):
    rect(SCREEN_WIDTH / 2, a, (SCREEN_WIDTH - 6*a-10), 1, color.BLACK)
    a = a + 1
a = 0
while(a <200):
    rect(SCREEN_WIDTH / 2, a, (SCREEN_WIDTH/2 - 3*a), 1, (64,64,0))
    a = a + 1
a = 0
while(a <200):
    rect(SCREEN_WIDTH / 2, a, (SCREEN_WIDTH/2 - 3*a-10), 1, color.BLACK)
    a = a + 1

在屏幕的最底下放一个路面会显得更好看,不过这个路是要近处的宽,远处的窄。

把程序合起来

from arcade import *
from arcade import color
from arcade import draw_rectangle_filled as rect
import random
import time

SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 600

class road:
    height = 0
    e = 0
    def shape(a,b,c,d):
        rect(a,b,c,d,color.WHITE)
    def blank(a,b,c,d):
        rect(a,b,c,d,color.BLACK)
         
    @classmethod
    def draw(css):
        if css.e % 3 == 0:
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,40):
                css.shape(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,40):
                css.shape(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
        elif css.e % 3 == 1:
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,40):
                css.shape(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,40):
                css.shape(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
        elif css.e % 3 == 2:
            for i in range(0,40):
                css.shape(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,40):
                css.shape(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,20):
                css.blank(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
            for i in range(0,40):
                css.shape(SCREEN_WIDTH / 2, css.height, (40-0.2*css.height),1)
                css.height += 1
        css.height = 0
        css.e += 1
            
        
def draw_bg():
    """ Draw the ground """
    
    a = 0
    while(a < 599):
        rect(SCREEN_WIDTH / 2, a, SCREEN_WIDTH, 1, (a/10, 0, a/5))
        a = a + 1
    a = 0
    #randomly draw 729 circles
    while(a<81):
        draw_circle_filled(random.randint(0,SCREEN_WIDTH-1),random.randint(SCREEN_HEIGHT / 3,SCREEN_HEIGHT-1),1,color.WHITE)
        a = a + 1
    a = 0
    while(a < 200):
        rect(SCREEN_WIDTH / 2, a, (SCREEN_WIDTH - 6*a), 1, color.DARK_GRAY)
        a = a + 1
    a = 0
    while(a < 200):
        rect(SCREEN_WIDTH / 2, a, (SCREEN_WIDTH - 6*a-10), 1, color.BLACK)
        a = a + 1
    a = 0
    while(a <200):
        rect(SCREEN_WIDTH / 2, a, (SCREEN_WIDTH/2 - 3*a), 1, (64,64,0))
        a = a + 1
    a = 0
    while(a <200):
        rect(SCREEN_WIDTH / 2, a, (SCREEN_WIDTH/2 - 3*a-10), 1, color.BLACK)
        a = a + 1
    road.draw()
def draw_star(self, indi):
    """ Draw a star """
    draw_circle_filled(self, indi, 3, color.LIGHT_SKY_BLUE)
    draw_triangle_filled((self-1), (indi+1), (self+1), (indi-1), (self+90), (indi+75), color.LIGHT_SKY_BLUE)
    draw_circle_filled(self,indi,2, color.WHITE)
    draw_triangle_filled((self-0.8),(indi+0.8),(self+0.8),(indi-0.8),(self+60),(indi+50),color.WHITE)
        
    
def on_draw(a):
    """ Draw everything """
    start_render()
    draw_bg()
    draw_star(on_draw.star_x1, on_draw.star_y1)
    a = random.randint(1,5)
    on_draw.star_x1 -= 10*a
    on_draw.star_y1 -= 5*a
    if(on_draw.star_y1 <= 200):
        on_draw.star_x1 = random.randint(800,1600)
        on_draw.star_y1 = 599
     
    draw_star(on_draw.star_x2, on_draw.star_y2)
    a = random.randint(1,5)
    on_draw.star_x2 -= 10*a
    on_draw.star_y2 -= 5*a
    if(on_draw.star_y2 <= 200):
        on_draw.star_x2 = random.randint(800,1600)
        on_draw.star_y2 = 599

    draw_star(on_draw.star_x3, on_draw.star_y3)
    a = random.randint(1,5)
    on_draw.star_x3 -= 10*a
    on_draw.star_y3 -= 5*a
    if(on_draw.star_y3 <= 200):
        on_draw.star_x3 = random.randint(800,1600)
        on_draw.star_y3 = 599

    draw_star(on_draw.star_x4, on_draw.star_y4)
    a = random.randint(1,5)
    on_draw.star_x4 -= 10*a
    on_draw.star_y4 -= 5*a
    if(on_draw.star_y4 <= 200):
        on_draw.star_x4 = random.randint(800,1600)
        on_draw.star_y4 = 599

    draw_star(on_draw.star_x5, on_draw.star_y5)
    a = random.randint(1,5)
    on_draw.star_x5 -= 10*a
    on_draw.star_y5 -= 5*a
    if(on_draw.star_y5 <= 200):
        on_draw.star_x5 = random.randint(800,1600)
        on_draw.star_y5 = 599
        
on_draw.star_x1 = random.randint(500,800)
on_draw.star_y1 = 599
on_draw.star_x2 = random.randint(500,800)
on_draw.star_y2 = 599
on_draw.star_x3 = random.randint(500,800)
on_draw.star_y3 = 599
on_draw.star_x4 = random.randint(500,800)
on_draw.star_y4 = 599
on_draw.star_x5 = random.randint(500,800)
on_draw.star_y5 = 599


    

def main():
    open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing with Functions")
    set_background_color(color.DARK_SLATE_BLUE)
    schedule(on_draw, 1/60)
    run()


main()


效果图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

A Python 萌新花花

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

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

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

打赏作者

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

抵扣说明:

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

余额充值