我的播放器碰到物体时会不断飞离地图。
在我的游戏中,玩家可以与墙壁和物体碰撞-它们都遵循相同的碰撞代码。我在屏幕的底部有一排盒子,每个盒子之间都有间隔。当玩家碰到一个盒子时,他们会不断在屏幕上拖动,直到飞离地图为止。
如果玩家在盒子和墙壁之间的小距离内奔跑,他们也会从地图上掉下来。
我知道它与我的碰撞代码有关,但是我不知道是什么。我只是按照与墙相同的碰撞规则将框添加到代码中,所以这才刚刚成为一个问题。
这是墙壁和盒子的一般碰撞代码
def collide_hit_rect(one, two):
return one.hit_rect.colliderect(two.rect)
def collide_with_walls(sprite, group, dir):
if dir == 'x':
hits = pg.sprite.spritecollide(sprite, group, False, collide_hit_rect)
if hits:
if sprite.vel.x > 0:
sprite.pos.x = hits[0].rect.left - sprite.hit_rect.width / 2
if sprite.vel.x < 0:
sprite.pos.x = hits[0].rect.right + sprite.hit_rect.width / 2
sprite.vel.x = 0
sprite.hit_rect.centerx = sprite.pos.x
if dir == 'y':
hits = pg.sprite.spritecollide(sprite, group, False, collide_hit_rect)
if hits:
if sprite.vel.y > 0:
sprite.pos.y = hits[0].rect.top - sprite.hit_rect.height / 2
if sprite.vel.y < 0:
sprite.pos.y = hits[0].rect.bottom + sprite.hit_rect.height / 2
sprite.vel.y = 0
sprite.hit_rect.centery = sprite.pos.y
玩家与之碰撞的盒子的代码看起来相同-只是说盒子而不是墙。这是我的播放器的详细信息:
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.layer = PLAYER_LAYER
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.player_img
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.hit_rect = PLAYER_HIT_RECT
self.hit_rect.center = self.rect.center
self.vel = vec(0, 0)
self.pos = vec(x, y)
self.rot = 0
self.last_shot = 0
self.health = PLAYER_HEALTH
self.item = False
self.box = True
def get_keys(self):
self.rot_speed = 0
self.vel = vec(0, 0)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT] or keys[pg.K_a]:
self.rot_speed = PLAYER_ROT_SPEED
if keys[pg.K_RIGHT] or keys[pg.K_d]:
self.rot_speed = -PLAYER_ROT_SPEED
if keys[pg.K_UP] or keys[pg.K_w]:
self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot)
if keys[pg.K_DOWN] or keys[pg.K_s]:
self.vel = vec(-PLAYER_SPEED / 2, 0).rotate(-self.rot)
if keys[pg.K_RETURN]:
if self.box == True:
#self.player.WEAPON.PLACED = True
now = pg.time.get_ticks()
if now - self.last_shot > LASER_RATE:
self.last_shot = now
dir = vec(1, 0).rotate(-self.rot)
pos = self.pos + BARREL_OFFSET.rotate(-self.rot)
Laser(self.game, pos, dir)
else:
pass
if keys[pg.K_SPACE]:
if self.item == True:
#self.player.WEAPON.PLACED = True
now = pg.time.get_ticks()
if now - self.last_shot > SHARD_RATE:
self.last_shot = now
dir = vec(1, 0).rotate(-self.rot)
pos = self.pos + BARREL_OFFSET.rotate(-self.rot)
Shard(self.game, pos, dir)
else:
pass
def update(self):
self.get_keys()
self.rot = (self.rot + self.rot_speed * self.game.dt) % 360
self.image = pg.transform.rotate(self.game.player_img, self.rot)
self.rect = self.image.get_rect()
self.rect.center = self.pos
self.pos += self.vel * self.game.dt
self.hit_rect.centerx = self.pos.x
collide_with_walls(self, self.game.walls, 'x')
self.hit_rect.centery = self.pos.y
collide_with_walls(self, self.game.walls, 'y')
self.rect.center = self.hit_rect.center
collide_with_boxs(self, self.game.boxs, 'x')
self.hit_rect.centery = self.pos.y
collide_with_boxs(self, self.game.boxs, 'y')
self.rect.center = self.hit_rect.center
我不确定我做错了什么或为什么会这样。即使击中游戏中的其他物体,如果它不是墙壁,玩家也会做这种炫目的颠簸,将自己向上推并围绕该物体,直到不再接触为止。