【发布时间】:2020-08-04 19:35:17
【问题描述】:
谁能告诉我要改变什么,以便我的船使用推力机制而不是像现在这样的静态运动?
我希望运动就像在小行星中一样,在你所面对的方向上加速并且速度在某个点上达到上限,然后如果你停止加速,它的速度会慢慢降低直到停止。
rn如果我按下前进按钮,它会直接以最大速度开始移动,当我松开时立即停止。
这是我的船级代码
class Ship:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 0
self.vel = 0
self.angle = 0
def draw(self):
ship_img = pygame.image.load("sprites/ship_off.png")
ship_img_copy = pygame.transform.rotate(ship_img, self.angle)
window.blit(ship_img_copy,
(self.x - (ship_img_copy.get_width()) / 2, self.y - (ship_img_copy.get_height()) / 2))
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
ship_img = pygame.image.load("sprites/ship_on.png")
ship_img_copy = pygame.transform.rotate(ship_img, self.angle)
window.blit(ship_img_copy,
(self.x - (ship_img_copy.get_width()) / 2, self.y - (ship_img_copy.get_height()) / 2))
def move(self):
keys = pygame.key.get_pressed()
# todo acceleration and thrust mechanics
if keys[pygame.K_w]:
self.x += self.vel * cos(self.angle * (pi / 180) + (90 * pi / 180))
self.y -= self.vel * sin(self.angle * (pi / 180) + 90 * (pi / 180))
# So that if it leaves one side it comes from the other
if self.y < 0:
self.y = (self.y - self.vel) % 600
elif self.y > 600:
self.y = (self.y + self.vel) % 600
elif self.x < 0:
self.x = (self.x - self.vel) % 800
elif self.x > 800:
self.x = (self.x + self.vel) % 800
if keys[pygame.K_a]:
self.angle += 7
if keys[pygame.K_d]:
self.angle -= 7
我试过了,但我做不到,所以这是我的代码
【问题讨论】: