我的第一个Python程序遇到了障碍,我认为我没有解决自己的知识。

这是二维表面上可控的太空飞船,我想增加动量/惯性
我有它,所以当发动机停止时,船继续沿原先的方向行驶。
但是,我只能使其“捕捉”到新的向量,使其立即旋转面对。

我想发生的是,惯性矢量在加速时会像新的旋转加速度一样缓慢地与新的指向矢量对齐? (我对数学不太热)-我可以旋转惯性矢量,但是我需要以某种方式将其与新的指向矢量进行比较,然后根据它们之间的差异对其进行修改?

如果有人可以建议我如何着手解决这个问题,那就太好了-我怀疑我是从完全错误的角度来解决这个问题的。
这是一些代码(请谨慎一点!)

使用的精灵是这样的:-ship.png

import pygame
import sys
from math import sin, cos, pi, atan2
from pygame.locals import *
import random
from random import randint
from pygame.math import Vector2
import operator

"""solar system generator"""
"""set screen size and center and some global namespace colors for ease of use"""
globalalpha = 255
screenx = int(1200)
screeny = int(700)
centerx = int(screenx / 2)
centery = int(screeny / 2)
center = (centerx, centery)
black = (  0,   0,   0)
white = (255, 255, 255)
red = (209,   2,  22)
TRANSPARENT = (255,0,255)
numstars = 150
DISPLAYSURF = pygame.display.set_mode((screenx, screeny), 0, 32)
clock = pygame.time.Clock()
globaltimefactor = 1
shipimage = pygame.image.load('ship.png').convert()
DISPLAYSURF.fill(black)
screen_rect = DISPLAYSURF.get_rect()


class Playership(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.imageorig = pygame.image.load('ship.png').convert_alpha()
        self.startpos = (screen_rect.center)
        self.image = self.imageorig.copy()
        self.rect = self.imageorig.get_rect(center=self.startpos)
        self.angle = 0
        self.currentposx = 600
        self.currentposy = 350
        self.tuplepos = (self.currentposx, self.currentposy)
        self.speed = 1
        self.rotatespeed = 1.5
        self.initialvec = (600, 0)
        self.destination = 0
        self.anglechange = 0
        self.currentspeed = 0
        self.maxspeed = 5
        self.engineon = False
        self.newvec = (600, 0)
        self.newdestination = 0
        self.acceleration = 0.015
        self.inertiaspeed = 0
        self.transitionalvec = self.initialvec

    def get_angleafterstopping(self):
        newvec = self.initialvec
        self.newvec = newvec

    def get_destinationafterstopping(self):
        x_dist = self.newvec[0] - self.tuplepos[0]
        y_dist = self.newvec[1] - self.tuplepos[1]
        self.newdestination = atan2(-y_dist, x_dist) % (2 * pi)

    def get_destination(self):
        x_dist = self.initialvec[0] - self.tuplepos[0]
        y_dist = self.initialvec[1] - self.tuplepos[1]
        self.destination = atan2(-y_dist, x_dist) % (2 * pi)

    def moveship(self):
        if self.engineon is True:
            self.currentspeed = self.currentspeed + self.acceleration
            if self.currentspeed > self.maxspeed:
                self.currentspeed = self.maxspeed
            elif self.currentspeed < 0:
                self.currentspeed = 0
            self.inertiaspeed = self.currentspeed
        elif self.engineon is False:
            self.currentposx = self.currentposx + (cos(self.newdestination) * self.inertiaspeed * globaltimefactor)
            self.currentposy = self.currentposy - (sin(self.newdestination) * self.inertiaspeed * globaltimefactor)
            self.tuplepos = (self.currentposx, self.currentposy)
            self.rect.center = self.tuplepos
            return
        self.get_destination()
        self.currentposx = self.currentposx + (cos(self.destination) * self.currentspeed * globaltimefactor)
        self.currentposy = self.currentposy - (sin(self.destination) * self.currentspeed * globaltimefactor)
        self.tuplepos = (self.currentposx, self.currentposy)
        self.rect.center = self.tuplepos

    def rotateship(self, rotation):
        self.anglechange = self.anglechange - (rotation * self.rotatespeed * globaltimefactor)
        self.angle += (rotation * self.rotatespeed * globaltimefactor)
        self.image = pygame.transform.rotate(self.imageorig, self.angle)
        self.rect = self.image.get_rect(center=self.rect.center)
        initialvec = self.tuplepos + Vector2(0, -600).rotate(self.anglechange * globaltimefactor)
        initialvec = int(initialvec.x), int(initialvec.y)
        self.initialvec = initialvec




myship = Playership()
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(myship)
firsttimedone = False


def main():

    done = False
    while not done:
        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_LEFT]:
            myship.rotateship(1)
        if keys_pressed[pygame.K_RIGHT]:
            myship.rotateship(-1)
        if keys_pressed[pygame.K_UP]:
            myship.engineon = True
            myship.moveship()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit();
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:
                    myship.engineon = False
                    myship.currentspeed = 0
                    myship.get_angleafterstopping()
                    myship.get_destinationafterstopping()
        DISPLAYSURF.fill(black)
        all_sprites_list.update()
        all_sprites_list.draw(DISPLAYSURF)
        pygame.draw.line(DISPLAYSURF, white, (myship.tuplepos), (myship.initialvec))
        pygame.draw.line(DISPLAYSURF, red, (myship.tuplepos), (myship.newvec))
        pygame.display.flip()
        if myship.engineon is False:
            myship.moveship()
        clock.tick(50)
        pygame.display.set_caption("fps: " + str(clock.get_fps()))


if __name__ == '__main__':
    pygame.init()
    main()
    pygame.quit(); sys.exit();


编辑:

我修复了它:只需要对向量有更好的了解
船舶以加速度和速度开始,均以向量表示。

self.position = vec(screenx / 2, screeny / 2)
self.vel = vec(0, 0)
self.acceleration = vec(0, -0.2)  # The acceleration vec points upwards from the starting ship position


旋转船使矢量旋转到位

self.acceleration.rotate_ip(self.angle_speed)
self.angle += self.angle_speed
self.image = pygame.transform.rotate(self.imageorig, -self.angle)
self.rect = self.image.get_rect(center=self.rect.center)


加速是这样的:

self.vel += self.acceleration * self.enginepower * globaltimefactor


更新位置:

self.position += self.vel
self.rect.center = self.position


我使它变得比原来更困难,必须保持恒定的速度,直到旋转的加速度矢量对其起作用。我不知道如何将向量加在一起等等。

最佳答案

我修复了它:只需要对向量有更好的了解
船舶以加速度和速度开始,均以向量表示。

self.position = vec(screenx / 2, screeny / 2)
self.vel = vec(0, 0)
self.acceleration = vec(0, -0.2)  # The acceleration vec points upwards from the starting ship position


旋转船使矢量旋转到位

self.acceleration.rotate_ip(self.angle_speed)
self.angle += self.angle_speed
self.image = pygame.transform.rotate(self.imageorig, -self.angle)
self.rect = self.image.get_rect(center=self.rect.center)


加速是这样的:

self.vel += self.acceleration * self.enginepower * globaltimefactor


更新位置:

self.position += self.vel
self.rect.center = self.position


我使它变得比原来更困难,必须保持恒定的速度,直到旋转的加速度矢量对其起作用。我不知道如何将向量加在一起等等。

09-10 01:46