Idea 1: Digital Garden – ASCCII ART Garden
Idea 2: Snake game – Snake eats, gets longer, it dies when it crashes into the edges.
Randomized short snake Randomized fluid Grows longer Working on snake turning gradually
Snake snake;
Food food;
int score;
void setup() {
size(600, 400);
frameRate(8);
snake = new Snake();
food = new Food();
rectMode(CENTER);
score = 0;
}
void draw() {
background(250, 250, 250);
write_score();
snake.run();
food.display();
// implement the rules of the game
if ( dist(food.xpos, food.ypos, snake.xpos.get(0), snake.ypos.get(0)) < snake.sidelength ) {
food.reset(); // generate new food
snake.grow(); // snake grows longer after eating food
}
if (snake.length > score) {
score= snake.length;
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) {
snake.direction = "left";
}
if (keyCode == RIGHT) {
snake.direction = "right";
}
if (keyCode == UP) {
snake.direction = "up";
}
if (keyCode == DOWN) {
snake.direction = "down";
}
}
}
void write_score() {
stroke(0);
textSize(17);
text( "Score = " + score, 50, 50);
}
class Snake {
int length;
float sidelength;
String direction;
ArrayList <Float> xpos, ypos;
Snake() {
length = 1; // snake starts with being 3 units long
sidelength = 10;
direction = "right";
xpos = new ArrayList();
ypos = new ArrayList();
xpos.add(random(width));
ypos.add(random(height));
}
// snake moving! how snake turns - the snake head turns first and then the body follows
// solved by using sidelength!
void move() {
for (int i = length - 1; i > 0; i = i -1 ) {
println("i is " + i);
Float xpimo = xpos.get(i - 1);
xpos.set(i, xpimo);
ypos.set(i, ypos.get(i - 1));
}
if (direction == "left") {
xpos.set(0, xpos.get(0) - sidelength);
}
if (direction == "right") {
xpos.set(0, xpos.get(0) + sidelength);
}
if (direction == "up") {
ypos.set(0, ypos.get(0) - sidelength);
}
if (direction == "down") {
ypos.set(0, ypos.get(0) + sidelength);
}
xpos.set(0, (xpos.get(0) + width) % width);
ypos.set(0, (ypos.get(0) + height) % height);
if (checkEdges() == true) {
length = 1;
float xtemp = xpos.get(0);
float ytemp = ypos.get(0);
xpos.clear();
ypos.clear();
xpos.add(xtemp);
ypos.add(ytemp);
}
}
void display() {
for (int i = 0; i <length; i++) {
stroke(179, 140, 198);
fill(100, 0, 100, map(i-1, 0, length -1, 250, 50));
rect(xpos.get(i), ypos.get(i), sidelength, sidelength);
}
}
void grow() {
xpos.add( xpos.get(length-1) + sidelength);
ypos.add( ypos.get(length-1) + sidelength);
length ++;
}
// when snake hits the wall
boolean checkEdges () {
for (int i = 1; i < length; i++) {
if ( dist(xpos.get(0), ypos.get(0), xpos.get(i), ypos.get(i)) < sidelength) {
return true;
}
}
return false;
}
void run(){
move();
display();
}
}
class Food {
PVector food_location;
float xpos, ypos;
//constructor for a single food item
Food() {
xpos = random(100, width - 10);
ypos = random(100, height - 10);
}
void display() {
fill(0, 128, 0);
ellipse(xpos, ypos, 17, 17);
}
void reset() {
xpos = random(100, width - 100);
ypos = random(100, height - 100);
}
}