// Gameplay parameters int numberOfPlayers = 4; int myWidth = 600; int myHeight = 400; float growSpeed = 0.3; float shrinkSpeed = 1.0; int tagbackTime = 2; // in seconds float winningSize = 100; int kidInitialSize = 20; // start positions int kid0x = 50; int kid0y = 50; int kid1x = myWidth-50; int kid1y = 50; int kid2x = myWidth-50; int kid2y = myHeight-50; int kid3x = 50; int kid3y = myHeight-50; // for no tagback tagback int framerate = 50; int tagbackLimit = tagbackTime*framerate; int justTagged=0; //the player number who just tagged int firstTag = 0; //to mark the first tag - only to solve the 0 player shrinking // basic setup Kid[] kids = new Kid[numberOfPlayers]; int gameState = 1; int IT; // IT is the player number who is currently IT void setup() { PFont titleFont; titleFont = loadFont("Algerian-48.vlw"); textFont(titleFont, 25); // active and size 25 textAlign(LEFT); size(600, 400); draw_background(); smooth(); frameRate(framerate); set_random_IT(); // create Kids create_kids(); setITs(IT); } void draw() { switch(gameState){ case 0: //reset background and players draw_background(); set_random_IT(); create_kids(); //set to playing gameState=1; break; case 1: //playing draw_background(); for (int i = 0; i < kids.length; i++){ kids[i].move(); kids[i].display(); kids[i].increment_tag_timer(); } IT = kids[IT].check_for_tag(IT); setITs(IT); kids[IT].grow(growSpeed); kids[IT].changeColor(); if(firstTag!=0){ // to make sure player 0 doesn't shrink on the first go kids[justTagged].shrink(shrinkSpeed); } //print(IT); check_winner(); break; case 2: //win fill(0,1,0); text("Kid "+(IT+1)+" rocks!\n\nEnter to play again", myWidth/2, myHeight/2); break; } } void create_kids(){ kids[0] = new Kid( kid0x, kid0y, kidInitialSize, 0); kids[0].setColors(102,0,102); if(numberOfPlayers>1){ kids[1] = new Kid( kid1x, kid1y, kidInitialSize, 1); kids[1].setColors(0,51,102); if(numberOfPlayers>2){ kids[2] = new Kid( kid2x, kid2y, kidInitialSize, 2); kids[2].setColors(0,0,163); if(numberOfPlayers>3){ kids[3] = new Kid( kid3x, kid3y, kidInitialSize, 3); kids[3].setColors(0,0,224); } } } } void set_random_IT(){ IT=int(random(-0.4,(numberOfPlayers-0.51))); } void draw_background() { background(102); fill(255, 255, 255); textAlign(LEFT); textSize(25); text("Can you\ncatch\nyellow?", myWidth/2, myHeight/6); textSize(16); text("Kid 1: cursors \nKid 2: x,v,d,c \nKid 3: q,e,2,w \nKid 4: j,l,i,k", myWidth/8, (3*myHeight)/4); textAlign(CENTER); textSize(25); } void check_winner(){ if (kids[IT].getSize()>=winningSize){ gameState = 2; } } void setITs(int iAmIT){ for (int i=0; iinitialSize){ size = size - s; println("shrinking!"); } } void changeColor(){ ITG = ITG-int(colorIncrement); } //-------------------------------------- //GET methods //-------------------------------------- // returnd the xPos and yPos values of the Kid float[] getPos(float[] positionVals) { positionVals[0] = xPos; positionVals[1] = yPos; return positionVals; } float getSize(){ return size; } int getPlayerNumber(){ return playerNumber; } int getTagback(){ return tagback; } //-------------------------------------- void check_distances(){ float xDist = 0; float yDist = 0; float hypothenuse = 0; for (int i = 0; i < kids.length; i++){ float[] posKid2 = new float[2]; float[] kidComparePos = kids[i].getPos(posKid2); //println("Kid "+i+"= x("+kidComparePos[0]+"), y("+kidComparePos[1]+")"); xDist = xPos-kidComparePos[0]; yDist = yPos-kidComparePos[1]; hypothenuse = sqrt(pow(xDist,2)+pow(yDist,2)); touching_distance[i] = hypothenuse - size/2 - kids[i].getSize()/2; //println("touching_distance["+i+"] ="+touching_distance[i]); } } int check_for_tag(int whoIsIT){ check_distances(); for(int i=0; itagbackLimit)){ justTagged = whoIsIT; //set the player who tagged as justTagged whoIsIT = i; //then set the person who is now IT // set the IT player to go faster kids[whoIsIT].setMoveSpeed(8); // fix the player who just tagged someone kids[justTagged].setTagback(0); kids[justTagged].reset_IT_colors(); kids[justTagged].setMoveSpeed(5); if (firstTag==0){firstTag=1;} } } return whoIsIT; } void increment_tag_timer(){ if(tagback<=tagbackLimit){ tagback = tagback+1; } //println("tagback "+playerNumber+":"+tagback); } void reset_IT_colors(){ ITR = 255; ITG = 255; ITB = 0; } void move() { if (((xPos+v[0]+(size/2))<=myWidth) && (0<=(xPos-(size/2)+v[0]))){ xPos = xPos + v[0]; } if(((yPos+v[1]+(size/2))<=myHeight) && (0<=(yPos-(size/2)+v[1]))){ yPos = yPos + v[1]; } } void display() { if (IT == true){ fill(ITR, ITG, ITB); } else if (IT == false){ fill(R, G, B); } ellipse(xPos, yPos, size, size); } } // Keys void keyPressed(){ //To set gameState to play if ((keyCode == ENTER) && (gameState==2)){ gameState = 0; } //Player 1 if (keyCode == UP) { kids[0].setVelocity(0,-1); } if (keyCode == DOWN) { kids[0].setVelocity(0,1); } if (keyCode == RIGHT) { kids[0].setVelocity(1,0); } if (keyCode == LEFT) { kids[0].setVelocity(-1,0); } if (numberOfPlayers>=2){ //Player 2 if ((key == 'd') || (key == 'D')) { kids[1].setVelocity(0,-1); } if ((key == 'c') || (key == 'C')) { kids[1].setVelocity(0,1); } if ((key == 'v') || (key == 'V')) { kids[1].setVelocity(1,0); } if ((key == 'x') || (key == 'x')) { kids[1].setVelocity(-1,0); } if (numberOfPlayers >=3){ //Player 3 if ((key == '2') || (key == '@')) { kids[2].setVelocity(0,-1); } if ((key == 'w') || (key == 'W')) { kids[2].setVelocity(0,1); } if ((key == 'e') || (key == 'E')) { kids[2].setVelocity(1,0); } if ((key == 'q') || (key == 'Q')) { kids[2].setVelocity(-1,0); } //Player 4 if (numberOfPlayers >= 4){ if ((key == 'i') || (key == 'I')) { kids[3].setVelocity(0,-1); } if ((key == 'k') || (key == 'K')) { kids[3].setVelocity(0,1); } if ((key == 'l') || (key == 'L')) { kids[3].setVelocity(1,0);; } if ((key == 'j') || (key == 'J')) { kids[3].setVelocity(-1,0); } } // end 4 player } //end 3 player } // end 2 player }