When you press UP you don't have to change the speed, but you have to set the acceleration:
self.vel = vec(PLAYER_SPEED, 0).rotate(-self.rot)
self.acc += vec(PLAYER_ACC, 0).rotate(-self.rot)
Add the acceleration to the velocity:
self.vel += self.acc
I recommend limiting the maximum. However, I recommend doing this separately for each direction:
max_vel = 2
self.vel[0] = max(-max_vel, min(max_vel, self.vel[0]))
self.vel[1] = max(-max_vel, min(max_vel, self.vel[1]))
Apply this to the method get_keys
:
class Player(pg.sprite.Sprite):
# [...]
def get_keys(self):
self.rot_speed = 0
self.acc = vec(0, PLAYER_GRAV)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.rot_speed = PLAYER_ROT_SPEED
if keys[pg.K_RIGHT]:
self.rot_speed = -PLAYER_ROT_SPEED
if keys[pg.K_UP]:
self.acc += vec(PLAYER_ACC, 0).rotate(-self.rot)
if keys[pg.K_SPACE]:
self.shoot()
self.vel += self.acc + self.vel * PLAYER_FRICTION
max_vel = 2
self.vel[0] = max(-max_vel, min(max_vel, self.vel[0]))
self.vel[1] = max(-max_vel, min(max_vel, self.vel[1]))
self.pos += self.vel
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…