微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在重力游戏中进行侧面碰撞

如何解决如何在重力游戏中进行侧面碰撞

所以我有一个重力游戏,玩家可以在其中切换重力,但我遇到了碰撞问题。当我的播放器与平台的侧面碰撞时,它只会进入平台内部,但我希望它停止与侧面碰撞。我与侧面发生了碰撞,但这种碰撞在我的游戏中不起作用,它只会让玩家冻结。我尝试过的一些东西正在使它不与我的平台碰撞以停止移动但也只是冻结了我的播放器,我也尝试在它与侧面碰撞时告诉它,但是播放器总是与平台碰撞平台.https://gyazo.com/0aa35529f5ecd7933f9e6c7c9d16f541

我尝试使用的碰撞

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 > playerman.speed:
        px -= playerman.speed

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

我的代码

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


class Platform:
    def __init__(self,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.rect = pygame.Rect(x,self.rect)


class Platform2:
    def __init__(self,self.rect)

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

# class's cordination,size,and color

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


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


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


# drawing things in main loop
def redraw():
    # background color
    window.fill((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,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:
            Platform.x += playerman.speed
        for Platform2 in platforms2:
            Platform2.x += playerman.speed

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

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

解决方法

最简单的方法是移动物体并检查它是否与某物碰撞或越界并回滚移动;游戏制作起来很有趣,所以我制作了自己的版本,希望对你有用。

import random
import pygame

FPS = 30
WX,WY = 700,500

pygame.init()
pygame.display.set_caption("Game")
window = pygame.display.set_mode((WX,WY))

class Player(pygame.Rect):
    def __init__(self,x1,y1,x2,y2,color):
        pygame.Rect.__init__(self,y2)
        self.color = color
        self.speed = 4
        self.move_hor = 0
        self.move_ver = 0

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

class Platform(pygame.Rect):
    def __init__(self,y2):
        pygame.Rect.__init__(self,y2)
        self.color = self.random_color()

    @staticmethod
    def random_color():
        color = [0,0]
        for i in range(3):
            color[i] = random.randint(80,230)
        return color

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

class Game:

    def __init__(self):
        self.player = Player(300,250,50,(255,255,255))
        self.world_obj = [
            Platform(0,WX,30),Platform(0,WY - 30,WY),Platform(300,100,305,105)
        ]
        self.clock = pygame.time.Clock()
        self.grav_sw = 0
        self.gravity = 1
        self.grav_sp = 4
        self.run = True

    def handle_events(self):
        self.player.move_hor = 0
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.run = False

        keys = pygame.key.get_pressed()
        remt = pygame.time.get_ticks()
        # A delay is required to prevent a double inversion
        if keys[pygame.K_SPACE] and remt - self.grav_sw >= 150:
            self.grav_sw = remt
            if self.gravity:
                self.gravity = 0
            else:
                self.gravity = 1

        if keys[pygame.K_a] or keys[pygame.K_LEFT]:
            self.player.move_hor = 2
        elif keys[pygame.K_d] or keys[pygame.K_RIGHT]:
            self.player.move_hor = 1

    def handle_position(self):
        if self.player.move_hor != 0:
            if self.player.move_hor == 1:
                if self.player.right + self.player.speed <= WX:
                    self.player.right += self.player.speed
            else:
                if self.player.left - self.player.speed >= 0:
                    self.player.left -= self.player.speed

        if self.gravity == 1:
            if self.player.bottom + self.grav_sp <= WY:
                self.player.bottom += self.grav_sp
        else:
            if self.player.top - self.grav_sp >= 0:
                self.player.top -= self.grav_sp

        for index in self.player.collidelistall(self.world_obj):
            o = self.world_obj[index]
            if self.gravity == 1:
                if self.player.top >= o.bottom:
                    self.player.top += self.grav_sp
                elif self.player.bottom >= o.top:
                    self.player.top -= self.grav_sp
            else:
                if self.player.top <= o.bottom:
                    self.player.top += self.grav_sp
                elif self.player.bottom <= o.top:
                    self.player.top -= self.grav_sp

            #TODO: x-axis collision

    def handle_render(self):
        window.fill((0,0))
        for obj in self.world_obj:
            obj.draw()
        self.player.draw()

    def mainloop(self):
        while self.run:
            self.handle_events()
            self.handle_position()
            self.handle_render()
            pygame.display.flip()
            self.clock.tick(FPS)
        pygame.quit()


if __name__ == '__main__':
    game = Game()
    game.mainloop()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。