在 Pygame 上玩 MIDI [英] Playing midi on Pygame

查看:91
本文介绍了在 Pygame 上玩 MIDI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这就是我得到的:

Ok, this is what I got:

import pygame
import sys

from pygame.locals import *

bif="bg.jpg"
mif="pkmn.png"
sif="bubble.png"
song_1="testaudio.mid"

pygame.init()

FPS = 30  # FPS
FPSCLOCK = pygame.time.Clock()  # FPS

screen = pygame.display.set_mode ((600,375),0,32)

intro=pygame.mixer.Sound(song_1)
intro.play()

background = pygame.image.load(bif).convert()

pygame.mouse.set_visible(0)

char = pygame.image.load(mif).convert_alpha()
x = screen.get_width()/2 - char.get_width()/2
y = screen.get_height() - char.get_height()

bubble = pygame.image.load(sif).convert_alpha()
shoot_y = 0

move_speed = 15  # FPS

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    pressed = pygame.key.get_pressed()
    if pressed[K_LEFT]:
        x -= move_speed
    if pressed[K_RIGHT]:
        x += move_speed
    if pressed[K_UP]:
        y -= move_speed
    if pressed[K_DOWN]:
        y += move_speed

    if event.type==KEYDOWN:
        if event.key==K_SPACE:
            shoot_y = y
            shoot_x = x

    screen.blit(background,(0,0))

    if shoot_y > 0:
        screen.blit(bubble,(shoot_x, shoot_y))
        shoot_y -= 10


    screen.blit(char,(x,y))

    pygame.display.update()
    FPSCLOCK.tick(FPS)  # FPS

所以我创建了一些midi文件,但是我一直收到无法打开文件'testaudio.mid",我尝试了很多但我真的很困惑,我是pygame的新手但无法理解这个出来,我到处找,但仍然无法让它工作,即使在同一个站点,但仍然无法弄清楚.

So I got some midi files I created, however I keep getting "Unable to open file 'testaudio.mid", I tried quite a bit but I'm really confused, I'm new to pygame but can't figure this out, I've looked everywhere but still can't get it to work, even in this same site but still couldn't figure it out.

我真的希望有人可以通过修改我的代码或向我展示一些更清晰的例子来帮助我,因为我一直无法理解我找到的那个.

I really hope someone can help me by modifying my code or showing me the way to some clearer example cause I've been unable to understand the one I found.

谢谢(:

推荐答案

以下代码将播放当前目录下的music.mid

The following code will play music.mid located in the current directory

import pygame
pygame.init()

pygame.mixer.music.load("music.mid")
pygame.mixer.music.play()

这篇关于在 Pygame 上玩 MIDI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆