静音和取消静音功能

时间:2021-02-15 23:33:41

标签: python python-3.x pygame

我的主屏幕上有静音和取消静音按钮,我遇到的问题是,当我按下静音按钮时,它变成了取消静音按钮,然后当我玩游戏死了,我又回到主屏幕,它将它变回静音按钮。我想让它这样,如果我将我的游戏静音并启动它并死了,它仍然是一个静音按钮,它不会恢复为取消静音按钮。我也尝试过使用 TrueFalse 语句。 https://gyazo.com/6b3d63d7615b07f2b4984d01ae239db5

我的完整代码

import pygame,random
pygame.init()

width = 500
height = 600
# Screen width and height
window = pygame.display.set_mode((width,height))
# Name of the Screen
pygame.display.set_caption("Game")
# For off and on music
music_on = pygame.image.load("img/music_on.png")
music_on = pygame.transform.scale(music_on,(music_on.get_width()//2, music_on.get_height()//2))
music_off = pygame.image.load("img/music_off.png")
music_off = pygame.transform.scale(music_off,(music_off.get_width()//2, music_off.get_height()//2))

# Start screen
def text_objects(font,text):
    textSurface = font.render(text,True,(0,0,0))
    return textSurface, textSurface.get_rect()

def button(msg,x,y,w,h,ac,ic,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(window,ac,(x,y,w,h))

    if click[0] == 1 and action != None:
        action()

    else:
        pygame.draw.rect(window,ic,(x,y,w,h))

def quitgame():
    pygame.quit()

def game2():
    
    # part of making background shift

    # player class
    class Player:
        def __init__(self,x,y,width,height,color):
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.color = color
            self.rect = pygame.Rect(x,y,width,height)
        def get_rect(self):
            self.rect.topleft = (self.x,self.y)
            return self.rect
        def draw(self):
            self.rect.topleft = (self.x,self.y)
            pygame.draw.rect(window,self.color,self.rect)


    # Button class
    class button11:
        def __init__(self,color,x,y,width,height,text=''):
            self.color = color
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.text = text
            self.over = False
        def draw(self,window, outline=None):
            # Call this method to draw button on screen
            if outline:
                pygame.draw.rect(window,outline(self.x-2,self.y-2,self.width+4,self.height+4),0)

            pygame.draw.rect(window,self.color,(self.x,self.y,self.width,self.height),0)

            if self.text != '':
                font = pygame.font.SysFont("comicsans",60)
                text = font.render(self.text,1,(0,0,0))
                window.blit(text, (self.x + (self.width/2 - text.get_width()/2), self.y+ (self.height/2 - text.get_height()/2)))

        def isOver(self,pos):
            # Pos is mouse position or tuple of (x,y) coordinates
            if pos[0] > self.x and pos[0] < self.x + self.width:
                if pos[1] > self.y and pos[1] < self.y + self.height:
                    return True
            
                return False

        def playSoundIfMouseIsOver(self,pos,sound):
            if self.isOver(pos):
                if not self.over:
                    Hover.play()
                    self.over = True
            else:
                self.over = False


    class Sweep:
        def __init__(self,x,y,width,height,color):
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.color = color
            self.speed = 2
            self.rect = pygame.Rect(x,y,width,height)
        def draw(self):
            self.rect.topleft = (self.x,self.y)
            pygame.draw.rect(window,self.color,self.rect)
            

    # Color
    white = (255,255,255)

    Black = (0,0,0)

    # Calss's cords,size, and color
    playerman = Player(230,150,40,40,white)

    right = Sweep(-600,0,600,800,Black)
    right2 = Sweep(530,0,600,800,Black)
    left1 = Sweep(-550,0,800,800,Black)
    left2 = Sweep(250,0,800,800,Black)

    sweeps = [right,right2,left1,left2]


    font = pygame.font.Font("img/Bubble.ttf",60)
    text = font.render("SPACE TO START!",True,(0,0,0))
    textRect = text.get_rect()
    textRect.center = ((250,400))

    Sbutton = button11((0,255,0),431,16,40,50, '')


    # Displaying class's in main loop
    def redrawwindow():
        window.fill((255,0,0))


         # Drawing my classes and other things
        playerman.draw()



        window.blit(text,textRect)

        
        if on:
            window.blit(music_on,(420,10))
        if off:
            window.blit(music_off,(420,10))

        for Sweep in sweeps:
            Sweep.draw()

    
            

    sound = True
    mouseisover = True
    switch = 1
    Stimer = 0
    off = False
    on = True
    timer = 0
    fps = 35
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                if switch == 1 and Sbutton.isOver(pos):
                    switch = 2
                    on = False
                    off = True
                else:
                    if switch == 2 and Sbutton.isOver(pos):
                        switch = 1
                        on = True
                        off = False


        timer += 1
        
        keys = pygame.key.get_pressed()

        if keys[pygame.K_SPACE] and timer >= 20:
            Stimer += 1

        if Stimer > 0 :
            Stimer += 1
            right.x += 20
            right2.x -= 20
        if Stimer >= 32:
            main_loop()
            Stimer = 0

        left1.x -= 20
        left2.x += 20
        
        
        redrawwindow()
        pygame.display.update()
    pygame.quit()


def main_loop():
    global lose
    # Making both peipes separate 
    Pipe_distance = 830
    # part of making background shift
    bg_shift = 0
    
    # player class
    # player class
    class Player:
        def __init__(self,x,y,width,height,color):
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.color = color
            self.speed = 5
            self.JumpCount = False
            self.isJump = 10
            self.fall = 0
            self.rect = pygame.Rect(x,y,width,height)
        def get_rect(self):
            self.rect.topleft = (self.x,self.y)
            return self.rect
        def draw(self):
            self.rect.topleft = (self.x,self.y)
            pygame.draw.rect(window,self.color,self.rect)


    # Pipe class
    class Pipe:
        def __init__(self,x,y,width,height,color):
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.color = color
            self.speed = 3
            self.rect = pygame.Rect(x,y,width,height)
        def get_rect(self):
            self.rect.topleft = (self.x,self.y)
            return self.rect
        def draw(self):
            self.rect.topleft = (self.x,self.y)
            pygame.draw.rect(window,self.color,self.rect)

    # Pipe2 class
    class Pipe2:
        def __init__(self,x,y,width,height,color):
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.color = color
            self.speed = 3
            self.rect = pygame.Rect(x,y,width,height)
        def get_rect(self):
            self.rect.topleft = (self.x,self.y)
            return self.rect
        def draw(self):
            self.rect.topleft = (self.x,self.y)
            pygame.draw.rect(window,self.color,self.rect)


    class Sweep:
        def __init__(self,x,y,width,height,color):
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.color = color
            self.speed = 2
            self.rect = pygame.Rect(x,y,width,height)
        def draw(self):
            self.rect.topleft = (self.x,self.y)
            pygame.draw.rect(window,self.color,self.rect)


    # Color
    white = (255,255,255)
    green = (0,255,0)
    blue = (0,0,255)
    Black = (0,0,0)
    # Calss's cords,size, and color
    playerman = Player(200,200,40,40,white)

    pipe1 = Pipe(350,900,60,700,green)
    pipe2 = Pipe2(350,-900,60,700,green)
    pipe3 = Pipe(720,9900,60,700,white)
    pipe4 = Pipe2(720,-9900,60,700,blue)
    left1 = Sweep(-550,0,800,800,Black)
    left2 = Sweep(250,0,800,800,Black)

    # All my list
    pipes = [pipe1,pipe3]
    pipes2 = [pipe2,pipe4]
    sweeps = [left1,left2]

    # Point system
    font = pygame.font.Font("img/CAT.ttf",60)
    score = 0
    text = font.render(""+str(score), True,(255,255,255))
    textRect = text.get_rect()
    textRect.center = ((250,60))

    lose = False

    
    def quitgame():
        pygame.quit()

    def unlose():
        global lose
        lose = False
        
    def lost():
        
        lfont = pygame.font.Font("img/CAT.ttf",60)
        ltext = lfont.render("You Died",True,(255,255,255))
        ltextRect = ltext.get_rect()
        ltextRect.center = ((250,90))

        lfont2 = pygame.font.Font("img/CAT.ttf",60)
        ltext2 = lfont2.render("Score",True,(255,255,255))
        ltextRect2 = ltext2.get_rect()
        ltextRect2.center = ((250,510))

        lfont3 = pygame.font.Font("img/CAT.ttf",50)
        ltext3 = lfont3.render("Space To Return",True,(255,0,255))
        ltextRect3 = ltext3.get_rect()
        ltextRect3.center = ((250,270))
        timer = 0
        while lose:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()

            timer += 1

            keys = pygame.key.get_pressed()


            if keys[pygame.K_SPACE] and timer >= 20:
                game2()
                

            window.fill((0,0,0))
            text = font.render(""+str(score), True,(255,255,255))
            textRect.center = ((250,450))

            window.blit(ltext,ltextRect)
            window.blit(ltext2,ltextRect2)
            window.blit(ltext3,ltextRect3)
            window.blit(text,textRect)

            pygame.display.update()
            clock.tick(15)


    # Displaying class's in main loop
    def redrawwindow():
        window.fill((0,0,0))


        # Drawing my classes and other things
        playerman.draw()

        for Pipe in pipes:
            Pipe.draw()
        for Pipe2 in pipes2:
            Pipe2.draw()
        for Sweep in sweeps:
            Sweep.draw() 
        window.blit(text,textRect)


    # To make the game more difuclt
    ptimer = 0
    # for playing sprite when player jumps
    # Making it so the rectangle dose not come for a whilw
    Ptimer = 0
    Ptimer2 = 0
    spcdown = False
    sound = True
    # for making firse play once
    fps = 35
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(fps)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        
        # Making most of my classes move
        for Pipe in pipes:
            Pipe.x  -= Pipe.speed
        for Pipe2 in pipes2:
            Pipe2.x -= Pipe2.speed
        for Pipe2 in pipes2:
            if Pipe2.x <= -200:
                Pipe2.x = 550


        left1.x -= 20
        left2.x += 20


        # Randomizing pipe
        for Pipe in pipes:
            if Pipe.x <= -200:
                Pipe.x = 550
                Pipe.y = random.randint(150,500)
                pipe2.y = pipe1.y - Pipe_distance
                pipe4.y = pipe3.y - Pipe_distance

                
          # Making the game faster
        ptimer += 1
        if ptimer == 10000:
            for Pipe in pipes:
                for Pipe2 in pipes2:
                    Pipe.speed += 1
                    Pipe2.speed += 1
            bg_shift += 3


        for Pipe in pipes:
            if playerman.rect.colliderect(Pipe.rect):
                lose = True
                lost()
        for Pipe2 in pipes2:
            if playerman.rect.colliderect(Pipe2.rect):
                lose = True
                lost()
            
        keys = pygame.key.get_pressed()


        if not keys[pygame.K_SPACE]:
            spcdown = False  # space released
        
        if keys[pygame.K_SPACE]:
            Jumping = 1
            if not spcdown:
                spcdown = True
            
            

        collide = False

        playerman.y += playerman.speed
        # bird moving
        if not playerman.isJump:
            # [...]

            # the bird is allowed to jump even if it is not colliding:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True

            if collide:
                playerman.fall = 0

        else:
            if playerman.JumpCount > 0:
                playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
                playerman.JumpCount -= 1
            else:
                playerman.JumpCount = 10
                
                # if K_SPACE is pressed, then the bird keeps jumping
                if not keys[pygame.K_SPACE]:
                    playerman.isJump = False


        
        
        redrawwindow()
        pygame.display.update()
    pygame.quit()
    unlose()
game2()
main_loop()

        

1 个答案:

答案 0 :(得分:2)

问题可能是由于使用两个单独的变量来控制声音的状态造成的:

const {_id} = req.params;

我建议只使用一个描述性变量名:

    if on:
        window.blit(music_on,(420,10))
    if off:
        window.blit(music_off,(420,10))

并在单击图像时更改它:

if ( sound_muted ):
    window.blit( music_on, (420,10) )   # show "un-mute" button
else:
    window.blit( music_off, (420,10) )  # show "mute" button
相关问题