我正在使用Python和Pygame开发一个驾驶模拟器。但是,我在处理汽车方面遇到了一些麻烦。每当我转弯时,汽车似乎漂浮在冰上或潮湿的路面上。有没有简单的方法可以防止这种情况发生?
从代码中摘录:
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
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.hit_rect = PLAYER_HIT_RECT
self.hit_rect.center = self.rect.center
self.vel = vec(0,0)
self.acc = vec(0,0)
self.pos = vec(x, y) * TILESIZE
self.rot = 0
def get_keys(self): #Where all the keypresses are detected and assigned to a feature e.g. movement.
self.rot_speed = 0
self.acc = vec(0, 0)
keys = pg.key.get_pressed()
if keys[pg.K_w]:
self.acc = vec(PLAYER_SPEED, 0).rotate(-self.rot)
if keys[pg.K_a]: # This part of the code is used to reverse the functions of the A and D key, so when you want to reverse, they actually rotate the sprite in the correct direction.
self.rot_speed = PLAYER_ROT_SPEED
if keys[pg.K_d]:
self.rot_speed = -PLAYER_ROT_SPEED
if keys[pg.K_s]:
self.acc = vec(-PLAYER_SPEED /5, 0).rotate(-self.rot)
self.acc += self.vel * PLAYER_FRICTION
self.vel += self.acc
self.pos += self.vel+ 0.02 * self.acc
self.rect.center = self.pos
感谢您的帮助! (某些注释不正确,因为它来自较旧版本的游戏)