以下是一段简单的程式舞曲源码:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 设置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
# 载入音乐
music = pygame.mixer.music.load("music.mp3")
pygame.mixer.music.play(-1)
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 定义舞蹈员
class Dancer:
def __init__(self, color):
self.color = color
self.x = random.randint(0, width)
self.y = random.randint(0, height)
self.speed_x = random.randint(-2, 2)
self.speed_y = random.randint(-2, 2)
self.size = random.randint(10, 30)
def move(self):
# 移动舞蹈员
self.x += self.speed_x
self.y += self.speed_y
# 碰到边界反向移动
if self.x < 0 or self.x > width:
self.speed_x *= -1
if self.y < 0 or self.y > height:
self.speed_y *= -1
def draw(self):
# 绘制舞蹈员
pygame.draw.circle(screen, self.color, (self.x, self.y), self.size)
# 创建舞蹈员列表
dancers = []
for i in range(20):
color = random.choice([BLUE, GREEN, RED])
dancer = Dancer(color)
dancers.append(dancer)
# 游戏循环
done = False
clock = pygame.time.Clock()
while not done:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# 移动舞蹈员
for dancer in dancers:
dancer.move()
# 绘制背景和舞蹈员
screen.fill(BLACK)
for dancer in dancers:
dancer.draw()
# 更新画面
pygame.display.flip()
# 控制帧率
clock.tick(60)
# 退出pygame
pygame.quit()
```
这段代码使用了pygame库,实现了一个简单的舞曲效果。舞蹈员是随机生成的,每个舞蹈员都有不同的颜色、大小和速度,碰到窗口边界会反向移动。整个舞曲的背景是黑色的,舞蹈员是随机分布在窗口中央的。播放了一首音乐作为背景音乐,不断循环播放。