python游戏入门儿童版二,让画画动起来

1,现在我们开始画一个组合的画,再让它动起来

我们先画出小车来,代码如下:

import pygame

pygame.init() # 初始化
screen = pygame.display.set_mode((800,600)) # 设置窗口的大小
pygame.display.set_caption("这是一个给我们画画用的窗口")

pygame.draw.rect(screen, (155, 155, 155), (300, 200, 100, 50), 0)#画一个长方形做车体
pygame.draw.circle(screen, (155, 155, 155), [315, 265], 15)#画一个圆形做轮胎
pygame.draw.circle(screen, (155, 155, 155), [385, 265], 15)#画一个圆形做轮胎
pygame.display.update()

going = True
while going:
    for event in pygame.event.get():  # 遍历事件
        if event.type == pygame.QUIT:  # 退出事件
            going=False

pygame.quit()

看下效果,再尝试看还能画出什么来

接下来我们放在循环里边,并更改坐标,代码如下:

import pygame

pygame.init() # 初始化
screen = pygame.display.set_mode((800,600)) # 设置窗口的大小
pygame.display.set_caption("这是一个给我们画画用的窗口")

fclock = pygame.time.Clock()  # 控制时间间隔

x=300
y=200
color = 155, 155, 155
going = True
while going:
    for event in pygame.event.get():  # 遍历事件
        if event.type == pygame.QUIT:  # 退出事件
            going=False

    x += 1
    pygame.draw.rect(screen, color, (x, y, 100, 50), 0)  # 画一个长方形做车体
    pygame.draw.circle(screen, color, [x+15, y+65], 15)  # 画一个圆形做轮胎
    pygame.draw.circle(screen, color, [x+85, y+65], 15)  # 画一个圆形做轮胎
    pygame.display.update()
    fclock.tick(200)

pygame.quit()

发现小车移动了,但是之前的位置的小车没有消息,于是我们需要把之前的小车用黑色再画一次覆盖掉他,这时候为了方便我们会用到函数,方便多个地方调用,而不需要去重复写代码

具体代码如下:

import pygame

#画小车的函数
def bus(x,y,color):
    pygame.draw.rect(screen, color, (x, y, 100, 50), 0)  # 画一个长方形做车体
    pygame.draw.circle(screen, color, [x+15, y+65], 15)  # 画一个圆形做轮胎
    pygame.draw.circle(screen, color, [x+85, y+65], 15)  # 画一个圆形做轮胎

pygame.init() # 初始化
screen = pygame.display.set_mode((800,600)) # 设置窗口的大小
pygame.display.set_caption("这是一个给我们画画用的窗口")

fclock = pygame.time.Clock()  # 控制时间间隔

x=300
y=200
color = 155, 155, 155
going = True
while going:
    bus(x, y, (0,0,0))
    for event in pygame.event.get():  # 遍历事件
        if event.type == pygame.QUIT:  # 退出事件
            going=False

    x += 1
    if(x>800):#当小车跑出画布之后,我们让它从最左边出来
        x=0
    bus(x,y,color)
    pygame.display.update()
    fclock.tick(200)

pygame.quit()

2*上升的降落伞

还记得自己小学时候学的的logo语言,最后一节课老师画了一个降落伞,并让它动起来,代码比较多,那时候我们只能抄下老师的代码,并输入到电脑上去看看效果,有些同学实现了,就会惊叫起来,其他同学都会去围观,那种心情现在都值得回味,现在我们尝试用python来实现它

结合一些初中的数学知识(没有初中知识也没关系,后边很少用到这复杂的计算),先画一个降落伞,然后我们要更新这个降落线的位置让它动起来,实际就是更新坐标,我们可以把这个降落伞放一进一个函数来实现,然后更改坐标,这次也轮到我做下老师把这个代码写出来

具体代码如下:

import pygame
import math

#画降落伞的函数
def jls(x,y,color):
    w = 100
    h = 100
    rect = x, y, w, h  # Rect(left, top, width, height)
    start_angle = math.radians(0)  # 根据度数求出弧
    stop_angle = math.radians(180)
    width = 1  # 线条粗细

    # 画弧形
    pygame.draw.arc(screen, color, rect, start_angle, stop_angle, width)
    abch = math.sqrt(100 ** 2 * 3 / 4)  # 100**2表示100的2次方,3/4表示4分之三,这里是根据勾股定理来求出三角形的高
    # 用连续线段画个等边三角形
    pygame.draw.lines(screen, color, False, [[x, y + 50], [x + 100, y + 50], [x + 50, y+50 + abch], [x, y+50]], 1)
    # 用连续线段画个等边四边形
    pygame.draw.lines(screen, color, False, [[x + 25, y + 50 + abch], [x + 75, y + 50 + abch], [x + 75, y + 100 + abch],
                                             [x + 25, y + 100 + abch], [x + 25, y + 50 + abch]], 1)

pygame.init() # 初始化
screen = pygame.display.set_mode((800,600)) # 设置窗口的大小
pygame.display.set_caption("这是一个给我们画画用的窗口")

fclock = pygame.time.Clock()  # 控制时间间隔

mx = 300
my = 200
going = True
while going:

    jls(mx, my, (0, 0, 0))#删除上次画的图
    for event in pygame.event.get():  # 遍历事件
        if event.type == pygame.QUIT:  # 退出事件
            going=False

    if(mx < 0):
        mx = 800
    if(my < 0):
        my = 600
    mx -= 1
    my -= 1
    jls(mx,my,(255, 0, 255))
    pygame.display.update()

    fclock.tick(200)

pygame.quit()

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

boyxgb

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

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

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

打赏作者

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

抵扣说明:

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

余额充值