玩家在不与平台 Pygame 碰撞时不会掉落

时间:2021-03-02 17:05:40

标签: python pygame

所以我在 pygame 中制作了这个 2D 游戏,我遇到了一个问题,当我试图跳出平台时,我的玩家只会漂浮而不是坠落 https://gyazo.com/82e9029b230326f182067a9f4a3cc47c。我一直在尝试使用真实和错误的陈述来使其工作,但大多数时候我的播放器根本不会移动,我也尝试告诉它是否与平台相撞并且不起作用。

这是我尝试过的事情之一

for Platform in platforms:
        if not playerman.rect.colliderect(Platform.rect):
            playerman.y -= playerman.speed

我的游戏演示

import pygame
pygame.init()


# width and height of winddow
window = pygame.display.set_mode((700,500))


# window name
pygame.display.set_caption("Game")


#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 = 4
        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)


class Platform:
    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 draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


class Platform2:
    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 draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

           
# Colors
white = (255,255,255)
green = (255,0,0)


# class's cordination, size, and color

#Player
playerman = Player(300,250,40,70,white)


#Platform
platform1 = Platform(0,460,800,40,white)
#The top platform
platform2 = Platform2(0,0,800,40,green)


# List's
platforms = [platform1]
platforms2 = [platform2]


# drawing things in main loop
def redraw():
    # background color
    window.fill((0,0,0))


    # drawing the player in main loop
    playerman.draw()


    # Drawing all the bottom platforms in the main loop
    for Platform in platforms:
        Platform.draw()


    # Drawining all the top platforms in the main loop
    for Platform2 in platforms2:
        Platform2.draw()


# Timer for player falling
#fps for the game
fps = 30
clock = pygame.time.Clock()
up = True
down = False
run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

            
    keys = pygame.key.get_pressed()
    px,py = playerman.x,playerman.y


    if down == False:
        for Platform in platforms2:
            if keys[pygame.K_SPACE] and playerman.rect.colliderect(Platform):
                up = True

                
    if up:
        playerman.y += 10
        fall = False


    for Platform in platforms:
        if playerman.rect.colliderect(Platform.rect):
            up = False


    if up == False and  keys[pygame.K_SPACE]:
        for Platform in platforms:
            if playerman.rect.colliderect(Platform.rect):
                down = True
                up = False

                
    if down:
        playerman.y -= 10
        playerman.direction = "tjump"
        fall = False


    for Platform in platforms2:
        if playerman.rect.colliderect(Platform.rect):
            down = False
            playerman.direction = "uidle"

   
    if keys[pygame.K_a] and px > playerman.speed:
        for Platform in platforms:
            for Platform2 in platforms2:
                Platform.x += playerman.speed
                Platform2.x += playerman.speed

                    
    elif keys[pygame.K_d] and px < 2000 - playerman.width + playerman.speed:
        for Platform in platforms:
            for Platform2 in platforms2:
                Platform.x -= playerman.speed
                Platform2.x -= playerman.speed
                

    redraw()
    pygame.display.update()
pygame.quit()

1 个答案:

答案 0 :(得分:0)

如果我现在抓住你,你想在玩家到达平台时粘住它。如果平台离开,您希望玩家再次弹跳。

您代码中的问题是您没有在平台离开时设置玩家的状态。您将 updown 都设置为 False 以在玩家到达底部平台时粘住玩家。如果底部平台遗漏,要让玩家移动,您需要设置 down=True。这就是我在 else 语句中所做的。通过设置down=True,我们需要确保玩家已经到达底部平台。我使用一个额外的布尔变量 reach_up 来做到这一点。

    # The bottom platform
    # The bottom has larger y value, so it's up direction
    for Platform in platforms:
        if playerman.rect.colliderect(Platform.rect):
            up = False
            reach_up = True
        else:
            if reach_up:
                down = True
                reach_up = False

示例代码是

up = True
down = False

reach_up = False
reach_down = False

while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    px,py = playerman.x,playerman.y

    if down == False:
        for Platform in platforms2:
            if keys[pygame.K_SPACE] and playerman.rect.colliderect(Platform):
                up = True

    if up == False and keys[pygame.K_SPACE]:
        for Platform in platforms:
            if playerman.rect.colliderect(Platform.rect):
                down = True

    if up:
        playerman.y += 10

    if down:
        playerman.y -= 10

    for Platform in platforms:
        if playerman.rect.colliderect(Platform.rect):
            up = False
            reach_up = True
        else:
            if reach_up:
                down = True
                reach_up = False

    for Platform in platforms2:
        if playerman.rect.colliderect(Platform.rect):
            down = False
            reach_down = True
        else:
            if reach_down:
                up = True
                reach_down = False

    if keys[pygame.K_a] and px > playerman.speed:
        for Platform in platforms:
            for Platform2 in platforms2:
                Platform.x += playerman.speed
                Platform2.x += playerman.speed


    elif keys[pygame.K_d] and px < 2000 - playerman.width + playerman.speed:
        for Platform in platforms:
            for Platform2 in platforms2:
                Platform.x -= playerman.speed
                Platform2.x -= playerman.speed


    redraw()
    pygame.display.update()

回到你的代码:

  1. 绿色的RGB值是(0,128,0)而不是(255,0,0)
  2. Platform2 类是什么意思?您可以像 platform2 = Platform(0,0,800,40,green) 一样初始化 platform2。