先看下样子
环境:
import pygame
主要是用了pygame库,作为画板用,下面是完整代码
import pygame
pygame.init()
SIZE = (600,750)
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("心")
heart_list = []
#因为目前没找到for循环实现浮点型的方法,改用while。
y = 1.5
while(y > -1.5):
# x的值要重置,不然执行一轮while之后,x=1.5,下一轮就没有了
x = -1.5
while(x < 1.5):
z = x*x + y*y -1
f = z*z*z - x*x*y*y*y
if(f <= 0):
a = int (x*100)+150
b = int (y*100)
b = 0 - b + 150 #因为心是倒的,我加这行让他翻转一下
heart_list.append([a,b])
x += 0.05
y -= 0.1
done = True
while(done):
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = False
screen.fill((0, 0, 0))
for i in range(len(heart_list)):
#RGB三色,可以自己调
pygame.draw.circle(screen,(255,0,0),heart_list[i][:2],1)
pygame.display.flip()
pygame.quit()
写这个心形代码期间学到的知识:
1.心形函数:
2.开多次方根问题:
8**(1/3)= 2.0
64**(1/3)= 3.9999999999999…6
import numpy
#由于pyhon开立方根,会因为优先级的关系出现问题
#调用numpy库里的cbrt()方法