在pygame中播放Suno制作的音乐

我的目的是为之前制作的苹果砸牛顿游戏配乐
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生成音乐,就可以不停地更新游戏音乐,让玩家永远听不腻。

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值