当前位置:首页 » 《随便一记》 » 正文

情人节到了,写一份爱心程序(python)

17 人参与  2024年02月21日 11:46  分类 : 《随便一记》  评论

点击全文阅读


前言

情人节到了,写一份爱心代码给喜欢的人呀

公式

首先我们介绍下爱心的公式的参数方程:

x = 16 s i n 3 ( t ) x = 16sin^3(t) x=16sin3(t)
y = 13 c o s ( t ) − 5 c o s ( 2 t ) − 2 c o s ( 3 t ) − c o s ( 4 t ) y = 13cos(t) - 5cos(2t) - 2cos(3t) - cos(4t) y=13cos(t)−5cos(2t)−2cos(3t)−cos(4t)

根据这个公式,我们可以实现基本的代码
首先使用 n p . l i n s p a c e ( a , b , n u m ) np.linspace(a, b, num) np.linspace(a,b,num) 这个函数生成 a到b区间内的num个数,这些数作为参数t,然后生成对应x,y的值,

t = np.linspace(0, 2 * np.pi, 100)x = 16 * np.sin(t)**3y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)

然后我们将生成的点平铺在二维坐标上

plt.plot(x, y, color='red')plt.title('Heart ')plt.axis('equal')  # 保持坐标轴相等plt.show()

最终效果
在这里插入图片描述
完整代码

import matplotlib.pyplot as pltimport numpy as np# 生成爱心形状的数据点t = np.linspace(0, 2 * np.pi, 100)x = 16 * np.sin(t)**3y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)# 绘制爱心plt.plot(x, y, color='red')plt.title('Heart ')plt.axis('equal')  # 保持坐标轴相等plt.show()

进阶

我们使用pygame创建一个界面,在界面上随机生成不同颜色的爱心

初始化pygame:

# 初始化 Pygamepygame.init()# 设置窗口大小width, height = 800, 600screen = pygame.display.set_mode((width, height))pygame.display.set_caption('Random Hearts')

编写绘制爱心的函数,根据形参x,y确定位置,color确定颜色

def draw_heart(x, y, color):    # 生成爱心形状的直角坐标数据点    t = np.linspace(0, 2 * np.pi, 1000)    heart_x = x + 16 * np.sin(t)**3    heart_y = y - (13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t))    # 绘制爱心形状    pygame.draw.lines(screen, color, False, list(zip(heart_x, heart_y)), 2)

在主循环中一直生成随机的位置和颜色,绘制pygame界面,然后刷新显示

# 主循环running = Truewhile running:    for event in pygame.event.get():        if event.type == pygame.QUIT:            running = False    # 随机生成爱心的位置和颜色    heart_x = random.randint(0, width)    heart_y = random.randint(0, height)    heart_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))    # 在随机位置绘制不清空的爱心    draw_heart(heart_x, heart_y, heart_color)    # 刷新屏幕    pygame.display.flip()    # 控制帧率    pygame.time.Clock().tick(1)

效果
在这里插入图片描述

完整代码

import pygameimport sysimport randomimport numpy as np# 初始化 Pygamepygame.init()# 设置窗口大小width, height = 800, 600screen = pygame.display.set_mode((width, height))pygame.display.set_caption('Random Hearts')def draw_heart(x, y, color):    # 生成爱心形状的直角坐标数据点    t = np.linspace(0, 2 * np.pi, 1000)    heart_x = x + 16 * np.sin(t)**3    heart_y = y - (13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t))    # 绘制爱心形状    pygame.draw.lines(screen, color, False, list(zip(heart_x, heart_y)), 2)# 主循环running = Truewhile running:    for event in pygame.event.get():        if event.type == pygame.QUIT:            running = False    # 随机生成爱心的位置和颜色    heart_x = random.randint(0, width)    heart_y = random.randint(0, height)    heart_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))    # 在随机位置绘制不清空的爱心    draw_heart(heart_x, heart_y, heart_color)    # 刷新屏幕    pygame.display.flip()    # 控制帧率    pygame.time.Clock().tick(1)# 退出程序pygame.quit()sys.exit()

点击全文阅读


本文链接:http://zhangshiyu.com/post/69293.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 我无名分,我不多嗔小说完结篇(陆景铄林悠悠)(我无名分,我不多嗔)全书无套路阅读无广告小说大结局
  • 奈何春风不曾渡我虐心反转_童烟沈叙白陆依在线看_小说后续在线阅读_无删减免费完结_
  • 鹿雪附加完整在线阅读(小祖宗她带着双宝闹翻天)最近更新列表
  • 你要战功我给你,妖兽入侵你哭啥高质量好文叶天凌筱完本_你要战功我给你,妖兽入侵你哭啥高质量好文(叶天凌筱)
  • 夫君和小三霸占了我的将军府,我杀疯了阅读_雪儿谭绍木兰全集_小说后续在线阅读_无删减免费完结_
  • 鹿溪小说(鹿溪)终章+番外(穿进恐怖游戏后,我和反派HE了)一口气阅读
  • 完结文留给儿子的副卡月月刷爆,他却因睡桥底被送进收容所列表_完结文留给儿子的副卡月月刷爆,他却因睡桥底被送进收容所(娄墨霆宋佑轩)
  • 你活着,却死在我等你的第三年后续+番外_谨言佳佳温以宁全集_小说后续在线阅读_无删减免费完结_
  • 相思溺于夏时雨小说(裴景澈初念可)章节目录+起始篇章(相思溺于夏时雨)全章无套路在线
  • 未婚夫重生归来叫停手术,把剥开肚子的我困在手术台上40分钟(裴临渊阮离歌)
  • 完结文离婚是你提,净身出户你又哭什么精彩分享列表_完结文离婚是你提,净身出户你又哭什么精彩分享(苏铭余素伊)
  • 全书浏览哥哥重生救我,我带队灭了组织!(陈致远陈知韫)_哥哥重生救我,我带队灭了组织!(陈致远陈知韫)全书结局

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1