简单python爱心代码(【全网最全爱心代码】谁说程序员不懂浪漫!)

wufei123 发布于 2024-09-19 阅读(8)

目录C语言简易爱心代码C语言动态爱心代码Python简易爱心代码Python动态爱心代码Python爱心泡泡1.C语言简易爱心代码原理:心形线直角坐标式(x^2+y^2-1)^3=x^2*y^3让a=x^2+y^2-1,那么a*a*a就是(x^2+y^2-1)^3,有数学定理易得(x^2+y^2-1)^3<=x^2*y^3是为心形线里面的部分包括心形线,那么只要满足(x^2+y^2-1)^3<=x^2*y^3就输出某个指定符号,不满足就输出空格,就可以获得由这个字符组成的爱心,下面我使用的是v当指定字符,用三目运算符判断是否满足(x^2+y^2-1)^3<=x^2*y^3,注意输出完一行要换行。

代码:#includeintmain(){ for (float y = 2.0f; y > -2.0f; y -= 0.1f) { for (float x = -2.0f; x <

2.0f; x += 0.05f) { float a = x * x + y * y - 1; putchar(a * a * a - x * x * y * y * y <= 0.0f ?

v : ); } putchar(\n); } }执行结果:

2.C语言动态爱心代码涉及知识点:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),字体色)SetConsoleTextAttribute()是Windows系统中一个可以设置控制台窗口字体颜色和背景色的计算机函数,常用的几种颜色:

0=黑色 1=蓝色 2=绿色 4=红色 3=湖蓝色 5=紫色 6=黄色 7=白色 8=灰色sheep()执行挂起一段时间代码:#include#include#include

#includefloatf(float x, float y, float z){ float a = x * x + 9.0f / 4.0f * y * y + z * z -

1; return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z; } floath(float x, float

z){ for (float y = 1.0f; y >= 0.0f; y -= 0.001f) if (f(x, y, z) <= 0.0f) return y; return0.0f

; } intmain(){ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xc

);//SetConsoleTextAttribute()是Windows系统中一个可以设置控制台窗口字体颜色和背景色的计算机函数 HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);

// GetStdHandle()检索指定标准设备的句柄(标准输入、标准输出或标准错误) _TCHAR buffer[25][80] = { _T() }; _TCHAR ramp[] = _T("vvvvvvvv"

); int count = 0; int count1 = 0; for (float t = 0.0f;; t += 0.1f) { int sy = 0; float s = sinf(t);

float a = s * s * s * s * 0.2f; for (float z = 1.3f; z > -1.2f; z -= 0.1f) { _TCHAR *p = &buffer[sy++][

0]; float tz = z * (1.2f - a); for (float x = -1.5f; x < 1.5f; x += 0.05f) { float tx = x * (

1.2f + a); float v = f(tx, 0.0f, tz); if (v <= 0.0f) { float y0 = h(tx, tz); float

ny = 0.01f; float nx = h(tx + ny, tz) - y0; float nz = h(tx, tz + ny) - y0; float nd =

1.0f / sqrtf(nx * nx + ny * ny + nz * nz); float d = (nx + ny - nz) * nd * 0.5f + 0.5f; *p++ = ramp[(

int)(d * 5.0f)]; } else *p++ = ; } } for (sy = 0; sy < 25; sy++) { COORD coord = {

0, sy }; SetConsoleCursorPosition(o, coord);//作用是设置控制台(cmd)光标位置 WriteConsole(o, buffer[sy], 79,

NULL, 0);//从当前光标位置开始,将字符串写入控制台屏幕缓冲区 } if (count <= 22) { printf("I Love You") ;//表白内容printf(

" To CSDN");// 被表白者的名字 count++; } else { printf("You Are My Best Lover.\n"); count++;

if (count >= 44) { count = 0; } } Sleep(36);//Sleep函数:执行挂起一段时间,也就是等待一段时间在继续执行 } }执行结果:

整理制作不易,大家记得点赞、关注+转发。感谢支持~3.Python简易爱心代码准备工作:下载matplotlib软件包

涉及知识点:np.linspace():用于返回指定区间等间隔的数组,例如np.linspace(0,2*np.pi)就是0到2π等间隔的数组np.sin():对中元素取正弦值np.cos():对中元素取余弦值

plt.plot():是matplotlib.pyplot模块下的一个函数, 用于画图,它可以绘制点和线plt.show():展示图像原理:原始的心形线的极坐标方程为r=a(1-cos)与其对应的参数方程是:

x()=2r(sin-(sin2)/2)y()= 2r(cos-(cos2)/2),(0<=<=2π)代码:import numpy as np import matplotlib.pyplot as plt t=np.linspace(

0,2*np.pi)#用于返回指定区间等间隔的数组 x=2*1*(np.cos(t)-np.cos(2*t)/2) y=2*1*(np.sin(t)-np.sin(2*t)/2) plt.plot(y,x,c=

purple)#c=控制颜色 plt.show()执行结果:

4.Python动态爱心代码这个也是最还原的代码:import random from math import sin, cos, pi, log from tkinter import \* CANVAS\_WIDTH =

640# 画布的宽 CANVAS\_HEIGHT = 480# 画布的高 CANVAS\_CENTER\_X = CANVAS\_WIDTH / 2# 画布中心的X轴坐标 CANVAS\_CENTER\_Y = CANVAS\_HEIGHT /

2# 画布中心的Y轴坐标 IMAGE\_ENLARGE = 11# 放大比例 HEART\_COLOR = "#ff2121"# 心的颜色,这个是中国红defheart\_function(t, shrink\_ratio: float = IMAGE\_ENLARGE)

:""" “爱心函数生成器” :param shrink_ratio: 放大比例 :param t: 参数 :return: 坐标 """# 基础函数 x =

16 \* (sin(t) \*\* 3) y = -(13 \* cos(t) - 5 \* cos(2 \* t) - 2 \* cos(3 \* t) - cos(4 \* t))

# 放大 x \*= shrink_ratio y \*= shrink_ratio # 移到画布中央 x += CANVAS\_CENTER\_X y += CANVAS\_CENTER\_Y

return int(x), int(y) defscatter\_inside(x, y, beta=0.15):""" 随机内部扩散 :param x: 原x :param y: 原y :param beta: 强度 :return: 新坐标 """

ratio_x = - beta \* log(random.random()) ratio_y = - beta \* log(random.random()) dx = ratio_x \* (x - CANVAS\_CENTER\_X) dy = ratio_y \* (y - CANVAS\_CENTER\_Y)

return x - dx, y - dy defshrink(x, y, ratio):""" 抖动 :param x: 原x :param y: 原y :param ratio: 比例 :return: 新坐标 """

force = -1 / (((x - CANVAS\_CENTER\_X) \*\* 2 + (y - CANVAS\_CENTER\_Y) \*\* 2) \*\* 0.6) # 这个参数...

dx = ratio \* force \* (x - CANVAS\_CENTER\_X) dy = ratio \* force \* (y - CANVAS\_CENTER\_Y)

return x - dx, y - dy defcurve(p):""" 自定义曲线函数,调整跳动周期 :param p: 参数 :return: 正弦 """# 可以尝试换其他的动态函数,达到更有力量的效果(贝塞尔?)

return2 \* (2 \* sin(4 \* p)) / (2 \* pi) classHeart:""" 爱心类 """def \_\_init\_\_(self, generate_frame=

20): self._points = set() # 原始爱心坐标集合 self._edge_diffusion_points = set() # 边缘扩散效果点坐标集合

self._center_diffusion_points = set() # 中心扩散效果点坐标集合 self.all_points = {} # 每帧动态点坐标

self.build(2000) self.random_halo = 1000 self.generate_frame = generate_frame

for frame in range(generate_frame): self.calc(frame) defbuild(self, number):# 爱心for _

in range(number): t = random.uniform(0, 2 \* pi) # 随机不到的地方造成爱心有缺口 x, y = heart\_function(t) self._points.add((x, y))

# 爱心内扩散for _x, _y in list(self._points): for _ in range(3): x, y = scatter\_inside(_x, _y,

0.05) self._edge_diffusion_points.add((x, y)) # 爱心内再次扩散 point_list = list(self._points)

for _ in range(4000): x, y = random.choice(point_list) x, y = scatter\_inside(x, y,

0.17) self._center_diffusion_points.add((x, y)) @staticmethoddefcalc\_position(x, y, ratio)

:# 调整缩放比例 force = 1 / (((x - CANVAS\_CENTER\_X) \*\* 2 + (y - CANVAS\_CENTER\_Y) \*\* 2) \*\*

0.520) # 魔法参数 dx = ratio \* force \* (x - CANVAS\_CENTER\_X) + random.randint(-1, 1) dy = ratio \* force \* (y - CANVAS\_CENTER\_Y) + random.randint(

-1, 1) return x - dx, y - dy defcalc(self, generate_frame): ratio = 10 \* curve(generate_frame /

10 \* pi) # 圆滑的周期的缩放比例 halo_radius = int(4 + 6 \* (1 + curve(generate_frame / 10 \* pi))) halo_number = int(

3000 + 4000 \* abs(curve(generate_frame / 10 \* pi) \*\* 2)) all_points = [] # 光环 heart_halo_point = set()

# 光环的点坐标集合for _ in range(halo_number): t = random.uniform(0, 2 \* pi) # 随机不到的地方造成爱心有缺口 x, y = heart\_function(t, shrink\_ratio=

11.6) # 魔法参数 x, y = shrink(x, y, halo_radius) if (x, y) notin heart\_halo\_point:

# 处理新的点 heart_halo_point.add((x, y)) x += random.randint(-14, 14) y += random.randint(

-14, 14) size = random.choice((1, 2, 2)) all_points.append((x, y, size))

# 轮廓for x, y in self._points: x, y = self.calc\_position(x, y, ratio) size = random.randint(

1, 3) all_points.append((x, y, size)) # 内容for x, y in self._edge_diffusion_points: x, y = self.calc\_position(x, y, ratio) size = random.randint(

1, 2) all_points.append((x, y, size)) for x, y in self._center_diffusion_points: x, y = self.calc\_position(x, y, ratio) size = random.randint(

1, 2) all_points.append((x, y, size)) self.all_points[generate_frame] = all_points

defrender(self, render_canvas, render_frame):for x, y, size in self.all_points[render_frame % self.generate_frame]: render_canvas.create\_rectangle(x, y, x + size, y + size, width=

0, fill=HEART\_COLOR) defdraw(main: Tk, render\_canvas: Canvas, render\_heart: Heart, render_frame=

0): render_canvas.delete(all) render_heart.render(render_canvas, render_frame) main.after(

160, draw, main, render_canvas, render_heart, render_frame + 1) if __name__ == \_\_main\_\_: root = Tk()

# 一个Tk canvas = Canvas(root, bg=black, height=CANVAS\_HEIGHT, width=CANVAS\_WIDTH) canvas.pack() heart = Heart()

# 心 draw(root, canvas, heart) # 开始画画~ root.mainloop() 执行结果:

5.Python爱心泡泡代码:import turtle import random import math # 初始化 turtle.setup(1280, 720) t = turtle.Pen() t.ht()

# 颜色 colors = [] t_list = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e",

"f"] for i in t\_list: t_str = "#ff00"for j in t\_list: colors.append(t_str+i+j) class

Love():def \_\_init\_\_(self): # 定义变量self.r = random.randint(4, 10) self.x = random.randint(-

900, 700) self.y = random.randint(-400, 400) self.i = random.randint(0, 10) self

.color = random.choice(colors) self.speed = random.randint(1, 8) defmove(self):

# 通过y坐标来控制爱心ifself.y <= 500: self.y += 2.5\*self.speed self.x = self.x + 1.5\*math.sin(

self.i)\*math.sqrt(self.i)\*self.speed self.i = self.i + 0.1else:self.y = -700self.r = random.randint(

5, 20) self.x = random.randint(-900, 700) self.i = 0self.color = random.choice(colors)

self.speed = random.randint(1, 8) defdraw(self): # 绘制爱心 t.pensize(self.r/2) t.penup() t.color(

self.color, self.color) t.goto(self.x, self.y) t.pendown() # 设置角度 t.setheading(

60) t.circle(self.r, 255) t.fd(2.4\*self.r) t.left(90) t.fd(2.4\*self

.r) t.circle(self.r, 255) love = [] for i in range(100): love.append(Love()) turtle.bgcolor(

"#000000") while1: turtle.tracer(0) t.clear() for i in range(80): love[i].move() love[i].draw() turtle.tracer(

1) 执行结果:

#编程##Python##程序员##干货分享##资料##IT##ai##计算机##互联网#

亲爱的读者们,感谢您花时间阅读本文。如果您对本文有任何疑问或建议,请随时联系我。我非常乐意与您交流。

发表评论:

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