如何创建一个“固体”块?小游戏

时间:2021-03-22 15:49:19

标签: python pygame

我想在我正在开发的简单平台游戏中实现实体块,但我不知道如何实现。 我尝试了很多东西,但没有任何效果。 我想做以下事情: 当我跳上去时,我会留在那里 当我跳到它下面时,我不会到达顶部 当我从左侧跳时,我不会到达右侧 当我从右侧跳时,我不会到达左侧

非常感谢您的帮助,谢谢。

我的代码:

from pygame.locals import *
SCREEN_WIDTH = 1923
SCREEN_HEIGHT = 1000


class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super(Player, self).__init__()
        self.surf = pygame.image.load('C:\Sprites\mrkva.png').convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)
        self.rect = self.surf.get_rect()
        self.rect.y = y
        self.rect.x = x

        self.isJump = False
        self.jumpCount = 9

    def update(self, pressed_keys):

        if pressed_keys[K_a]:
            self.rect.move_ip(-10, 0)
        if pressed_keys[K_d]:
            self.rect.move_ip(10, 0)

        if pressed_keys[K_LEFT]:
            self.rect.move_ip(-10, 0)
        if pressed_keys[K_RIGHT]:
            self.rect.move_ip(10, 0)

        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > SCREEN_WIDTH:
            self.rect.right = SCREEN_WIDTH
        if self.rect.top <= 0:
            self.rect.top = 0
        if self.rect.bottom >= 970:
            self.rect.bottom = 970
    
    def jump(self):
        if self.isJump:
            if self.jumpCount >= -10:
                neg = 3
                if self.jumpCount < 0:
                    neg = -3
                self.rect.y -= self.jumpCount**2 * 0.101 * neg
                self.jumpCount -= 1
            else:
                self.isJump = False
                self.jumpCount = 9

class Platform(pygame.sprite.Sprite):
    def __init__(self, xloc, yloc):
        super(Platform, self).__init__()
        self.surf = pygame.image.load('C:\Sprites\kocka.png').convert()
        self.surf.set_colorkey((255, 255, 255), RLEACCEL)
        self.rect = self.surf.get_rect()
        self.rect.y = yloc
        self.rect.x = xloc

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

pygame.display.set_caption('gameee')
platform = Platform(1, 527)
player = Player(1, 969)

all_sprites = pygame.sprite.Group()
all_sprites.add(platform)
all_sprites.add(player)


clock = pygame.time.Clock()
run = True
while run:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                run = False
            elif event.key == K_SPACE:
                player.isJump = True

        elif event.type == QUIT:
            run = False

    clock.tick(35)
    screen.blit(player.surf, player.rect)
    screen.fill((28, 173, 234))
    for entity in all_sprites:
        screen.blit(entity.surf, entity.rect)
    pygame.display.flip()

    pressed_keys = pygame.key.get_pressed()

    player.jump()
    player.update(pressed_keys)

    pygame.display.update()
pygame.quit()```

1 个答案:

答案 0 :(得分:2)

在平台游戏中执行碰撞测试时,您必须始终考虑玩家的移动方向。当玩家从上方或下方撞击平台时,他们的反应是不同的。这可能会导致您的代码出现不同的情况。

我将举例说明如何根据您的代码登陆平台。


首先,更正让玩家跳跃的代码。为跳跃计数添加一个常量并四舍五入玩家的 y 坐标。所以玩家在向上移动时落在相同的距离上:

JUMP_COUNT = 10

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        # [...]

        self.isJump = False
        self.jumpCount = JUMP_COUNT

    # [...]
    
    def jump(self):
        if self.isJump:
            if self.jumpCount >= -JUMP_COUNT:
                neg = 3
                if self.jumpCount < 0:
                    neg = -3
                self.rect.y -= round(self.jumpCount**2 * 0.101 * neg)
                self.jumpCount -= 1
            else:
                self.isJump = False
                self.jumpCount = JUMP_COUNT

在移动玩家之前保存玩家的 y 坐标。在玩家移动后检查碰撞。如果玩家摔倒并检测到碰撞,请将玩家的底部放在平台顶部:

run = True
while run:
    # [...]

    prev_y = player.rect.y
    
    player.jump()
    player.update(pressed_keys)

    if player.rect.colliderect(platform.rect):
        dir_y = player.rect.y - prev_y
        if dir_y > 0:
            player.isJump = False
            player.jumpCount = JUMP_COUNT
            player.rect.bottom = platform.rect.top

    pygame.display.update()