简体   繁体   English

Pygame-旋转和移动宇宙飞船(多边形)

[英]Pygame - Rotate and move a spaceship (Polygon)

Ok, I've been working all day on this and I haven't found the logic. 好的,我整天都在为此工作,但我还没有找到逻辑。

I wanna make a classic style asteroids game, and I'm starting with the spaceship. 我想做一个经典风格的小行星游戏,我从飞船开始。

What I did was draw some lines with the shape of a spaceship: 我所做的就是画出一些形状像飞船的线条:

import pygame
import colors
from  helpers import *

class Ship :
    def __init__(self, display, x, y) :
        self.x = x
        self.y = y
        self.width = 24
        self.height = 32
        self.color = colors.green
        self.rotation = 0
        self.points = [
            #A TOP POINT
            (self.x, self.y - (self.height / 2)),
            #B BOTTOM LEFT POINT
            (self.x - (self.width / 2), self.y + (self.height /2)),
            #C CENTER POINT
            (self.x, self.y + (self.height / 4)),
            #D BOTTOM RIGHT POINT
            (self.x + (self.width / 2), self.y + (self.height / 2)),
            #A TOP AGAIN
            (self.x, self.y - (self.height / 2)),
            #C A NICE LINE IN THE MIDDLE
            (self.x, self.y + (self.height / 4)),
        ]

    def move(self, strdir) :
        dir = 0
        if strdir == 'left' :
            dir = -3
        elif strdir == 'right' :
            dir = 3

        self.points = rotate_polygon((self.x, self.y), self.points, dir)

    def draw(self, display) :
        stroke = 2
        pygame.draw.lines(display, self.color, False, self.points, stroke)

The ship looks like this: 船看起来像这样:

在此处输入图片说明

Now important things to know: 现在要知道的重要事项:

The tuple (self.x, self.y) is the middle of the spaceship. 元组(self.x,self.y)在太空飞船的中间。

Using this function I managed to rotate (spin) it on command using the keys A and D 使用此功能,我设法使用A和D键在命令上旋转(旋转)它

def rotate_polygon(origin, points, angle) :
    angle = math.radians(angle)
    rotated_polygon = []

    for point in points :
        temp_point = point[0] - origin[0] , point[1] - origin[1]
        temp_point = (temp_point[0] * math.cos(angle) - temp_point[1] * math.sin(angle), 
                      temp_point[0] * math.sin(angle) + temp_point[1] * math.cos(angle))
        temp_point = temp_point[0] + origin[0], temp_point[1] + origin[1]
        rotated_polygon.append(temp_point)

    return rotated_polygon

在此处输入图片说明

The problem is: How can I make it move forward or backwards in the direction the spaceship is pointing? 问题是:如何使它沿着宇宙飞船指向的方向前进或后退?

OR 要么

How can I update the self.x and self.y values and update them inside the self.points list and preserve rotation? 如何更新self.x和self.y值并在self.points列表内更新它们并保留旋转?

The easiest, most general way to deal with movement and rotation would to use some vector math (can apply to 3D graphics as well). 处理运动和旋转的最简单,最通用的方法是使用一些矢量数学(也可以应用于3D图形)。 You could keep a 2D vector representing the forward direction of your ship. 您可以保留一个二维矢量,表示您的船的前进方向。 For example, if your ship starts facing upwards and your (0,0) coordinate is the top left. 例如,如果您的船开始朝上,而(0,0)坐标位于左上方。 You could do. 你可以的

self.forward = Vector2D(0, -1)  # Vector2D(x, y)

When you rotate you must rotate this vector. 旋转时,必须旋转此向量。 You can rotate using the following. 您可以使用以下方法旋转。

self.forward.x = self.forward.x * cos(angle) - self.forward.y * sin(angle)
self.forward.y = self.forward.x * sin(angle) + self.forward.y * cos(angle)

Then when you want to move the ship you can transform the ship points relative to this vector. 然后,当您要移动船舶时,可以相对于该矢量转换船舶点。 For example. 例如。

self.x += forward.x * velocity.x
self.y += forward.y * velocity.y

I would highly recommend you write a little Vector2D class which can do some of the basic operations, eg dot, cross, mult, add, sub, normalize, etc. 我强烈建议您编写一个Vector2D小类,该类可以执行一些基本操作,例如,点,叉,多重,加,乘,归一化等。

If you are familiar with matrices then these operations can become easier if you implement them using matrices instead of a system of linear equations. 如果您熟悉矩阵,那么如果您使用矩阵而不是线性方程组来实现它们,则这些操作将变得更加容易。

It seems to me that you should be able to simply do the following. 在我看来,您应该能够简单地执行以下操作。

def updatePosition(self, dx, dy):
    self.x += dx
    self.y += dy

    newPoints = []
    for (x,y) in self.points:
       newPoints.append((x+dx, y+dy))

    self.points = newPoints

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM