如何使播放器与我的平台发生碰撞

时间:2020-10-02 04:20:56

标签: python pygame

因此,我一直想知道如何做到这一点,例如,当播放器触摸平台的任何一侧时,播放器的行为就像那里有一堵墙,

https://gyazo.com/272b729154b0790fd3e004c761cdb658

它只是掠过侧面,只与顶部碰撞。我希望它停止这样做,并实际上与侧面和底部发生冲突,我尝试看了一些教程,但是其中大多数教程都没有在谈论另一个主题,或者我只是听不懂,而且我也尝试过代码显示在下面,但是不起作用。

我尝试过的代码


    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_d:
             x_change = -7
        if event.key == pygame.K_a:
             x_change = 7

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_d or event.key == pygame.K_a:
             x_change = 0

        x += x_change
        if x > 500 - playerman.width or x < 0:
            x = old_x

我的完整代码

import pygame
pygame.init

window = pygame.display.set_mode((700,500))

pygame.display.set_caption("Noobs First Game")


# Playerman
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.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

class bullet(object):
    def __init__(self,x,y,color):
        self.x = x
        self.y = y
        self.color = color
        self.speed = 10
        self.color = color
        self.rect.topleft = (self.x,self.y)
    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.color = color
        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 for hitbox

white = (255,255,255)

# Drawing Player
playerman = Player(255,255,40,40,white)

#Drawing Platforms
platform1 = Platform(200,470,100,30,white)
platform2 = Platform(400,410,100,30,white)



# List
platforms = [platform1,platform2]




# Windows color
def redrawwindow():
    window.fill((0,0,0))

# Drawing the player and other stuff to the screen
    playerman.draw()

    for Platform in platforms:
        Platform.draw()


x = 10
y = 10

x_change = 0
y_change = 0
old_x = x
old_y = y

        


fps = (30)
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.KEYDOWN:
        if event.key == pygame.K_d:
             x_change = -7
        if event.key == pygame.K_a:
             x_change = 7

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_d or event.key == pygame.K_a:
             x_change = 0

        x += x_change
        if x > 500 - playerman.width or x < 0:
            x = old_x

            
    # lets player move
    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]and playerman.x > playerman.speed:
            playerman.x -= playerman.speed

    if keys[pygame.K_d]and playerman.x < 700 - playerman.height - playerman.speed:
        playerman.x += playerman.speed

    if keys[pygame.K_w]and playerman.y > playerman.speed:
        playerman.y -= playerman.speed

    if keys[pygame.K_s]and playerman.y <500 - playerman.width - playerman.speed:
        playerman.y += playerman.speed

  
    # About isJump
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False

        # this part lets you jump on platform only the top 
        collide = False
        for Platform in platforms:
            if playerman.rect.colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height + 1
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right
        
           
                    
            # colliding with floor      
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.Jumpcount = 10
                playerman.y = 500 - playerman.height

        # Jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0

    # Jump Count

    else:
        if playerman.JumpCount >= 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10


    

                

    
    
        




    redrawwindow()
    pygame.display.update()
pygame.quit()
        


        
        



2 个答案:

答案 0 :(得分:0)

要与侧面碰撞,请使用以下内容:

x = (#whatever)
y = (#whatever)

x_change = 0
y_change = 0
old_x = x
old_y = y
if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
         x_change = -7
    if event.key == pygame.K_RIGHT:
         x_change = 7

if event.type == pygame.KEYUP:
    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
         x_change = 0

x += x_change
if x > display_width - player_width or x < 0:
        x = old_x

需要设置一些变量,例如display_width和player_width,x和y。

答案 1 :(得分:0)

在浮点属性xy中,而不是在属性rect中,语句的实际位置。向类播放器添加方法get_rect(),该方法将更新属性rect。碰撞测试需要它。

class Player:
    # [...]

     def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect

    def draw(self):
        pygame.draw.rect(window,self.color,self.get_rect())

将播放器的位置复制到局部变量(px py)中并更改这些变量,而不是移动播放器时的位置:

# lets player move
keys = pygame.key.get_pressed()
px, py = playerman.x, playerman.y
if keys[pygame.K_a] and px > playerman.speed:
        px -= playerman.speed
if keys[pygame.K_d] and px < 700 - playerman.height - playerman.speed:
    px += playerman.speed
if keys[pygame.K_w] and py > playerman.speed:
    py -= playerman.speed
if keys[pygame.K_s] and py <500 - playerman.width - playerman.speed:
    py += playerman.speed

获取所有平台矩形的列表以及带有播放器新位置的矩形:

platform_rect_list = [p.rect for p in platforms]
player_rect = playerman.get_rect()
player_rect.topleft = (px, py)

如果玩家没有摔倒,请使用collidelist()测试它是否与任何平台发生碰撞。 如果玩家没有与任何平台发生碰撞,请更新玩家的x坐标。这样可以防止播放器在平台上“运行”:

playerman.y = py
if not playerman.isJump and player_rect.collidelist(platform_rect_list) < 0:
    playerman.x = px

使用get_rect()进行碰撞测试。如果玩家在平台上方,则正确的位置是playerman.y = Platform.rect.top - playerman.height而不是playerman.y = Platform.rect.top - playerman.height + 1

# About isJump
if not playerman.isJump:
    playerman.y += playerman.fall
    playerman.fall += 1
    playerman.isJump = False

    # this part lets you jump on platform only the top 
    collide = False
    for Platform in platforms:
        if playerman.get_rect().colliderect(Platform.rect): # <--- get_rect()
            collide = True
            playerman.isJump = False
            
            # playerman.y = Platform.rect.top - playerman.height + 1   <--- DELETE
            playerman.y = Platform.rect.top - playerman.height
            
            if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                playerman.x = Platform.rect.left - playerman.width
            if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                playerman.x = Platform.rect.right

完整示例

import pygame
pygame.init()

window = pygame.display.set_mode((700,500))
pygame.display.set_caption("Noobs First Game")

# Playerman
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.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.rect = pygame.Rect(x,y,height,width)
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        pygame.draw.rect(window,self.color,self.get_rect())

class bullet(object):
    def __init__(self,x,y,color):
        self.x = x
        self.y = y
        self.color = color
        self.speed = 10
        self.color = color
        self.rect.topleft = (self.x,self.y)
    def draw(self):
        self.rect.topleft = round(self.x), round(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.color = color
        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 for hitbox
white = (255,255,255)

# Drawing Player
playerman = Player(255,255,40,40,white)

#Drawing Platforms
platform1 = Platform(200,470,100,30,white)
platform2 = Platform(400,410,100,30,white)

# List
platforms = [platform1,platform2]

# Windows color
def redrawwindow():
    window.fill((0,0,0))

    # Drawing the player and other stuff to the screen
    playerman.draw()

    for Platform in platforms:
        Platform.draw()

x = 10
y = 10
x_change = 0
y_change = 0
old_x = x
old_y = y
fps = (30)
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.KEYDOWN:
            if event.key == pygame.K_d:
                x_change = -7
            if event.key == pygame.K_a:
                x_change = 7

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_d or event.key == pygame.K_a:
                x_change = 0

            x += x_change
            if x > 500 - playerman.width or x < 0:
                x = old_x
           
    # lets player move
    keys = pygame.key.get_pressed()
    px, py = playerman.x, playerman.y
    if keys[pygame.K_a] and px > playerman.speed:
            px -= playerman.speed
    if keys[pygame.K_d] and px < 700 - playerman.height - playerman.speed:
        px += playerman.speed
    if keys[pygame.K_w] and py > playerman.speed:
        py -= playerman.speed
    if keys[pygame.K_s] and py <500 - playerman.width - playerman.speed:
        py += playerman.speed

    platform_rect_list = [p.rect for p in platforms]
    player_rect = playerman.get_rect()
    player_rect.topleft = (px, py)

    playerman.y = py
    if not playerman.isJump and player_rect.collidelist(platform_rect_list) < 0:
        playerman.x = px
  
    # About isJump
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False

        # this part lets you jump on platform only the top 
        collide = False
        for Platform in platforms:
            if playerman.get_rect().colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right
                       
            # colliding with floor      
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.Jumpcount = 10
                playerman.y = 500 - playerman.height

        # Jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
            playerman.fall = 0

    # Jump Count

    else:
        if playerman.JumpCount >= 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10

    redrawwindow()
    pygame.display.update()
pygame.quit()