如何使玩家的直肠在我的敌人的侧面和底部碰撞?Pygame

哈比卜·伊斯梅尔|

我试图让我的玩家rect与我的敌人rect在侧面和底部发生碰撞,因此玩家rect不会去扔敌人的rect,但是我不确定为什么它会不断地传送我,使其完全无法工作VIDEo <<在视频中看到任何帮助,我都需要帮助,当我在侧面碰撞时,不要摔或爬

我的碰撞部分


    playerman.isJump = False
    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



没有图像,只有正方形,您可以测试代码

import pygame,random

window = pygame.display.set_mode((800,800), pygame.NOFRAME)

# player factory
class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.speed = 4
        self.fall = 0
        self.isJump = False
        self.jumpCount = 10
        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)

# player factory
class platform:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        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)


white = (255,255,255)
platform1 = platform(590,590,60,60,white)

platforms = [platform1]

# make the player
pink = (205,105,205)
playerman = player(300,300,60,60,pink)



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

def keyevents():
    if keys[pygame.K_w]:
        playerman.y -= playerman.speed

    if keys[pygame.K_s]:
        playerman.y += playerman.speed

    if keys[pygame.K_d]:
        playerman.x += playerman.speed

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

    if keys[pygame.K_SPACE]:
        playerman.isJump = True



                
def jump():
    if playerman.isJump:
        if playerman.jumpCount >= -10:
            neg = 1
            if playerman.jumpCount < 0:
                neg = -1
            playerman.y -= playerman.jumpCount**2 * 0.5 * neg
            playerman.jumpCount -= 1
        else:
            playerman.isJump = False
            playerman.jumpCount = 10

    collide = False        # this makes the player fall down up to 
         # move the player
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False
                    
        if playerman.rect.bottom >= 680:
            collide = True
            playerman.isJump = False
            playerman.JumpCount = 10
            playerman.y = 680 - playerman.height

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




# redraw the window
def redraw():
    playerman.draw()
    platform.draw()
# main loop
runninggame = True
while runninggame:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runningggame = False

    # key event
    keys = pygame.key.get_pressed()

    # this is the key events
    keyevents()

    jump() # JUMP AND COLLISION on the grawn

    playerman.isJump = False
    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





    window.fill((0,0,0))
    redraw()
    pygame.display.update()
pygame.quit()

我只是在寻找基本的碰撞方式,我左右左右分别触碰敌人,不会摔到底部,如果我在顶部发生碰撞,它会让我的玩家站在上面

M陈3

我终于成功地提出了解决方案!这里有一些代码可以使一切正常工作。如果您愿意,我可以在聊天中向您描述我所做的更改及其所做的事情。请注意,我是在Python 3中编写的,因此您可能需要进行一些更改才能使其在Python 2中运行。

import pygame,random

window = pygame.display.set_mode((800,800), pygame.NOFRAME)

# player factory
class player:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.moveleft = True
        self.moveright = True
        self.color = color
        self.speed = 4
        self.fall = 0
        self.isJump = False
        self.jumpCount = 10
        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)

# player factory
class platform:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        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)


white = (255,255,255)
platform1 = platform(590,590,60,60,white)

platforms = [platform1]

# make the player
pink = (205,105,205)
playerman = player(300,300,60,60,pink)



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

def keyevents():
    if keys[pygame.K_w]:
        playerman.y -= playerman.speed

    if keys[pygame.K_s]:
        playerman.y += playerman.speed

    if keys[pygame.K_d]:
        if playerman.moveright:
            playerman.x += playerman.speed    

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

    if keys[pygame.K_SPACE]:
        playerman.isJump = True



                
def jump():
    if playerman.isJump:
        if playerman.jumpCount >= -10:
            neg = 1
            if playerman.jumpCount < 0:
                neg = -1
            playerman.y -= playerman.jumpCount**2 * 0.5 * neg
            playerman.jumpCount -= 1
        else:
            playerman.isJump = False
            playerman.jumpCount = 10

    collide = False        # this makes the player fall down up to 
         # move the player
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False
                    
        if playerman.rect.bottom >= 680:
            collide = True
            playerman.isJump = False
            playerman.JumpCount = 10
            playerman.y = 680 - playerman.height

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




# redraw the window
def redraw():
    playerman.draw()
    platform.draw()
# main loop
runninggame = True
while runninggame:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            runningggame = False

    # key event
    keys = pygame.key.get_pressed()

    # this is the key events
    keyevents()

    jump() # JUMP AND COLLISION on the grawn

    playerman.isJump = False
    collide = False
    for platform in platforms:
        if playerman.rect.colliderect(platform.rect):
            collide = True
            playerman.isJump = False
            if (platform.rect.collidepoint(playerman.rect.right, playerman.rect.bottom) or
                platform.rect.collidepoint(playerman.rect.left, playerman.rect.bottom)):
                playerman.y = platform.rect.top - playerman.height + 1
                playerman.moveright = True
                playerman.moveleft = True
            
            if (platform.rect.collidepoint(playerman.rect.right, playerman.rect.top) or
                platform.rect.collidepoint(playerman.rect.right, playerman.rect.bottom - 10)):
                playerman.moveright = False
            elif (platform.rect.collidepoint(playerman.rect.left, playerman.rect.top) or
                  platform.rect.collidepoint(playerman.rect.left, playerman.rect.bottom - 10)):
                playerman.moveleft = False
        else:
            playerman.moveright = True
            playerman.moveleft = True
                
                

    window.fill((0,0,0))
    redraw()
    pygame.display.update()
pygame.quit()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当玩家与敌人发生碰撞时如何阻止敌人旋转

来自分类Dev

Pygame碰撞不断传送我的玩家如何解决?

来自分类Dev

如何让敌人跟随pygame中的玩家?

来自分类Dev

如何让敌人跟随 Pygame 中的玩家?

来自分类Dev

我如何让敌人向玩家移动并预测其在pygame中的路径

来自分类Dev

pygame敌人向玩家的2维运动,如何计算x和y速度?

来自分类Dev

pygame敌人向玩家的2维运动,如何计算x和y速度?

来自分类Dev

JS打砖块碰撞检测(侧面与底部和顶部)

来自分类Dev

当玩家不再碰撞时,敌人停止移动

来自分类Dev

玩家与敌人碰撞时发生错误

来自分类Dev

当我的枪旋转时,我该如何使我的直肠跟随我的枪尖?Pygame

来自分类Dev

与pygame.sprite.spritecollide()的敌人碰撞

来自分类Dev

让我的敌人走向我的玩家C ++

来自分类Dev

玩家与敌人之间无法进行碰撞检测

来自分类Dev

我如何只用1个坐标跟随我的玩家位置来移动敌人

来自分类Dev

我如何在pygame中产生多个敌人

来自分类Dev

如何将敌人移向移动的玩家?

来自分类Dev

如何阻止敌人立即杀死玩家?

来自分类Dev

我试图统一检查子弹和敌人的碰撞情况,但无法正常工作

来自分类Dev

Pygame 简单运动:敌人模仿玩家运动并回溯步骤

来自分类Dev

如何在Java中高效地编码大量/多个敌人的生成和碰撞检测

来自分类Dev

当与敌人不起作用pygame碰撞时减负生命

来自分类Dev

当播放器不再碰撞时,敌人停止移动。Pygame

来自分类Dev

当与敌人不起作用pygame碰撞时减负生命

来自分类Dev

玩家与正方形的碰撞和相交

来自分类Dev

不与平台Pygame碰撞时,玩家不会掉下

来自分类Dev

为什么我的敌人的Healthbar与子弹碰撞后无法收缩?

来自分类Dev

时间和敌人出现的问题(Python,Pygame)

来自分类Dev

玩家总是与敌人接触

Related 相关文章

  1. 1

    当玩家与敌人发生碰撞时如何阻止敌人旋转

  2. 2

    Pygame碰撞不断传送我的玩家如何解决?

  3. 3

    如何让敌人跟随pygame中的玩家?

  4. 4

    如何让敌人跟随 Pygame 中的玩家?

  5. 5

    我如何让敌人向玩家移动并预测其在pygame中的路径

  6. 6

    pygame敌人向玩家的2维运动,如何计算x和y速度?

  7. 7

    pygame敌人向玩家的2维运动,如何计算x和y速度?

  8. 8

    JS打砖块碰撞检测(侧面与底部和顶部)

  9. 9

    当玩家不再碰撞时,敌人停止移动

  10. 10

    玩家与敌人碰撞时发生错误

  11. 11

    当我的枪旋转时,我该如何使我的直肠跟随我的枪尖?Pygame

  12. 12

    与pygame.sprite.spritecollide()的敌人碰撞

  13. 13

    让我的敌人走向我的玩家C ++

  14. 14

    玩家与敌人之间无法进行碰撞检测

  15. 15

    我如何只用1个坐标跟随我的玩家位置来移动敌人

  16. 16

    我如何在pygame中产生多个敌人

  17. 17

    如何将敌人移向移动的玩家?

  18. 18

    如何阻止敌人立即杀死玩家?

  19. 19

    我试图统一检查子弹和敌人的碰撞情况,但无法正常工作

  20. 20

    Pygame 简单运动:敌人模仿玩家运动并回溯步骤

  21. 21

    如何在Java中高效地编码大量/多个敌人的生成和碰撞检测

  22. 22

    当与敌人不起作用pygame碰撞时减负生命

  23. 23

    当播放器不再碰撞时,敌人停止移动。Pygame

  24. 24

    当与敌人不起作用pygame碰撞时减负生命

  25. 25

    玩家与正方形的碰撞和相交

  26. 26

    不与平台Pygame碰撞时,玩家不会掉下

  27. 27

    为什么我的敌人的Healthbar与子弹碰撞后无法收缩?

  28. 28

    时间和敌人出现的问题(Python,Pygame)

  29. 29

    玩家总是与敌人接触

热门标签

归档