코드 그리기
이 기사의 효과를 실현하기 위한 전반적인 아이디어는 라이브러리 로드-배경 음악 선택-심장의 외부 윤곽 그리기-심장을 채우고 고백 편지를 쓰기-심쿵 라인을 그리는 것입니다
1.도입고
이 기사의 효과를 얻으려면 먼저 파이썬에 이 기사를 로드해야 하는 라이브러리를 로드해야 합니다.만약 당신이 아직 설치하지 않은 라이브러리가 있어서 실행 코드가 잘못되면 아나콘다 프롬프트에서 pip 방법으로 설치할 수 있습니다
import os
import pygame
import turtle as t
이 논문은 os, pygame 및 turtle의 세 가지 라이브러리만 사용하여 더 적은 라이브러리를 적용합니다.
os 라이브러리는 파일을 읽을 위치를 설정할 수 있습니다.
pygame 라이브러리는 그리기 과정을 더 재미있게 하기 위해 그리기 과정에 배경음악을 추가했으며 배경음악이 필요하지 않으면 라이브러리를 로드할 필요가 없습니다.
터틀 라이브러리는 그림 라이브러리로, 캔버스에 수학적 논리 제어 코드로 그림을 완성할 수 있는 브러시를 주는 것과 같습니다.
2.배경음악 선택
이어 파이게임 라이브러리를 활용해 배경음악을 재생했는데, 본문의 음악은 '순간의 영원'이다
#음악을 틀다
print('음악을 틀다')
pygame.mixer.init()
pygame.mixer.music.load(r"F:
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-30*2, 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)
#제1하첨봉
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()
'개발 꿀팁 > PYTHON' 카테고리의 다른 글
파이썬, 햄스터 그리기 (1) | 2022.11.21 |
---|---|
Python으로 피카츄 그리기 (10) | 2022.11.21 |
520 전용 파이썬 코드 (파이썬이 520을 만나다) (0) | 2022.11.21 |
Python불꽃놀이코드 (0) | 2022.11.18 |
Pycharm 사용법 (0) | 2022.11.18 |