首页 > 解决方案 > 我如何让玩家不会从平台方面受到影响

问题描述

我已经开始了这个游戏,我一直在努力做到这一点,所以当玩家触摸我的方块的侧面时,它不会消失并实际上停止,我希望它像一个完整的普通方块一样工作,让你在触摸时停止双方。

https://gyazo.com/272b729154b0790fd3e004c761cdb658

正如你所看到的,每次我触摸我的街区的侧面时,它都会穿过它,但我不想让它困扰,我希望它真正接触到侧面,我知道如何让它接触到我的顶部平台,但我不知道如何让它接触平台的侧面和底部。我已经尝试了下面显示的代码,但它不起作用。

我试过的代码

   if event.type == pygame.K_s:
        if keys == pygame.K_a:
             x_change = -7
    if keys == pygame.K_d:
         x_change = 7

    if event.type == pygame.KEYUP:
        if keys == pygame.K_a or keys == pygame.K_d:
             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 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 = 255
y = 255

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

    # 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


    if event.type == pygame.K_s:
        if keys == pygame.K_a:
             x_change = -7
    if keys == pygame.K_d:
         x_change = 7

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

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





    # 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()
        


        
        

标签: pythonpygame

解决方案


当我制作平台游戏时,我会检查每一帧,看看玩家是否与障碍物发生碰撞。伪代码:

if player.movement_vel.x>0 and player.collide_obst():
    player.right=player.collide_obst().rect.left

不过这有点抽象。我只是假设 player.collide_obst() 函数返回他碰撞的一个障碍物。当然你必须: - 让它也适用于玩家左右移动,我建议你这样做,所以 collide_obst() 函数返回一个你循环的列表。学习此类内容的一个很好的来源是: https ://www.youtube.com/watch?v=Qdeb1iinNtk他在视频中以同样的方式介绍了碰撞。我希望我能帮助你一点,拉斯


推荐阅读