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

角色慢慢跌落地面而不是完全停止

如何解决角色慢慢跌落地面而不是完全停止

我的角色正在慢慢跌入地面,而不是完全停下来。冲突代码在第四个函数def update()中。我确实有第二个文件,主要处理了sprite冲突,这是self.player导入的来源,但对问题影响不大。

import pygame as pg
from settings import *
from sprites import *

class Game:
 def __init__(self):
    # ==============================================================================================================
    # initialize game window,etc
    pg.init()
    pg.mixer.init()
    self.screen = pg.display.set_mode(SCREEN)
    pg.display.set_caption(TITLE)
    self.clock = pg.time.Clock()
    self.spritesheet = Spritesheet(Mario)
    self.running = True

def new(self):
    # ==============================================================================================================
    # start a new game
    self.all_sprites = pg.sprite.layeredUpdates()
    self.player = pg.sprite.Group()
    self.platforms = pg.sprite.Group()
    self.player = Player(self)
    self.level()
    self.run()

def run(self):
    # ==============================================================================================================
    # Game Loop
    self.playing = True
    while self.playing:
        self.clock.tick(FPS)
        self.events()
        self.update()
        self.draw()


def update(self):
    # Game Loop - Update
    self.all_sprites.update()

    # ==============================================================================================================
    # COLLISION BETWEEN PLAYER AND PLATFORMS GROUP
    hits = pg.sprite.spritecollide(self.player,self.platforms,False)

    # ==============================================================================================================
    # GOING RIGHTWARDS COLLISION
    if self.player.vel.x > 0:
        if hits:
            for hit in hits:
                if self.player.rect.right > hit.rect.right:
                    self.player.rect.right = hit.rect.right
                    self.player.vel.x = 0
                    self.player.rect.midbottom = self.player.pos

    # ==============================================================================================================
    # GOING LEFT WARDS COLLISION
    elif self.player.vel.x < 0:
        if hits:
            for hit in hits:
                if self.player.rect.left < hit.rect.left:
                    self.player.rect.right = hit.rect.left
                    self.player.vel.x = 0
                    self.player.rect.midbottom = self.player.pos

    # ==============================================================================================================
    # ASCENDING COLLISION
    elif self.player.vel.y < 0:
        if hits:
            for hit in hits:
                if self.player.rect.top > hit.rect.bottom:
                    self.player.rect.top = hit.rect.bottom
                    self.player.vel.y = 0
                    self.player.jumping = False
                    self.player.rect.midbottom = self.player.pos

    # ==============================================================================================================
    # FALLING COLLISION
    elif self.player.vel.y > 0:
        if hits:
            for hit in hits:
                if hit.rect.top:
                    self.player.rect.bottom = hit.rect.top
                    self.player.vel.y = 0
                    self.player.jumping = False
                    self.player.rect.midbottom = self.player.pos



def events(self):
    # Game Loop - events
    for event in pg.event.get():
        # check for closing window
        if event.type == pg.QUIT:
            self.playing = False
            self.running = False
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_SPACE:
                self.player.jump()
        if event.type == pg.KEYUP:
            if event.key == pg.K_SPACE:
                self.player.jump_cut()

def level(self):
    # ==============================================================================================================
    # MAKE LEVEL
    self.levelsss = level
    y=0
    for row in self.levelsss:
        x = 0
        for col in row:
            if col == "1":
                block(self,x,y)
            x += 1
        y+= 1


def draw(self):
    # ==============================================================================================================
    # DRAW SPRITES
    self.screen.fill(DEEP_SKY_BLUE)
    self.all_sprites.draw(self.screen)
    # *after* drawing everything,flip the display
    pg.display.flip()

def show_start_screen(self):
    # game splasrh/start screen
    pass

def show_go_screen(self):
    # game over/continue
    pass

g = Game()
g.show_start_screen()
while g.running:
  g.new()
  g.show_go_screen()

pg.quit()

解决方法

您必须对垂直和水平运动实施单独的碰撞测试。在您的代码中,如果播放器未沿x轴移动,则会执行y轴的碰撞测试:

if self.player.vel.x > 0:
   # [...]
elif self.player.vel.x < 0:
   # [...]
elif self.player.vel.y < 0:
   # [...]
elif self.player.vel.y > 0:
   # [...]

将第二个elif更改为if

# test for collision in x direction
if self.player.vel.x > 0:
    # [...]
elif self.player.vel.x < 0:
    # [...]

# test for collision in y direction
if self.player.vel.y < 0:
    # [...]
elif self.player.vel.y > 0:
    # [...]

如果水平移动非常大,则可以通过在x方向上处理碰撞后重复碰撞测试来实现改进:

hits = pg.sprite.spritecollide(self.player,self.platforms,False)
if hits:
    if self.player.vel.x > 0:
        # [...]
    elif self.player.vel.x < 0:
        # [...]


hits = pg.sprite.spritecollide(self.player,False)
if hits:
    if self.player.vel.y < 0:
        # [...]
    elif self.player.vel.y > 0:
        # [...]

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