-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVectorClass.py
More file actions
48 lines (40 loc) · 1.57 KB
/
VectorClass.py
File metadata and controls
48 lines (40 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
###################################################################################
###################################################################################
# Sprite Movement Towards Target Example
# programed/documented by Mad Cloud Games
# contact Mad Cloud Games @ madcloudgames@gmail.com with any comments, questions, or changes
#
# This project is released under the GNU General Public License V3
# This code is open source
# We would love to know what it gets used for
###################################################################################
###################################################################################
import pygame, math
from pygame.locals import *
class Vector():
'''
Class:
creates operations to handle vectors such
as direction, position, and speed
'''
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self): # used for printing vectors
return "(%s, %s)"%(self.x, self.y)
def __getitem__(self, key):
if key == 0:
return self.x
elif key == 1:
return self.y
else:
raise IndexError("This "+str(key)+" key is not a vector key!")
def __sub__(self, o): # subtraction
return Vector(self.x - o.x, self.y - o.y)
def length(self): # get length (used for normalize)
return math.sqrt((self.x**2 + self.y**2))
def normalize(self): # divides a vector by its length
l = self.length()
if l != 0:
return (self.x / l, self.y / l)
return None