如何让我的玩家在python 3和pygame中跳墙?

时间:2016-02-03 05:42:52

标签: python python-3.x pygame

我试图在python / pygame中制作游戏,玩家可以在平台上跑步,跳跃和行走,但我无法让玩家能够跳墙跳跃。我想做到这一点,所以玩家可以降落在墙壁一侧,然后跳下它,但我无法专门做到这一点。这是我的代码,虽然它有点长:

import pygame

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

class Player(pygame.sprite.Sprite):

    def __init__(self, color, X, Y):

        super().__init__()

        width = 40
        height = 40

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

        self.rect = self.image.get_rect()

        self.image.fill(color)

        self.rect.x = X
        self.rect.y = Y

        self.change_x = 0
        self.change_y = 0

        self.jumping = True

    def calc_grav(self):

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

        if self.rect.y >= 460:
            self.rect.y = 460
            self.change_y = 0

    def update(self, platforms):

        self.calc_grav()

        self.rect.x += self.change_x

        platform_hit_list = pygame.sprite.spritecollide(self, platforms, False)

        for platform in platform_hit_list:
            if self.change_x > 0:
                self.rect.right = platform.rect.left
            else:
                self.rect.left = platform.rect.right

        self.rect.y += self.change_y

        platform_hit_list = pygame.sprite.spritecollide(self, platforms, False)

        for platform in platform_hit_list:
            if self.change_y > 0:
                self.rect.bottom = platform.rect.top
                self.change_y = 0
            elif self.change_y < 0:
                self.rect.top = platform.rect.bottom
                self.change_y = 0
        if self.rect.x >= 680:
            self.rect.x = 680
        elif self.rect.x <= 0:
            self.rect.x = 0

    def jump(self):

        self.rect.y -= 2
        self.change_y = -10
        self.jumping = True

    def bounce(self, x):

        self.rect.x += x
        self.jump()

class Platform(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height, color):

        super().__init__()

        self.image = pygame.Surface([width, height])
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.image.fill(color)

pygame.init()

all_sprites_list = pygame.sprite.Group()
platforms = pygame.sprite.Group()

size = (700, 500)

screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

player = Player(RED, 100, 100)
platform = Platform(50, 300, 200, 50, BLUE)

platforms.add(platform)
all_sprites_list.add(player, platform)

done = False

clock = pygame.time.Clock()

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                player.change_x = -6
            elif event.key == pygame.K_d:
                player.change_x = 6
            elif event.key == pygame.K_SPACE and player.change_y == 0:
                player.jump()
            elif event.key == pygame.K_LSHIFT:
                if player.change_x == 6:
                    player.change_x = 9
                elif player.change_x == -6:
                    player.change_x = -9

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                player.change_x = 0
            if event.key == pygame.K_d:
                player.change_x = 0

    player.update(platforms)

    screen.fill(WHITE)
    all_sprites_list.draw(screen)
    pygame.display.flip()

    clock.tick(60)

pygame.quit()

1 个答案:

答案 0 :(得分:1)

如果y速度为零,您的密钥修改码中的条件只允许调用jump

        elif event.key == pygame.K_SPACE and player.change_y == 0:
            player.jump()

所以这可能就是为什么你不能在与墙壁接触时跳跃(你的速度不是零)。我建议在播放器类中添加变量以指示它们何时“滑动”并将其作为条件添加到上面的键处理程序中。例如:

        elif event.key == pygame.K_SPACE and (player.change_y == 0 or player.wall_sliding):
            player.jump()
相关问题