Skip to content

Instantly share code, notes, and snippets.

@microlith57
Last active August 25, 2021 02:32
Show Gist options
  • Save microlith57/f32e4c98cb57a82d0f3d to your computer and use it in GitHub Desktop.
Save microlith57/f32e4c98cb57a82d0f3d to your computer and use it in GitHub Desktop.
Agar
# Agar
# Made for Pythonista on iPad
from scene import *
from random import randint
from math import pi, pow
BACKGROUND_COLOR = Color(0, 0, 0) # The color of the background.
BLOB_COLOR = Color(0.5, 0.5, 1, 0.5) # The least background-like blob color possible.
TEXT_COLOR = Color(0.8, 0.8, 1, 0.8) # The color of the mass counter text.
SANDBOX = True # Sandbox mode (no motion, mass constant)
GENERATE = True # Generate at launch?
GENERATE_NUM = 100 # Number to generate if GENERATE == True
class Blob (object):
x = 0
y = 0
z = 0
motion = [0.0, 0.0]
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
self.motion = [randint(-10, 10) / 100.0, randint(-10, 10) / 100.0]
def draw(self, offsetx, offsety):
if not SANDBOX:
self.z -= 0.02
self.x += self.motion[0]
self.y += self.motion[1]
fill(BLOB_COLOR.r, BLOB_COLOR.g, BLOB_COLOR.b, BLOB_COLOR.a)
ellipse((self.x - (offsetx * self.z)) - (self.z / 2), (self.y - (offsety * self.z)) - (self.z / 2), self.z, self.z)
fill(BACKGROUND_COLOR.r, BACKGROUND_COLOR.g, BACKGROUND_COLOR.b, 1 + (0 - (float(self.z) / 100)))
ellipse((self.x - (offsetx * self.z)) - (self.z / 2), (self.y - (offsety * self.z)) - (self.z / 2), self.z, self.z)
def cmpBlobs(x, y): return cmp(x.z, y.z)
class MyScene (Scene):
textx, texty = 10, 10
offsetx, offsety = 0, 0
blobs = []
def setup(self):
if GENERATE:
for i in range(GENERATE_NUM):
self.blobs.append(Blob(randint(-50, self.size.w + 50), randint(-50, self.size.h + 50), randint(10, 100)))
def draw(self):
totalMass = 0
for blob in self.blobs:
totalMass += pow((2.0/3.0)*pi*(blob.z/2), 3)
self.blobs.sort(cmpBlobs)
background(BACKGROUND_COLOR.r, BACKGROUND_COLOR.g, BACKGROUND_COLOR.b)
g = gravity()
if abs(self.offsetx - (g.x * 3)) > 0.01:
if g.x * 3 < self.offsetx: self.offsetx -= abs(self.offsetx - (g.x * 3)) / 10
else: self.offsetx += abs(self.offsetx - (g.x * 3)) / 10
if abs(self.offsety - (g.y * 3)) > 0.01:
if g.y * 3 < self.offsety: self.offsety -= abs(self.offsety - (g.y * 3)) / 10
else: self.offsety += abs(self.offsety - (g.y * 3)) / 10
tint(TEXT_COLOR.r, TEXT_COLOR.g, TEXT_COLOR.b, TEXT_COLOR.a)
text('Total Mass: %skg' % (int(round(totalMass / 10000)*10)), alignment = 9, x = self.textx, y = self.texty)
for blob in self.blobs:
blob.draw(self.offsetx, self.offsety)
if blob.z <= 1: self.blobs.remove(blob)
def touch_began(self, touch):
self.blobs.append(Blob(touch.location.x, touch.location.y, randint(10, 100)))
def touch_moved(self, touch):
pass
def touch_ended(self, touch):
pass
run(MyScene())
@microlith57
Copy link
Author

Python Project: Agar
Agar is a sphere-based game where you can create cool 3D artworks. In sandbox mode there is no movement whatsoever but if sandbox is disabled the mass of spheres decreases over time and spheres move about with constant velocity. Made for the iPad app Pythonista.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment