Skip to content

Instantly share code, notes, and snippets.

@jkwok91
Created April 14, 2014 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/588b024b29c4133c8f92 to your computer and use it in GitHub Desktop.
Save jkwok91/588b024b29c4133c8f92 to your computer and use it in GitHub Desktop.
class Cannon {
float x;
Cannon(float i) {
x = i;
}
Scoop releaseScoop() {
return new Scoop(x, 10);
}
void display() {
stroke(0);
fill(color(255, 0, 0));
rect(x, height-20, 20, 20);
}
}
/*
resources:
http://krazydad.com/tutorials/makecolors.php
*/
color getColor(float i) {
int center = 205; //pastelly
int widthh = 205;
double frequency = .3;
int red = (int)Math.ceil(Math.sin(frequency*i+0) * widthh + center);
int green = (int)Math.ceil(Math.sin(frequency*i+2) * widthh + center);
int blue = (int)Math.ceil(Math.sin(frequency*i+4) * widthh + center);
return color(red, green, blue);
}
/*
chia ice cream factory
because hey if my destiny is reverse engineering neopets, then so be it
april 13, 2014
*/
int numCannons, cannonW;
boolean gameOver;
int level;
float chance, speed;
Player player;
Cannon[] cannons;
ArrayList<Scoop> launched;
int avoided;
void setup() {
size(300, 300);
background(255);
cannonW = 25;
player = new Player();
numCannons = width/cannonW; //each cannon is 20px wide
avoided = 0;
cannons = new Cannon[numCannons];
for (int i = 0; i < cannons.length; i++) {
cannons[i] = new Cannon((i*cannonW)+2);
}
startGame();
}
void draw() {
background(255);
drawCannons();
player.display();
// chance of any cannon being fired in this frame
boolean isFiring = (random(0, 1) > chance);
if (isFiring) {
fireCannon();
}
for (int i = launched.size()-1; i >= 0 && !gameOver; i--) {
Scoop s = launched.get(i);
if (avoided >= (level*(level+1))) {
level++;
chance -= 0.01;
speed += -1.0/level;
}
s.update();
if (s.loc.y < 0) {
launched.remove(i);
avoided++;
}
s.display();
if (s.collision(player.rad)) {
player.die();
}
fill(0);
text("heyyo! avoided "+avoided, 0, 20);
text("level: "+level, 0, 40);
}
if (player.isDead) {
gameOver = true;
endGame();
}
}
void drawCannons() {
for (int i = 0; i < cannons.length; i++) {
cannons[i].display();
}
}
void fireCannon() {
Scoop scoop = cannons[(int)random(numCannons)].releaseScoop();
launched.add(scoop);
}
void startGame() {
gameOver = false;
player.revive();
chance = 0.95;
avoided = 0;
level = 1;
speed = -1;
launched = new ArrayList<Scoop>();
}
void endGame() {
background(255);
fill(0);
text("score: "+avoided, width/2, height/2);
text("click to restart", width/2, height/2+20);
noLoop();
}
void mousePressed() {
if (gameOver) {
startGame();
loop();
}
}
class Player {
int rad;
boolean isDead;
Player() {
isDead = false;
rad = 12;
}
/*
accessor methods that aren't really necessary?
*/
void die() {
isDead = true;
}
void revive() {
isDead = false;
}
void display() {
stroke(0);
fill(0);
ellipse(mouseX, mouseY, rad*2, rad*2);
}
}
class Scoop {
color c;
int rad;
PVector loc, vel, acc;
Scoop(float x, int r) {
c = getColor(level);
rad = r;
// initially located at bottom of screen
loc = new PVector(x+rad, height);
// will move upwards towards player
// the two dimensional vector is really not necessary here.
vel = new PVector(0, speed);
}
void update() {
loc.add(vel);
}
boolean collision(int pR) {
float distance = dist(mouseX, mouseY, loc.x, loc.y);
return (distance < rad+pR);
}
void display() {
stroke(c);
fill(c);
ellipse(loc.x, loc.y, rad*2, rad*2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment