小爱心换着玩

news2024/9/28 13:30:46

使用Python画出小人发射爱心的代码

from turtle import *
from time import sleep
 
def go_to(x, y):
  up()
  goto(x, y)
  down()
 
def head(x,y,r):
  go_to(x,y)
  speed(1)
  circle(r)
  leg(x,y)
 
def leg(x,y):
 
  right(90)
  forward(180)
  right(30)
  forward(100)
  left(120)
  go_to(x,y-180)
  forward(100)
  right(120)
  forward(100)
  left(120)
  hand(x,y)
 
 
def hand(x,y):
  go_to(x,y-60)
  forward(100)
  left(60)
  forward(100)
  go_to(x, y - 90)
  right(60)
  forward(100)
  right(60)
  forward(100)
  left(60)
  eye(x,y)
 
def eye(x,y):
  go_to(x-50,y+130)
  right(90)
  forward(50)
  go_to(x+40,y+130)
  forward(50)
  left(90)
 
 
def big_Circle(size):
  speed(20)
  for i in range(150):
    forward(size)
    right(0.3)
def line(size):
  speed(1)
  forward(51*size)
 
def small_Circle(size):
  speed(10)
  for i in range(210):
    forward(size)
    right(0.786)
 
 
 
def heart(x, y, size):
  go_to(x, y)
  left(150)
  begin_fill()
  line(size)
  big_Circle(size)
  small_Circle(size)
  left(120)
  small_Circle(size)
  big_Circle(size)
  line(size)
  end_fill()
 
def main():
  pensize(2)
  color('red', 'pink')
  head(-120, 100, 100)
  heart(250, -80, 1)
  go_to(200, -300)
  write("To: 智慧与美貌并存的", move=True, align="left", font=("楷体", 20, "normal"))
  done()
 
main()

基于Python绘制表白代码

绘制代码
实现本文效果的整体思路是:加载库—选择背景音乐—绘制心的外轮廓—填充心并写告白信—绘制心动线。

1.导入库

import os
import pygame
import turtle as t

本文应用到的库较少,只应用了os、pygame和turtle三个库。

os库可以设置文件读取的位置。

pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐,如果无需背景音乐,不用加载该库。

turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。
2.选择背景音乐
接着应用pygame库播放背景音乐,本文的音乐是《 爱你》。
#播放音乐
print(‘播放音乐’)
pygame.mixer.init()
pygame.mixer.music.load(r"E:\预备\520\王心凌-《爱你》.mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(1, 10)

这一部分的代码和整体代码是剥离的,可以选泽在最开始放上该代码,也可以直接删除。如果选择播放音乐,需要在代码music.load函数中把你想放音乐的地址填进去。
3.绘制心的外轮廓
然后绘制心的外轮廓,代码如下:
t.title(‘阿黎逸阳的代码公众号’)
t.speed(10)
#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
t.hideturtle()
print(‘画爱心’)
#画爱心
def heart(x, y):
t.penup()
t.goto(x, y)
t.pendown()
t.color(‘pink’)
t.setheading(50)
t.circle( -5, 180)
t.circle( -45, 12)
t.setheading(130)
t.circle( -45, 12)
t.circle( -5, 180)
heart(-30, 155)
heart(-220, 145)
heart(-210, 60)
heart(-100, 100)
heart(-20, 20)
heart(-70, 130)
heart(-140, -20)
heart(30, 100)
heart(-60, -20)
heart(10, 60)
heart(-100, -70)
heart(20, 145)
heart(-140, -20)
heart(-130, 130)
heart(-180, 20)
heart(-170, 155)
heart(-230, 100)

关键代码详解:

t.penup():抬起画笔,一般用于另起一个地方绘图使用。

t.goto(x,y):画笔去到某个位置,参数为(x,y),对应去到的横坐标和纵坐标。

t.pendown():放下画笔,一般和penup组合使用。

t.color(color):设置画笔的颜色。

t.setheading(θ):设置海龟头与横坐标偏离的度数。

t.circle(radius,extent,steps):radius指半径,若为正,半径在小乌龟左侧radius远的地方,若为负,半径在小乌龟右侧radius远的地方;extent指弧度;steps指阶数。画外轮廓的关键是:通过调节circle函数中的半径和弧度来调节曲线的弧度,从而使得小蜜蜂的轮廓比较流畅。

4.填充心并写告白信
接下来边填充心,边写告白信,代码如下:
def write_mes(x, y, size, ss):
t.hideturtle()
t.penup()
t.goto(x, y)
t.pendown()
t.pencolor(‘black’)
t.write(ss, font=(‘Times New Roman’, size, ‘normal’))
#画红心
print(‘画红心’)
def heart_fill(x, y):
t.penup()
t.goto(x, y)
t.pendown()
t.color(‘red’, ‘red’)
t.begin_fill()
t.setheading(50)
t.circle( -5, 180)
t.circle( -45, 12)
t.setheading(130)
t.circle( -45, 12)
t.circle( -5, 180)
t.end_fill()
x = 90
y = 110
#右边爱心
write_mes(x, y, 11, ‘想陪你不只一天’)
heart_fill(-100, 100)
heart_fill(-70, 130)
heart_fill(-30, 155)
heart_fill(20, 145)
heart_fill(30, 100)
write_mes(x, y-30, 11, ‘多一点让我心甘情愿 爱你’)
heart_fill(10, 60)
heart_fill(-20, 20)
heart_fill(-60, -20)
heart_fill(-100, -70)
#左边爱心
write_mes(x, y-302, 11, ‘就这样 一天多一点’)
heart_fill(-140, -20)
heart_fill(-180, 20)
heart_fill(-210, 60)
heart_fill(-230, 100)
write_mes(x, y-30
3, 11, ‘慢慢地累积感觉’)
heart_fill(-220, 145)
heart_fill(-170, 155)
heart_fill(-130, 130)
write_mes(x, y-30*4, 11, ‘两人的世界就能够贴近一点’)

5.画心动线
最后是写姓名并画心动线,代码如下:
t.speed(15)
print(‘画心动线’)
def heart_bit():
#画心动线
t.penup()
t.goto(-170, 40)
t.pendown()
t.pencolor(‘red’)
t.setheading(0)
t.pensize(2)
t.forward(10)
#第一个小波浪
t.setheading(45)
t.circle(50, 10)
t.setheading(0)
t.circle(-3,90)
t.circle(50, 5)
#横线
t.setheading(0)
t.forward(10)
#第一个下尖峰
t.setheading(-80)
t.forward(7)
t.setheading(70)
t.forward(25)
t.setheading(-85)
t.forward(29)
t.setheading(70)
t.forward(13)
t.setheading(0)
t.forward(15)

#画心
t.setheading(150)
t.circle(-20, 40)
t.circle(-10, 170)
t.setheading(70)
t.circle(-10, 170)
t.circle(-20, 40)
t.setheading(0)
t.forward(15)
#2
t.setheading(-80)
t.forward(7)
t.setheading(70)
t.forward(25)
t.setheading(-85)
t.forward(29)
t.setheading(70)
t.forward(13)
t.setheading(0)
t.forward(15)
t.setheading(0)
t.forward(10)
t.setheading(45)
t.circle(50, 10)
t.setheading(0)
t.circle(-3,90)
t.circle(50, 5)
t.setheading(0)
t.forward(10)

def write_name(x, y, size, ss):
t.hideturtle()
t.penup()
t.goto(x, y)
t.pendown()
t.pencolor(‘black’)
t.write(ss, font=(‘Times New Roman’, size, ‘normal’))
def undo_back():
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
def undo_back2():
t.undo()
t.undo()
def name_heart_bit():
#写两个人的姓名(需替换成真实姓名)
write_name(-180, 70, 11, ‘韩商言’)
write_name(-180, 70, 11, ‘韩商言’)
write_name(-180, 70, 11, ‘韩商言’)
heart_bit()
write_name(-60, 70, 11, ‘佟年’)
write_name(-60, 70, 11, ‘佟年’)
write_name(-60, 70, 11, ‘佟年’)
write_name(-60, 70, 11, ‘佟年’)
write_name(-60, 70, 11, ‘佟年’)
undo_back()
undo_back()
undo_back()
undo_back()
undo_back()
undo_back()
undo_back()
undo_back()
undo_back()
undo_back2()
while 1:
name_heart_bit()

绘制成品
在这里插入图片描述

利用turtle画爱心代码

思路:
第一步,考虑从“心尖”的位置开始,先向左上角画一条直线,与y轴的夹角设为45度:
在这里插入图片描述

第二步,心形的上方是一个弧形,可以设计成从原先的斜率开始,每前进1单位转1单位角度,画一个半圆:
在这里插入图片描述

第三步,右侧基本重复,但调整一下旋转角度,相应的各个参数的关系也很容易推:
在这里插入图片描述
那么代码如下:

import turtle
# set a window
wn = turtle.Screen()
wn.bgcolor("black")
wn.screensize(800, 600)
 
 
# set a pen
pen = turtle.Turtle()
pen.color("pink")
pen.shape("turtle")
 
#画笔形状设成了非常可爱的海龟hh
pen.fillcolor("pink")
turtle.speed(10)
 
 
# define functions
def curve():
   for i in range(180):
       pen.right(1)
       pen.forward(2)
def heart():
   pen.left(135)
   pen.forward(720/3.14)
   curve()
   pen.left(90)
   curve()
   pen.forward(720/3.14)
def text():
   pen.penup()
   pen.setpos(-50,50)
   pen.pendown()
   pen.color("white")
   pen.write(input("enter the words:"), font = ("Verdana", 30, "bold"))
 
 
# painting!
pen.begin_fill()
pen.penup()
pen.setpos(0,-150)
pen.pendown()
heart()
pen.end_fill()
text()
turtle.done()

效果:
在这里插入图片描述

another-python爱心代码

import turtle
import time


# 画爱心的顶部
def LittleHeart():
  for i in range(200):
    turtle.right(1)
    turtle.forward(2)


# 输入表白的语句,默认I Love you
love = input('Please enter a sentence of love, otherwise the default is "I Love you": ')
# 输入署名或者赠谁,没有不执行
me = input('Please enter pen name, otherwise the default do not execute: ')
if love == '':
  love = 'I Love you'
# 窗口大小
turtle.setup(width=900, height=500)
# 颜色
turtle.color('red', 'pink')
# 笔粗细
turtle.pensize(3)
# 速度
turtle.speed(1)
# 提笔
turtle.up()
# 隐藏笔
turtle.hideturtle()
# 去到的坐标,窗口中心为0,0
turtle.goto(0, -180)
turtle.showturtle()
# 画上线
turtle.down()
turtle.speed(1)
turtle.begin_fill()
turtle.left(140)
turtle.forward(224)
# 调用画爱心左边的顶部
LittleHeart()
# 调用画爱右边的顶部
turtle.left(120)
LittleHeart()
# 画下线
turtle.forward(224)
turtle.end_fill()
turtle.pensize(5)
turtle.up()
turtle.hideturtle()
# 在心中写字 一次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('#CD5C5C', 'pink')
# 在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
time.sleep(2)
# 在心中写字 二次
turtle.goto(0, 0)
turtle.showturtle()
turtle.color('red', 'pink')
turtle.write(love, font=('gungsuh', 30,), align="center")
turtle.up()
turtle.hideturtle()
# 写署名
if me != '':
  turtle.color('black', 'pink')
  time.sleep(2)
  turtle.goto(180, -180)
  turtle.showturtle()
  turtle.write(me, font=(20,), align="center", move=True)

# 点击窗口关闭
window = turtle.Screen()
window.exitonclick()

使用C描绘心形

#include <stdio.h>

void main()

{

  int i, j, k, l, m;  char c=3;

  for (i=1; i<=5; i++)      printf("\n");

  for (i=1; i<=3; i++)

  {

    for (j=1; j<=32-2*i; j++)  printf(" ");  

    for (k=1; k<=4*i+1; k++)  printf("%c", c);

    for (l=1; l<=13-4*i; l++)  printf(" ");

    for (m=1; m<=4*i+1; m++)  printf("%c", c);

    printf("\n");

  }

  for (i=1; i<=3; i++)

  {

    for (j=1; j<=24+1; j++)    printf(" ");  

    for (k=1; k<=29; k++)    printf("%c", c);

    printf("\n");

  }

  for (i=7; i>=1; i--)

  {

    for (j=1; j<=40-2*i; j++)  printf(" ");  

    for (k=1; k<=4*i-1; k++)  printf("%c", c);

    printf("\n");

  }

  for (i=1; i<=39; i++)      printf(" ");

  printf("%c\n", c);

  for (i=1; i<=4; i++)      printf("\n");

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2173644.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

大模型与智能体的市场调研分析

2024年&#xff0c;很多人都在谈论智能体&#xff0c;我老婆这样的美术老师&#xff0c;也让我给她科普一下&#xff0c;于是我花了几天时间&#xff0c;系统学习和深入调研了一下&#xff0c;在此分享给大家。 时代背景 人工智能就像电力一样&#xff0c;如果你的竞争对手正…

【二十七】【QT开发应用】VS如何复制项目,QT无边窗窗口Pro版本,信号与信号槽的应用,背景图片自适应控件大小

VS复制项目 在使用VS的过程中,有的时候我们需要复制我们已经存在的项目. 我们可以先创建一个新的项目. 接着把需要复制的项目的文件复制粘贴到新的项目文件夹中. 不要忘记添加现有项目. CFrameLessWidgetBase.h #pragma once #include <QWidget> class CFrameLessWi…

2025秋招内推--招联金融

【投递方式】 直接扫下方二维码&#xff0c;或点击内推官网https://wecruit.hotjob.cn/SU61025e262f9d247b98e0a2c2/mc/position/campus&#xff0c;使用内推码 igcefb 投递&#xff09; 【招聘岗位】 后台开发 前端开发 数据开发 数据运营 算法开发 技术运维 软件测试 产品策…

【AHK】打造炒股利器系列——用数组和循环来简化语音报时器

上一篇文章&#xff0c;【AHK】打造炒股利器系列——语音报时器 作为AHK入门&#xff0c;讲解了 注释、赋值、if语句、逻辑运算符、定时器等基本知识。本篇将引入Array和Loop语句来简化化这个语音报时器&#xff0c;让代码更优雅&#xff0c;代码越简单越不容易出错误&#xff…

JavaWeb——Vue组件库Element(2/6):常见组件:Table表格、Pagination分页(介绍,属性,事件)

目录 常见组件-表格 介绍 属性 常见组件-分页 介绍 属性 事件 了解完了 Element 的快速入门程序之后&#xff0c;接下来要了解 Element 当中所提供的一些常见组件。对于 Element 当中常见组件的学习非常简单&#xff0c;基本上就是 CtrlC 复制、CtrlV 粘贴的过程。学习…

在 CentOS 8 服务器上运行 Selenium 代码

1.安装依赖包 sudo dnf update -y sudo dnf install -y wget unzip2. 安装 Google Chrome wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm sudo dnf localinstall google-chrome-stable_current_x86_64.rpm -y3. 安装 ChromeDriver 3.1首…

【深度学习】05-Rnn循环神经网络-01- 自然语言处理概述/词嵌入层/循环网络/文本生成案例精讲

循环神经网络&#xff08;RNN&#xff09;主要用于自然语言处理的。 循环神经网络&#xff08;RNN&#xff09;、卷积神经网络&#xff08;CNN&#xff09;和全连接神经网络&#xff08;FCN&#xff09;是三种常见的神经网络类型&#xff0c;各自擅长处理不同类型的数据。下面…

转做大模型开发,能不能挽救职业生涯?

大模型算是当之无愧最火的一个方向了&#xff0c;算是新时代的风口。有小伙伴觉得&#xff0c;既然是新领域、新方向&#xff0c;那么&#xff0c;人才需求肯定比较大&#xff0c;相应的人才缺乏&#xff0c;竞争也会更少&#xff0c;那转行去做大模型是不是一个更好的选择呢&a…

Leetcode 2320. 统计放置房子的方式数

原题链接&#xff1a;. - 力扣&#xff08;LeetCode&#xff09; 一条街道上共有 n * 2 个 地块 &#xff0c;街道的两侧各有 n 个地块。每一边的地块都按从 1 到 n 编号。每个地块上都可以放置一所房子。 现要求街道同一侧不能存在两所房子相邻的情况&#xff0c;请你计算并…

高并发内存池(五):ThreadCache、CentralCache和PageCache的内存回收机制 及 释放内存过程的调试

目录 ThreadCache的内存回收机制 补充内容1 补充内容2 补充内容3 新增关键函数ListTooLong CentralCache的内存回收机制 补充内容1 新增关键函数MapObjectToSpan 新增关键函数ReleaseListToSpans PageCache的内存回收机制 补充内容1 补充内容2 新增关键函数Releas…

初试React前端框架

文章目录 一、React概述二、React核心特性1、组件化设计2、虚拟DOM3、生态系统 三、实例操作1、准备工作2、创建项目结构3、启动项目4、编写React组件5、添加React样式6、运行项目&#xff0c;查看效果 四、实战小结 一、React概述 大家好&#xff0c;今天我们将一起探索React…

c语言 memmove模拟和momcpy模拟的比较

1.memcpy&#xff08;两者引用的头文件均是<stdlib.h>) 这个函数适用于开辟了两个空间的字符串数组&#xff0c;无法进行自身与自身的拷贝。eg: char* my_memcpy(void* s1, void* s2,int count) {char* start s1;while (count--) {*(char*)s1 *(char*)s2;(char*)s1 …

windows10使用bat脚本安装前后端环境之nginx注册服务

首先需要搞清楚nginx本地是怎么安装配置的、然后在根据如下步骤编写bat脚本&#xff1a; 思路 1.下载nginx-1.26 zip压缩包安装包 2.调整conf配置 3.借助winsw将nginx应用注册为服务&#xff0c;winsw下载地址 然后重命名nginx_service.exe 4.配置nginx-service.xml 5.注册wi…

【运维资料】系统运维管理方案(Doc原件2024)

1 编制目的 2 系统运行维护 2.1 系统运维内容 2.2 日常运行维护方案 2.2.1 日常巡检 2.2.2 状态监控 2.2.3 系统优化 2.2.4 软件系统问题处理及升级 2.2.5 系统数据库管理维护 2.2.6 灾难恢复 2.3 应急运行维护方案 2.3.1 启动应急流程 2.3.2 成立应急小组 2.3.3 应急处理过程 …

【数据结构】栈和队列(有完整的模拟实现代码!)

1、栈 1.1 栈的概念及结构 栈&#xff1a;一种特殊的线性表&#xff0c;其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶&#xff0c;另一端称为栈底。栈中的数据元素遵守后进先出LIFO&#xff08;Last In First Out&#xff09;的原则。…

爬取元气手机壁纸简单案例(仅用于教学,禁止任何非法获利)

爬虫常用的库 爬虫&#xff08;Web Scraping&#xff09;是一种从网页上提取数据的技术。在 Python 中&#xff0c;有许多库可以帮助实现这一目标。以下是一些常用的爬虫库&#xff0c;以及对 BeautifulSoup 的详细介绍。 常用爬虫库 1.Requests ​ a.功能&#xff1a;用于发…

利用 Llama-3.1-Nemotron-51B 推进精度-效率前沿的发展

今天&#xff0c;英伟达™&#xff08;NVIDIA&#xff09;发布了一款独特的语言模型&#xff0c;该模型具有无与伦比的准确性和效率性能。Llama 3.1-Nemotron-51B 源自 Meta 的 Llama-3.1-70B&#xff0c;它采用了一种新颖的神经架构搜索&#xff08;NAS&#xff09;方法&#…

MySQL的安装(环境为CentOS云服务器)

卸载内置环境 我们初期使用root账号&#xff0c;后期再切换成普通账号 使用 ps axj | grep mysql 查看系统中是否有MySQL相关的进程 使用 systemctl stop mysqld 关停进程 使用 rpm -qa | grep mysql 查看MySQL相关的安装包 使用 rpm -qa | grep mysql | xargs yum -y remo…

试用Debian12.7和Ubuntu24.4小札

Debian GNU/Linux 12 (bookworm)和Ubuntu 24.04.1 LTS是现阶段&#xff08;2024年9月26日&#xff09;两个发行版的最新版本。Ubuntu Server版本默认就不带桌面&#xff08;ubuntu-24.04-live-server-amd64.iso&#xff09;&#xff0c;这个默认就是最小化安装&#xff08;安装…

长芯微LPQ76930锂电池组保护芯片完全P2P替代BQ76930

LPQ76930系列芯片可作为 3-15 节串联电池组监控和保护解决方案的一部分。通过 TWI 通信&#xff0c;MCU 可以使用 LPQ76930 来执行电池管理功能1&#xff0c;例如监测&#xff08;电池电压、电池 组电流、电池组温度&#xff09;、保护&#xff08;控制充电/放电 FET&#xff0…