forked from gonnzo/pymiento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagario.pyde
140 lines (101 loc) · 3.87 KB
/
agario.pyde
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
#########################################################
#
# The Pymiento Project
# Idea: Agar.io simulation
# Description: Bigger balls eat the smaller ones. The red balls, the splitters, blow white balls up keeping similar mass.
# Autor: Iván Gonzalo Moyano Pérez
#
#########################################################
class ballUniverse(object):
def __init__(self):
self.Balls = []
self.amount = 30
self.splitter_amount = 5
for i in range(0, self.amount):
self.Balls.append(Ball(self))
for i in range(0, self.splitter_amount):
self.Balls.append(Ball(self, 'splitter', '', '', 20, 0))
class Ball(object):
def __init__(self, parent, splitter='', x='', y='', diam='', safe=''):
self.direction = [-1, 1]
self.parent = parent
self.diam = random(20, 50)
self.step = random(2, 3)
self.dir_x = self.direction[int(random(0, 2))]
self.dir_y = self.direction[int(random(0, 2))]
self.deg_x = random(0, 2)
self.deg_y = random(0, 2)
self.x = random(50, width)
self.y = random(50, height)
self.id = random(0, 10000)
self.splitter = splitter
self.safe = 50
self.growTo = 0
if(x != ''):
self.x = x
if(y != ''):
self.y = y
if(diam != ''):
self.diam = diam
if(safe != ''):
self.safe = safe
def isCollision(self, b1, b2):
return sqrt((b1.x - b2.x) * (b1.x - b2.x) + (b1.y - b2.y) * (b1.y - b2.y)) < b1.diam / 2 + b2.diam / 2
def splitBall(self, b1):
diam = b1.diam
for n in range(1, 100):
nro = n
if diam / nro < 20:
break
x = self.x
y = self.y
diam = diam / nro
for j in range(0, nro):
self.parent.Balls.append(Ball(self.parent, '', x, y, diam, 100))
self.parent.Balls.remove(b1)
background("#ff0000")
def run(self):
if(self.safe > 0):
self.safe -= 1
for i in range(0, len(self.parent.Balls)):
if(self.id != self.parent.Balls[i].id and self.isCollision(self, self.parent.Balls[i]) and self.safe == 0 and self.parent.Balls[i].safe == 0 and self.splitter == '' and self.parent.Balls[i].splitter == ''):
if(self.parent.Balls[i].diam < self.diam):
self.growTo += self.parent.Balls[i].diam
self.parent.Balls.remove(self.parent.Balls[i])
else:
self.parent.Balls[i].growTo += self.diam
self.parent.Balls.remove(self)
break
elif(self.id != self.parent.Balls[i].id and self.safe == 0 and self.parent.Balls[i].safe == 0 and self.isCollision(self, self.parent.Balls[i])):
self.splitBall(
self.parent.Balls[i] if(self.splitter == 'splitter') else self)
break
fill(("#333333" if self.safe > 0 else (
"#ffffff" if self.splitter == '' else '#ff0000')))
ellipse(self.x, self.y, self.diam, self.diam)
if(self.growTo > 0):
self.growTo -= 2
self.diam += 2
if(self.x > width - self.diam / 2):
self.dir_x = -1
if(self.y > height - self.diam / 2):
self.dir_y = -1
if(self.x < self.diam / 2):
self.dir_x = 1
if(self.y < self.diam / 2):
self.dir_y = 1
self.x = self.x + \
((self.step + self.safe / 10) * self.deg_x) * self.dir_x
self.y = self.y + \
((self.step + self.safe / 10) * self.deg_y) * self.dir_y
def setup():
global bU
fullScreen(P2D, 2)
noStroke()
bU = ballUniverse()
def draw():
background(0)
for i in range(0, len(bU.Balls)):
if(i >= len(bU.Balls)):
break
bU.Balls[i].run()