import controlP5.*; import ddf.minim.*; import processing.serial.*; Serial port; //valP_move1 is the value received from the slider sensor for player1 //valP_move2 is the value received from the slider sensor for player2 //valp_shoot1 is the value received from the force sensor for player1 //valp_shoot2 is the value received from the force sensor for player2 //valP_sShoot1 is the value received from the 2nd force sensor for player1 //valP_sShoot2 is the value received from the 2nd force sensor for player2 int valP_move1, valP_move2, valP_shoot1, valP_shoot2, valP_sShoot1, valP_sShoot2; byte[] inBuffer = new byte[255]; //Controls for the sound effects Minim minim; AudioPlayer player, shoot, hit; AudioInput input; ControlP5 controlP5; Page newPage; String player1 = ""; String player2 = ""; Textfield myTextfield1, myTextfield2; ArrayList blocks = new ArrayList(); //An arraylist of blocks int block_num = 56; //number of blocks in the game int[] weapon = new int[4]; //index of blocks in the arraylist ArrayList beams1 = new ArrayList(); //An arraylist for the beams shot by player1 ArrayList beams2 = new ArrayList(); //An arraylist for the beams shot by player2 boolean startTimer1 = false; //Start the timer when player1 presses the force senors boolean startTimer2 = false; //Start the timer when player2 presses the force senors int timer1, timer2; //Timers to restrict players from continuously shooting beams PImage backgroundImage, missileImg, shieldImg; boolean gameStart = false; Player p1, p2; void setup() { // port = new Serial(this, "/dev/cu.usbserial-A4001sZZ", 9600); port = new Serial(this, "COM3", 9600); newPage = new Page(); controlP5 = new ControlP5(this); size(1024, 700); timer1 = 0; timer2 = 0; minim = new Minim(this); player = minim.loadFile("absolution.mp3"); shoot = minim.loadFile("sfx.wav"); hit = minim.loadFile("hit.wav"); player.play(); player.loop(); //Selecting random blocks to assign special weapons for (int i = 0; i < 4; i++) { weapon[i] = (int) random(0, 56); } //Drawing blocks on the game screen for (int i = 0; i < block_num; i++) { if (i == weapon[0] || i == weapon[1]) blocks.add(new Block(i, true, false)); //missiles else if (i == weapon[2] || i == weapon[3]) blocks.add(new Block(i, false, true)); //shields else blocks.add(new Block(i, false, false)); } backgroundImage = loadImage("background.jpg"); missileImg = loadImage("missile.png"); shieldImg = loadImage("shield.png"); //intro page image(backgroundImage, 0, 0); newPage.displayMenu(); //buttons controlP5.addButton("START", 0, 100, 150, 200, 50); controlP5.addButton("INSTRUCTIONS", 0, 100, 230, 200, 50); controlP5.addButton("CREDITS", 0, 100, 310, 200, 50); controlP5.addButton("EXIT", 0, 100, 390, 200, 50); controlP5.addButton("BACK", 0, width-120, height-80, 80, 50); controlP5.addButton("START GAME", 0, width-250, height-80, 80, 50); controlP5.addButton("RESTART", 0, width/2, height/2+50, 80, 50); controlP5.addButton("MAIN MENU", 0, width/2, height/2+150, 80, 50); //players' names myTextfield1 = controlP5.addTextfield("player1", 250, height/2-90, 200, 50); myTextfield2 = controlP5.addTextfield("player2", 250, height/2+10, 200, 50); myTextfield1.setFocus(true); myTextfield2.setFocus(true); //Setting the visibilties of the buttons and texts controlP5.controller("BACK").setVisible(false); controlP5.controller("START GAME").setVisible(false); controlP5.controller("RESTART").setVisible(false); controlP5.controller("MAIN MENU").setVisible(false); controlP5.controller("player1").setVisible(false); controlP5.controller("player2").setVisible(false); //creating objects for the players p1 = new Player(120, valP_move1*3); p2 = new Player(900, valP_move2*3); } void draw() { getSenVal(); //Getting values form the sensors if (gameStart == true) { //starting the game drawGame(); //draw the players p1.drawPlayer1(); p2.drawPlayer2(); //move the player p1.setY(valP_move1*3); p2.setY(valP_move2*3); //blocks for (int i = 0; i < blocks.size(); i++) { Block block = (Block) blocks.get(i); block.drawBlock(); //draw the blocks if (block.timer != 0) block.timer++; //for special weapons only, to keep the special weapons on the screen longer if (block.timer == 60) block.destroy(blocks); //for special weapons only, special weapons disappear after 2 seconds being shot //Beams for player1 for (int k = 0; k < beams1.size(); k++) { Beam beam = (Beam) beams1.get(k); //Collision detection with blocks if (abs(block.x - beam.x) < 25 && (beam.y > block.y-25 && beam.y <= block.y+25)) { beam.destroyBeam(beams1); //remove beam block.hit = true; if (block.missile || block.shield) { //if the block contains special weapons block.timer++; if (block.missile) { //if the block contains a missile p1.missileMode = true; p1.missileCount += 5; //5 missiles given to the player } if (block.shield) p1.shieldMode = true; //if the block contains a shield, the player is given a shield } else { block.destroy(blocks); //the block is removed if it is empty } } } //Beams for player2 for (int k = 0; k < beams2.size(); k++) { Beam beam = (Beam) beams2.get(k); //Collision detection with blocks if (abs(block.x - beam.x) < 25 && (beam.y > block.y-25 && beam.y <= block.y+25)) { beam.destroyBeam(beams2); //remove beam block.hit = true; if (block.missile || block.shield) { //if the block contains special weapons block.timer++; if (block.missile) { //if the block contains a missile p2.missileMode = true; p2.missileCount += 5; //5 missiles given to the player } if (block.shield) p2.shieldMode = true; //if the block contains a shield, the player is given a shield } else { block.destroy(blocks); //the block is removed if it is empty } } } } if (startTimer1 == true) timer1++; //imcrementing the timer1 if (timer1 == 30) { //when the timer gets to 30 frames, player1 is allowed to shoot again startTimer1 = false; timer1 = 0; //resetting the timer } if (startTimer2 == true) timer2++; //imcrementing the timer1 if (timer2 == 30) { //when the timer gets to 30 frames, player1 is allowed to shoot again startTimer2 = false; timer2 = 0; //resetting the timer } //player1 shooting normal beams if (valP_shoot1 == 1) { //if player1 has pressed the force sensor for normal beams if (timer1 == 0) { //when player1 is allowed to shoot again beams1.add(new Beam(p1.getX()+10, p1.getY()+35, false)); //add a Beam object to the arraylist shoot.play(); //play sound effect startTimer1 = true; //start the timer1 } } //player1 shooting missiles if (valP_sShoot1 == 1) { //if player1 has pressed the 2nd force sensor for special weapons if (timer1 == 0 && p1.missileMode) { //when player1 is allowed to shoot again and has missiles beams1.add(new Beam(p1.getX()+10, p1.getY()+35, true)); //add a Beam object specified as missile to the arraylist startTimer1 = true; //start the timer1 p1.missileCount--; //minus the number of missiles left if (p1.missileCount == 0) p1.missileMode = false; //turn off missile mode when player1 uses up all the missiles } } //Moving the beams for (int i = 0; i < beams1.size(); i++) { Beam j = (Beam) beams1.get(i); j.setX(+30); //setting the speed of the beams j.drawBeam1(); //draw beams if (j.getX()+30 > width) { beams1.remove(j); //remove the beam when it goes out of the screen } p2.playerandbeamCollide(j); //collision detection with player2 if (p2.getHit() == true) { //if player2 gets hit hit.play(); //play sound effect beams1.remove(j); //remove beam p2.setHit(false); //reset the status of player2 } } if (p1.missileMode) { //if player1 has missiles textAlign(LEFT); fill(100); text("Missile x" + p1.missileCount, 10, height-10); //show the number of the remaining missiles } if (p1.shieldMode) { //if player1 has a shield textAlign(LEFT); fill(100); text("Shield " + p1.shieldTime, 10, height-80); //show the remaining time of the shield mode } //player2 shooting normal beams if (valP_shoot2 == 1) { //if player2 has pressed the force sensor for normal beams if (timer2 == 0) { //when player2 is allowed to shoot again beams2.add(new Beam(p2.getX()-10, p2.getY()-35, false)); //add a Beam object to the arraylist shoot.play(); //play sound effect startTimer2 = true; //start the timer2 } } //player1 shooting missiles if (valP_sShoot2 == 1) { //if player2 has pressed the 2nd force sensor for special weapons if (timer2 == 0 && p2.missileMode) { //when player2 is allowed to shoot again and has missiles beams2.add(new Beam(p2.getX()-10, p2.getY()-35, true)); //add a Beam object specified as missile to the arraylist startTimer2 = true; //start the timer1 p2.missileCount--; //minus the number of missiles left if (p2.missileCount == 0) p2.missileMode = false; //turn off missile mode when player1 uses up all the missiles } } //Moving the beams for (int i = 0; i < beams2.size(); i++) { Beam j = (Beam) beams2.get(i); j.setX(-30); //setting the speed of the beams j.drawBeam2(); //draw beams if (j.getX()-30 < 0) { beams2.remove(j); //remove the beam when it goes out of the screen } p1.playerandbeamCollide(j); //collision detection with player1 if (p1.getHit() == true) { //if player1 gets hit hit.play(); //play sound effect beams2.remove(j); //remove beam p1.setHit(false); //reset the status of player1 } } if (p2.missileMode) { //if player1 has missiles textAlign(RIGHT); fill(100); text("Missile x" + p2.missileCount, width-10, height-10); //show the number of the remaining missiles } if (p2.shieldMode) { //if player1 has a shield textAlign(RIGHT); fill(100); text("Shield " + p2.shieldTime, width-10, height-80); //show the remaining time of the shield mode } if (p2.getHealth() <= 0 || p1.getHealth() <= 0) { //if any one of the players dies if (p2.getHealth() <= 0) { //if player2 dies p2.setY(10000); //remove player2 from the screen p2.setHealth(0); textAlign(CENTER); text(player1+", you're the boss!", width/2, height/2); //anouncing the winner } if (p1.getHealth() <= 0) { //if player1 dies p1.setY(10000); //remove player1 from the screen p1.setHealth(0); textAlign(CENTER); text(player2+", you're the boss!", width/2, height/2); //anouncing the winner } newPage.displayVictory(); //display "Victory" controlP5.controller("RESTART").setVisible(true); //make the restart button visible controlP5.controller("MAIN MENU").setVisible(true); //make the main menu button visible } } } //Listening to controlP5 actions void controlEvent(ControlEvent theEvent) { if (theEvent.isController()) { if (theEvent.controller().name()=="EXIT") exit(); //if the players click the exit button, exit the game if (theEvent.controller().name()=="START") { //if the players click the start button image(backgroundImage, 0, 0); newPage.displayNames(); //displaying buttons for the menu controlP5.controller("START").setVisible(false); controlP5.controller("INSTRUCTIONS").setVisible(false); controlP5.controller("CREDITS").setVisible(false); controlP5.controller("EXIT").setVisible(false); controlP5.controller("RESTART").setVisible(false); controlP5.controller("MAIN MENU").setVisible(false); controlP5.controller("BACK").setVisible(true); controlP5.controller("START GAME").setVisible(true); controlP5.controller("player1").setVisible(true); controlP5.controller("player2").setVisible(true); } if (theEvent.controller().name()=="INSTRUCTIONS") { //if the players click the instruction button image(backgroundImage, 0, 0); newPage.displayInstructions(); //display related buttons controlP5.controller("START").setVisible(false); controlP5.controller("INSTRUCTIONS").setVisible(false); controlP5.controller("CREDITS").setVisible(false); controlP5.controller("EXIT").setVisible(false); controlP5.controller("RESTART").setVisible(false); controlP5.controller("MAIN MENU").setVisible(false); controlP5.controller("BACK").setVisible(true); controlP5.controller("START GAME").setVisible(false); } if (theEvent.controller().name()=="BACK") { //if the players click the back button image(backgroundImage, 0, 0); newPage.displayMenu(); //display related buttons controlP5.controller("START").setVisible(true); controlP5.controller("INSTRUCTIONS").setVisible(true); controlP5.controller("CREDITS").setVisible(true); controlP5.controller("EXIT").setVisible(true); controlP5.controller("BACK").setVisible(false); controlP5.controller("player1").setVisible(false); controlP5.controller("player2").setVisible(false); controlP5.controller("START GAME").setVisible(false); //clear the text fields myTextfield1.clear(); myTextfield2.clear(); } if (theEvent.controller().name()=="CREDITS") { //if the players click the credits button image(backgroundImage, 0, 0); newPage.displayCredits(); //display related buttons controlP5.controller("START").setVisible(false); controlP5.controller("INSTRUCTIONS").setVisible(false); controlP5.controller("CREDITS").setVisible(false); controlP5.controller("EXIT").setVisible(false); controlP5.controller("BACK").setVisible(true); controlP5.controller("START GAME").setVisible(false); controlP5.controller("RESTART").setVisible(false); controlP5.controller("MAIN MENU").setVisible(false); } if (theEvent.controller().name()=="START GAME") { //if the players click the start game button //receives names player1 = myTextfield1.getText(); player2 = myTextfield2.getText(); if (player1.equals("") || player2.equals("")) { //if any player didn't enter name newPage.displayWarning(); //display warning page } else { gameStart = true; ///start the game } } if (theEvent.controller().name()=="MAIN MENU") { //if the players click the main menu button //display related buttons image(backgroundImage, 0, 0); newPage.displayMenu(); controlP5.controller("START").setVisible(true); controlP5.controller("INSTRUCTIONS").setVisible(true); controlP5.controller("CREDITS").setVisible(true); controlP5.controller("EXIT").setVisible(true); controlP5.controller("BACK").setVisible(false); controlP5.controller("START GAME").setVisible(false); controlP5.controller("RESTART").setVisible(false); controlP5.controller("MAIN MENU").setVisible(false); //resetting all data of the game p1.setHealth(100); p2.setHealth(100); p1.setY(valP_move1); p2.setY(valP_move2); myTextfield1.clear(); myTextfield2.clear(); p1.missileMode = false; p2.missileMode = false; p1.missileCount = 0; p2.missileCount = 0; p1.shieldMode = false; p2.shieldMode = false; blocks.clear(); //clear all Block objects in the arraylist for (int i = 0; i < 4; i++) { weapon[i] = (int) random(0, 56); //randomly pick blocks to put special weapons in } for (int i = 0; i < block_num; i++) { //creating blocks if (i == weapon[0] || i == weapon[1]) blocks.add(new Block(i, true, false)); //missiles else if (i == weapon[2] || i == weapon[3]) blocks.add(new Block(i, false, true)); //shields else blocks.add(new Block(i, false, false)); } //end of resetting data gameStart = false; } if (theEvent.controller().name()=="RESTART") { //if the players click the restart button //restart all data of the game p1.setHealth(100); p2.setHealth(100); p1.setY(valP_move1); p2.setY(valP_move2); p1.missileMode = false; p2.missileMode = false; p1.missileCount = 0; p2.missileCount = 0; p1.shieldMode = false; p2.shieldMode = false; image(backgroundImage, 0, 0); blocks.clear(); for (int i = 0; i < 4; i++) { weapon[i] = (int) random(0, 56); } for (int i = 0; i < block_num; i++) { if (i == weapon[0] || i == weapon[1]) blocks.add(new Block(i, true, false)); //missiles else if (i == weapon[2] || i == weapon[3]) blocks.add(new Block(i, false, true)); //shields else blocks.add(new Block(i, false, false)); } //end of resetting data //make restart and main menu buttons invisible controlP5.controller("RESTART").setVisible(false); controlP5.controller("MAIN MENU").setVisible(false); } } } //Drawing the main game playing screen void drawGame() { image(backgroundImage, 0, 0); textAlign(LEFT); textFont(newPage.generalFont); fill(255); text(p1.health+"%\n"+player1, 30, 100); //showing the health of player1 fill(newPage.skyBlue); fill(255); textFont(newPage.generalFont); textAlign(RIGHT); text(p2.health+"%\n"+player2, width-30, 100); //showing the health of player2 //displaying related buttons controlP5.controller("START").setVisible(false); controlP5.controller("INSTRUCTIONS").setVisible(false); controlP5.controller("CREDITS").setVisible(false); controlP5.controller("EXIT").setVisible(false); controlP5.controller("BACK").setVisible(false); controlP5.controller("START GAME").setVisible(false); controlP5.controller("player1").setVisible(false); controlP5.controller("player2").setVisible(false); } //Get values from the sensors void getSenVal() { if (0 < port.available()) { println(" "); port.readBytesUntil('&', inBuffer); if (inBuffer != null) { String myString = new String(inBuffer); String[] p = splitTokens(myString, "&"); if (p.length < 2) return; //exit this function if packet is broken //Value from player1's slider sensor String[] move1_sensor = splitTokens(p[0], "a"); if (move1_sensor.length != 3) return; valP_move1 = int(move1_sensor[1]); print("move1 sensor:"); print(valP_move1); println(" "); //Value from player1's force sensor for normal beams String[] shoot1_sensor = splitTokens(p[0], "b"); if (shoot1_sensor.length != 3) return; valP_shoot1 = int(shoot1_sensor[1]); print("shoot1 sensor:"); print(valP_shoot1); println(" "); //Value from player1's 2nd force sensor foor special weapons String[] sShoot1_sensor = splitTokens(p[0], "c"); if (sShoot1_sensor.length != 3) return; valP_sShoot1 = int(sShoot1_sensor[1]); print("sShoot1 sensor:"); print(valP_sShoot1); println(" "); //Value from player2's slider sensor String[] move2_sensor = splitTokens(p[0], "d"); if (move2_sensor.length != 3) return; valP_move2 = int(move2_sensor[1]); print("move2 sensor:"); print(valP_move2); println(" "); //Value from player2's force sensor for normal beams String[] shoot2_sensor = splitTokens(p[0], "e"); if (shoot2_sensor.length != 3) return; valP_shoot2 = int(shoot2_sensor[1]); print("shoot2 sensor:"); print(valP_shoot2); println(" "); //Value from player2's 2nd force sensor for special weapons String[] sShoot2_sensor = splitTokens(p[0], "f"); if (sShoot2_sensor.length != 3) return; valP_sShoot2 = int(sShoot2_sensor[1]); print("sShoot2 sensor:"); print(valP_sShoot2); println(" "); } } } //Stop the song void stopMusic() { player.close(); minim.stop(); super.stop(); }