当前位置: 首页 > news >正文

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

前言

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

公式

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

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)**3
y = 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 plt
import numpy as np# 生成爱心形状的数据点
t = np.linspace(0, 2 * np.pi, 100)
x = 16 * np.sin(t)**3
y = 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:

# 初始化 Pygame
pygame.init()# 设置窗口大小
width, height = 800, 600
screen = 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)**3heart_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 = True
while 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 pygame
import sys
import random
import numpy as np# 初始化 Pygame
pygame.init()# 设置窗口大小
width, height = 800, 600
screen = 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)**3heart_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 = True
while 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://www.lryc.cn/news/299172.html

相关文章:

  • Java图形化界面编程—— 基本组件和对话框 笔记
  • 使用Docker,拉取Nginx镜像,创建并运行Nginx容器
  • InstantBox:开箱即用的临时 Linux 环境
  • 【面试】国家公务员考试复试,面试内容准备方向(非技术面试考察点)
  • 点餐|外卖订餐小程序|基于微信小程序的外卖订餐系统设计与实现(源码+数据库+文档)
  • 使用Cargo创建、编译与运行Rust项目
  • Js中toFixed(2)精度问题的原因及解决办法
  • 【医学大模型 知识增强】SMedBERT:结构化语义知识 + 医学大模型 = 显著提升大模型医学文本挖掘性能
  • Python爬虫:安全与会话管理
  • [Python进阶] 识别验证码
  • 华为问界M9:全方位自动驾驶技术解决方案
  • Java 与 JavaScript 的区别与联系
  • React18原理: 时间分片技术选择
  • 【QT+QGIS跨平台编译】之三十三:【SpatiaLite+Qt跨平台编译】(一套代码、一套框架,跨平台编译)
  • 【JavaEE】_CSS选择器
  • Flaurm实现中文搜索
  • STM32自学☞定时器外部时钟案例
  • PyCharm中无法调用ffmpeg命令行
  • Go基础知识学习-习题题解
  • MyBatis中的XML实现和动态SQL实现
  • clickhouse计算前后两点间经纬度距离
  • 【51单片机】DS18B20(江科大)
  • Windows平台git clone文件路径太长报错
  • 中科大计网学习记录笔记(十):P2P 应用
  • Python算法题集_LRU 缓存
  • 局部加权回归
  • 国内国外最好的数据恢复软件评测,哪种数据恢复软件最有效?
  • bugku 1
  • C++ bfs再探迷宫游戏(五十五)【第二篇】
  • 【Spring原理进阶】SpringMVC调用链+JSP模板应用讲解