Created
May 5, 2017 21:19
-
-
Save anonymous/07c67adbfb818e005783f35464b8f444 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Name: BTU Boxing | |
//By: Claudia Escue | |
//Purpose: * Creates a game that is navigated through with acceleration of a punching bag | |
// * Teaches control of strikes | |
//import serial library | |
import processing.serial.*; | |
//create a variable to control what screen is displayed | |
int start = 0; | |
//create a variable to hold the number of points scored | |
int points = 0; | |
//create a maximum value the goal to be set to | |
float goalMax = 4.000; | |
//create sensor values measured in units of g | |
float x; | |
float y; | |
float z; | |
//create a value to hold the total acceleration | |
float total; | |
//create and initialzies variables for the maximum hit, difficulty setting, and goal hit | |
float maxNum = 0; | |
float difficulty = .45; | |
float goal = random(0.500, goalMax); | |
//create images for the background and for the point display | |
PImage imgGreen; | |
PImage imgYellow; | |
PImage imgRed; | |
PImage imgWhite; | |
//create and instance of the serial libaray | |
Serial myPort; | |
//create an instance of a font | |
PFont f; | |
void setup() { | |
//set the size of the game screen | |
size(500, 500); | |
//my port is: /dev/tty.usbmodem1411 | |
String portName = "/dev/tty.usbmodem1411"; | |
myPort = new Serial(this, portName, 9600); | |
//create a font of size 16 Helvetica | |
f = createFont("Helvetica", 16, true); | |
//load images | |
imgGreen = loadImage("fistGreen.png"); | |
imgYellow = loadImage("fistYellow.png"); | |
imgRed = loadImage("fistRed.png"); | |
imgWhite = loadImage("fistWhite.png"); | |
} | |
void draw() { | |
//set the background color to black the the font to size 36 | |
background(#000000); | |
textFont(f,36); | |
//calculate the total acceleration | |
total = sqrt(sq(x) + sq(y) + sq(z)); | |
//determine whether the bag is speeding up or slowing down my comparing the total to the maxNum | |
maxNum = max(total, maxNum); | |
//call the score update function | |
ScoreUpdate(); | |
//determine the color of the image based on the goal and the size of the fist based on the number of points scored | |
imageMode(CENTER); //orrient the image based on the center point | |
if (goal <= 1.666) { | |
image(imgGreen, 250, 245, 110+(30*points), 110+(30*points)); //display the green fist (light hit) | |
} else if (goal > 1.666 && goal <= 3.333) { | |
image(imgYellow, 250, 245, 110+(30*points), 110+(30*points)); //display the yellow fist (medium hit) | |
} else { | |
image(imgRed, 250, 245, 110+(30*points), 110+(30*points)); //display the red fist (hard hit) | |
} | |
//set the text fill color to white, display the goal and hit texts, and the goal and acceleration to 1 decimal | |
fill(#ffffff); | |
String goalText = "Goal: " + nf(goal, 1, 1); | |
text(goalText, 10, 40); | |
String hitText = "Hit: " + nf(maxNum, 1, 1); | |
text(hitText, 380, 40); | |
//create the strings for the difficulty setting | |
String difEasy = "Difficulty: Easy"; | |
String difMed = "Difficulty: Moderate"; | |
String difDif = "Difficulty: Difficult"; | |
//determine which difficulty setting string to display based on the difficulty selected on the instructions screen | |
if (difficulty == .45) { | |
text(difEasy, 10, 480); | |
} else if (difficulty == .35) { | |
text(difMed, 10, 480); | |
} else if (difficulty == .25) { | |
text(difDif, 10, 480); | |
} | |
//call the screen function | |
Screen(); | |
} | |
//determing what screen to show | |
void Screen() { | |
if (start == 0) { | |
Main(); //shown first becuase the start variable is initialized to 0 | |
} else if (start == 2) { | |
Instructions(); //move to instructions screen | |
} else if (points == 10) { | |
YouWin(); //move to win screen | |
} | |
} | |
//create the main screen | |
void Main() { | |
//create a black background with a white fist | |
background(#000000); | |
image(imgWhite, 250, 250, 490, 490); | |
//show the title of the game | |
textFont(f,78); | |
fill(#bc091b); //red fill color | |
String BTUText = "BTU BOXING"; | |
text(BTUText, 10, 230); | |
//show instructions to get to the next screen | |
textFont(f, 25); | |
String startText = "Hit to see instructions and select level!"; | |
text(startText, 35, 260); | |
//if the bag is hit set the start variable to 2 to move to the instructions screen | |
if (total > .5 && x < 0 && y < 0 && z < 0) { | |
delay(1000); | |
start = 2; | |
} | |
} | |
//creates the display for the instructions screen | |
void Instructions() { | |
background(#000000); | |
//set font size to 30 | |
textFont(f,30); | |
fill(#bc091b); //red fill color | |
String instructText = "Instructions:"; | |
text(instructText, 175, 100-30); | |
//set text size to 25 | |
textFont(f,25); | |
//display line by line instructions | |
String instructText1 = "1. Try to hit the bag to match the goal"; | |
text(instructText1, 10, 130-30); | |
String instructText2 = "2. The fist will grow when you score a point"; | |
text(instructText2, 10, 160-30); | |
String instructText3 = "3. The fist will change color based on how"; | |
text(instructText3, 10, 190-30); | |
String instructText4 = " hard you must hit it"; | |
text(instructText4, 10, 220-30); | |
//display small green fist and text | |
image(imgGreen, 50, 240-30, 25, 25); | |
String instructText6 = ":light hit"; | |
text(instructText6, 70, 250-30); | |
//display small yellow fist and text | |
image(imgYellow, 50, 270-30, 25, 25); | |
String instructText8 = ":meduium hit"; | |
text(instructText8, 70, 280-30); | |
//display small red fist and text | |
image(imgRed, 50, 300-30, 25, 25); | |
String instructText10 = ":hard hit"; | |
text(instructText10, 70, 310-30); | |
//display more instructions | |
String instructText11 = "4. You win when the fist wont fit the screen"; | |
text(instructText11, 10, 340-30); | |
//display instructions for how to select level | |
String instructText12 = "5. Hit to select level:"; | |
text(instructText12, 10, 370-30); | |
String instructText13 = " Hit light for easy"; | |
text(instructText13, 10, 400-30); | |
String instructText14 = " Hit meduium for moderate"; | |
text(instructText14, 10, 430-30); | |
String instructText15 = " Hit hard for difficult"; | |
text(instructText15, 10, 460-30); | |
//set the difficulty and start points at 0 | |
if (total > .25 && total <= 1.666 && x < 0 && y < 0 && z < 0) { | |
delay(1000); | |
start = 1; | |
points = 0; | |
difficulty = 0.45; //easy difficulty | |
} else if (total > 1.666 && total <= 3.333 && x < 0 && y < 0 && z < 0) { | |
delay(1000); | |
start = 1; | |
points = 0; | |
difficulty = 0.35; //moderate difficulty | |
} else if (total > 3.333 && x < 0 && y < 0 && z < 0) { | |
delay(1000); | |
start = 1; | |
points = 0; | |
difficulty = 0.25; //difficult difficulty | |
} | |
} | |
//Updates the score when a point is scored | |
void ScoreUpdate() { | |
//compare the maxNum to the goal +/- the difficulty | |
//if x, y, and z are less than 0 then the bag is moving back | |
if (maxNum > (goal-difficulty) && maxNum < (goal+difficulty) && x < 0 && y < 0 && z < 0){ | |
points += 1; | |
delay(1000); //something to delay for a moment so the bag does not hit the goal on the way back down | |
maxNum = total; | |
goal = random(0.500, goalMax); //create a new goal | |
} maxNum = total; //reset the maxNum | |
} | |
//create the win screen | |
void YouWin() { | |
//create a balck background with a white fist | |
background(#000000); | |
image(imgWhite, 250, 250, 490, 490); | |
//create and display win text | |
textFont(f,78); | |
fill(#bc091b); | |
String winText = "YOU WIN!"; | |
text(winText, 60, 230); | |
//display how to play again | |
textFont(f, 25); | |
String returnText = "Hit to play again!"; | |
text(returnText, 155, 260); | |
//return to main screen if the bag is hit harder than 1.50g | |
if (total > 1.50 && x < 0 && y < 0 && z < 0) { | |
delay(1000); | |
points = 0; | |
start = 0; | |
Screen(); | |
} | |
} | |
//listening for serial events | |
void serialEvent(Serial myPort) { | |
String input = myPort.readStringUntil('\n'); | |
if (input != null) { | |
input = trim(input); | |
float[] sensors = float(split(input, ',')); //create an array for the sensor values | |
println(sensors); | |
// if the length of the sensors array is longer than 2 | |
if (sensors.length > 2) { | |
//set each variable to its given value from the potentiometer | |
x = sensors[0] - 0.98; //subtract gravity | |
y = sensors[1] - 0.06; //start at about 0 | |
z = sensors[2] - 0.25; //start at about 0 | |
} | |
} | |
myPort.write('x'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment