好的,这就是我得到的:
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
Run Code Online (Sandbox Code Playgroud)
所以我得到了一些我创建的midi文件,但是我一直得到"无法打开文件'testaudio.mid",我尝试了很多但是我真的很困惑,我是pygame的新手,但是无法解决这个问题,我到处寻找,但仍然无法让它工作,即使在同一个网站,但仍然无法弄明白.
我真的希望有人可以通过修改我的代码或向我展示一些更清晰的例子来帮助我,因为我无法理解我找到的那个.
谢谢 (:
以下代码将播放位于当前目录中的music.mid
import pygame
pygame.init()
pygame.mixer.music.load("music.mid")
pygame.mixer.music.play()
Run Code Online (Sandbox Code Playgroud)
如果您有一个简单的程序在播放指令后退出,音乐播放将停止。
您应该添加以下两行:
while pygame.mixer.music.get_busy():
pygame.time.wait(1000)
Run Code Online (Sandbox Code Playgroud)
以防止程序在音乐播放完毕之前退出。