Skip to content

Instantly share code, notes, and snippets.

@Orpheon
Created October 4, 2013 21:41
Show Gist options
  • Select an option

  • Save Orpheon/6833246 to your computer and use it in GitHub Desktop.

Select an option

Save Orpheon/6833246 to your computer and use it in GitHub Desktop.
from __future__ import division, print_function
import pygame, random
import gameobject
import vector
import constants
class Creature(gameobject.GameObject):
OBJECT_TYPE = "Swarm particles"
MAX_SPEED = 10
ACCELERATION = 15/constants.FPS
SIZE = 2
TARGET_ATTRACTION = 10000
def __init__(self, pos, game):
super(Creature, self).__init__(pos, game)
self.velocity = vector.Vector((random.randint(0, 5), random.randint(0, 5)))
def step(self, game):
goal = vector.Vector((0, 0))# A number larger than the distance to the nearest red point
for target in game.targets:
vec = target.pos - self.pos
vec.length = self.TARGET_ATTRACTION/vec.length**2
goal += vec
goal.length *= self.ACCELERATION
self.velocity += vec
def render(self, surface):
pygame.draw.circle(surface, constants.COLOR_GREEN, (int(self.pos.x), int(self.pos.y)), self.SIZE)
class Target(gameobject.GameObject):
OBJECT_TYPE = "Swarm target"
SIZE = 2
RANGE_MIN = 1/5
RANGE_MAX = 4/5
def __init__(self, game):
pos = vector.Vector()
pos.x = random.randint(int(constants.WINDOW_WIDTH*self.RANGE_MIN), int(constants.WINDOW_WIDTH*self.RANGE_MAX))
pos.y = random.randint(int(constants.WINDOW_HEIGHT*self.RANGE_MIN), int(constants.WINDOW_HEIGHT*self.RANGE_MAX))
gameobject.GameObject.__init__(self, pos, game)
def step(self, game):
for creature in game.entities["Swarm particles"]:
if abs(creature.pos - self.pos) < self.SIZE + creature.SIZE and False:
self.pos.x = random.randint(int(constants.WINDOW_WIDTH*self.RANGE_MIN), int(constants.WINDOW_WIDTH*self.RANGE_MAX))
self.pos.y = random.randint(int(constants.WINDOW_HEIGHT*self.RANGE_MIN), int(constants.WINDOW_HEIGHT*self.RANGE_MAX))
def render(self, surface):
pygame.draw.circle(surface, constants.COLOR_RED, (int(self.pos.x), int(self.pos.y)), self.SIZE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment