目前只是在尝试 pygame,我创建了一个白色背景的窗口,只有一个图像。我希望能够使用箭头键移动图像(工作正常)以及按下箭头键时,我想要播放引擎声音 mp3。这是我目前得到的代码:
image_to_move = "dodge.jpg"
import pygame
from pygame.locals import *
pygame.init()
pygame.display.set_caption("Drive the car")
screen = pygame.display.set_mode((800, 800), 0, 32)
background = pygame.image.load(image_to_move).convert()
pygame.init()
sound = pygame.mixer.music.load("dodgeSound.mp3")
x, y = 0, 0
move_x, move_y = 0, 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
break
#Changes the moving variables only when the key is being pressed
if event.type == KEYDOWN:
pygame.mixer.music.play()
if event.key == K_LEFT:
move_x = -2
if event.key == K_RIGHT:
move_x = 2
if event.key == K_DOWN:
move_y = 2
if event.key == K_UP:
move_y = -2
#Stops moving the image once the key isn't being pressed
elif event.type == KEYUP:
pygame.mixer.music.stop()
if event.key == K_LEFT:
move_x = 0
if event.key == K_RIGHT:
move_x = 0
if event.key == K_DOWN:
move_y = 0
if event.key == K_UP:
move_y = 0
x+= move_x
y+= move_y
screen.fill((255, 255, 255))
screen.blit(background, (x, y))
pygame.display.update()
图像可以正常加载,我可以在屏幕上移动,但是根本没有声音