Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame bug,音乐无法正常播放,因为它只是循环开始部分_Python_Python 3.x_Pygame_Python 3.7 - Fatal编程技术网

Python Pygame bug,音乐无法正常播放,因为它只是循环开始部分

Python Pygame bug,音乐无法正常播放,因为它只是循环开始部分,python,python-3.x,pygame,python-3.7,Python,Python 3.x,Pygame,Python 3.7,我正在Pygame中制作一个基本的射击游戏(我正在学习教程)。我决定添加背景音乐来保持游戏的美观。 代码(相当长!): 导入pygame 从pygame导入图像作为精灵 pygame.init() dow=pygame.display.set_模式((1000700)) pygame.display.set_标题(“PY.Touhou”) clock=pygame.time.clock() rightSprites=[sprite.load('ness-right1.png'), pygame.

我正在Pygame中制作一个基本的射击游戏(我正在学习教程)。我决定添加背景音乐来保持游戏的美观。 代码(相当长!):

导入pygame
从pygame导入图像作为精灵
pygame.init()
dow=pygame.display.set_模式((1000700))
pygame.display.set_标题(“PY.Touhou”)
clock=pygame.time.clock()
rightSprites=[sprite.load('ness-right1.png'),
pygame.image.load('ness-right2.png'),
pygame.image.load('ness-right3.png'),
pygame.image.load('ness-right4.png'),
pygame.image.load('ness-right5.png'),
pygame.image.load('ness-right6.png'),
pygame.image.load('ness-right7.png'),
pygame.image.load('ness-right8.png')
]
左精灵=[
sprite.load('ness-left1.png'),
sprite.load('ness-left2.png'),
sprite.load('ness-left3.png'),
sprite.load('ness-left4.png'),
sprite.load('ness-left5.png'),
sprite.load('ness-left6.png'),
sprite.load('ness-left7.png'),
sprite.load('ness-left8.png')
]
scene=pygame.image.load('bg.png')
idle=pygame.image.load('ness-idle.png')
类播放器(对象):
定义初始值(自、x位置、y位置、w、h):
self.x_pos=x_pos
self.y_pos=y_pos
self.w=w
self.h=h
自速度=15
self.JUMPbool=False
self.atleft=False
self.atright=False
self.steps=0
自升高度=12
self.hitbox=(self.x_位置+50,self.y_位置,73227)
self.still=True
def移动(自我):
如果self.steps+1>=24:
self.steps=0
如果不是自我静止:
如果self.atleft:
dow.blit(leftSprites[self.steps//4],(self.x_pos,self.y_pos))
self.steps+=1
elif self.atright:
dow.blit(rightSprites[self.steps//4],(self.x_pos,self.y_pos))
self.steps+=1
其他:
如果self.atright:
道琼斯指数(rightSprites[1],(self.x_pos,self.y_pos))
其他:
道琼斯指数(leftSprites[1],(self.x_pos,self.y_pos))
self.hitbox=(self.x_位置+50,self.y_位置,73227)
#pygame.draw.rect(道指(0,0255),self.hitbox,2)
类别IceCullet(对象):
定义初始(自身、x位置、y位置、半径、颜色、方向):
self.x_pos=x_pos
self.y_pos=y_pos
自半径=半径
self.color=颜色
方向
自速度=8*方向
def召唤(自我):
pygame.draw.circle(道指,自彩色,(自x位置,自y位置),自半径)
类EnemyCirno(对象):
erightSprites=[sprite.load('e-right%s.png'%pic'),用于范围(1,9)内的pic)
eleftSprites=[sprite.load('e-left%s.png'%pic'),用于范围(1,9)内的pic)
定义初始(self,x_pos,y_pos,w,h,end):
self.x_pos=x_pos
self.y_pos=y_pos
self.w=w
self.h=h
self.end=结束
self.path=[self.x_pos,self.end]
self.hitbox=(self.x_位置+50,self.y_位置,73227)
self.steps=0
自我健康=20
self.isAlive=True
自速度=4
def召唤(自我):
self.move()
如果self.isAlive:
如果self.steps+1>=24:
self.steps=0
如果self.velocity>0:
dow.blit(self.erightSprites[self.steps//4],(self.x_pos,self.y_pos))
self.steps+=1
其他:
dow.blit(self.eleftSprites[self.steps//4],(self.x_pos,self.y_pos))
self.steps+=1
pygame.draw.rect(道指(255,10,0)、(100110400,20))
pygame.draw.rect(道琼斯指数,(5255510),(100110200-(20*(10-自我健康)),20))
敌方生命=斜体样式。渲染('敌方生命',1,(255,0,0))
道琼斯工业平均指数(敌方健康指数(100150))
self.hitbox=(self.x_位置+50,self.y_位置,73227)
#pygame.draw.rect(道指,(0,0,0),self.hitbox,2)
def移动(自我):
如果self.velocity>0:
如果self.x_pos+self.velocityself.path[0]:
self.x_pos+=自速度
其他:
自速度=自速度*-1
self.steps=0
def命中(自身):
如果self.health>1:
自我健康-=0.50
hit=pygame.mixer.Sound('hit.wav'))
hit.play()
其他:
self.isAlive=False
dead=pygame.mixer.Sound('death\u Sound.wav'))
死了,玩
#主起动回路。
letterStyle=pygame.font.SysFont('Bookman Old Style',50)
italicStyle=pygame.font.SysFont('Bookman Old Style',30,False,True)
cirno=玩家(300470160233)
分数=0
埃维西诺=Enemycrino(50470160233800)
maxShots=0
项目符号=[]
运行=真
如果运行是正确的:
时钟滴答作响(24)
pygame。时间。延迟(10)
music=pygame.mixer.music.load('main_theme.wav')#这就是bug开始的地方!
pygame.mixer.music.play()
对于pygame.event.get()中的事件:
如果events.type==pygame.QUIT:
运行=错误
key=pygame.key.get_pressed()
def更新_屏幕():
道指布利特(场景,(0,0))
textdisplay=letterStyle.render('Score:'+str(Score),1,(0,255,255))
won=italicStyle.render(“敌人被打败了!”,1,(1025519))
道琼斯指数(文本显示,(50,30))
如果没有,请输入No.isAlive:
道-布利特指数(韩元(100150))
cirno.move()
不,传唤
对于子弹射击:
射击。召唤()
pygame.display.update()
如果maxShots>0:
maxShots+=1
import pygame
from pygame import image as sprite

pygame.init()

dow = pygame.display.set_mode((1000, 700))

pygame.display.set_caption("PY.Touhou")

clock = pygame.time.Clock()
rightSprites = [sprite.load('ness-right1.png'),
                pygame.image.load('ness-right2.png'),
                pygame.image.load('ness-right3.png'),
                pygame.image.load('ness-right4.png'),
                pygame.image.load('ness-right5.png'),
                pygame.image.load('ness-right6.png'),
                pygame.image.load('ness-right7.png'),
                pygame.image.load('ness-right8.png')
                ]
leftSprites = [
    sprite.load('ness-left1.png'),
    sprite.load('ness-left2.png'),
    sprite.load('ness-left3.png'),
    sprite.load('ness-left4.png'),
    sprite.load('ness-left5.png'),
    sprite.load('ness-left6.png'),
    sprite.load('ness-left7.png'),
    sprite.load('ness-left8.png')
]

scene = pygame.image.load('bg.png')
idle = pygame.image.load('ness-idle.png')


class Player(object):
    def __init__(self, x_pos, y_pos, w, h):
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.w = w
        self.h = h
        self.velocity = 15
        self.JUMPbool = False
        self.atleft = False
        self.atright = False
        self.steps = 0
        self.jumpheight = 12
        self.hitbox = (self.x_pos + 50, self.y_pos, 73, 227)
        self.still = True

    def move(self):
        if self.steps + 1 >= 24:
            self.steps = 0

        if not self.still:

            if self.atleft:
                dow.blit(leftSprites[self.steps // 4], (self.x_pos, self.y_pos))
                self.steps += 1

            elif self.atright:
                dow.blit(rightSprites[self.steps // 4], (self.x_pos, self.y_pos))
                self.steps += 1

        else:
            if self.atright:
                dow.blit(rightSprites[1], (self.x_pos, self.y_pos))
            else:
                dow.blit(leftSprites[1], (self.x_pos, self.y_pos))
        self.hitbox = (self.x_pos + 50, self.y_pos, 73, 227)
        # pygame.draw.rect(dow, (0, 0, 255), self.hitbox, 2)


class IceBullet(object):
    def __init__(self, x_pos, y_pos, radius, color, direction):
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.radius = radius
        self.color = color
        self.direction = direction
        self.velocity = 8 * direction

    def summon(self):
        pygame.draw.circle(dow, self.color, (self.x_pos, self.y_pos), self.radius)


class EnemyCirno(object):
    erightSprites = [sprite.load('e-right%s.png' % pic) for pic in range(1, 9)]
    eleftSprites = [sprite.load('e-left%s.png' % pic) for pic in range(1, 9)]

    def __init__(self, x_pos, y_pos, w, h, ending):
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.w = w
        self.h = h
        self.ending = ending
        self.path = [self.x_pos, self.ending]
        self.hitbox = (self.x_pos + 50, self.y_pos, 73, 227)
        self.steps = 0
        self.health = 20
        self.isAlive = True
        self.velocity = 4

    def summon(self):
        self.move()
        if self.isAlive:
            if self.steps + 1 >= 24:
                self.steps = 0
            if self.velocity > 0:
                dow.blit(self.erightSprites[self.steps // 4], (self.x_pos, self.y_pos))
                self.steps += 1
            else:
                dow.blit(self.eleftSprites[self.steps // 4], (self.x_pos, self.y_pos))
                self.steps += 1
            pygame.draw.rect(dow, (255, 10, 0), (100, 110, 400, 20))
            pygame.draw.rect(dow, (5, 255, 10), (100, 110, 200 - (20 * (10 - self.health)), 20))
            enemy_health = italicStyle.render('Enemy Health', 1, (255, 0, 0))
            dow.blit(enemy_health, (100, 150))
            self.hitbox = (self.x_pos + 50, self.y_pos, 73, 227)
        # pygame.draw.rect(dow, (0, 0, 0), self.hitbox, 2)

    def move(self):
        if self.velocity > 0:
            if self.x_pos + self.velocity < self.path[1]:
                self.x_pos += self.velocity
            else:
                self.velocity = self.velocity * -1
                self.steps = 0
        else:
            if self.x_pos - self.velocity > self.path[0]:
                self.x_pos += self.velocity
            else:
                self.velocity = self.velocity * -1
                self.steps = 0

    def hit(self):
        if self.health > 1:
            self.health -= 0.50
            hit = pygame.mixer.Sound('hit.wav')
            hit.play()
        else:
            self.isAlive = False
            dead = pygame.mixer.Sound('death_sound.wav')
            dead.play()


# Main starting loop.
letterStyle = pygame.font.SysFont('Bookman Old Style', 50)
italicStyle = pygame.font.SysFont('Bookman Old Style', 30, False, True)
cirno = Player(300, 470, 160, 233)
score = 0
evilCirno = EnemyCirno(50, 470, 160, 233, 800)
maxShots = 0
bullets = []
running = True
while running is True:
    clock.tick(24)
    pygame.time.delay(10)
    music = pygame.mixer.music.load('main_theme.wav') # And here is where the bug begins!
    pygame.mixer.music.play()
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            running = False
    key = pygame.key.get_pressed()


    def update_screen():
        dow.blit(scene, (0, 0))
        textdisplay = letterStyle.render('Score: ' + str(score), 1, (0, 255, 255))
        won = italicStyle.render("Enemy defeated!", 1, (10, 255, 19))
        dow.blit(textdisplay, (50, 30))
        if not evilCirno.isAlive:
            dow.blit(won, (100, 150))
        cirno.move()
        evilCirno.summon()
        for shot in bullets:
            shot.summon()

        pygame.display.update()


    if maxShots > 0:
        maxShots += 1
    if maxShots > 5:
        maxShots = 0
    for shot in bullets:
        if evilCirno.isAlive:
            if shot.y_pos - shot.radius < evilCirno.hitbox[1] + evilCirno.hitbox[3] and shot.y_pos + shot.radius > \
                    evilCirno.hitbox[1]:
                if shot.x_pos + shot.radius > evilCirno.hitbox[0] and shot.x_pos - shot.radius < evilCirno.hitbox[0] + \
                        evilCirno.hitbox[2]:
                    evilCirno.hit()
                    score += 50
                    bullets.pop(bullets.index(shot))

        if 1000 > shot.x_pos > 0:
            shot.x_pos += shot.velocity
        else:
            bullets.pop(bullets.index(shot))

    if key[pygame.K_z] and maxShots == 0:
        shoot = pygame.mixer.Sound('bullet.wav')
        shoot.play()
        if cirno.atleft:
            facing = -1
        else:
            facing = 1
        if len(bullets) < 5:
            bullets.append(
                IceBullet(round(cirno.x_pos + cirno.w // 2), round(cirno.y_pos + cirno.h // 1.2), 20, (0, 0, 255),
                          facing))

        maxShots = 1

    if key[pygame.K_LEFT] and cirno.x_pos > cirno.velocity:  # Will stop ASAP when reached
        cirno.x_pos -= cirno.velocity
        cirno.atleft = True
        cirno.atright = False
        cirno.still = False

    elif key[
        pygame.K_RIGHT] and cirno.x_pos < 1000 - cirno.velocity - cirno.w:  # If it goes past this, it will stop ASAP, as well
        cirno.x_pos += cirno.velocity
        cirno.atright = True
        cirno.atleft = False
        cirno.still = False
    else:
        cirno.steps = 0
        cirno.still = True
    if not cirno.JUMPbool:
        if key[pygame.K_UP]:
            cirno.JUMPbool = True
            cirno.steps = 0

    else:
        if cirno.jumpheight >= -12:  # What to do if pygame.K_SPACE is pressed down.

            cirno.y_pos -= (cirno.jumpheight * abs(cirno.jumpheight)) * 0.5
            cirno.jumpheight -= 1
        else:
            cirno.jumpheight = 12
            cirno.JUMPbool = False

    update_screen()

pygame.quit()
while running is True:
    [...]
    music = pygame.mixer.music.load('main_theme.wav')
    pygame.mixer.music.play()
music = pygame.mixer.music.load('main_theme.wav')
pygame.mixer.music.play()

while running is True:
    [...]