python中动态爱心代码
时间: 2023-11-19 16:57:37 浏览: 134
python实现动态爱心源码.doc
以下是基于Python实现动态漂浮爱心的源码:
```python
import random as ra
import math
import os
import time
class Heart():
#每个爱心(爱心类)
def __init__(self):
self.r = ra.randint(10,15) #爱心的半径
self.x = ra.randint(-1000,1000) #爱心的横坐标
self.y = ra.randint(-1000,1000) #爱心的纵坐标
self.color = (ra.randint(100,255),ra.randint(100,255),ra.randint(100,255)) #爱心的颜色
def draw(self,canvas):
#绘制爱心
for i in range(360):
x = self.r*math.sin(math.radians(i))*math.sin(math.radians(i))*math.sin(math.radians(i))
y = self.r*math.cos(math.radians(i))*math.cos(math.radians(i))*math.cos(math.radians(i))
canvas.set_at((int(self.x+x),int(self.y-y)),self.color)
class HeartWall():
#爱心墙(爱心墙类)
def __init__(self):
self.hearts = []
self.num = 100 #爱心的数量
self.screen = None
def create(self):
#创建爱心
for i in range(self.num):
self.hearts.append(Heart())
def draw(self):
#绘制爱心
for heart in self.hearts:
heart.draw(self.screen)
def move(self):
#移动爱心
for heart in self.hearts:
heart.y += 1
if heart.y > 1000:
heart.y = -1000
def run(self):
#运行爱心墙
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (300,100) #设置窗口位置
os.environ['SDL_VIDEO_CENTERED'] = '0' #窗口居中
os.environ['SDL_AUDIODRIVER'] = 'dsp' #音频驱动
import pygame
pygame.init()
self.screen = pygame.display.set_mode((2000,1000))
pygame.display.set_caption("Heart Wall")
self.create()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
self.screen.fill((0,0,0))
self.draw()
self.move()
pygame.display.update()
time.sleep(0.01)
heart_wall = HeartWall()
heart_wall.run()
```
阅读全文