pygame问题创建音乐静音按钮并显示背景

时间:2017-12-01 17:56:51

标签: python pygame

好吧,我在这里与平台游戏有两个主要问题。对于最近刚刚开始使用pygame的人而言,并不是特别大但令人沮丧。

1)我的音乐很好,没有问题。但是,我不能让一个静音按钮工作。在我的代码中,我试图在M上使用“pygame.mixer.pause”并使用“pygame.mixer.unpause”重新启动N但是似乎无法使其工作。香港专业教育学院也试图制作一个“切换课”来打开和关闭音乐,但这不起作用。

2)无法让我的背景图像正确显示。目前是白色背景,当我使用下面的代码时,玩家不会移动。图像的像素大小相同,所以我不认为它是一个问题,更可能是我的代码问题。我试过了

backgroundimage = pygame.image.load("forest.png").convert()
backgroundimage
screen.blit(backgroundimage,(0,0))
pygame.display.update()

在一个单独的文件上工作但不会出现在这个game.link到我试图使用的背景图片文件 - https://dribbble.com/shots/1491895-Forest-Jungle-Game-Background-Game-Assets-Sprite-Sheet-Sidescrol

import pygame
import random
import mixer

from settings import *


size = [WIDTH, HEIGHT]
screen = pygame.display.set_mode(size)

def start_screen():
        pass
def end_screen():
        pass


class Player(pygame.sprite.Sprite):
    def __init__(self):

        super().__init__()

        self.image = pygame.image.load("red_mushroom.PNG").convert_alpha()
        Player_colour = self.image.fill(BLACK)

        self.rect = self.image.get_rect()

        self.change_x = 0
        self.change_y = 0

        self.level = None


    def update(self):

        self.calc_grav()
        self.rect.x += self.change_x


        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:

            if self.change_x > 0:
                self.rect.right = block.rect.left
            elif self.change_x < 0:

                self.rect.left = block.rect.right


        self.rect.y += self.change_y


        block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        for block in block_hit_list:


            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            elif self.change_y < 0:
                self.rect.top = block.rect.bottom


            self.change_y = 0

    def calc_grav(self):

        if self.change_y == 0:
            self.change_y = 1
        else:
            self.change_y += .35

        if self.rect.y >= HEIGHT - self.rect.height and self.change_y >= 0:
            self.change_y = 0
            self.rect.y = HEIGHT - self.rect.height

    def jump(self):

        self.rect.y += 2
        platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
        self.rect.y -= 2


        if len(platform_hit_list) > 0 or self.rect.bottom >= HEIGHT:
            self.change_y = -10


    def go_left(self):
        self.change_x = -6

    def go_right(self):
        self.change_x = 6

    def stop(self):
        self.change_x = 0


class Platform(pygame.sprite.Sprite):
    def __init__(self, width, height):

        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(BLACK)

        self.rect = self.image.get_rect()

class Level():

    def __init__(self, player):
        self.platform_list = pygame.sprite.Group()
        self.enemy_list = pygame.sprite.Group()
        self.player = player

        self.world_shift = 0

    def update(self):
        self.platform_list.update()
        self.enemy_list.update()

    def draw(self, screen):
        screen.fill(WHITE)
        self.platform_list.draw(screen)
        self.enemy_list.draw(screen)

    def shift_world(self, shift_x):

        self.world_shift += shift_x

        for platform in self.platform_list:
            platform.rect.x += shift_x

        for enemy in self.enemy_list:
            enemy.rect.x += shift_x



class Level_one(Level):
    def __init__(self, player):

        Level.__init__(self, player)

        self.level_limit = -1000

        level = [[210, 70, 500, 500],
                 [210, 70, 500, 100],
                 [70, 210, 450, 100],
                 [40, 40, 800, 300],
                 [40, 40, 800, 150],
                 [210, 70, 800, 400],
                 [210, 70, 1000, 500],
                 [210, 70, 1220, 280],
                 [70, 210, 1000, 100],
                 [70, 210, 1000, 300],
                 [70, 310, 1220, 0],
                 ]

        for platform in level:
            block = Platform(platform[0], platform[1])
            block.rect.x = platform[2]
            block.rect.y = platform[3]
            block.player = self.player
            self.platform_list.add(block)



class Level_two(Level):
    def __init__(self, player):


        Level.__init__(self, player)

        self.level_limit = -1000

        level = [[210, 30, 450, 570],
                 [210, 30, 850, 420],
                 [210, 30, 850, 600],
                 [210, 30, 600, 280],
                 ]


        for platform in level:
            block = Platform(platform[0], platform[1])
            block.rect.x = platform[2]
            block.rect.y = platform[3]
            block.player = self.player
            self.platform_list.add(block)


word_colour = BLACK

def main():

    pygame.init()

    sound = pygame.mixer.Sound("Mario_Jumping-Mike_Koenig-989896458.WAV")
    back_music = pygame.mixer.music.load("Sneaky_Snitch_Kevin_MacLeod_-_Gaming_Background_Mu.WAV")
    pygame.mixer.music.play(-1)

    backgroundimage = pygame.image.load("forest.png").convert()
    backgroundimage
    screen.blit(backgroundimage,(0,0))
    pygame.display.update()

    pygame.display.set_caption(TITLE)

    player = Player()

    level_list = []
    level_list.append(Level_one(player))
    level_list.append(Level_two(player))

    current_level_no = 0
    current_level = level_list[current_level_no]

    active_sprite_list = pygame.sprite.Group()
    player.level = current_level

    player.rect.x = 340
    player.rect.y = HEIGHT - player.rect.height
    active_sprite_list.add(player)


    done = False

    clock = pygame.time.Clock()


    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True


            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    player.go_left()
                if event.key == pygame.K_RIGHT:
                    player.go_right()
                if event.key == pygame.K_UP:
                    player.jump()
                    sound.play()
                if event.key == pygame.K_m:
                        pygame.mixer.pause
                if event.key == pygame.K_n:
                        pygame.mixer.unpause

                if event.key == pygame.K_LEFT and player.change_x < 0:
                    player.stop()
                if event.key == pygame.K_RIGHT and player.change_x > 0:
                    player.stop()

        active_sprite_list.update()
        current_level.update()


        if player.rect.right >= 500:
            diff = player.rect.right - 500
            player.rect.right = 500
            current_level.shift_world(-diff)

        if player.rect.left <= 120:
            diff = 120 - player.rect.left
            player.rect.left = 120
            current_level.shift_world(diff)

        # If the player gets to the end of the level, go to the next level
        current_position = player.rect.x + current_level.world_shift
        if current_position < current_level.level_limit:
            player.rect.x = 120
            if current_level_no < len(level_list)-1:
                current_level_no += 1
                current_level = level_list[current_level_no]
                player.level = current_level

        current_level.draw(screen)
        active_sprite_list.draw(screen)

        clock.tick(60)


        pygame.display.flip()

    pygame.quit()

if __name__ == "__main__":
    main()

设置文件是分开的。它包含

import pygame

#screen prep
WIDTH = 800
HEIGHT = 600
FPS = 60

#colours
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0,0,255)

TITLE = "Not Mario"

1 个答案:

答案 0 :(得分:1)

您忘记了pygame.mixer.pauseunpause后面的括号,因此您永远不会调用这些函数。您还必须致电pygame.mixer.music.pause()以停止播放音乐。 pygame.mixer.pause()只会停止所有声音,而不是音乐。

if event.key == pygame.K_m:
    pygame.mixer.music.pause()
if event.key == pygame.K_n:
    pygame.mixer.music.unpause()

关于背景图片,您必须在关卡的draw方法中对其进行blit,或者移除screen.fill(WHITE)行并在主while循环中blit背景。

要修复此动作,请在停止播放器之前添加一行以检查event.type是否为pygame.KEYUP

if event.type == pygame.KEYUP:
    if event.key == pygame.K_LEFT and player.change_x < 0:
        player.stop()
    if event.key == pygame.K_RIGHT and player.change_x > 0:
        player.stop()