我的目的是为之前制作的苹果砸牛顿游戏配乐
1.用Suno制作一首关于苹果的歌曲
歌词最后两句“我要追寻自己的梦想,像成熟的苹果一样自由飞翔”竟然还进行了升华。
2.在pygame中播放该歌曲
import pygame as pg
import random
from pygame import mixer
import time
import os
pg.init()
os.environ['SDL_AUDIODRIVER'] = 'directsound'
mixer.init()
mixer.music.load(r"C:\Users\zhour\Downloads\苹果掉落.mp3")# 使用原始字符串,确保反斜杠不被解释为转义字符
mixer.music.play()
screen = pg.display.set_mode((400, 400))
image = pg.image.load("C:\\Users\\zhour\\Pictures\\niuDun.png") #载入图片
imageApple = pg.image.load("C:\\Users\\zhour\\Pictures\\apple.png") #载入图片
imageApple2 = pg.image.load("C:\\Users\\zhour\\Pictures\\apple.png") #载入图片
imageApple3 = pg.image.load("C:\\Users\\zhour\\Pictures\\apple.png") #载入图片
image_rect_apple = imageApple.get_rect() #默认位于左上角(topleft=(0, 0))
image_rect_apple2 = imageApple2.get_rect() #默认位于左上角(topleft=(0, 0))
image_rect_apple3 = imageApple3.get_rect() #默认位于左上角(topleft=(0, 0))
image_rect_apple.x= random.randint(0, 350)
image_rect_apple2.x= random.randint(0, 350)
image_rect_apple3.x= random.randint(0, 350)
image_rect = image.get_rect() #默认位于左上角(topleft=(0, 0))
image_rect.y=350
image_rect.x=200
clock = pg.time.Clock() #Clock对象可以控制FP
while True:
screen.fill((0, 0, 0)) #填充为黑色
screen.blit(image, image_rect)
screen.blit(imageApple, image_rect_apple)
screen.blit(imageApple2, image_rect_apple2)
screen.blit(imageApple3, image_rect_apple3)
image_rect_apple.y += 1
image_rect_apple2.y += 1
image_rect_apple3.y += 1
if image_rect.colliderect(image_rect_apple):#碰撞检测
print('you lose')
break
if image_rect.colliderect(image_rect_apple2):#碰撞检测
print('you lose')
break
if image_rect.colliderect(image_rect_apple3):#碰撞检测
print('you lose')
break
if image_rect_apple.y>400:
image_rect_apple.x= random.randint(0, 350)
image_rect_apple.y=0
if image_rect_apple2.y>400:
image_rect_apple2.x= random.randint(0, 350)
image_rect_apple2.y=0
if image_rect_apple3.y>400:
image_rect_apple3.x= random.randint(0, 350)
image_rect_apple3.y=0
# 获取所有按键的状态
keys_pressed = pg.key.get_pressed()
# 检测特定按键是否被持续按下
if keys_pressed[pg.K_LEFT]:
image_rect.x -= 3
if keys_pressed[pg.K_RIGHT]:
image_rect.x += 3
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
#防止牛顿走出界面外
if image_rect.x<0:
image_rect.x=0
if image_rect.x>370:
image_rect.x=370
clock.tick(60) #设置最高FPS为60
pg.display.flip()
用Suno制作游戏歌曲能解决当前游戏歌曲单一的问题。举一个现实的例子,有的游戏比如某杀,其背景音乐只有七八首,我都听腻了。有了ai生成音乐,就可以不停地更新游戏音乐,让玩家永远听不腻。