[英]How do I add background music to my python game
I have been trying different methods of adding background music to my game, but none of them works the way I want it to. 我一直在尝试将不同的背景音乐添加到我的游戏中的方法,但是没有一种方法能达到我想要的效果。 I the music to play throughout the gameplay.
我会在整个游戏过程中播放音乐。
Here is the code: 这是代码:
#!/usr/bin/env python
import pygame
from pygame.locals import * # noqa
import sys
import random
class FlappyBird:
def __init__(self):
self.screen = pygame.display.set_mode((400, 708))
self.bird = pygame.Rect(65, 50, 50, 50)
self.background = pygame.image.load("assets/background1.png").convert()
self.birdSprites =
[pygame.image.load("assets/superman.png").convert_alpha(),
pygame.image.load("assets/superman.png").convert_alpha(),
pygame.image.load("assets/supermangrayed.png")]
self.wallUp = pygame.image.load("assets/s_purple.png").convert_alpha() #
Top of the screen
self.wallDown = pygame.image.load("assets/cloud.png").convert_alpha() #
Bottom of the screen
self.gap = 25 # Gap between the clouds and the building
self.wallx = 400
self.birdY = 25
self.jump = 1
self.jumpSpeed = 10
self.gravity = 10
self.dead = False
self.sprite = 0
self.counter = 0
self.offset = random.randint(-110, 110)
# I haven't used this variable yet
self.sound = path.join(path.dirname(__file__), 'assets')
def updateWalls(self):
self.wallx -= 2
if self.wallx < -80:
self.wallx = 400
self.counter += 1
self.offset = random.randint(-50, 50)
def birdUpdate(self):
if self.jump:
self.jumpSpeed -= 0.5
self.birdY -= self.jumpSpeed
self.jump -= 1
else:
self.birdY += self.gravity
self.gravity += 0.1
self.bird[1] = self.birdY
upRect = pygame.Rect(self.wallx,
360 + self.gap - self.offset + 10,
self.wallUp.get_width() - 10,
self.wallUp.get_height())
downRect = pygame.Rect(self.wallx,
0 - self.gap - self.offset - 5,
self.wallDown.get_width() - 10,
self.wallDown.get_height())
if upRect.colliderect(self.bird):
self.dead = True
if downRect.colliderect(self.bird):
self.dead = True
if not 0 < self.bird[1] < 720:
self.bird[1] = 50
self.birdY = 50
self.dead = False
self.counter = 0
self.wallx = 400
self.offset = random.randint(-110, 110)
self.gravity = 3
def run(self):
clock = pygame.time.Clock()
pygame.font.init()
font = pygame.font.SysFont("calibri", 50)
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if (event.type == pygame.KEYDOWN or event.type ==
pygame.MOUSEBUTTONDOWN) and not self.dead:
self.jump = 10
self.gravity = 4.5
self.jumpSpeed = 10.5
self.screen.fill((255, 255, 255))
self.screen.blit(self.background, (0, 0))
self.screen.blit(self.wallUp,
(self.wallx, 300 + self.gap - self.offset))
self.screen.blit(self.wallDown,
(self.wallx, 1 - self.gap - self.offset))
self.screen.blit(font.render(str(self.counter),
-1,
(255, 255, 255)),
(200, 50))
if self.dead:
self.sprite = 2
elif self.jump:
self.sprite = 1
self.screen.blit(self.birdSprites[self.sprite], (50, self.birdY))
if not self.dead:
self.sprite = 0
self.updateWalls()
self.birdUpdate()
pygame.display.update()
if __name__ == "__main__":
FlappyBird().run()
Thank you in advance 先感谢您
Please try pygame.mixer 请尝试pygame.mixer
pygame.mixer.music.load("music.mp3")
pygame.mixer.music.play(-1,0.0)
Rule of thumb: Check documentation before Stack Overflow. 经验法则:在堆栈溢出之前检查文档。
https://www.pygame.org/docs/ref/mixer.html https://www.pygame.org/docs/ref/mixer.html
Use pygame.mixer.init()
to initialize the mixer. 使用
pygame.mixer.init()
初始化混音器。
Use pygame.mixer.music.load("file")
to load music into the mixer. 使用
pygame.mixer.music.load("file")
将音乐加载到混音器中。
Use pygame.mixer.music.play(loops, start)
to play the music loaded into the mixer. 使用
pygame.mixer.music.play(loops, start)
播放加载到混音器中的音乐。
ETC. 等等。
Just read the documentation, that's what it is there for! 只需阅读文档,这就是它的用途!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.