-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathagar.py
330 lines (274 loc) · 9.83 KB
/
agar.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import pygame,random,math
# Dimension Definitions
SCREEN_WIDTH, SCREEN_HEIGHT = (800,500)
PLATFORM_WIDTH, PLATFORM_HEIGHT = (2000,2000)
# Other Definitions
NAME = "agar.io"
VERSION = "0.2"
# Pygame initialization
pygame.init()
pygame.display.set_caption("{} - v{}".format(NAME, VERSION))
clock = pygame.time.Clock()
try:
font = pygame.font.Font("Ubuntu-B.ttf",20)
big_font = pygame.font.Font("Ubuntu-B.ttf",24)
except:
print("Font file not found: Ubuntu-B.ttf")
font = pygame.font.SysFont('Ubuntu',20,True)
big_font = pygame.font.SysFont('Ubuntu',24,True)
# Surface Definitions
MAIN_SURFACE = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
SCOREBOARD_SURFACE = pygame.Surface((95,25),pygame.SRCALPHA)
LEADERBOARD_SURFACE = pygame.Surface((155,278),pygame.SRCALPHA)
SCOREBOARD_SURFACE.fill((50,50,50,80))
LEADERBOARD_SURFACE.fill((50,50,50,80))
# Auxiliary Functions
def drawText(message,pos,color=(255,255,255)):
"""Blits text to main (global) screen.
"""
MAIN_SURFACE.blit(font.render(message,1,color),pos)
def getDistance(a, b):
"""Calculates Euclidean distance between given points.
"""
diffX = math.fabs(a[0]-b[0])
diffY = math.fabs(a[1]-b[1])
return ((diffX**2)+(diffY**2))**(0.5)
# Auxiliary Classes
class Painter:
"""Used to organize the drawing/ updating procedure.
Implemantation based on Strategy Pattern.
Note that Painter draws objects in a FIFO order.
Objects added first, are always going to be drawn first.
"""
def __init__(self):
self.paintings = []
def add(self, drawable):
self.paintings.append(drawable)
def paint(self):
for drawing in self.paintings:
drawing.draw()
class Camera:
"""Used to represent the concept of POV.
"""
def __init__(self):
self.x = 0
self.y = 0
self.width = SCREEN_WIDTH
self.height = SCREEN_HEIGHT
self.zoom = 0.5
def centre(self,blobOrPos):
"""Makes sure that the given object will be at the center of player's view.
Zoom is taken into account as well.
"""
if isinstance(blobOrPos, Player):
x, y = blobOrPos.x, blobOrPos.y
self.x = (x - (x*self.zoom)) - x + (SCREEN_WIDTH/2)
self.y = (y - (y*self.zoom)) - y + (SCREEN_HEIGHT/2)
elif type(blobOrPos) == tuple:
self.x, self.y = blobOrPos
def update(self, target):
self.zoom = 100/(target.mass)+0.3
self.centre(blob)
class Drawable:
"""Used as an abstract base-class for every drawable element.
"""
def __init__(self, surface, camera):
self.surface = surface
self.camera = camera
def draw(self):
pass
class Grid(Drawable):
"""Used to represent the backgroun grid.
"""
def __init__(self, surface, camera):
super().__init__(surface, camera)
self.color = (230,240,240)
def draw(self):
# A grid is a set of horizontal and prependicular lines
zoom = self.camera.zoom
x, y = self.camera.x, self.camera.y
for i in range(0,2001,25):
pygame.draw.line(self.surface, self.color, (x, i*zoom + y), (2001*zoom + x, i*zoom + y), 3)
pygame.draw.line(self.surface, self.color, (i*zoom + x, y), (i*zoom + x, 2001*zoom + y), 3)
class HUD(Drawable):
"""Used to represent all necessary Head-Up Display information on screen.
"""
def __init__(self, surface, camera):
super().__init__(surface, camera)
def draw(self):
w,h = font.size("Score: "+str(int(blob.mass*2))+" ")
MAIN_SURFACE.blit(pygame.transform.scale(SCOREBOARD_SURFACE, (w, h)),
(8,SCREEN_HEIGHT-30))
MAIN_SURFACE.blit(LEADERBOARD_SURFACE,(SCREEN_WIDTH-160,15))
drawText("Score: " + str(int(blob.mass*2)),(10,SCREEN_HEIGHT-30))
MAIN_SURFACE.blit(big_font.render("Leaderboard", 0, (255, 255, 255)),
(SCREEN_WIDTH-157, 20))
drawText("1. G #1",(SCREEN_WIDTH-157,20+25))
drawText("2. G #2",(SCREEN_WIDTH-157,20+25*2))
drawText("3. ISIS",(SCREEN_WIDTH-157,20+25*3))
drawText("4. ur mom",(SCREEN_WIDTH-157,20+25*4))
drawText("5. w = pro team",(SCREEN_WIDTH-157,20+25*5))
drawText("6. jumbo",(SCREEN_WIDTH-157,20+25*6))
drawText("7. [voz]plz team",(SCREEN_WIDTH-157,20+25*7))
drawText("8. G #3",(SCREEN_WIDTH-157,20+25*8))
drawText("9. doge",(SCREEN_WIDTH-157,20+25*9))
if(blob.mass <= 500):
drawText("10. G #4",(SCREEN_WIDTH-157,20+25*10))
else:
drawText("10. Viliami",(SCREEN_WIDTH-157,20+25*10),(210,0,0))
class Player(Drawable):
"""Used to represent the concept of a player.
"""
COLOR_LIST = [
(37,7,255),
(35,183,253),
(48,254,241),
(19,79,251),
(255,7,230),
(255,7,23),
(6,254,13)]
FONT_COLOR = (50, 50, 50)
def __init__(self, surface, camera, name = ""):
super().__init__(surface, camera)
self.x = random.randint(100,400)
self.y = random.randint(100,400)
self.mass = 20
self.speed = 4
self.color = col = random.choice(Player.COLOR_LIST)
self.outlineColor = (
int(col[0]-col[0]/3),
int(col[1]-col[1]/3),
int(col[2]-col[2]/3))
if name: self.name = name
else: self.name = "Anonymous"
self.pieces = []
def collisionDetection(self, edibles):
"""Detects cells being inside the radius of current player.
Those cells are eaten.
"""
for edible in edibles:
if(getDistance((edible.x, edible.y), (self.x,self.y)) <= self.mass/2):
self.mass+=0.5
edibles.remove(edible)
def move(self):
"""Updates players current position depending on player's mouse relative position.
"""
dX, dY = pygame.mouse.get_pos()
# Find the angle from the center of the screen to the mouse in radians [-Pi, Pi]
rotation = math.atan2(dY - float(SCREEN_HEIGHT)/2, dX - float(SCREEN_WIDTH)/2)
# Convert radians to degrees [-180, 180]
rotation *= 180/math.pi
# Normalize to [-1, 1]
# First project the point from unit circle to X-axis
# Then map resulting interval to [-1, 1]
normalized = (90 - math.fabs(rotation))/90
vx = self.speed*normalized
vy = 0
if rotation < 0:
vy = -self.speed + math.fabs(vx)
else:
vy = self.speed - math.fabs(vx)
tmpX = self.x + vx
tmpY = self.y + vy
self.x = tmpX
self.y = tmpY
def feed(self):
"""Unsupported feature.
"""
pass
def split(self):
"""Unsupported feature.
"""
pass
def draw(self):
"""Draws the player as an outlined circle.
"""
zoom = self.camera.zoom
x, y = self.camera.x, self.camera.y
center = (int(self.x*zoom + x), int(self.y*zoom + y))
# Draw the ouline of the player as a darker, bigger circle
pygame.draw.circle(self.surface, self.outlineColor, center, int((self.mass/2 + 3)*zoom))
# Draw the actual player as a circle
pygame.draw.circle(self.surface, self.color, center, int(self.mass/2*zoom))
# Draw player's name
fw, fh = font.size(self.name)
drawText(self.name, (self.x*zoom + x - int(fw/2), self.y*zoom + y - int(fh/2)),
Player.FONT_COLOR)
class Cell(Drawable): # Semantically, this is a parent class of player
"""Used to represent the fundamental entity of game.
A cell can be considered as a quantom of mass.
It can be eaten by other entities.
"""
CELL_COLORS = [
(80,252,54),
(36,244,255),
(243,31,46),
(4,39,243),
(254,6,178),
(255,211,7),
(216,6,254),
(145,255,7),
(7,255,182),
(255,6,86),
(147,7,255)]
def __init__(self, surface, camera):
super().__init__(surface, camera)
self.x = random.randint(20,1980)
self.y = random.randint(20,1980)
self.mass = 7
self.color = random.choice(Cell.CELL_COLORS)
def draw(self):
"""Draws a cell as a simple circle.
"""
zoom = self.camera.zoom
x,y = self.camera.x, self.camera.y
center = (int(self.x*zoom + x), int(self.y*zoom + y))
pygame.draw.circle(self.surface, self.color, center, int(self.mass*zoom))
class CellList(Drawable):
"""Used to group and organize cells.
It is also keeping track of living/ dead cells.
"""
def __init__(self, surface, camera, numOfCells):
super().__init__(surface, camera)
self.count = numOfCells
self.list = []
for i in range(self.count): self.list.append(Cell(self.surface, self.camera))
def draw(self):
for cell in self.list:
cell.draw()
# Initialize essential entities
cam = Camera()
grid = Grid(MAIN_SURFACE, cam)
cells = CellList(MAIN_SURFACE, cam, 2000)
blob = Player(MAIN_SURFACE, cam, "GeoVas")
hud = HUD(MAIN_SURFACE, cam)
painter = Painter()
painter.add(grid)
painter.add(cells)
painter.add(blob)
painter.add(hud)
# Game main loop
while(True):
clock.tick(70)
for e in pygame.event.get():
if(e.type == pygame.KEYDOWN):
if(e.key == pygame.K_ESCAPE):
pygame.quit()
quit()
if(e.key == pygame.K_SPACE):
del(cam)
blob.split()
if(e.key == pygame.K_w):
blob.feed()
if(e.type == pygame.QUIT):
pygame.quit()
quit()
blob.move()
blob.collisionDetection(cells.list)
cam.update(blob)
MAIN_SURFACE.fill((242,251,255))
# Uncomment next line to get dark-theme
#surface.fill((0,0,0))
painter.paint()
# Start calculating next frame
pygame.display.flip()