-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFollowingOn.py
More file actions
169 lines (142 loc) · 4.9 KB
/
Copy pathFollowingOn.py
File metadata and controls
169 lines (142 loc) · 4.9 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from math import sqrt
from time import sleep
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500, 500))
w = screen.get_width()
h = screen.get_height()
colours = [(255, 255, 255), (0, 100, 50), (100, 0, 50),
(0, 200, 100), (200, 0, 100)]
blocks = []
playerturn = 0
totalturns = 0
takenspace = []
def dist(x1, y1, x2, y2):
return sqrt(((x1 - x2)**2) + ((y1 - y2)**2))
class InvalidOperation(Exception):
pass
class InvalidIndex(Exception):
pass
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __abs__(self):
return dist(0, 0, self.x, self.y)
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y)
def __mul__(self, other):
if isinstance(other, (type(3), type(0.3))):
return Vector(self.x * other, self.y * other)
elif isinstance(other, type(Vector(0, 0))):
return (self.x * other.x) + (self.y * other.y)
else:
raise InvalidOperation
def __truediv__(self, other):
if isinstance(other, (type(3), type(0.3))):
return Vector(self.x / other, self.y / other)
else:
raise InvalidOperation
def __getitem__(self, index):
if index == 0:
return self.x
elif index == 1:
return self.y
else:
raise InvalidIndex
def __iter__(self):
yield int(self.x)
yield int(self.y)
def __str__(self):
return "Vector({}, {})".format(self.x, self.y)
def norm(self):
if abs(self) == 0:
return Vector(0, 0)
else:
return self / abs(self)
instructiondex = {K_w: Vector(0, -100),
K_a: Vector(-100, 0),
K_s: Vector(0, 100),
K_d: Vector(100, 0)}
class Block:
def __init__(self, pos, leader=-1, cursor=0):
# Vectors, pls
self.pos = pos
self.targ = pos
if leader == -1:
self.isleader = True
self.instructions = []
else:
self.isleader = False
self.instructions = leader.instructions
self.cursor = cursor
self.moved = False
self.crashed = False
self.followers = []
self.id = len(blocks)
blocks.append(self)
takenspace.append(tuple(self.pos))
def show(self, turn):
#print(self.id, turn)
turnfinished = 0
if turn == self.id:
if self.targ == self.pos:
if not self.moved:
if self.cursor < len(self.instructions):
self.targ = self.pos + instructiondex[self.instructions[self.cursor]]
outside = self.targ.x >= w or self.targ.x < 0 or self.targ.y >= h or self.targ.y < 0
if tuple(self.targ) in takenspace or outside:
self.targ = self.pos
self.crashed =
else:
takenspace.remove(tuple(self.pos))
takenspace.append(tuple(self.targ))
self.moved = True
elif not self.isleader:
self.moved = True
else:
turnfinished = 1
self.cursor += 1
self.moved = False
else:
diff = (self.targ - self.pos).norm()
if abs(diff) < 0.1:
self.pos = self.targ
else:
self.pos += diff
screen.fill(colours[0], (self.pos.x + 5, self.pos.y + 5, 90, 90))
pygame.draw.circle(screen, colours[1 + self.isleader + (self.crashed * 2)],
(int(self.pos.x) + 50, int(self.pos.y) + 50),
30)
for b in self.followers:
turnfinished += b.show(turn)
return turnfinished
firstblock = Block(Vector(0, 0))
addedblock = True
while True:
screen.fill(0)
#print(playerturn)
turninc = firstblock.show(playerturn)
print(turninc)
totalturns += turninc * (playerturn == 0)
playerturn = (playerturn + turninc) % len(blocks)
if totalturns % 10 == 0:
if not addedblock:
firstblock.followers.append(Block(Vector(0, 0), firstblock))
addedblock = True
else:
addedblock = False
pygame.display.flip()
for e in pygame.event.get():
if e.type == QUIT:
quit()
elif e.type == KEYDOWN:
if e.key == K_ESCAPE:
exit()
elif e.key in list(instructiondex.keys()):
firstblock.instructions.append(e.key)
elif e.key == K_SPACE:
firstblock.followers.append(Block(Vector(0, 0), firstblock))