繁体   English   中英

如何让我的播放器与我的平台底部发生碰撞

[英]how do I make my player collide with the bottom of my platform

所以我想知道如何做到这一点,以便当我的玩家与我的平台底部发生碰撞时,它不仅会传送到我的平台顶部,而且会让玩家表现得就像平台底部在那里一样。 我尝试使用与平台侧面相同的方法,但将其更改为与平台底部发生碰撞,但这对我不起作用。

发生了什么: https://gyazo.com/0854093887dfa2ada9e22da18d9ef751

我的平台 class

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.stone = pygame.image.load("stone.png")
        self.stone = pygame.transform.scale(self.stone,(self.stone.get_width()+28,self.stone.get_height()+28))
        self.rect = pygame.Rect(x,y,width,height)
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        platform_rect = self.stone.get_rect(center = self.get_rect().center)
        platform_rect.centerx += 2
        platform_rect.centery += 0
        window.blit(self.stone,platform_rect)

我的完整代码

import pygame
pygame.init()
# The screen width and height
window = pygame.display.set_mode((700,500))
# The name of the screen
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
        # Animation for player
        self.idle = [pygame.image.load("HRI1.png"),
                    pygame.image.load("HRI2.png"),
                    pygame.image.load("HRI3.png"),
                    pygame.image.load("HRI4.png"),]
                    
        self.idlel = [pygame.image.load("HLI1.png"),
                      pygame.image.load("HLI2.png"),
                      pygame.image.load("HLI3.png"),
                      pygame.image.load("HLI4.png")]
        
        self.walk = [pygame.image.load("HRW1.png"),
                     pygame.image.load("HRW2.png"),
                     pygame.image.load("HRW3.png"),
                     pygame.image.load("HRW4.png"),
                     pygame.image.load("HRW5.png"),
                     pygame.image.load("HRW6.png")]
        
        self.lwalk = [pygame.image.load("HLW1.png"),
                      pygame.image.load("HLW2.png"),
                      pygame.image.load("HLW3.png"),
                      pygame.image.load("HLW4.png"),
                      pygame.image.load("HLW5.png"),
                      pygame.image.load("HLW6.png")]
        
        self.jump = [pygame.image.load("HRJ1.png"),
                     pygame.image.load("HRJ2.png"),
                     pygame.image.load("HRJ3.png")]

        self.ljump = [pygame.image.load("HLJ1.png"),
                     pygame.image.load("HLJ2.png"),
                     pygame.image.load("HLJ3.png")]

        self.direction = "idle"
        self.direction = "idlel"
        self.direction = "walk"
        self.direction = "lwalk"
        self.direction = "jump"
        self.direction = "ljump"
        
        self.speed =5
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.rect = pygame.Rect(x,y,width,height)
        self.next_frame_time = 0
        self.fps = 10
        self.clock = pygame.time.Clock
        self.anim_index = 0
        self.idle = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.idle]
        self.idlel = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.idlel]
        self.walk = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.walk]
        self.lwalk = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.lwalk]
        self.jump = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.jump]
        self.ljump = [pygame.transform.scale(image,(image.get_width()*3,image.get_width()*3))for image in self.ljump]

    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        if self.direction == "idle":
            image_list = self.idle
        if self.direction == "idlel":
            image_list = self.idlel
        if self.direction == "walk":
            image_list = self.walk
        if self.direction == "lwalk":
            image_list = self.lwalk
        if self.direction == "jump":
            image_list = self.jump
        if self.direction == "ljump":
            image_list = self.ljump
        # Is it time to show next frame?
        time_now = pygame.time.get_ticks()
        if (time_now > self.next_frame_time):
            # time till the nect frame
            inter_time_delay = 1000 // self.fps
            self.next_frame_time = time_now + inter_time_delay
            # Show the next frame
            self.anim_index += 1
            if self.anim_index >= len(image_list):
                self.anim_index = 0

        if self.anim_index >= len(image_list):
            self.anim_index = 0
        player_image = image_list[self.anim_index]

        player_rect = player_image.get_rect(center = self.get_rect().center)
        player_rect.centerx += 3
        player_rect.centery -= 3
        window.blit(player_image, player_rect)
    
            
        
        

# Platform class
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.stone = pygame.image.load("stone.png")
        self.stone = pygame.transform.scale(self.stone,(self.stone.get_width()+28,self.stone.get_height()+28))
        self.rect = pygame.Rect(x,y,width,height)
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)

        platform_rect = self.stone.get_rect(center = self.get_rect().center)
        platform_rect.centerx += 2
        platform_rect.centery += 0
        window.blit(self.stone,platform_rect)
        



        

# The color of **** hitbox
white = (255,255,255)

# Player size,cords, and hitbox color
playerman = Player(255,255,40,40,white)

# Platform size,cords,and hitbox color
platform1 = Platform(255,255,40,40,white)



# Platform list
platforms = [platform1]


#redrawing window so player and other stuff dose not make the screen a mess
def redrawwindow():
    window.fill((0,0,0))

    playerman.draw()

    # making it so I do not have to draw every platform 1 by 1
    for Platform in  platforms:
        Platform.draw()

    
# Fps of the game
fps = 30
clock = pygame.time.Clock()

# Runing the game/ the main loop
run = True
while run:
    # Making game run with fps
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False




        

    



   
        
    

    # telling what to do when we say the word 'key'
    keys = pygame.key.get_pressed()

    # Shortining playerman.x and playerman.y
    px,py = playerman.x,playerman.y



    # player 'A' movment
    if keys[pygame.K_a] and px > playerman.speed:
        px -= playerman.speed
        playerman.direction = "lwalk"
        
    # player 'D' movment
    elif keys[pygame.K_d] and playerman.x < 700 - playerman.width - playerman.speed:
        px += playerman.speed
        playerman.direction = "walk"

 
    # Playing idle animation
    else:
        if playerman.direction == "walk":
            playerman.direction = "idle"
        else:
            if playerman.direction == "lwalk":
                playerman.direction = "idlel"

    
                    

    
    
    
    # player 'W' movment
    if keys[pygame.K_w] and py > playerman.speed:
        py -= playerman.speed

    # player 'S' movment
    if keys[pygame.K_s] and py < 500 - playerman.height - 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 player_rect.collidelist(platform_rect_list) < 0:
        playerman.x = px


    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 0.5
        playerman.isJump = False
        

    # For player to get on top of platform
        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
 

            # Making it so player wont fall out of the map 
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.JumpCount = 10
                playerman.y = 500 - playerman.height

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

        # What will happen when player jumps
    else:
        if playerman.JumpCount >= 0:    
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10


    # redrawing the window
    redrawwindow()
    # updating the game
    pygame.display.update()
# quiting the game
pygame.quit()



如果您希望玩家在跳跃时从平台的底部边缘反弹,您必须在playerman.isJump的情况下这样做。 测试玩家是否与平台发生碰撞。 将玩家的 position 限制在平台底部( playerman.y = platform.rect.bottom )并停止跳跃:

while run:
    # [...]

    if not playerman.isJump:
        # [...]


        # What will happen when player jumps
    else:

        collideBottom = False
        for platform in platforms:
            if (playerman.get_rect().colliderect(platform.rect) and 
                playerman.y + playerman.height > platform.rect.bottom):
                
                collideBottom = True
                playerman.y = platform.rect.bottom
                break

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

    # redrawing the window
    redrawwindow()

一旦发生碰撞,请尝试关闭.isJump

    # Player jumping
    if collide:
        playerman.isJump = False
        playerman.fall = 1

此外,它不会改变任何东西,但.isJump行是多余的。

if not playerman.isJump:  ##  a.k.a.  playerman.isJump == False
    playerman.y += playerman.fall
    playerman.fall += 0.5
    ##  playerman.isJump = False  <--  omit this line.  already False

编辑:
摆脱: playerman.y = Platform.rect.top - playerman.height

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM