As a final revisit to our ICM class, I’ve created another game for the Telecommunications homework unlike the one-that-tried to-be-ambitious-with-rotation-but-failed. This game is a two-player Hangman (under 10 letters or less). The first player sets the word and then the second player actually guesses. And the actual communication part is to indicate loss or win by LEDS! There is also a RESET function if you lose or wish to choose another word.
**Disclaimer: capital letters aren’t recognized and you can’t use backspace. Furthermore, when guessing a letter, “spaces” are counted as incorrect and somehow if you don’t enter anything (i.e. press “ENTER” without an actual input), it may break my game.
And here is the code:
import processing.serial.*;
Serial myPort;
int pos_X = 125;
int pos_Y = 125;
float size = 1.25;
int person_width = (int)(40*size);
int person_height = (int)((65*size) + (65*size) + (35*size));
//Typing for guessing:
String typing = "";
char saved;
char [] temporary;
//Object:
Letters set;
//Game stages:
//Set word:
boolean reset = false;
boolean start;
PFont font;
String set_word = "";
String final_word = "";
int word_length =0;
//Guess word:
boolean guess;
boolean background_on;
int lose_x;
int lose_y;
boolean increment_lose;
int total_lose = 10;
boolean word_right = false;
//Winner
boolean win;
void setup() {
size(800, 600);
start = true;
guess = false;
win = false;
font = loadFont("Baskerville-48.vlw");
lose_x = 400;
lose_y=200;
//Processing - ARDUINO
printArray(Serial.list());
String portName = Serial.list()[5];
myPort = new Serial(this, portName, 9600);
}//End of setup
void draw() {
if (start) {
if (reset) {
background(150, 150, 255);
reset = false;
}
background(150, 150, 255);
textFont(font, 32);
fill(255);
text("Type in the word (10 letters or less) that someone will guess.\n Press Enter to begin the game", 20, 50);
text(set_word, 100, 400);
}
else if (guess) {
if (background_on) {
background(255);
background_on = false;
}
textFont(font, 28);
fill(184, 92, 245);
text("Only the first character will be guessed.\n Press Enter to guess: "+typing, 300, 550);
//Hangman's stand
fill(121, 63, 13);
rect(40, 40, 150, 20); // top beam
rect(40, 60, 30, person_height+60); // stand
rect(40, person_height+120, 150, 30);//base
fill(0);
ellipse(53, 50, 7, 8);
//ROPE
stroke(245, 222, 179);
strokeWeight(4);
line(150, 60, 150, 100);
set.draw_lines();
if (set.check_letter(saved)) {
set.draw_letter(saved);
}
else {
if (increment_lose) {
fill(184, 92, 245);
text(saved, lose_x, lose_y);
lose_x+=30;
increment_lose = false;
total_lose --;
if (lose_x > 700) {
lose_x = 400;
lose_y+= 50;
}
}
}
losing();
if (word_length == 0) {
guess = false;
textFont(font, 30);
text("You've won! \n Press 1 to reset the game & play again.", 200, 350);
myPort.write(1);
}
}//end of guess
else if (win) {
}
}//End of Draw
void keyPressed() {
if (start) {
if (key == '\n' ) {
final_word = set_word;
set = new Letters(final_word);
word_length = final_word.length();
System.out.println("start "+word_length);
start = false;
background_on = true;
guess = true;
}
else {
// Otherwise, concatenate the String
// Each character typed by the user is added to the end of the String variable.
set_word = set_word + key;
}
}
else if (guess) {
if (key == '\n' ) {
word_right = true;
temporary = typing.toCharArray();
saved = temporary[0];
increment_lose = true;
strokeWeight(0);
stroke(255);
typing = "";
fill(255);
rect(540, 560, 100, 30);
// A String can be cleared by setting it equal to ""
}
else {
// Otherwise, concatenate the String
// Each character typed by the user is added to the end of the String variable.
typing = typing + key;
}
if (key == '1') {
start = true;
reset = true;
guess = false;
set_word = "";
final_word = "";
total_lose = 9;
word_length =0;
myPort.write(3);
typing = "";
}
}
else if(word_length == 0){
if (key == '1') {
start = true;
reset = true;
guess = false;
set_word = "";
final_word = "";
total_lose = 9;
myPort.write(3);
typing = "";
}
}
//end of guess
}//End of key pressed
void losing() {//DONT FORGET YOU'RE COUNTING DOWN!
strokeWeight(2);
stroke(0, 0, 0);
switch(total_lose) {
case 0:
textFont(font, 30);
fill(255, 0, 0);
text("You have lost!\n Press '1' to reset the game", 200, 350);
myPort.write(2);
break;
case 1:
strokeWeight(1);
line(145, 130, 155, 135);
line(155, 130, 145, 135);
break;
case 2:
strokeWeight(1);
line(155, 115, 160, 120);
line(160, 115, 155, 120);
break;
case 3:
strokeWeight(1);
line(140, 115, 145, 120);
line(145, 115, 140, 120);
break;
case 4:
line( pos_X+20*size, pos_Y+70*size, pos_X+40*size, pos_Y+130*size) ; // right leg
break;
case 5:
line( pos_X+20*size, pos_Y+70*size, pos_X, pos_Y+130*size) ; //Left leg
break;
case 6:
line( pos_X+20*size, pos_Y+30*size, pos_X+40*size, pos_Y+70*size) ; // right arm
break;
case 7:
line( pos_X+20*size, pos_Y+30*size, pos_X, pos_Y+70*size) ; // left arm
break;
case 8:
line(pos_X+20*size, pos_Y+(16.4*size), pos_X+20*size, pos_Y+70*size); // Body
break;
case 9:
fill(255, 0, 100); // head fill
ellipse(pos_X+20*size, pos_Y, 40*size, 35*size) ; // head
break;
}//end of switch :P
}
And here is the letter class:
class Letters{
char [] to_char;
PFont text;
//Letter CONSTRUCTOR: Takes in the string
Letters(String word){
to_char = word.toCharArray();
text = loadFont("Baskerville-48.vlw");
}//End of constructor
void draw_lines(){
int x = 40;
for (int i=0;i<to_char.length;i++) {
stroke(0);
strokeWeight(2);
line(x,500,x+50,500);
x+=75;
}
}
boolean check_letter(char example){
for (int i = 0; i<to_char.length;i++){ //scans letters of the actual word
if(example == to_char[i]){
return true;
}
}
return false;
}
void draw_letter(char example){
int x = 50;
if (word_right) {
for (int i=0;i<to_char.length;i++) {
// System.out.println(example);
// System.out.println(to_char[i]);
if(example == to_char[i]){
//System.out.println("hahahahah");
fill(184,92,245);
textFont(text, 42);
text(example, x, 490);
word_length--;
System.out.println(word_length);
}
x+=75;
}
word_right = false;
}
// if(example)
}//end
}//letters class
For my final project, I made another game. There were about 4 factors that went into this game idea.
Unlike my other games, I wanted to challenge myself and not be reliant on PImages for the actual graphics and physically draw shapes and lines myself.
Since we so recently wrote our blog post on Computational Media, I think I was still very oriented towards that meditation on computers and their capabilities, etc.
I wanted to incorporate the first part of our class that focused on the Arduino because it was so integral to our course with its scope on the physical world of computing.
So with all those factors in mind, I thought about the circle and how basically it is a polygon with an infinite amount of sides. This topic of “infinity” reminded me of David Deutsch’s Beginning of Infinity , where he claims that progress is boundless – or infinite. So by somehow weirdly merging the those thoughts in my mind, the game where the goal was to become a circle (that represented infinity with its “infinite” number of sides) became its own ” progress towards infinity”.
This is my first multiplayer game (woohooo, double the fun!) and the movement of the players are controlled by the ASDW and the arrow keys. In each level, there are foreign words floating around that mean the number of the next shape the player must become. To advance, the player must retrieve all of his respective words. For example, for a square to become a polygon, the player must collect all 5 words that mean “5”. The words are also the same color as the player so it makes the game easier than just a mere translation exercise and actually possible to finish within a more reasonable amount of time.
And here is the scary code. The switch statements got very repetitive but I wasn’t able to modularize the actual level changing function because then it ended up being that I would have to return an array and somehow pass it back into the same function (all because I have so many switches within each level).
import processing.serial.*;
Serial myPort;
int pot1;
int pot2;
int pot3;
int converted1;
int converted2;
int converted3;
Shape player_1;
Shape player_2;
Text opening;
ArrayList<Text>player1text;
ArrayList<Text>player2text;
//OPENING:
PFont type;
//Beginning:
boolean start;
boolean player_1_choice;
boolean player_2_choice;
int final_red1;
int final_green1;
int final_blue1;
int final_red2;
int final_green2;
int final_blue2;
//GAME
boolean game_on;
int level_count1= 1;
int level_count2 = 1;
boolean level_start;
boolean level_start2;
//WINNING:
boolean finish;
Boolean win1;
Boolean win2;
boolean win_keys;
boolean win_reset1;
boolean win_reset2;
boolean win_text1;
boolean win_text2;
void setup() {
size(1000, 700,P2D);
//Beginning:
start = true;
game_on = false;
type = loadFont("Cochin-48.vlw");
boolean player_1_choice = true;
boolean player_2_choice = false;
int final_red1=0;
int final_green1=0;
int final_blue1=0;
int final_red2=0;
int final_green2=0;
int final_blue2=0;
//Gameplay:
level_start = false;
level_start2 = false;
player_1 = new Shape();
player_1.set_Position(200,300,4);
player_2 = new Shape();
player_2.set_Position(800,300,-4);
player1text = new ArrayList<Text>();
player2text = new ArrayList<Text>();
//Only for level 1:
for (int i = 0;i<2;i++) {
player1text.add(new Text((int)random(1, 11), 1));
player1text.get(i).set_text_color(final_red1, final_green1, final_blue1);
player2text.add(new Text((int)random(1, 11), 1));
player2text.get(i).set_text_color(final_red2, final_green2, final_blue2);
}
//Winning:
finish = false;
win1 = false;
win2= false;
win_keys = false;
win_reset1 = false;
win_reset2 = false;
win_text1 = false;
win_text2 = false;
//ARDUINO --> PROCESSING:
println(Serial.list());
String portName = Serial.list()[5];
myPort = new Serial(this, portName, 9600);
myPort.readStringUntil('\n');
}//End of setup
void draw() {
background(255);
if (start) {
background(255);
fill(0);
textFont(type, 36);
text("Welcome to the beginning of infinity.\n Please press the '1' to begin color selection for player 1.\n Mouse click to re-orient the dial inputs.", width/10, height /4);
if (player_1_choice) {
background(255);
text("Player 1: please select color by turning each dial. \n Press '2' to save your selection and let Player 2 choose.", width/9, height/4);
final_red1 = converted1;
final_green1 = converted3;
final_blue1 = converted2;
fill(final_red1, final_green1, final_blue1);
rect(100, height/2, 200, 200);
}
else if (player_2_choice) {
background(255);
text("Player 2: please select your color by turning the dial.\n To begin the game press 'ENTER'", width/8, height/4);
final_red2 = converted1;
final_green2 = converted3;
final_blue2 = converted2;
fill(final_red2,final_green2, final_blue2);
rect(700, height/2, 200, 200);
}
}
//game start
if (game_on) {
//PLAYER 1 SWITCH
switch (level_count1) {
case 1:
player_1.drawShape1();
;
player_1.moveShape();
if (player_1.checkBoundaries()) {
player_1.move_back();
}
//TEXT
for (int i = 0;i<player1text.size();i++) {
player1text.get(i).set_text_color(final_red1, final_green1, final_blue1);
player1text.get(i).drawText();
player1text.get(i).moveText();
if (player1text.get(i).checkBoundaries()) {
player1text.get(i).move_back_text();
}
if (player1text.get(i).check_Collision(player_1)) {
player1text.remove(i);
i--;
if (player1text.isEmpty()) {
level_count1++;
level_start = true;
}
}
}
break;
case 2:
player_1.drawLine();
;
player_1.moveShape();
if (player_1.checkBoundaries()) {
player_1.move_back();
}
//Text
if (level_start) {
for (int i = 0;i<3;i++) {
player1text.add(new Text((int)random(1, 11), 2));
}
for (int i = 0; i<3; i++) {
player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
}
level_start = false;
}
for (int i = 0;i<player1text.size();i++) {
player1text.get(i).drawText();
player1text.get(i).moveText();
if (player1text.get(i).checkBoundaries()) {
player1text.get(i).move_back_text();
}
if (player1text.get(i).check_Collision(player_1)) {
player1text.remove(i);
i--;
if (player1text.isEmpty()) {
level_count1++;
level_start = true;
}
}
}
break;
case 3:
player_1.drawShape2(3);
player_1.moveShape();
if (player_1.checkBoundaries()) {
player_1.move_back();
}
//Text
if (level_start) {
for (int i = 0;i<4;i++) {
player1text.add(new Text((int)random(1, 11), 3));
}
for (int i = 0; i<4; i++) {
player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
}
level_start = false;
}
for (int i = 0;i<player1text.size();i++) {
player1text.get(i).drawText();
player1text.get(i).moveText();
if (player1text.get(i).checkBoundaries()) {
player1text.get(i).move_back_text();
}
if (player1text.get(i).check_Collision(player_1)) {
player1text.remove(i);
i--;
if (player1text.isEmpty()) {
level_count1++;
level_start = true;
}
}
}
break;
case 4:
player_1.drawShape2(4);
;
player_1.moveShape();
if (player_1.checkBoundaries()) {
player_1.move_back();
}
//Text
if (level_start) {
for (int i = 0;i<5;i++) {
player1text.add(new Text((int)random(1, 11), 4));
}
for (int i = 0; i<5; i++) {
player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
}
level_start = false;
}
for (int i = 0;i<player1text.size();i++) {
player1text.get(i).drawText();
player1text.get(i).moveText();
if (player1text.get(i).checkBoundaries()) {
player1text.get(i).move_back_text();
}
if (player1text.get(i).check_Collision(player_1)) {
player1text.remove(i);
i--;
if (player1text.isEmpty()) {
level_count1++;
level_start = true;
}
}
}
break;
case 5:
player_1.drawShape2(5);
;
player_1.moveShape();
if (player_1.checkBoundaries()) {
player_1.move_back();
}
//Text
if (level_start) {
for (int i = 0;i<6;i++) {
player1text.add(new Text((int)random(1, 11), 5));
}
for (int i = 0; i<6; i++) {
player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
}
level_start = false;
}
for (int i = 0;i<player1text.size();i++) {
player1text.get(i).drawText();
player1text.get(i).moveText();
if (player1text.get(i).checkBoundaries()) {
player1text.get(i).move_back_text();
}
if (player1text.get(i).check_Collision(player_1)) {
player1text.remove(i);
i--;
if (player1text.isEmpty()) {
level_count1++;
level_start = true;
}
}
}
break;
case 6:
player_1.drawShape2(6);
;
player_1.moveShape();
if (player_1.checkBoundaries()) {
player_1.move_back();
}
//Text
if (level_start) {
for (int i = 0;i<7;i++) {
player1text.add(new Text((int)random(1, 11), 6));
}
for (int i = 0; i<7; i++) {
player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
}
level_start = false;
}
for (int i = 0;i<player1text.size();i++) {
player1text.get(i).drawText();
player1text.get(i).moveText();
if (player1text.get(i).checkBoundaries()) {
player1text.get(i).move_back_text();
}
if (player1text.get(i).check_Collision(player_1)) {
player1text.remove(i);
i--;
if (player1text.isEmpty()) {
level_count1++;
level_start = true;
}
}
}
break;
case 7:
player_1.drawShape2(7);
player_1.moveShape();
if (player_1.checkBoundaries()) {
player_1.move_back();
}
//Text
if (level_start) {
for (int i = 0;i<8;i++) {
player1text.add(new Text((int)random(1, 11), 7));
}
for (int i = 0; i<8; i++) {
player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
}
level_start = false;
}
for (int i = 0;i<player1text.size();i++) {
player1text.get(i).drawText();
player1text.get(i).moveText();
if (player1text.get(i).checkBoundaries()) {
player1text.get(i).move_back_text();
}
if (player1text.get(i).check_Collision(player_1)) {
player1text.remove(i);
i--;
if (player1text.isEmpty()) {
level_count1++;
level_start = true;
}
}
}
break;
case 8:
player_1.drawShape2(8);
;
player_1.moveShape();
if (player_1.checkBoundaries()) {
player_1.move_back();
}
//Text
if (level_start) {
for (int i = 0;i<9;i++) {
player1text.add(new Text((int)random(1, 11), 8));
}
for (int i = 0; i<9; i++) {
player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
}
level_start = false;
}
for (int i = 0;i<player1text.size();i++) {
player1text.get(i).drawText();
player1text.get(i).moveText();
if (player1text.get(i).checkBoundaries()) {
player1text.get(i).move_back_text();
}
if (player1text.get(i).check_Collision(player_1)) {
player1text.remove(i);
i--;
if (player1text.isEmpty()) {
level_count1++;
level_start = true;
}
}
}
break;
case 9:
player_1.drawShape2(9);
player_1.moveShape();
if (player_1.checkBoundaries()) {
player_1.move_back();
}
//Text
if (level_start) {
for (int i = 0;i<10;i++) {
player1text.add(new Text((int)random(1, 11), 9));
}
for (int i = 0; i<10; i++) {
player1text.get(i).set_text_color(final_red1, final_green1,final_blue1);
}
level_start = false;
}
for (int i = 0;i<player1text.size();i++) {
player1text.get(i).drawText();
player1text.get(i).moveText();
if (player1text.get(i).checkBoundaries()) {
player1text.get(i).move_back_text();
}
if (player1text.get(i).check_Collision(player_1)) {
player1text.remove(i);
i--;
if (player1text.isEmpty()) {
level_count1++;
level_start = true;
}
}
}
break;
case 10:
game_on = false;
finish = true;
win1 = true;
win_keys = true;
win_reset1 = true;
break;
} // End of player 1
//PLAYER 2
switch (level_count2) {
case 1:
player_2.drawShape1();
player_2.moveShape();
if (player_2.checkBoundaries()) {
player_2.move_back();
}
//TEXT
for (int i = 0;i<player2text.size();i++) {
player2text.get(i).set_text_color(final_red2, final_green2, final_blue2);
player2text.get(i).drawText();
player2text.get(i).moveText();
if (player2text.get(i).checkBoundaries()) {
player2text.get(i).move_back_text();
}
if (player2text.get(i).check_Collision(player_2)) {
player2text.remove(i);
i--;
if (player2text.isEmpty()) {
level_count2++;
level_start2 = true;
}
}
}
break;
case 2:
player_2.drawLine();
player_2.moveShape();
if (player_2.checkBoundaries()) {
player_2.move_back();
}
//Text
if (level_start2) {
for (int i = 0;i<3;i++) {
player2text.add(new Text((int)random(1, 11), 2));
}
for (int i = 0; i<3; i++) {
player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
}
level_start2 = false;
}
for (int i = 0;i<player2text.size();i++) {
player2text.get(i).drawText();
player2text.get(i).moveText();
if (player2text.get(i).checkBoundaries()) {
player2text.get(i).move_back_text();
}
if (player2text.get(i).check_Collision(player_2)) {
player2text.remove(i);
i--;
if (player2text.isEmpty()) {
level_count2++;
level_start2 = true;
}
}
}
break;
case 3:
player_2.drawShape2(3);
player_2.moveShape();
if (player_2.checkBoundaries()) {
player_2.move_back();
}
//Text
if (level_start2) {
for (int i = 0;i<4;i++) {
player2text.add(new Text((int)random(1, 11), 3));
}
for (int i = 0; i<4; i++) {
player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
}
level_start2 = false;
}
for (int i = 0;i<player2text.size();i++) {
player2text.get(i).drawText();
player2text.get(i).moveText();
if (player2text.get(i).checkBoundaries()) {
player2text.get(i).move_back_text();
}
if (player2text.get(i).check_Collision(player_2)) {
player2text.remove(i);
i--;
if (player2text.isEmpty()) {
level_count2++;
level_start2 = true;
}
}
}
break;
case 4:
player_2.drawShape2(4);
player_2.moveShape();
if (player_2.checkBoundaries()) {
player_2.move_back();
}
//TEXT
//Text
if (level_start2) {
for (int i = 0;i<5;i++) {
player2text.add(new Text((int)random(1, 11), 4));
}
for (int i = 0; i<5; i++) {
player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
}
level_start2 = false;
}
for (int i = 0;i<player2text.size();i++) {
player2text.get(i).drawText();
player2text.get(i).moveText();
if (player2text.get(i).checkBoundaries()) {
player2text.get(i).move_back_text();
}
if (player2text.get(i).check_Collision(player_2)) {
player2text.remove(i);
i--;
if (player2text.isEmpty()) {
level_count2++;
level_start2 = true;
}
}
}
break;
case 5:
player_2.drawShape2(5);
player_2.moveShape();
if (player_2.checkBoundaries()) {
player_2.move_back();
}
//TEXT
//Text
if (level_start2) {
for (int i = 0;i<6;i++) {
player2text.add(new Text((int)random(1, 11), 5));
}
for (int i = 0; i<6; i++) {
player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
}
level_start2 = false;
}
for (int i = 0;i<player2text.size();i++) {
player2text.get(i).drawText();
player2text.get(i).moveText();
if (player2text.get(i).checkBoundaries()) {
player2text.get(i).move_back_text();
}
if (player2text.get(i).check_Collision(player_2)) {
player2text.remove(i);
i--;
if (player2text.isEmpty()) {
level_count2++;
level_start2 = true;
}
}
}
break;
case 6:
player_2.drawShape2(6);
player_2.moveShape();
if (player_2.checkBoundaries()) {
player_2.move_back();
}
//Text
if (level_start) {
for (int i = 0;i<7;i++) {
player2text.add(new Text((int)random(1, 11), 6));
}
for (int i = 0; i<7; i++) {
player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
}
level_start2 = false;
}
for (int i = 0;i<player2text.size();i++) {
player2text.get(i).drawText();
player2text.get(i).moveText();
if (player2text.get(i).checkBoundaries()) {
player2text.get(i).move_back_text();
}
if (player2text.get(i).check_Collision(player_2)) {
player2text.remove(i);
i--;
if (player2text.isEmpty()) {
level_count2++;
level_start2 = true;
}
}
}
break;
case 7:
player_2.drawShape2(7);
player_2.moveShape();
if (player_2.checkBoundaries()) {
player_2.move_back();
}
//Text
if (level_start) {
for (int i = 0;i<8;i++) {
player2text.add(new Text((int)random(1, 11), 7));
}
for (int i = 0; i<8; i++) {
player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
}
level_start2 = false;
}
for (int i = 0;i<player2text.size();i++) {
player2text.get(i).drawText();
player2text.get(i).moveText();
if (player2text.get(i).checkBoundaries()) {
player2text.get(i).move_back_text();
}
if (player2text.get(i).check_Collision(player_2)) {
player2text.remove(i);
i--;
if (player2text.isEmpty()) {
level_count2++;
level_start2 = true;
}
}
}
break;
case 8:
player_2.drawShape2(8);
player_2.moveShape();
if (player_2.checkBoundaries()) {
player_2.move_back();
}
//Text
if (level_start2) {
for (int i = 0;i<9;i++) {
player2text.add(new Text((int)random(1, 11), 8));
}
for (int i = 0; i<9; i++) {
player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
}
level_start2 = false;
}
for (int i = 0;i<player2text.size();i++) {
player2text.get(i).drawText();
player2text.get(i).moveText();
if (player2text.get(i).checkBoundaries()) {
player2text.get(i).move_back_text();
}
if (player2text.get(i).check_Collision(player_2)) {
player2text.remove(i);
i--;
if (player2text.isEmpty()) {
level_count2++;
level_start2 = true;
}
}
}
break;
case 9:
player_2.drawShape2(9);
player_2.moveShape();
if (player_2.checkBoundaries()) {
player_2.move_back();
}
//Text
if (level_start2) {
for (int i = 0;i<10;i++) {
player2text.add(new Text((int)random(1, 11), 9));
}
for (int i = 0; i<10; i++) {
player2text.get(i).set_text_color(final_red2, final_green2,final_blue2);
}
level_start2 = false;
}
for (int i = 0;i<player2text.size();i++) {
player2text.get(i).drawText();
player2text.get(i).moveText();
if (player2text.get(i).checkBoundaries()) {
player2text.get(i).move_back_text();
}
if (player2text.get(i).check_Collision(player_2)) {
player2text.remove(i);
i--;
if (player2text.isEmpty()) {
level_count2++;
level_start2 = true;
}
}
}
break;
case 10:
game_on = false;
finish = true;
win2 = true;
win_keys = true;
win_reset2 = true;
break;
} // End of player 2
}// End of game_on
//WINNERS:
else if (finish) {
//player 1 win:
if (win1) {
if (win_reset1) {
player_1.win_reset();
win_reset1 = false;
}
player_1.win_animation();
if (player_1.get_win()) {
textFont(type, 32);
fill(255);
text("To Infinity and Beyond", 500, 300);
}
}//play1win
//player 2 win:
if (win2) {
if (win_reset2) {
player_2.win_reset();
win_reset1 = false;
}
player_2.win_animation();
if (player_2.get_win()) {
textFont(type, 32);
fill(255);
text("To Infinity and Beyond", 500, 300);
}
}//play1win
}//end of finish
}// End of Draw
void keyPressed() {
if (start) {
if (key=='1'){
player_1_choice = true;
}
if (key=='2') {
player_1.set_color(final_red1, final_green1, final_blue1);
player_1_choice = false;
player_2_choice = true;
}
else if (key==ENTER && player_2_choice == true) {
player_2.set_color(final_red2, final_green2, final_blue2);
start = false;
game_on = true;
}
}//end of if start
if (win_keys==false) {
if (key==CODED) {
if (keyCode == UP) {
player_1.set_y_velocity(-2);
player_1.set_x_velocity(0);
}
else if (keyCode == DOWN) {
player_1.set_y_velocity(2);
player_1.set_x_velocity(0);
}
else if (keyCode == RIGHT) {
player_1.set_x_velocity(2);
player_1.set_y_velocity(0);
}
else if (keyCode == LEFT) {
player_1.set_x_velocity(-2);
player_1.set_y_velocity(0);
}
} //end of key code
else if (key=='a') {
player_2.set_x_velocity(-2);
player_2.set_y_velocity(0);
}
else if (key=='d') {
player_2.set_x_velocity(2);
player_2.set_y_velocity(0);
}
else if (key=='w') {
player_2.set_y_velocity(-2);
player_2.set_x_velocity(0);
}
else if (key=='s') {
player_2.set_y_velocity(2);
player_2.set_x_velocity(0);
}
}
}//End of keyPress
void serialEvent(Serial _port) { //this is a callback function
if (myPort == null) return; //this is a hack to cover a bug where the port does not get set up in time.
///this says if the port is not set up yet, bail (for now.)
String input = myPort.readStringUntil('\n');
if (input != null) { //if a '\n' character has in fact now arrived
String [] dual_inputs = split(input, ",");
input = input.trim(); //Take off the '\n' character
pot1 = int(dual_inputs[0]); //Turn it into number
pot2 = int(dual_inputs[1]);
pot3 = int(dual_inputs[2]);
converted1 = (int)map(pot1, 0, 1024, 0, 256);
converted2 = (int)map(pot2, 0, 1024, 0, 256);
converted3= (int)map(pot3, 0, 1024, 0, 256);
println(converted1, converted3, converted2);
myPort.write("Go");
}
}//END OF KEY EVENT
void mousePressed(){
myPort.write("A");
}
Here is the SHAPE class:
class Shape {
float x;
float y;
int velocity_x;
int velocity_y;
int radius;
float new_x;
float new_y;
int magnitude = 2;
int shape_height = radius * 2;
int shape_width = radius * 2;
int red;
int green;
int blue;
PShape player;
//Stages of Win:
boolean win = false;
//Constructor:
Shape() {
velocity_x = 4;
velocity_y=0;
radius = 60;
shape_height = radius * 2;
shape_width = radius * 2;
}
void drawShape1() {
fill(red,green,blue);
ellipse(x, y, 8, 8);
}
void drawLine(){
fill(red, green, blue);
line(x-(radius/2), y,x+(2 * radius),y);
ellipse(x,y, 2,2);
}
void drawShape2(int number_of_sides) {
FloatList new_x_coords;
new_x_coords = new FloatList();
FloatList new_y_coords;
new_y_coords = new FloatList();
float angle = 0;
//FILL ARRAY WITH VERTEXES
for (int i=0;i<number_of_sides; i++) {
new_x = cos(angle) * radius;
new_y = sin(angle) * radius;
new_x_coords.append(new_x);
new_y_coords.append(new_y);
angle+= ((2*PI)/(number_of_sides));
}
player = createShape();
player.beginShape();
for (int i = 0; i<new_x_coords.size(); i++){
player.vertex(new_x_coords.get(i), new_y_coords.get(i));}
player.endShape();
player.setStroke(color(0));
player.setFill((color(red,green,blue)));
shapeMode(CENTER);
shape(player, x, y);
} //End of drawing shape again
//Set velocity:
void set_x_velocity(int direction ) {
velocity_x = direction * magnitude;
}
void set_y_velocity(int direction ) {
velocity_y = direction * magnitude;
}
void set_magnitude(int potentiometer_input) {
magnitude = potentiometer_input;
}
//MOVEMENT
void moveShape() {
x+= velocity_x;
y+= velocity_y;
}
boolean checkBoundaries() {
boolean out_of_bounds = false;
//Out of screen (horizontal)
if (x>(width+(shape_width))|| x < (0 - (shape_width))) {
out_of_bounds = true;
}
//Out of screen (vertical)
if (y>(height+(shape_height)) || y < (0 - (shape_height))) {
out_of_bounds = true;
}
return out_of_bounds;
}
void move_back() {
if (y < 0 - shape_height) {
y = height;
}
else if (y > height+shape_height) {
y = 0;
}
else if (x < 0 - shape_width) {
x = width;
}
else if (x>width+shape_width) {
x = 0;
}
}
//Getters:
float get_X_Position() {
return x;
}
float get_Y_Position() {
return y;
}
int get_shape_Height() {
return shape_height;
}
int get_shape_Width() {
return shape_width;
}
boolean get_win() {
return win;
}
//Setters:
void set_Position(float x, float y, int x_v) {
this.x = x;
this.y =y;
this.velocity_x = x_v;
}
void set_color(int _red, int _green, int _blue){
red = _red;
green = _green;
blue = _blue;
}
//WINNER:
void win_reset() {
x = 0+shape_width;
y = height / 2;
velocity_x = 2;
velocity_y = 0;
}
void drawCircle() {
fill(red, green, blue);
ellipse(x, y, radius*2, radius*2);
}
void win_animation() {
if (x < width / 5) {
this.drawShape2(10);
this.moveShape();
}
else if (x < (2*(width / 5)) && x >= (width / 5)) {
this.drawShape2(14);
this.moveShape();
}
else if (x < (3*(width / 5)) && x >= (2*width) / 5) {
this.drawShape2(18);
this.moveShape();
}
else if (x < (4*(width / 5)) && x >= (3*width) / 5) {
this.drawShape2(22);
this.moveShape();
}
else if (x < width && x >= (4*(width / 5))) {
this.drawCircle();
this.moveShape();
if (x == width - 60) {
velocity_x=0;
if (radius< 1010) {
radius+=10;
}
else if (radius == 1010) {
win = true;
}//end of win sign
}//side of screen
}//end of last else-if
}//win animation
}//End of class
Here is the TEXT class:
class Text {
//Different languages arrays that must be converted into stringsList
String [] EnglishArray = {
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"
};
StringList English = new StringList(EnglishArray);
String [] GermanArray = {
"eins", "zwei", "drei", "vier", "funf", "sechs", "sieben", "acht", "neun", "zehn"
};
StringList German = new StringList(GermanArray);
String [] FrenchArray = {
"un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix"
};
StringList French = new StringList(FrenchArray);
String [] HindiArray = {
"ek", "do", "teen", "char", "panch", "chech", "saat", "aath", "nau", "das"
};
StringList Hindi = new StringList(HindiArray);
String [] JapaneseArray = {
"ichi", "ni", "san", "yon", "go", "roku", "nana", "hatchi", "kyu", "ju"
};
StringList Japanese = new StringList(JapaneseArray);
String [] MandarinArray= {
"yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu", "shi"
};
StringList Mandarin = new StringList(MandarinArray);
String [] ItalianArray = {
"uno", "due", "tre", "quattro", "cinque", "sei", "sette", "otto", "nove", "dieci"
};
StringList Italian = new StringList(ItalianArray);
String [] SpanishArray = {
"uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez"
};
StringList Spanish = new StringList(SpanishArray);
String [] RegularNumbersArray = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
};
StringList RegularNumbers = new StringList(RegularNumbersArray);
String [] BinaryArray = {
"1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010"
};
StringList Binary = new StringList(BinaryArray);
String [] SwahiliArray = {
"moja", "mbili", "tatu", "nne", "tano", "sita", "saba", "nane", "tisa", "kumi"
};
PFont basic_font = loadFont("ACaslonPro-Regular-32.vlw");
float x;
float y;
float velocity_x;
float velocity_y;
float text_height;
float text_width;
String words;
int which_language;
int index;
int red;
int green;
int blue;
//Constructor for load screen/win
Text(String input, int fontsize) {
x = width / 2;
y = height / 4;
textFont(basic_font);
words = input;
textSize(fontsize);
textAlign(LEFT);
}
//Constructor for regular game:
Text(int language, int number) {
x = random(0,width+1 - text_width);
y = random(0, height+1 - text_height);
velocity_x = random(0,6);
velocity_y = random(1,6);
which_language = language;
index = number;
}
void drawText() {
fill(red, green, blue);
String word="";
switch(which_language) {
case 1:
word = EnglishArray[index];
break;
case 2:
word = GermanArray[index];
break;
case 3:
word = FrenchArray[index];
break;
case 4:
word = HindiArray[index];
break;
case 5:
word = MandarinArray[index];
break;
case 6:
word = ItalianArray[index];
break;
case 7:
word = SpanishArray[index];
break;
case 8:
word = RegularNumbersArray[index];
break;
case 9:
word = BinaryArray[index];
break;
case 10:
word = SwahiliArray[index];
break;
}
text(word, x,y);
textFont(basic_font);
textSize(25);
textAlign(LEFT);
text_height = textDescent() + textAscent();
text_width = textWidth(word);
} // End of Draw
void moveText() {
x+= velocity_x;
y+= velocity_y;
}
//MOVEMENT
boolean checkBoundaries() {
boolean out_of_bounds = false;
//Out of screen (horizontal)
if (x>(width+(text_width))|| x < (0 - (text_width))) {
out_of_bounds = true;
}
//Out of screen (vertical)
if (y>(height+(text_height)) || y < (0 - (text_height))) {
out_of_bounds = true;
}
return out_of_bounds;
}
void move_back_text() {
//Will randomize the side from which the text returns
//Will maintain the same magnitude of velocity
/* Must figure out magnitude from original x and y velocities
Then use magnitude to determine the new velocities
Use random to find new angle of reentry (radians)
magintude SIN (theta) = y
magnitude COS (theta) = X
*/
float theta = random(.1*PI, .9*PI);
float magnitude = 0;
magnitude = sqrt((float)(Math.pow(velocity_x, 2))+(float)(Math.pow(velocity_y, 2)));
int side = (int) random(1, 5);
switch (side) {
//Top
case 1:
y = 0 - text_height;
x = width/2;
velocity_y = (magnitude * sin(theta));
velocity_x = (magnitude * cos(theta));
break;
//Bottom
case 2:
y = height + text_height;
x = 450;
velocity_y = -(magnitude * sin(theta));
velocity_x = (magnitude * cos(theta));
break;
//Left
case 3:
y = 375;
x = 0 - text_width;
velocity_y = (magnitude * sin(theta));
velocity_x = (magnitude * cos(theta));
break;
//Right
case 4:
y = 375;
x = width + text_width;
velocity_y = (magnitude * sin(theta));
velocity_x = -(magnitude * cos(theta));
break;
}
}
//COLLISION
boolean check_Collision(Shape temporary) {
boolean signal = false;
int temp_shape_height = temporary.get_shape_Height();
int temp_shape_width = temporary.get_shape_Width();
float temp_x = temporary.get_X_Position()-temp_shape_width/2;
float temp_y = temporary.get_Y_Position()-temp_shape_height/2;
if (x < (temp_x + temp_shape_width) && (x+text_width) > (temp_x )) {
if (y < (temp_y + temp_shape_height) && (y+text_height) > (temp_y )) {
signal = true;
}
else {
signal = false;
}
}
return signal;
}
void set_text_color(int _red, int _green, int _blue){
red = _red;
green = _green;
blue = _blue;
}
}//End of text
To improve on this game, and to progress on this further, I’d maybe think about venturing into the third dimension for the shapes, making it more interactive (with the video tracking) so it can be a team game, and making obstacles!
I had a really fun time this class and I had a lotta fun with you all, through bugs of frustration to sheer hilarity to coding successes! : )
My impression of computers has always been is the cold and logical digital machine of 1’s and 0’s. It is capable of extreme computation, a brain whose organization can is far superior than the human brain’s. It’s still unbelievable that these machines are the driving force behind latest innovations of today. I’m used to thinking the computer as a GPT (general purpose technology) with the “potential of impacting different sectors of the economy. And as a potential computer science major, I am want delve into the possibilities. And in this class, I was fortunate enough to explore the more physical aspect of it with the Arduino and the 3D printer. The field of computer science has been reeking with progress and inventions.
Until this class, I had never really thought about the actual communication between humans and computers. David Deutsch in The Beginning of Infinity argues that the jump to universality, or as he says: “the tendency of gradually improving systems to undergo a sudden large increase in functionality, becoming universal in some domain” and that it is solely through digital systems that this jump is reached. And he explores language and its evolution as a kind of digital process because of all the rules of language that “cover not only every word but also every possible word.” One the one hand, computers with their own language of 1’s and 0’s are able to accomplish many more things than humans. With algorithms and functions, they are able compute more faster and more efficiently than us. But on the other hand, humans with their own language have furthering their ideas much longer than computers have been (since the beginning of civilization), and are also the ones responsible for the creation of the computer itself. And it is because of these “digital” rules that humans have been able to achieve so much.
But there is also the “analog” aspect of human language, and it is how humans have tried to translate these aspects into their created digital computer language that is truly interesting. The digital is cold-hearted yes/no, whil the analog is the more emotional and tonal part of language. While we have the digital rules to construct sentences, there are far more nuances to the human language that convey how we are “feeling”. The best way to understand the complication between translating the analog aspect of the human language and the digital understanding of the computer is a conversation by email vs. a conversation on the phone vs. even in person. The digital email message is just the basic message itself but the actual feeling of the writer is often lost or misunderstood. However, in person or on the phone, the listener can understand the communicator more thoroughly because he is aided by gestures, facial expressions, and the tone of voice. Thus it is putting the analog part of human language back into the digital computer language that I think is one biggest goals of computational media.
Often times when choosing between analog and digital there are clear sides, but when they try to interact and “work together” it is interesting to see the result. And I think digital art is one of the clearest way people have been successfully expressing themselves and their feelings through computers. I haven’t had much exposure to it.e traditional sense of the fine arts, but I think because of the growing potential computers have, the potential of digital art is also growing too.
The other interesting field I don’t know much about but I think interesting is AI. I really don’t have much exposure to this field, but it is interesting to compare a more human-like robot to the more pure analytically-minded IBM’s Watson who participated in Jeopardy. In this field, I think that the analog communication of human language will be successfully interpreted by these digital machines of ours.
Anyways, to finish off this meditation, here is an digital art installation at a gallery in Japan that I worked at.
It’s simple in design, but I like how it captures the wonder and appreciation of, and curiosity about the world — the more analog feelings of my heartstrings.
import java.io.*;
StringDict store_input;
PFont type;
PFont read;
int word_count;
boolean search_switch;
PImage permanent;
PImage note;
// Variable to store text currently being typed
String typing = "";
// Variable to store saved text when return is hit
String saved = "";
String [] keys = {
"",
"adjective 1",
"adjective 2",
"noun 1",
"noun 2",
"noun 3",
"noun 4",
"adjective 3",
"adjective 4",
"adjective 5",
"verb 1 (in -ing) form",
"verb 2",
"adverb 1",
"adjective 5",
"pronoun 1",
"verb 2",
"adjective 6",
"noun 5",
"noun 6",
"adjective 8",
"adjective 9",
"noun 7",
"noun 8",
"adjective 11",
"adjective 12",
"your name",
"adjective 13",
"noun 9",
"noun 10"
};
void setup() {
size(1000, 700);
word_count = 0;
type = loadFont("Cochin-48.vlw");
read = loadFont("ComicSansMS-24.vlw");
store_input = new StringDict();
search_switch = true;
note = loadImage("note.png");
}
void draw() {
//TEXT ENTRY:
if (word_count<29) {
textFont(type);
fill(0);
background(255);
text("Let's play madlibs!", 100, 40);
text("Please enter: "+keys[word_count], 100, 100);
text("Your input: "+typing, 100, 200);
text("You have entered: "+saved, 100,250);
store_input.set( keys[word_count],saved);
System.out.println(word_count);
}//End of text entry
//Text feedback:
else if (word_count >=29) {
image(note, 0,0);
fill(0);
textFont(read);
text("Dear "+store_input.get(keys[1])+" parents,\n I'm sorry I haven't written to you in a while, I'm writing to you guys to ",50, 30);
text("tell you that I've officially settled in at NYU! It's been a "+store_input.get(keys[2]), 50, 90);
text(" week, but I've finally finalized my classes. I'm taking the history of", 50, 120);
text(store_input.get(keys[3])+" the science of "+store_input.get(keys[4])+" a seminar on "+ store_input.get(keys[5])+" and my elective that" ,50, 150);
text("is about "+store_input.get(keys[6])+". One of my classes is so "+store_input.get(keys[7])+" because my teacher",50, 175);
text("is "+store_input.get(keys[8])+". He has this "+store_input.get(keys[9])+" habit of"+store_input.get(keys[10])+" that makes me want to ", 50, 210);
text(store_input.get(keys[11])+" so "+store_input.get(keys[12])+". My roommate is pretty "+store_input.get(keys[13])+".", 50, 240);
text("Once, in the middle of the night, "+store_input.get(keys[14])+ " suddenly started to "+store_input.get(keys[15])+".",50, 270);
text("It was so "+store_input.get(keys[16])+"! But, "+store_input.get(keys[14])+" reassured me everything was ok.", 50, 300);
text("Also, "+store_input.get(keys[14])+" has this ginormous "+store_input.get(keys[17])+" that blocks the "+store_input.get(keys[18])+", but ultimately", 50, 330);
text("we get along. New York is the most "+store_input.get(keys[19]),50,360);
text("city I have ever seen! It's full of "+store_input.get(keys[20])+" people", 50, 390);
text("and is a huge cultural, transportation, and "+store_input.get(keys[21])+", center!", 50, 420);
text("There are some downsides living here. For the first time today, a homeless guy", 50, 450);
text("threw a "+store_input.get(keys[22])+" at me. It was a little "+store_input.get(keys[23]), 50, 480);
text("but now I'm fine, so don't worry. Anyways, I have to do my", 50, 510);
text("homework now, so I'll email you like a more "+store_input.get(keys[24])+" person next time.", 50, 540);
text("Love, "+store_input.get(keys[23])+", your "+store_input.get(keys[26])+" "+store_input.get(keys[27]), 50, 570);
text("P.S. Do you like the "+store_input.get(keys[28])+" I sent you?\n I got it at the NYU Bookstore!",50, 610);
if (search_switch){
queryGoogleImages(store_input.get(keys[28]));
search_switch = false;
}
image(permanent, 800,475, 150, 150);
}
} // End of Draw
void keyPressed() {
// If the return key is pressed, save the String and clear it
if (key == '\n' ) {
saved = typing;
// A String can be cleared by setting it equal to ""
typing = "";
word_count++;
}
else {
// Otherwise, concatenate the String
// Each character typed by the user is added to the end of the String variable.
typing = typing + key;
}
}
void queryGoogleImages(String _q) {
String url = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" + _q;
//this is a special url for getting google image results as json
println(url);
JSONObject result = loadJSONObject(url);
//make a network call and get the results back into a json object
println(result);
//make sure the results are not an error
int status = result.getInt("responseStatus");
if (status != 200) {
String reason = result.getString("responseDetails");
//this sucks that google does not allow you to do very many of these in a row
println("You are too fast, take a break." + reason);
return;
}
//go one level down in to the results to just get the data not the info about the package
JSONObject response = result.getJSONObject("responseData");
//get the arrary of stuff you got back
JSONArray values = response.getJSONArray("results");
for (int i = 0; i < min(values.size(),3); i++) {
//pick out the picture information for each picture
JSONObject thisGuy = values.getJSONObject(0);
String r = thisGuy.getString("url");
PImage thisImage = loadImage(r);
permanent = loadImage(r);
if (thisImage != null) {
image(thisImage, 770,475, 200, 200);
}
//make vOffset bigger so we will draw the next picture below this one.
}//end for
}
This game was supposed to be a simple “Welcome” to the spring and a farewell to the winter. I used the color tracking technique, so the player is a bird. In the background, the snow and rain fall. The bird must simply catch the sun and then slowly the bad weather will clear.
To downlaod the e
import processing.video.Capture;
Capture cam;//
int threshold = 20;
int aveX, aveY;
float objectR =255;
float objectG = 0;
float objectB = 0;
Shape self;
ArrayList<Shape> sun;
ArrayList<Shape> rain;
ArrayList<Shape> snow;
PFont text;
PImage spring;
int count;
boolean start;
boolean win;
void setup() {
size(1280, 720);
println(Capture.list());
cam = new Capture(this, width, height);
cam.start();
//GAME STUFF:
spring = loadImage("spring.jpg");
self = new Shape(0, aveX, aveY);
sun = new ArrayList<Shape>();
for (int i=0;i<30; i++) {
sun.add(new Shape(1));
}
rain = new ArrayList<Shape>();
for (int i=0;i<40; i++) {
rain.add(new Shape(3));
}
snow = new ArrayList<Shape>();
for (int i=0;i<20; i++) {
snow.add(new Shape(2));
}
count = 0;
text = loadFont("HanziPenSC-W5-48.vlw");
//win = loadImage;
start = false;
win = false;
}
void draw() {
if (cam.available()) {
cam.read();
int totalFoundPixels= 0; //we are going to find the average location of change pixes so
int sumX = 0; //we will need the sum of all the x find, the sum of all the y find and the total finds
int sumY = 0;
//enter into the classic nested for statements of computer vision
for (int row = 0; row < cam.height; row++) {
for (int col = 0; col < cam.width; col++) {
//the pixels file into the room long line you use this simple formula to find what row and column the sit in
int offset = row * cam.width + col;
//pull out the same pixel from the current frame
int thisColor = cam.pixels[offset];
//pull out the individual colors for both pixels
float r = red(thisColor);
float g = green(thisColor);
float b = blue(thisColor);
//in a color "space" you find the distance between color the same whay you would in a cartesian space, phythag or dist in processing
float diff = dist(r, g, b, objectR, objectG, objectB);
if (diff < threshold) { //if it is close enough in size, add it to the average
sumX = sumX + col;
sumY= sumY + row;
totalFoundPixels++;
//if (debug) cam.pixels[offset] = 0xff000000;//debugging
}
}
}
if (totalFoundPixels > 0) {
aveX = sumX/totalFoundPixels;
aveY = sumY/totalFoundPixels;
}
if (start==false && win == false) {
image(cam, 0, 0);
textFont(text, 32);
text("Mouse click to begin tracking. Then press '1' to begin the game!", 250, 100);
self.self_move(aveX, aveY, 0);
}
else if (start == true && win == false) {//begin game
background(70, 130, 180);
System.out.println("1");
//GAME CODE:
if (count < 10) {
//40 rain, 20 snow
//MOVE WEATHER:
for (int i=0;i<snow.size()-1; i++) {
snow.get(i).moveWeather(2);
if (snow.get(i).checkBoundaries()) {
snow.get(i).reset_position();
}
}
for (int i=0;i<rain.size()-1; i++) {
rain.get(i).moveWeather(3);
if (rain.get(i).checkBoundaries()) {
rain.get(i).reset_position();
}
}
System.out.println(count);
for (int i=0;i<sun.size()-1; i++) {
sun.get(i).moveWeather(1);
if (sun.get(i).checkBoundaries()) {
sun.get(i).reset_position();
}
if (sun.get(i).check_Collision(self)) {
count++;
System.out.println(count);
sun.remove(i);
}
}
self.self_move(aveX, aveY, 0);
}//end of score 1 (count < 10)
else if (count < 15 && count >= 10) { //other score:
System.out.println("2");
background(133, 228, 247);
//30 rain, 10 snow
for (int i = 0; i<rain.size()-10; i++) {
rain.remove(i);
}
for (int i = 0; i<snow.size()-10; i++) {
snow.remove(i);
}
//MOVE WEATHER:
for (int i=0;i<snow.size()-1; i++) {
snow.get(i).moveWeather(2);
if (snow.get(i).checkBoundaries()) {
snow.get(i).reset_position();
}
}
for (int i=0;i<rain.size()-1; i++) {
rain.get(i).moveWeather(3);
if (rain.get(i).checkBoundaries()) {
rain.get(i).reset_position();
}
}
for (int i=0;i<sun.size()-1; i++) {
sun.get(i).moveWeather(1);
if (sun.get(i).checkBoundaries()) {
sun.get(i).reset_position();
}
if (sun.get(i).check_Collision(self)) {
count++;
sun.remove(i);
}
}
self.self_move(aveX, aveY, 0);
}// End of Score
else if (count < 20 && count >= 15) { //other score
System.out.println("3");
background(131, 205, 247);
//25 rain drops, 5 snow
for (int i = 0; i<rain.size()-4; i++) {
rain.remove(i);
}
for (int i = 0; i<snow.size()-4; i++) {
snow.remove(i);
if (snow.get(i).checkBoundaries()) {
snow.get(i).reset_position();
}
}
for (int i=0;i<rain.size()-1; i++) {
rain.get(i).moveWeather(3);
if (rain.get(i).checkBoundaries()) {
rain.get(i).reset_position();
}
}
for (int j=0;j<snow.size()-1; j++) {
snow.get(j).moveWeather(2);
if (snow.get(j).checkBoundaries()) {
snow.get(j).reset_position();
}
}
for (int i=0;i<sun.size()-1; i++) {
sun.get(i).moveWeather(1);
if (sun.get(i).checkBoundaries()) {
sun.get(i).reset_position();
}
if (sun.get(i).check_Collision(self)) {
count++;
sun.remove(i);
}
}
self.self_move(aveX, aveY, 0);
} //End of Score
else if (count >= 20) { //other score:
System.out.println("4");
//15 RAIN DROPS
background(135, 206, 250);
for (int i = 0; i<rain.size()-4; i++) {
rain.remove(i);
}
snow.clear();
System.out.println("This is count "+ count);
//MOVE WEATHER:
for (int i=0;i<rain.size()-1; i++) {
rain.get(i).moveWeather(3);
if (rain.get(i).checkBoundaries()) {
rain.get(i).reset_position();
}
}
for (int i=0;i<sun.size()-1; i++) {
sun.get(i).moveWeather(1);
if (sun.get(i).checkBoundaries()) {
sun.get(i).reset_position();
}
if (sun.get(i).check_Collision(self)) {
count++;
sun.remove(i);
}
}
self.self_move(aveX, aveY, 0);
} //End of Score
else if (count == 30) {
System.out.println("5");
win = true;
start = false;
self.self_move(aveX, aveY, 0);
}
}//END OF GAME START
else if(win == true && start == false){
sun.clear();
rain.clear();
background(0, 191, 255);
image(spring, 0, 0);
textFont(text, 32);
text("Happy Spring! Enjoy the warmth.", 500, 100);
self.self_move(aveX, aveY, 0);
//draw other image?
}
}//End of camera available
}//End of draw
void mousePressed() {
//if they click, use that picture for the new thing to follow
int offset = mouseY * cam.width + mouseX;
//pull out the same pixel from the current frame
int foundColor = cam.pixels[offset];
//pull out the individual colors for both pixels
objectR = red(foundColor);
objectG = green(foundColor);
objectB = blue(foundColor);
}
void keyPressed() {
if (key == '1') {
println("Start the game");
start = true;
}
}
Here is my SHAPE class.
class Shape {
//shape color:
int r;
int g;
int b;
//shape size
int shape_width;
int shape_height;
int shape_size;
//shape position
int x;
int y;
//shape speed
float velocity_x;
float velocity_y;
//Images:
PImage sun;
PImage snow;
PImage bird;
//for rain:
int radius;
//Constuctor 2: for SNOW + SUN + RAIN
Shape (int type) {
switch(type)
{
case 1:
sun = loadImage("Sun.png");
shape_width = 50;
shape_height=50;
x = (int)random(width);
y = 0 - shape_height;
velocity_y=(int)random(1, 9);
break;
case 2:
snow = loadImage ("snowflake.png");
shape_width = 50;
shape_height=50;
x = (int)random(width);
y = 0 - shape_height;
velocity_y = (int)random(1, 9);
break;
case 3:
radius = 8;
x = (int)random(width);
y= -r*4;
velocity_y = (int)random(1, 9);
shape_height = r*4;
break;
}
}
Shape(int type, int _x, int _y){
bird = loadImage("Bird.png");
shape_width=75;
shape_height=75;
x = _x;
y = _y;
}
void drawshape(int type) {
stroke(0, 0, 255);
fill(0, 0, 255);
switch (type) {
case 0:
image(bird, x, y);
break;
case 1:
image(sun, x, y);
break;
case 2:
image(snow, x, y);
break;
case 3:
for (int i = 2; i < radius; i++ ) {
ellipse(x, y + i*4, i*2, i*2);
}
break;
}
}
//MOVING shape:
//for "INPUTS"
void self_move(int input_x, int input_y,int _type) {
x=input_x;
y=input_y;
this.drawshape(_type);
}
void moveWeather(int type) {
y+=velocity_y;
this.drawshape(type);
}
boolean checkBoundaries() {
boolean out_of_bounds = false;
//BOTTOM
if (y>(height+(1.25*shape_height))) {
out_of_bounds = true;
}
return out_of_bounds;
}
void reset_position(){
y = 0 - shape_height;
x = (int)random(width);
}
//Getter Methods for collision detection
int get_X_Position() {
return x;
}
int get_Y_Position() {
return y;
}
int get_shape_Height() {
return shape_height;
}
int get_shape_Width() {
return shape_width;
}
//COLLISION
boolean check_Collision(Shape temporary) {
boolean signal = false;
int temp_x = temporary.get_X_Position();
int temp_y = temporary.get_Y_Position();
int temp_shape_height = temporary.get_shape_Height();
int temp_shape_width = temporary.get_shape_Width();
if (x < (temp_x + temp_shape_width) && (x+shape_width) > (temp_x )) {
if (y < (temp_y + temp_shape_height) && (y+shape_height) > (temp_y )) {
signal = true;
}
else {
signal = false;
}
}
return signal;
}
}// End of shape class
I think this week’s assignment adequately embodies the Of Mice and Men quote: “best laid plans o’mice and men” often go awry. I had a much better idea but it really fell apart. So now I’m saving that one for a rainy day presenting a lamer substitute for this week’s assignment.
Basically, the player of the game is a (supposed to be) constantly-moving, never-tiring person who runs around the screen. The potentiometer input is supposed to dictate the angle of movement and the goal of the game is to just run around the screen, avoiding the 5 rectangle obstacles and the edges.
The potentiometer input manages to be processed by my Processing program, but there are some other problems that I wasn’t able to fix in time.
Here’s my main code:
import processing.serial.*;
Serial myPort;
boolean main_game;
int reading;
float angle;
Person self;
PFont wound;
int wound_count;
PShape starry;
Obstacles [] rectangle = new Obstacles[5];
ArrayList<Star>counting_star;
void setup() {
size(800, 750, P2D);
self = new Person(400, 700, 1);
wound = loadFont("AmericanTypewriter-CondensedBold-32.vlw");
wound_count = 0;
counting_star = new ArrayList<Star>();
main_game = true;
//Arduino --> Processing:
println(Serial.list());
String portName = Serial.list()[0];
myPort = new Serial(this, "/dev/tty.usbmodem1411", 9600);
myPort.readStringUntil('\n');
//For creating a star:
PShape starry = createShape();
starry.beginShape();
// You can set fill and stroke
starry.fill(255, 255, 0);
starry.stroke(255, 255, 0);
// Here, we are hardcoding a series of vertices
starry.vertex(0, -50);
starry.vertex(14, -20);
starry.vertex(47, -15);
starry.vertex(23, 7);
starry.vertex(29, 40);
starry.vertex(0, 25);
starry.vertex(-29, 40);
starry.vertex(-23, 7);
starry.vertex(-47, -15);
starry.vertex(-14, -20);
starry.endShape(CLOSE);
for (int i=0;i<rectangle.length;i++) {
rectangle[i] = new Obstacles((int)random(0, width-100), (int)random(0, height-100));
}
for (int i = 0; i < 0; i++) {
counting_star.add(new Star(starry));
}
} // End of SETUP
void draw() {
if (main_game) {
background (0, 145, 145);
//Obstacles:
for (int i=0;i<rectangle.length;i++) {
rectangle[i].drawRect();
}
//Star:
for (int i = 0; i < counting_star.size(); i++) {
counting_star.get(i).drawStar();
}
//Person:
self.changeDirectionAngle();
self.set_theta(angle);
self.movePerson();
//self.rotate_person();
if (self.checkBoundaries()) {
self.changeColor();
//self.set_random_move();
self.bounce();
textFont(wound, 48);
text("Ouch!", 350, 50);
wound_count++;
counting_star.add(new Star(starry));
}
// for (int i=0;i<rectangle.length; i++) {
if (self.check_Collision(rectangle[i])) {
wound_count++;
counting_star.add(new Star(starry));
}
// }
}
if (wound_count==6) {
main game = false;
background(255, 0, 0);
textFont(wound, 48);
text("You've died!", 350, 50);
}
} // End of DRAW
void serialEvent(Serial _port) { //this is a callback function
if (myPort == null) return; //this is a hack to cover a bug where the port does not get set up in time.
//this says if the port is not set up yet, bail (for now.)
String input = myPort.readStringUntil('\n');
if (input != null) { //if a '\n' character has in fact now arrived
input = input.trim(); //Take off the '\n' character
reading = int(input); //Turn it into number
System.out.println("Here is regular number"+reading);
angle = map(reading, 0, 1000, 0, 360);
println("Here is angle"+angle);
}
}
Obstacles class:
class Obstacles {
int x;
int y;
int r = (int)random(0,255);
int b =(int)random(0,255);
int g = (int)random(0,255);
int obstacle_width = (int)(random(50,150));
int obstacle_height = (int)(random(50,150));
//Constructor
Obstacles(int _x, int _y){
x = _x;
y = _y;
}
void drawRect(){
stroke(r, b, g);
fill(r, b, g);
rect(x, y, obstacle_width, obstacle_height);
}
int get_X_Position(){
return x;
}
int get_Y_Position(){
return Y;
}
int get_Width(){
return obstacle_width;
}
int get_Height(){
return obstacle_height;
}
//void check_Collision(){
}
Person class:
class Person{
int pos_X;
int pos_Y;
int person_width = 0;
int person_height =0;
float size;
//Coloring head:
int r = 255;
int b = 255;
int g = 255;
//MOVEMENT:
float velocity_X =3;
float velocity_Y =3;
float theta; //potentiometer input
//Constructor
Person(int x, int y, float _size){
pos_X = x;
pos_Y = y;
size = _size;
}
void drawPerson() {
line( pos_X+20*size, pos_Y, pos_X+20*size, pos_Y+70*size) ; // Body
line( pos_X+20*size, pos_Y+70*size, pos_X, pos_Y+130*size) ; //Left leg
line( pos_X+20*size, pos_Y+70*size, pos_X+40*size, pos_Y+130*size) ; // right leg
line( pos_X+20*size, pos_Y+30*size, pos_X, pos_Y+70*size) ; // left arm
line( pos_X+20*size, pos_Y+30*size, pos_X+40*size, pos_Y+70*size) ; // right arm
fill(r,b,g) ; // grey for head fill
ellipse(pos_X+20*size, pos_Y, 40*size, 35*size) ; // head
person_width = (int)(40*size);
person_height = (int)((65*size) + (65*size) + (35*size));
}
void movePerson(){
pos_X += velocity_X;
pos_Y += velocity_Y;
this.drawPerson();
}
void changeColor(){
r = (int)random(0, 255);
b = (int)random(0, 255);
g = (int)random (0, 255);
}
boolean checkBoundaries() {
boolean out_of_bounds = false;
//Out of screen (horizontal)
if (pos_X+(40*size) > width || pos_X < 0) {
out_of_bounds = true;
}
//Out of screen (vertical)
if (pos_Y + person_height >height ||pos_Y <0){
out_of_bounds = true;
}
return out_of_bounds;
}
void bounce(){
if (pos_X + person_width >width || pos_X < 0) {
velocity_X *= -1;
}
//Out of screen (vertical)
if (pos_Y + person_height > height ||pos_Y <0 ) {
velocity_Y *= -1;
}
}
void set_random_move(){
velocity_X = (int)random(1,5);
velocity_Y = (int)random(1,5);
}
void set_theta(float _theta){
theta = radians(_theta);
System.out.println("here is theta"+theta);
}
void changeDirectionAngle() {
//"Rotate" the face's velocities with given angle:
velocity_X = cos(theta);
velocity_Y = sin(theta);
}
//COLLISION
boolean check_Collision(Obstacles temporary) {
boolean signal = false;
int temp_x = temporary.get_X_Position();
int temp_y = temporary.get_Y_Position();
int temp_height = temporary.get_Height();
int temp_width = temporary.get_Width();
if (pos_X < (temp_x + temp_width) && (x+person_width) > (temp_x )) {
if (pos_Y < (temp_y + temp_height) && (y+person_height) > (temp_y )) {
signal = true;
}
else {
signal = false;
}
}
return signal;
}
/*
void rotate_person(){
pushMatrix();
rotate(theta);
this.movePerson();
popMatrix();
}
*/
}//end of person class
Star class:
class Star{
PShape star;
int x= (int)random(0,width);
int y = (int)random(0, height);
Star( PShape create_star){
star = create_star;
}
void drawStar(){
shape(star, x,y);
}
}
So the problems I’m having:
– The bounce function is ignored somehow
– The potentiometer angle isn’t processed completely as I hoped and the movement is jerky. Sometimes the person doesn’t move at all (which may be due to a mathematical computation but I’m very bewildered by this).
This simple game revolves around protecting the miracle tulip that is struggling to grow and flourish in this otherwise barren stretch of land. The player must water the plant from the very obliging and cheerful rain cloud (mouse press over the cloud) while preventing the pesky deer from nibbling on miracle plant.
Let it growwww, let it growww
In order to play the game properly, all the images and font set must be downloaded as well. They can be downloaded: here
Here is all the code:
Flower flower;
int [] flower_width = {
32, 43, 57, 88, 95, 94, 122, 122
};
int[] flower_height = {
60, 80, 100, 120, 140, 160, 180, 200
};
Rain rain;
Deer deer;
int deer_direction = 0;
PFont deerCollide;
PImage dead_Flower;
Sun sun;
Thunder thunder;
PImage lightening_bolt;
//Clicking cloud:
boolean overCloud = false;
boolean locked = false;
int rain_click = 0;
void setup() {
size(700, 450);
flower = new Flower("1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png");
rain = new Rain("Cloud3.png");
thunder = new Thunder("Thunder3.png");
lightening_bolt = loadImage("Lightening.png");
//FOR DEER: (String filename1, String filename2, int _x, int _y, int _velocity)
deer = new Deer("DeerLeft.png", "DeerRight.png", -5, 1);
deerCollide = loadFont ("ArialMT-16.vlw");
dead_Flower = loadImage("skull.png");
sun = new Sun("Sun.png");
}
void draw () {
//Background and Soil
fill(139, 69, 19);
rect(0, 425, width, 70);
fill(179, 232, 255);
rect(0, 0, width, 425);
//Use counter variable each time a certain button is pressed --> use if statement and increment array
thunder.thunder_Draw(75, 100);
sun.sun_Draw(620, -45);
//SWITCH DEER ORIENTATION
deer.deer_Move(deer_direction);
rain.rain_Draw(350 - (111/2), 100);
fill(00, 00, 255, 50);
//CLICKABLE AREA for RAIN CLOUD
if (mouseX >= 295 && mouseX <=495 &&
mouseY >= 100 && mouseY <= 311) {
overCloud = true;
}
else {
overCloud = false;
}
if (rain_click <= 10){
flower.flower_Draw(350, (height-(flower_height[0]+18)), 1);
}
if (rain_click <= 20 && rain_click > 10){
flower.flower_Draw(350, (height-(flower_height[1]+18)), 2);
}
if (rain_click <= 30 && rain_click > 20){
flower.flower_Draw(350, (height-(flower_height[2]+18)), 3);
}
if (rain_click <= 40 && rain_click > 30){
flower.flower_Draw(350, (height-(flower_height[3]+18)), 4);
}
if (rain_click <= 50 && rain_click > 40){
flower.flower_Draw(350, (height-(flower_height[4]+18)), 5);
}
if (rain_click <= 60 && rain_click > 50){
flower.flower_Draw(350, (height-(flower_height[5]+18)), 6);
}
if (rain_click <= 70 && rain_click > 6){
flower.flower_Draw(350, (height-(flower_height[6]+18)), 7);
}
if (rain_click >= 80){
flower.flower_Draw(350, (height-(flower_height[7]+18)), 8);
}
textFont(deerCollide, 16);
fill(0);
deer.check_collision();
} // END OF DRAW
//Flower Class
class Flower {
PImage flower1;
PImage flower2;
PImage flower3;
PImage flower4;
PImage flower5;
PImage flower6;
PImage flower7;
PImage flower8;
int x;
int y;
//Constructor
Flower(String filename1, String filename2, String filename3, String filename4, String filename5, String filename6, String filename7, String filename8) {
flower1 = loadImage (filename1);
flower2 = loadImage (filename2);
flower3 = loadImage (filename3);
flower4 = loadImage (filename4);
flower5 = loadImage (filename5);
flower6 = loadImage (filename6);
flower7 = loadImage (filename7);
flower8 = loadImage (filename8);
}
void flower_Draw(float x, float y, int flower_type) {
switch (flower_type) {
case 0:
image(flower1, x, y);
break;
case 1:
image(flower2, x, y);
break;
case 2:
image(flower3, x, y);
break;
case 3:
image(flower5, x, y);
break;
case 4:
image(flower6, x, y);
break;
case 5:
image(flower7, x, y);
break;
case 6:
image(flower8, x, y);
}
}
//End of Flower Class
}
//Sun class
class Sun {
PImage sun_ball;
int x;
int y;
//Sun constructor
Sun(String filename1) {
sun_ball = loadImage(filename1);
}
void sun_Draw(int x, int y) {
image(sun_ball, x, y);
}
}
//End of Sun class
//Rain class
class Rain {
PImage cloud;
int cloud_x;
int cloud_y;
//Cloud constructor
Rain(String filename) {
cloud = loadImage(filename);
}
void rain_Draw(float x, float y) {
image(cloud, x, y);
}
}
//End of Rain class
//Thunder class
class Thunder {
PImage lightening;
int cloud_x;
int cloud_y;
//Cloud constructor
Thunder(String filename) {
lightening = loadImage(filename);
}
void thunder_Draw(int x, int y) {
image(lightening, x, y);
}
}//End of THUNDER class
//Deer class
class Deer {
PImage predator1;
PImage predator2;
int x;
int y;
float velocity;
//Deer constructor
Deer(String filename1, String filename2, int _x, float _velocity) {
predator1 = loadImage(filename1);
predator2 = loadImage(filename2);
x = _x;
velocity = _velocity;
}
void deer_Draw(int x, int deer_direction) {
y = 340;
switch (deer_direction) {
case 0:
image(predator1, x, y);
break;
case 1:
image(predator2, x, y);
break;
}
}
//Move deer
int get_x () {
return x;
}
//Get direction:
int get_direction() {
return deer_direction;
}
float get_velocity() {
return velocity;
}
void deer_Move(int deer_direction) {
x += velocity;
this.deer_Draw(x, deer_direction);
}
void deer_Reset() {
int side = (int) random(1, 3);
if (side ==1 ) {
x = -100;
velocity = 2;
deer_direction = 0;
}
else {
x = width+50;
velocity = -2;
deer_direction = 1;
}
}
boolean check_collision() {
boolean signal = false;
if (deer_direction == 0) {
if (((x + 100) > 350) && (x) < 475) {
image(dead_Flower, 310, 255);
signal = true;
rain_click = 0;
}
}
else if (deer_direction == 1) {
if ((x) < 350) {
image(dead_Flower, 310, 255);
signal = true;
rain_click= 0;
}
}
return signal;
}
}// End of Deer class
void keyPressed () {
deer.deer_Reset();
image(lightening_bolt, 150, 205);
textFont(deerCollide, 16);
fill(255, 255, 102);
text ("ZAP", 100, 250);
text ("ZAP", 225, 250);
}
void mousePressed() {
if (overCloud) {
textFont(deerCollide, 16);
fill(0, 0, 255);
text ("Whee water!", 299, 100);
draw_rain();
rain_click += 1;
}
else {
locked = false;
}
}
void draw_rain() {
float x = 0;
x = (random(294, 800));
System.out.println(x);
float y = 0;
int velocity_y = 3;
for (int i = 0; i < 40; i++) {
x = (random(294, 400));
y = random(220, height);
y+= velocity_y;
fill(00, 00, 255);
ellipse(x, y, 4, 7);
}
}
PS. (Disclaimer): Sorry I meant to post this a while ago but after actual presentation day when this project was due, but I had no internet that day (which is why I used a USB to present) and I delayed in posting it out of sheer forgetfulness…Awkward: I think after that delay I think I prematurely saved the draft and quit before actually posting (apologies!!)
It’s a fish-eat-fish world out there, and you start out on the bottom of the food chain in this game.. As a tiny white fish (controlled by your mouse), gobble your prey and if you eat enough, you grow in size. Just make sure to avoid your predators!
Can you make it to the top?
Here’s the entire setup (I include pictures and a certain font type so they’ll be necessary for running it successfully)
//Fish Objects:
Fish self;
Fish test;
Fish[] prey_array = new Fish[10];
Fish[] predator_array = new Fish[7];
int number_of_predator; //Uses to manipulate predator array length for each level
int number_of_prey_eaten; //Uses to manipulate prey array lengths for interactions
int temp_num_prey_eaten; //Changes inside the prey collision for loop and will be set to the prey_eaten after the for loop (without dynamically changing the for loop)
int[] temporary_array_shift = new int[10]; //stores the indexes of all the prey objects that have been eaten
int actual_prey_eaten_per_level;
/*Will be set to the difference between temporary prey eaten and regular prey eaten and will be used to set the for loop parameters
Will be use "see" how many fish were consumed per draw loop (not per level)
*/
int index_of_new_array; //
//Setting level booleans: used to "start" the each level once without it looping and constantly restarting
boolean level_1, level_2, level_3, level_4, level_5;
//Counter that sets parameters of each level:
int total_eat_count;
//Used to control lose screen:
boolean lose;
//Used for controlling special self and prey object (surprise!)
boolean self_surprise; boolean prey_surprise;
PImage sea_win;
PImage diver;
PImage bone;
PFont font;
void setup() {
size(900, 700);
//Set all the booleans true to start each level. They will be used once to intialize the start of each level ONCE
level_1=true;
level_2=true;
level_3=true;
level_4=true;
level_5=true;
total_eat_count = 0;
number_of_predator = 0;
initializePrey(247, 77, 214, 25);
initializePredator(60, 245, 60, 75);
index_of_new_array=0;
number_of_prey_eaten = 0;
temp_num_prey_eaten = 0;
self = new Fish(0, 0, 0, 40, 1, 1, 0, 0);
lose = false;
diver = loadImage("Diver.png");
sea_win = loadImage("Merman.png");
bone = loadImage("bone.jpg");
//Set up font:
font = loadFont("ACaslonPro-Semibold-48.vlw");
self_surprise = false;
}
void draw() {
if (lose==false){
background(00, 00, 200);
//moving self
if (self_surprise == false){
self.move_Self();
}
else if(self_surprise){
self.changetoMerman();
}
//PREDATOR:
for (int i=0;i<predator_array.length - number_of_predator;i++) {
predator_array[i].moveFish();
if (predator_array[i].checkBoundaries()) {
predator_array[i].move_back_Fish();
}
if (predator_array[i].check_Collision(self)){
lose = true;
}
}
//PREY:
index_of_new_array =0;
//Moving Fish:
for (int i=0;i<prey_array.length- number_of_prey_eaten;i++) {
//Use boolean for surprise!
if (prey_surprise == false){
//Moving fish (and accomodating when it goes out of bounds)
prey_array[i].moveFish();
}
else if(prey_surprise)
{
prey_array[0].draw_diver();
}
if (prey_array[i].checkBoundaries()) {
prey_array[i].move_back_Fish();
}
if (prey_array[i].check_Collision(self)) {
total_eat_count++;
temp_num_prey_eaten++;
temporary_array_shift[index_of_new_array]=i;
index_of_new_array++;
}
}
actual_prey_eaten_per_level = temp_num_prey_eaten - number_of_prey_eaten;
number_of_prey_eaten = temp_num_prey_eaten;
for (int i=0;i<actual_prey_eaten_per_level;i++) {
shifting_index(temporary_array_shift[i]); //For EACH fish that is eaten (for loop), the index of the prey_array will be shifted over
}
nextLevel();
}
else if(lose == true){
background (255,0,0);
image(bone, 300, 400, 200, 150);
textFont(font, 48);
fill(0,0,0);
text ( "You've been eaten!" ,200,300);
}
}
//END OF DRAW
//Other Methods:
//Initializing Prey/Predator:
void initializePrey(int red, int green, int blue, int size) {
Fish[] temp = new Fish[10];
for (int i=0;i<prey_array.length;i++) {
temp[i] = new Fish(red, green, blue, size, (int)random(0, width), (int)random(0, height), random(-3, 3), random(-3, 3));
}
for (int i=0;i<prey_array.length;i++) {
prey_array[i] = temp[i];
}
}
void initializePredator(int red, int green, int blue, int size) {
for (int i=0;i<predator_array.length;i++) {
predator_array[i] = new Fish(red, green, blue, size, (int)random(0, width), (int)random(0, height), random(-3, 3), random(-3, 3));
}
}
//"Shifting" remaining uneaten fish over one index to the left
void shifting_index(int index) {
for (int j = index; j<prey_array.length-1;j++) {
prey_array[index] = prey_array[index+1];
}
}
//Next Level functions:
void nextLevel() {
int level = 0;
//Convert number of prey eaten to level amounts:
if (total_eat_count <= 9) {
level = 1;
if (level_1) {
initializePrey(247, 77, 214, 25);
initializePredator(60, 245, 60, 75 );
self.change_fish_color(255, 255, 255);
self.change_fish_size(50);
number_of_prey_eaten=0;
number_of_predator = 0;
temp_num_prey_eaten =0;
level_1=false;
}
}
if (total_eat_count <= 18 && total_eat_count > 9) {
level = 2;
if (level_2) {
initializePrey(255, 255, 255, 50);
initializePredator(153, 76, 0, 100);
number_of_prey_eaten=1;
temp_num_prey_eaten=1;
number_of_predator = 3;
self.change_fish_color(60, 245, 60);
self.change_fish_size(75);
level_2=false;
}
}
if (total_eat_count <= 23 && total_eat_count > 18 ) {
level = 3;
if (level_3) {
initializePrey(60, 245, 60, 75);
initializePredator(135, 31, 166, 175);
self.change_fish_color(153, 76, 0);
self.change_fish_size(100);
number_of_predator = 4;
number_of_prey_eaten=5;
temp_num_prey_eaten=5;
level_3=false;
}
}
if (total_eat_count == 24) {
level = 4;
if (level_4) {
initializePrey(255, 255, 255, 75);
prey_array[0].draw_diver();
initializePredator(0, 0, 0, 250);
self.change_fish_color(132, 224, 247);
self.change_fish_size(175);
number_of_prey_eaten=9;
temp_num_prey_eaten=9;
number_of_predator = 6;
prey_surprise = true;
level_4=false;
}
total_eat_count=24;
}
if (total_eat_count == 25) {
level = 5;
if (level_5) {
number_of_prey_eaten=10;
temp_num_prey_eaten=10;
number_of_predator = 7;
level_5=false;
}
self_surprise = true;
textFont(font, 36);
fill(255,255,255);
text ( "You're the king of the sea!" ,250,300);
}
}//End of next_level function INSERT PIC OF HUMAN SWIMMING case 4: initializePrey
//Level reset function:
void level_reset(){
//Set all level booleans back so that levels can re-initialize again & reset level_progression variable back to 0
level_1=true;
level_2=true;
level_3=true;
level_4=true;
level_5=true;
total_eat_count = 0;
self_surprise= false;
prey_surprise = false;
}
void keyPressed(){
lose = false;
level_reset();
}
And here is my separate fish class file:
class Fish {
//Fish color:
int r;
int g;
int b;
//Fish size
int fish_width;
int fish_height;
int fish_size;
//Fish position
int x;
int y;
//Fish speed
float velocity_x;
float velocity_y;
//For when fish is eaten --> variable
boolean eaten = false;
//Constructor
Fish(int _r, int _g, int _b, int _fish_size, int _x, int _y, float _velocity_x, float _velocity_y) {
r = _r;
g = _g;
b = _b;
fish_size = _fish_size;
fish_width = 2 * _fish_size;
fish_height = _fish_size;
x = _x;
y = _y;
velocity_x = _velocity_x;
velocity_y = _velocity_y;
}
void drawFish() {
stroke(r, g, b);
fill(r, g, b);
triangle(x, y, x, y+fish_height, x+ (fish_width/3), y+(fish_height/ 2));
ellipse((x+((10*fish_width/18))), y+(fish_height/2), ((2*fish_width)/3), (15*fish_height/16));
}
void moveFish() {
x+=velocity_x;
y+=velocity_y;
this.drawFish();
}
boolean checkBoundaries() {
boolean out_of_bounds = false;
//Out of screen (horizontal)
if (x>(width+(1.5* fish_width))|| x < (0 - (1.5*fish_width))) {
out_of_bounds = true;
}
//Out of screen (vertical)
if (y>(height+(1.5*fish_height)) || y < (0 - (1.5* fish_height))) {
out_of_bounds = true;
}
return out_of_bounds;
}
void move_Self() {
x = mouseX -(fish_width / 2);
y = mouseY - (fish_height / 2);
this.drawFish();
}
void move_back_Fish() {
//Will randomize the side from which the fish returns
//Will maintain the same magnitude of velocity
/* Must figure out magnitude from original x and y velocities
Then use magnitude to determine the new velocities
Use random to find new angle of reentry (radians)
magintude SIN (theta) = y
magnitude COS (theta) = X
*/
float theta = random(.1*PI, .9*PI);
float magnitude = 0;
magnitude = sqrt((float)(Math.pow(velocity_x, 2))+(float)(Math.pow(velocity_y, 2)));
int side = (int) random(1, 5);
switch (side) {
//Top
case 1:
y = 0 - fish_height;
x = width/2;
velocity_y = (magnitude * sin(theta));
velocity_x = (magnitude * cos(theta));
break;
//Bottom
case 2:
y = height + fish_height;
x = 450;
velocity_y = -(magnitude * sin(theta));
velocity_x = (magnitude * cos(theta));
break;
//Left
case 3:
y = 375;
x = 0 - fish_width;
velocity_y = (magnitude * sin(theta));
velocity_x = (magnitude * cos(theta));
break;
//Right
case 4:
y = 375;
x = width + fish_width;
velocity_y = (magnitude * sin(theta));
velocity_x = -(magnitude * cos(theta));
break;
}
}
//Getter Methods for collision detection
int get_X_Position() {
return x;
}
int get_Y_Position() {
return y;
}
int get_fish_Height() {
return fish_height;
}
int get_fish_Width() {
return fish_width;
}
//COLLISION
boolean check_Collision(Fish temporary) {
boolean signal = false;
int temp_x = temporary.get_X_Position();
int temp_y = temporary.get_Y_Position();
int temp_fish_height = temporary.get_fish_Height();
int temp_fish_width = temporary.get_fish_Width();
if (x < (temp_x + temp_fish_width) && (x+fish_width) > (temp_x )) {
if (y < (temp_y + temp_fish_height) && (y+fish_height) > (temp_y )) {
signal = true;
}
else {
signal = false;
}
}
return signal;
}
//If prey and self have collided, sets collided prey to eaten
void set_to_Eaten() {
eaten = true;
}
//Getter method to check if fish have been eaten
boolean get_if_Eaten() {
return eaten;
}
//Change fish color:
void change_fish_color(int _r, int _g, int _b){
r = _r;
g = _g;
b = _b;
}
void change_fish_size(int _size){
fish_size = _size;
fish_width = 2 * _size;
fish_height = _size;
}
void draw_diver(){
float temp_x = x;
float temp_y = y;
image (diver, temp_x, temp_y, 150, 75);
x+=velocity_x;
y+=velocity_y;
}
void changetoMerman(){
image(sea_win,mouseX,mouseY,250,400);
x = mouseX -(fish_width / 2);
y = mouseY - (fish_height / 2);
}
//End of Fish Class
}
PS. Make it to level 4 and you’ll see a surprise. Can you conquer the sea?
Potential improvement:
-I think I’d like to to make a smoother transition from game over to the reset level 1. I’ve had the displeasure of the pressing a key to reset the level but then automatically dying again because my fish is placed over where the predators are drawn..
– Fix this weird glitch where sometimes two fish are “eaten”
I tried organizing my code into methods (as best I could) and clean up what was in the the void draw().
It’s still a pretty intimidating chunk of code, since a lot of my variables are declared globally, and not everything was passed by value in those methods since I kept bugging my program when I tried to make some variables local.
int circleX;
int circleY;
int velocityX;
int velocityY;
int bubblecount;
int bubblecount2;
boolean bubbleadd;
//MousePressed/keyPressed
boolean disco;
//Use arrays to store color values:
int[] red = {
255, 255, 255, 0, 0, 75, 143
};
int[] green = {
0, 127, 255, 255, 0, 0, 0
};
int[] blue = {
0, 0, 0, 0, 255, 130, 255
};
int cycle_color;
int cycle_color2;
//Use arrays to store coordinates of new bubbles
int[] bubbleX = new int[1000];
int[] bubbleY = new int[1000];
//Use arrays to store velocities of new bubbles
int[] bubblevelocityX = new int [1000];
int[] bubblevelocityY = new int [1000];
void setup() {
strokeWeight(0);
stroke(100, 0, 150);
size(800, 600);
circleX = mouseX;
circleY = mouseY;
velocityX = 7;
velocityY = 7;
bubblecount = 0;
bubblecount2 = 0;
bubbleX[0]=circleX;
bubbleY[0]=circleY;
bubblevelocityX[0]=velocityX;
bubblevelocityY[0]=velocityY;
//colors of background/bubbles(2)
cycle_color = 0;
cycle_color2 = 1;
//cool effect
disco = true;
}
void draw() {
background(red[cycle_color], green[cycle_color], blue[cycle_color]);
bubbleadd = true;
bubblecount2 = bubblecount;
for (int i = 0; i <= bubblecount2; i++) {
if (disco) {
fill(red[cycle_color2], green[cycle_color2], blue[cycle_color2]);
}
else {
fill(random(255), random(255), random(255));
}
bubble_speed(i);
check_sides(i);
check_top_and_bottom(i);
draw_bubble(i);
change_color();
}
}
void check_sides(int index) {
if (bubbleX[index] >= width || bubbleX[index] <= 0) {
bubblevelocityX[index] *= -1;
if (bubbleadd) {
bubblecount += 1;
bubbleX[bubblecount] = bubbleX[index];
bubbleY[bubblecount] = bubbleY[index];
bubblevelocityX[bubblecount] = bubblevelocityX[index];
bubblevelocityY[bubblecount] = -bubblevelocityY[index]+(int)random(5);
bubbleadd = false;
}
}
}
void check_top_and_bottom (int index) {
if (bubbleY[index] >= (height) || bubbleY[index] <= 0) {
bubblevelocityY[index] *= -1;
if (bubbleadd) {
bubblecount += 1;
bubbleX[bubblecount] = bubbleX[index];
bubbleY[bubblecount] = bubbleY[index];
bubblevelocityX[bubblecount] =-bubblevelocityX[index]+(int)random(5);
bubblevelocityY[bubblecount] = bubblevelocityY[index];
bubbleadd = false;
}
}
}
void draw_bubble(int i) {
strokeWeight(0);
stroke(red[cycle_color2], green[cycle_color2], blue[cycle_color2]);
ellipse(bubbleX[i], bubbleY[i], 120, 120);
}
void bubble_speed (int index) {
bubbleX[index]+= bubblevelocityX[index];
bubbleY[index]+= bubblevelocityY[index];
}
void change_color () {
if (bubblecount2 > 500) {
bubblecount2=0;
bubblecount=0;
cycle_color += 1;
cycle_color2 += 1;
if (cycle_color >= 7){
cycle_color=0;
cycle_color2 += 1;
}
if (cycle_color2 >= 7){
cycle_color2=0;
}
}
}
//Cool effect (on)
void mousePressed() {
disco=false;
}
//Cool effect (off)
void keyPressed() {
disco=true;
}
This is a rudimentary model of the iPhone game, Flappy Birds. I originally wanted the pipe structure to be automated by a motor, but that system had too many complications. Thus the version you see in the video is simply hand-operated. Don’t hit the pipes or the ceiling or your flappy bird will fall.
The green LED indicates that the game is on, while the red LED indicates that the game is over.
To the flappiest of the birds, you will be missed.
In this animation, a single bubble will bounce around the screen. However, it will spawn itself upon hitting the walls and the colors will eventually cycle through the rainbow.
Something mysterious will happen when the mouse is pressed (and if it’s too much, you can undo the coolness by pressing a key)
Orange is a terrible color
int circleX;
int circleY;
int velocityX;
int velocityY;
int bubblecount;
int bubblecount2;
boolean bubbleadd;
//MousePressed/keyPressed
boolean disco;
//Use arrays to store color values:
int[] red = {255, 255, 255, 0, 0, 75, 143};
int[] green = {0, 127, 255, 255, 0, 0, 0};
int[] blue = {0,0,0,0,255,130,255};
int cycle_color;
int cycle_color2;
//Use arrays to store coordinates of new bubbles
int[] bubbleX = new int[1000];
int[] bubbleY = new int[1000];
//Use arrays to store velocities of new bubbles
int[] bubblevelocityX = new int [1000];
int[] bubblevelocityY = new int [1000];
void setup() {
strokeWeight(0);
stroke(100, 0, 150);
size(800, 600);
circleX = mouseX;
circleY = mouseY;
velocityX = 7;
velocityY = 7;
bubblecount = 0;
bubblecount2 = 0;
bubbleX[0]=circleX;
bubbleY[0]=circleY;
bubblevelocityX[0]=velocityX;
bubblevelocityY[0]=velocityY;
//colors of background/bubbles
cycle_color = 0;
cycle_color2 = 1;
//cool effect
disco = true;
}
void draw() {
background(red[cycle_color],green[cycle_color],blue[cycle_color]);
bubbleadd = true;
bubblecount2 = bubblecount;
//Array is used to store
//For loop checks activities of each bubble that is created
//Bubblecount2 is used to prevent the for loop from expanding itself during the iteration
//Bubblecount is used to refer to the next index where the new bubble would be created
for (int i = 0; i <= bubblecount2; i++) {
if (disco) {
fill(red[cycle_color2],green[cycle_color2],blue[cycle_color2]);
}
else {
fill(random(255),random(255),random(255));
}
bubbleX[i]+= bubblevelocityX[i];
bubbleY[i]+= bubblevelocityY[i];
if (bubbleX[i] >= width || bubbleX[i] <= 0) {
bubblevelocityX[i] *= -1;
if (bubbleadd) {
bubblecount += 1;
bubbleX[bubblecount] = bubbleX[i];
bubbleY[bubblecount] = bubbleY[i];
bubblevelocityX[bubblecount] = bubblevelocityX[i];
bubblevelocityY[bubblecount] = -bubblevelocityY[i]+(int)random(5);
bubbleadd = false;
}
}
if (bubbleY[i] >= (height) || bubbleY[i] <= 0) {
bubblevelocityY[i] *= -1;
if (bubbleadd) {
bubblecount += 1;
bubbleX[bubblecount] = bubbleX[i];
bubbleY[bubblecount] = bubbleY[i];
bubblevelocityX[bubblecount] =-bubblevelocityX[i]+(int)random(5);
bubblevelocityY[bubblecount] = bubblevelocityY[i];
bubbleadd = false;
}
}
//drawing new bubbles
strokeWeight(0);
stroke(red[cycle_color2],green[cycle_color2],blue[cycle_color2]);
ellipse(bubbleX[i], bubbleY[i], 120, 120);
}
if (bubblecount2 > 500){
bubblecount2=0;
bubblecount=0;
cycle_color += 1;
if (cycle_color >= 7)
cycle_color=0;
cycle_color2 += 1;
if (cycle_color2 >= 7)
cycle_color2=0;
}
}
//Cool effect (on)
void mousePressed(){
disco=false;
}
//Cool effect (off)
void keyPressed() {
disco=true;
}
Continuing from the first part of the pocket switch, this new part incorporates a a watch. Basically, the user now has the option to turn off the motor and lit LED that activate when the phone/wallet is removed. With the power of the switch, the user can turn it to the “Off” setting to turn off the LED and motor. Once the phone/wallet is back in the pocket, the switch can be flipped back to reset the security.
Many people I know/see on the street walk around with their cellphones and wallets in their back pockets. I used to be one of those people (still admittedly, am sometimes) until last summer where I lost two iPhones, and I grew increasingly aware of the security I needed to maintain to ensure these expensive phones would no longer be lost/stolen. But now, butt-pocket users, fear no more – my switch adds security to your life style.
My switch involves two plates that will line the pocket and when touching, the switch is “on.” The current is interrupted when a wallet or cellphone is placed between these two. Because one of the two plates is spring loaded with paper-clip springs, when the phone/wallet is removed, the plate will spring back and reconnect the current, and the switch will turn on. When the switch is on, the LED will light up, but also a littlemotor will vibrate to notify you that your stuff’s gone. Hopefully one day, when people start using Samsung watches or Google glasses, a message will be sent to those devices.
The spring device made of paperclipsStripped wire is threaded to maintain conductivityPlates are covered in tin foilThe tiny little vibrating motorThe whole spiel ( – the phone/wallet)