-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblk.py
80 lines (63 loc) · 1.6 KB
/
blk.py
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
import vectormath as vmath
from random import random, randrange
import time
WORLD_HEIGHT = 100
WORLD_WIDTH = 100
MAX_ACCL = 10
class BlkWorld:
def __init__(self, height=WORLD_HEIGHT, width=WORLD_WIDTH):
self.blks = []
self.steps = 0
self.height = height
self.width = width
def createBlk(self, _id, userId):
blk = Blk(_id,userId)
self.blks.append(blk)
def step(self):
for blk in self.blks:
blk.step()
self.steps += 1
def getData(self):
data = []
for blk in self.blks:
data.append(blk.getData())
return data
class Blk:
def __init__(self, _id, userId):
self.id = _id
self.userId = userId
self.pos = vmath.Vector2(randrange(WORLD_HEIGHT), randrange(WORLD_WIDTH))
self.vel = vmath.Vector2(random() - 0.5, random()- 0.5)
self.acc = vmath.Vector2(random()- 0.5, random()- 0.5)
self.size = 1
def step(self):
self.vel += self.acc
self.pos += self.vel
if self.pos.x < 0:
self.pos.x = 0
self.vel.x = -self.vel.x
if self.pos.x > WORLD_WIDTH:
self.pos.x = WORLD_WIDTH
self.vel.x = -self.vel.x
if self.pos.y < 0:
self.pos.y = 0
self.vel.y = -self.vel.y
if self.pos.y > WORLD_WIDTH:
self.pos.y = WORLD_WIDTH
self.vel.y = -self.vel.y
# Set acceleration to 0
self.acc *= 0
def applyForce(self, force):
self.acc += vmath.Vector2(force,force)
if(self.acc > MAX_ACCL):
force = MAX_ACCL
def getData(self):
data = {'id':self.id, 'userId': self.userId, 'x': self.pos.x, 'y': self.pos.y}
return data
if __name__ == '__main__':
world = BlkWorld()
world.createBlk('test')
while True:
world.step()
print(world.getData())
time.sleep(0.05)