-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.py
More file actions
79 lines (72 loc) · 2.46 KB
/
cube.py
File metadata and controls
79 lines (72 loc) · 2.46 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pygame
from settings import ROWS, WIDTH, SQRT2
class Cube(object):
global ROWS, WIDTH, SQRT2
def __init__(self, start, dirx=1, diry=0, color=(255,0,0)):
self.pos = start
self.dirx = 1
self.diry = 0
self.color = color
def isHorizontal(self, cube, distances = True):
'''
Returns horizontal distance between 2 cubes. (If their vertical distances are same). else returns 0
'''
if self.pos[1] == cube.pos[1]:
distance = self.pos[0] - cube.pos[0]
if distances:
return distance
else:
if distance > 0: return 1
else: return -1
return 0
def isVertical(self, cube, distances = True):
'''
Returns vertical distance between 2 cubes. (If their horizontal distances are same). else returns 0
'''
if self.pos[0] == cube.pos[0]:
distance = self.pos[1] - cube.pos[1]
if distances:
return distance
else:
if distance > 0: return 1
else: return -1
return 0
def is135or315(self, cube, distances = True):
'''
Returns distance between 2 cubes. (If they lie on the line x + y = 0). else returns 0
'''
if self.pos[0] - cube.pos[0] == self.pos[1] - cube.pos[1]:
distance = SQRT2 * (self.pos[0] - cube.pos[0])
if distances:
return distance
else:
if distance > 0: return 1
else: return -1
return 0
def is45or225(self, cube, distances = True):
'''
Returns distance between 2 cubes. (If they lie on the line x - y = 0). else returns 0
'''
if self.pos[0] - cube.pos[0] == cube.pos[1] - self.pos[1]:
distance = SQRT2 * (self.pos[0] - cube.pos[0])
if distances:
return distance
else:
if distance > 0: return 1
else: return -1
return 0
def move(self, dirx, diry):
'''
Changes direction of motion of cube
'''
self.dirx = dirx
self.diry = diry
self.pos = (self.pos[0]+self.dirx, self.pos[1]+self.diry)
def draw(self, window):
'''
Draws cube on window
'''
dis = WIDTH // ROWS
i = self.pos[0]
j = self.pos[1]
pygame.draw.rect(window, self.color, (i*dis, j*dis, dis, dis))