更新:我在pygame中使用了不同的声音功能来尝试进行故障排除。这是我的新计划。
#pygame mixer testing
import pygame
pygame.init()
sound = pygame.mixer.music.load("jan_amit.wav")
这是错误。
Traceback (most recent call last):
File "C:\Users\Will\Desktop\Programming\Python\Programs\shmup\snd\pygame mixer testing.py", line 5, in <module>
pygame.mixer.music.load("jan_amit.wav")
pygame.error: Unknown WAVE data format
如何更改wav数据格式?
(旧帖子)---我是初学者,我正在尝试学习如何使用pygame。但是,当我尝试使用pygame.mixer.Sound()
将声音加载到我的游戏中时,我收到此错误:
C:\Users\Will\Desktop\Programming\Python\Programs\shmup\snd\bullet_shoot.wav
Traceback (most recent call last):
File "C:\Users\Will\Desktop\Programming\Python\Programs\shmup\shmup.py", line 127, in <module>
shoot_sound = pygame.mixer.Sound(path.join(snd_dir,"bullet_shoot.wav"))
pygame.error: Unable to open file 'C:\\Users\\Will\\Desktop\\Programming\\Python\\Programs\\shmup\\snd\\bullet_shoot.wav'
[Finished in 6.5s]
看来这个pygame函数不知何故在每个其他反斜杠旁边插入一个额外的反斜杠。所以为了排除故障,我打印出了我正在输入功能的字符串。果然,单反斜杠。
C:\Users\Will\Desktop\Programming\Python\Programs\shmup\snd\bullet_shoot.wav
这是完整的代码,因为我不确定发生了什么。
#shmup
#music by Jan Amit
import pygame
import random
from os import path
img_dir = path.join(path.dirname(__file__),'img')
snd_dir = path.join(path.dirname(__file__),'snd')
WIDTH = 480
HEIGHT = 480
FPS = 60
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Shoot'em Up!")
clock = pygame.time.Clock()
font_name = pygame.font.match_font("arial")
def draw_text(surf,text,size,x,y):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(player_img,(50,38))
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.radius = 19
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 10
self.speedx = 0
def update(self):
speed = 8
self.speedx = 0
keystate = pygame.key.get_pressed()
if not keystate[pygame.K_LEFT] or not keystate[pygame.K_RIGHT]:
if keystate[pygame.K_LEFT]:
self.speedx = -speed
if keystate[pygame.K_RIGHT]:
self.speedx = speed
self.rect.x += self.speedx
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
shoot_sound.play()
class Mob(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image_orig = meteor_img
self.image_orig.set_colorkey(BLACK)
self.image = self.image_orig.copy()
self.rect = self.image.get_rect()
self.radius = int(self.rect.width * .85 / 2)
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100,-40)
self.speedy = random.randrange(1,8)
self.speedx = random.randrange(-3,3)
self.rot = 0
self.rot_speed = random.randrange(-8,8)
self.last_update = pygame.time.get_ticks()
def update(self):
self.rotate()
self.rect.y += self.speedy
self.rect.x += self.speedx
if self.rect.top > HEIGHT + 10 or self.rect.left < -20 or self.rect.right > WIDTH + 20:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100,-40)
self.speedy = random.randrange(1,8)
self.speedx = random.randrange(-3,3)
def rotate(self):
now = pygame.time.get_ticks()
if now - self.last_update > 50:
self.last_update = now
self.rot = (self.rot + self.rot_speed) % 360
new_image = pygame.transform.rotate(self.image_orig,self.rot)
old_center = self.rect.center
self.image = new_image
self.rect = self.image.get_rect()
self.rect.center = old_center
class Bullet(pygame.sprite.Sprite):
def __init__(self,x,y):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_img
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.speedy = -10
def update(self):
self.rect.y += self.speedy
if self.rect.bottom < 10:
self.kill()
#load all game graphics
background = pygame.image.load(path.join(img_dir,'space_field.png')).convert()
background_rect = background.get_rect()
player_img = pygame.image.load(path.join(img_dir,"playerShip1_orange.png")).convert()
meteor_img = pygame.image.load(path.join(img_dir,"meteorBrown_med1.png")).convert()
bullet_img = pygame.image.load(path.join(img_dir,"laserBlue05.png")).convert()
#load game sounds
print(path.join(snd_dir, 'bullet_shoot.wav'))
shoot_sound = pygame.mixer.Sound(path.join(snd_dir,"bullet_shoot.wav"))
all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
m = Mob()
all_sprites.add(m)
mobs.add(m)
score = 1
#game loop
GAME_RUNNING = True
while GAME_RUNNING == True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
GAME_RUNNING = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
all_sprites.update()
hits = pygame.sprite.groupcollide(mobs,bullets,True,True)
for hit in hits:
score += 1
m = Mob()
all_sprites.add(m)
mobs.add(m)
hits = pygame.sprite.spritecollide(player, mobs, False, pygame.sprite.collide_circle)
if hits:
GAME_RUNNING = False
#draw and render
screen.fill(BLACK)
screen.blit(background, background_rect)
all_sprites.draw(screen)
draw_text(screen,str(score), 18, WIDTH/2, 15)
pygame.display.flip()
pygame.quit()