pygame--向量在编程中的使用

参考资料:https://eyehere.net/2011/python-pygame-novice-professional-9/   

向量原理实例:

import math

class Vector2():
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y
    def __str__(self):
        return "(%s, %s)"%(self.x, self.y)

    @classmethod
    def from_points(cls, P1, P2):  #根据两点计算向量
        return (P2[0]-P1[0],P2[1]-P1[1])

    @classmethod
    def get_magnitude(cls,p):  #根据向量求向量的大小
        return math.sqrt(p[0]**2+p[1]**2)

    @classmethod
    def normalize(cls,p):  #求指定向量的单位向量
        L=Vector2.get_magnitude(p)
        return (p[0]/L,p[1]/L)

A = (10.0, 20.0)  #坐标点
B = (30.0, 35.0)
AB = Vector2.from_points(A, B)
print(AB)
x=Vector2.get_magnitude(AB)
print(x)
d=Vector2.normalize(AB)
print(d)

 

 

 gameobjects  是向量库--只可惜现在还不支持python3.x------支持期待中

 

 

 

 

posted @ 2020-06-15 07:22  天子骄龙  阅读(268)  评论(0编辑  收藏  举报