voidcollision(GameObjectmoving, GameObjectnotMoving) { //Set up what happens when there is a collision between an object that tried to move and another object
intcheckAnswer() { //Check to see if user clicked yes or no
306
if (mouseX>=questionX&&mouseX<=questionX+questionWidth/2&&mouseY>=questionY+questionHeight/2&&mouseY<=questionY+questionHeight&&askingQuestion==true) {
307
loop();
308
askingQuestion=false;
309
answer=true;
310
returnYES;
311
}
312
if (mouseX>=questionX+questionWidth/2&&mouseX<=questionX+questionWidth&&mouseY>=questionY+questionHeight/2&&mouseY<=questionY+questionHeight&&askingQuestion==true) {
313
loop();
314
askingQuestion=false;
315
answer=false;
316
returnNO;
317
}
318
returnNO_ANSWER;
319
}
320
321
intcheckCollision(GameObjecttest) { //Check to see if the object hits another object, and return it's index (-1 if no object)
322
if (getIndex(test) ==-1) {
323
return-1;
324
}
325
for (inta=0; a<arrayIndex; a++) {
326
if (test!=objects[a] &&test.x<=objects[a].x+objects[a].spriteWidth&&objects[a].x<=test.x+test.spriteWidth
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
Click start wave, then select which tower and place it on a squar
A fork of Tower Game by Shreya
CC Attribution ShareAlike
Tower Game- Shreya
xxxxxxxxxx
color backgroundColor = color(50, 50, 50);
static int gameWidth = 600, gameHeight = 400;
GameObject base;
float[] xCoord = {42, 82, 82, 142, 142, 202, 202, 262, 262, 322, 322, 382, 382, 442, 442, 502};
float[] yCoord = {22, 22, 362, 362, 22, 22, 362, 362, 22, 22, 362, 362, 22, 22, 362, 362};
int coordMax = 16;
GameObject[] enemyList = new GameObject[200];
int[] enemyCoord = new int[200];
int[] enemyHealth = new int[200];
int listMax = 0;
GameObject controller;
//wave Variables
int waveNumber = 0;
int enemyCountWave1 = 0;
int totalWave1 = 0;
boolean waveStarted = false;
boolean gameOver = false;
//colors
color enemyColor = color(255, 0, 0);
color whiteColor = color(255, 255, 255);
color blackColor = color(0, 0, 0);
color projectileColor = color(255, 255, 50);
//interface Variables
color waveNotStartedColor = color(150, 20, 20), waveStartedColor = color(20, 150, 20);
int waveButtonX = 520, waveButtonY = 10, waveButtonWidth = 80, waveButtonHeight = 60;
int stateDisplayX = 530, stateDisplayY = 220, stateDisplayWidth = 60, stateDisplayHeight = 50;
int moneyDisplayX = 520, moneyDisplayY = 290, moneyDisplayWidth = 80, moneyDisplayHeight = 45;
int money = 40, health = 100;
//tower variables
int tower1ButtonX = 520, tower1ButtonY = 80, tower1ButtonWidth = 80, tower1ButtonHeight = 60;
color tower1Color = color(20, 150, 150), tower2Color = color(20, 50, 100), defaultTowerColor = color(100, 100, 100);
int tower1Cost = 10, tower2Cost = 20;
int tower1Range = 50, tower2Range = 100;
final int NO_TOWER = 0, TOWER1 = 1, TOWER2 = 2;
int towerState = NO_TOWER;
int tower2ButtonX = 520, tower2ButtonY = 150, tower2ButtonWidth = 80, tower2ButtonHeight = 60;
float[] towerXCoord = {110, 110, 110, 110, 230, 230, 230, 230, 350, 350, 350, 350, 470, 470, 470, 470};
float[] towerYCoord = {40, 130, 220, 310, 40, 130, 220, 310, 40, 130, 220, 310, 40, 130, 220, 310};
int towerWidth = 20, towerHeight = 20;
GameObject[] towerList;
int[] towerStateList;
int totalTowers = 16;
//base variables
int baseX = 500, baseY = 350, baseWidth = 40, baseHeight = 40;
color baseColor = color(0, 0, 255);
void setup() { //Any code that has to happen at the start of the game goes here
size(600, 400);
noStroke();
base = new GameObject(baseX, baseY, baseWidth, baseHeight, baseColor);
base.classTag = "base";
controller = new GameObject(-10, -10, 0, 0);
controller.classTag = "controller";
towerList = new GameObject[totalTowers];
towerStateList = new int[totalTowers];
for(int i = 0; i < totalTowers; i++)
{
towerList[i] = new GameObject(towerXCoord[i], towerYCoord[i], towerWidth, towerHeight, defaultTowerColor);
towerList[i].classTag = "tower"
towerStateList[i] = NO_TOWER;
}
waveNumber = 0;
waveStarted = false;
health = 100;
money = 40;
gameOver = false;
}
void draw() { //The draw loops continuously in order to run the game, change code here that you want to check every game loop
background(backgroundColor);
noStroke();
updateObjects();
fill(whiteColor);
for(i = 1; i < 8; i++)
{
rect(20 + (60 * i), 20 , 20, 360);
}
for(i = 0; i < 4; i++)
{
rect(40 + (120 * i), 20 , 60, 20);
}
for(i = 0; i < 4; i++)
{
rect(80 + (120 * i), 360 , 60, 20);
}
drawObjects();
if(waveStarted == false)
{
fill(waveNotStartedColor);
rect(waveButtonX, waveButtonY, waveButtonWidth, waveButtonHeight);
fill(blackColor);
text("Start Wave: " + (waveNumber+1), waveButtonX+2, waveButtonY+20);
}
else
{
fill(waveStartedColor);
rect(waveButtonX, waveButtonY, waveButtonWidth, waveButtonHeight);
fill(blackColor);
text("Wave: " + waveNumber, waveButtonX+2, waveButtonY+20);
}
fill(tower1Color);
rect(tower1ButtonX, tower1ButtonY, tower1ButtonWidth, tower1ButtonHeight);
fill(blackColor);
text("Tower 1: " + tower1Cost, tower1ButtonX+2, tower1ButtonY+20);
clearObjects();
fill(tower2Color);
rect(tower2ButtonX, tower2ButtonY, tower2ButtonWidth, tower2ButtonHeight);
fill(blackColor);
text("Tower 2: " + tower2Cost, tower2ButtonX+2, tower2ButtonY+20);
switch(towerState)
{
case NO_TOWER:
fill(defaultTowerColor);
break;
case TOWER1:
fill(tower1Color);
break;
case TOWER2:
fill(tower2Color);
break;
}
rect(stateDisplayX, stateDisplayY, stateDisplayWidth, stateDisplayHeight);
fill(blackColor);
text("Current", stateDisplayX+2, stateDisplayY+20);
text("Tower", stateDisplayX+2, stateDisplayY+35);
fill(whiteColor);
rect(moneyDisplayX, moneyDisplayY, moneyDisplayWidth, moneyDisplayHeight);
fill(blackColor);
text("Money: " + money, moneyDisplayX + 2, moneyDisplayY + 20);
text("Health: " + health, moneyDisplayX + 2, moneyDisplayY + 35);
if(gameOver)
{
drawQuestion("Game Over: Play Again?");
}
clearObjects();
}
void keyPressed() { //Set up what happens when a key is pressed
}
void mousePressed() { //Set up what happens when the mouse is pressed
if(mouseX > waveButtonX && mouseX < waveButtonX + waveButtonWidth && mouseY > waveButtonY && mouseY < waveButtonY + waveButtonHeight
&& waveStarted == false && waveNumber == 0)
{
waveNumber++;
controller.setAlarm(100);
waveStarted = true;
}
if(mouseX > tower1ButtonX && mouseX < tower1ButtonX + tower1ButtonWidth && mouseY > tower1ButtonY && mouseY < tower1ButtonY + tower1ButtonHeight)
{
towerState = TOWER1;
}
if(mouseX > tower2ButtonX && mouseX < tower2ButtonX + tower2ButtonWidth && mouseY > tower2ButtonY && mouseY < tower2ButtonY + tower2ButtonHeight)
{
towerState = TOWER2;
}
for(int i = 0; i < totalTowers; i++)
{
if(mouseX > towerXCoord[i] && mouseX < towerXCoord[i] + towerWidth && mouseY > towerYCoord[i] && mouseY < towerYCoord[i] + towerHeight)
{
if(towerState == TOWER1 && money >= tower1Cost)
{
towerStateList[i] = towerState;
towerList[i].idTag = "tower1";
towerList[i].objectColor = tower1Color;
money -= tower1Cost;
towerList[i].setAlarm(2000);
}
if(towerState == TOWER2 && money >= tower2Cost)
{
towerStateList[i] = towerState;
towerList[i].idTag = "tower2";
towerList[i].objectColor = tower2Color;
money -= tower2Cost;
towerList[i].setAlarm(1000);
}
}
}
int check = checkAnswer();
if(check == YES)
{
restartGame();
}
else if(check == NO)
{
println("Game Over!");
endGame();
}
}
void alarm(GameObject alarmObject) { //Fires when an object alarm counts down to zero
if(alarmObject.classTag == "controller")
{
if(int(random(1, 6)) == 5)
{
enemyList[listMax] = new GameObject(xCoord[0], yCoord[0], 15, 15, color(0, 255, 150));
enemyList[listMax].classTag = "enemy";
enemyList[listMax].idTag = "enemy2";
enemyList[listMax].speed = 4;
enemyCoord[listMax] = 0;
enemyHealth[listMax] = 5;
listMax++;
alarmObject.setAlarm(3000);
}
else
{
enemyList[listMax] = new GameObject(xCoord[0], yCoord[0], 15, 15, enemyColor);
enemyList[listMax].classTag = "enemy";
enemyList[listMax].speed = 2;
enemyCoord[listMax] = 0;
enemyHealth[listMax] = 3;
listMax++;
alarmObject.setAlarm(3000);
}
}
if(alarmObject.classTag == "tower")
{
if(alarmObject.idTag == "tower1")
{
for(int a; a < listMax; a++)
{
alarmObject.setAlarm(2000);
float xdist = alarmObject.x - enemyList[a].x;
float ydist = alarmObject.y - enemyList[a].y;
if(sqrt(xdist*xdist + ydist*ydist) <= tower1Range)
{
GameObject projectile = new GameObject(alarmObject.x, alarmObject.y, 10, 10, projectileColor);
projectile.setTowards(enemyList[a]);
projectile.speed = 10;
projectile.classTag = "projectile";
}
}
}
else if(alarmObject.idTag == "tower2")
{
for(int a; a < listMax; a++)
{
alarmObject.setAlarm(1000);
float xdist = alarmObject.x - enemyList[a].x;
float ydist = alarmObject.y - enemyList[a].y;
if(sqrt(xdist*xdist + ydist*ydist) <= tower2Range)
{
GameObject projectile = new GameObject(alarmObject.x, alarmObject.y, 10, 10, projectileColor);
projectile.setTowards(enemyList[a]);
projectile.speed = 10;
projectile.classTag = "projectile";
}
}
}
}
}
void collision(GameObject moving, GameObject notMoving) { //Set up what happens when there is a collision between an object that tried to move and another object
if(moving.classTag == "projectile" && notMoving.classTag == "enemy")
{
moving.destroy();
if(damageEnemy(notMoving))
{
notMoving.destroy();
clearEnemy(notMoving);
}
}
if(moving.classTag == "enemy" && notMoving.classTag == "base")
{
moving.destroy();
if(moving.idTag == "enemy2")
{
health -= 40;
}
else
{
health -= 20;
}
clearEnemy(notMoving);
if(health <= 0)
{
gameOver = true;
}
}
}
/*Base code for Processing Games*/
static int objectArraySize = 400;
static GameObject[] objects = new GameObject[objectArraySize]; //Array of Game Objects, setting an upper limit on how many objects you can run
static int arrayIndex = 0; //current index of the last object; effectively the size of the array of objects
static int objectId = 100000;
static boolean askingQuestion = false, answer = false;
static int questionX=gameWidth/2-80, questionY=gameHeight/2-40, questionWidth=160, questionHeight=40;
final static int NO_ANSWER = 0;
final static int YES = 1;
final static int NO = 2;
int checkAnswer() { //Check to see if user clicked yes or no
if (mouseX >= questionX && mouseX <= questionX+questionWidth/2 && mouseY >= questionY+questionHeight/2 && mouseY <= questionY+questionHeight && askingQuestion == true) {
loop();
askingQuestion = false;
answer = true;
return YES;
}
if (mouseX >= questionX+questionWidth/2 && mouseX <= questionX+questionWidth && mouseY >= questionY+questionHeight/2 && mouseY <= questionY+questionHeight && askingQuestion == true) {
loop();
askingQuestion = false;
answer = false;
return NO;
}
return NO_ANSWER;
}
int checkCollision(GameObject test) { //Check to see if the object hits another object, and return it's index (-1 if no object)
if (getIndex(test) == -1) {
return -1;
}
for (int a = 0; a < arrayIndex; a++) {
if (test != objects[a] && test.x <= objects[a].x + objects[a].spriteWidth && objects[a].x <= test.x + test.spriteWidth
&& test.y <= objects[a].y + objects[a].spriteHeight && objects[a].y <= test.y + test.spriteHeight) {
return a;
}
}
return -1;
}
void drawQuestion(String question) { //Draw a question on the screen
stroke(0);
fill(255);
rect(questionX-2, questionY-2, questionWidth+4, questionHeight+4);
rect(questionX, questionY, questionWidth, questionHeight);
rect(questionX, questionY+questionHeight/2, questionWidth/2, questionHeight/2);
rect(questionX+questionWidth/2, questionY+questionHeight/2, questionWidth/2, questionHeight/2);
stroke(255);
fill(0);
text(question, questionX+4, questionY+questionHeight-25);
text("Yes", questionX+4, questionY+questionHeight-2);
text("No", questionX+questionWidth/2+4, questionY+questionHeight-2);
askingQuestion = true;
noLoop();
}
void drawObjects() { //Draw all of the objects in the game
for (int a = 0; a < arrayIndex; a++) {
objects[a].drawObject();
}
}
void endGame() { //End the game loop
noLoop();
}
int getIndex(GameObject test) { //Return the index of the test object in the objects array
for (int a = 0; a < arrayIndex; a++) {
if (test == objects[a]) {
return a;
}
}
return -1;
}
void restartGame() { //Restart the game by destroying all objects, clearing them, and calling setup again
for (int a = 0; a < arrayIndex; a++) {
objects[a].destroy();
}
clearObjects();
setup();
}
void updateObjects() { //Move all objects based on their speed and direction; also check for collisions
for (int a = 0; a < arrayIndex; a++) {
if(objects[a].classTag == "enemy")//enemy movement
{
enemyUpdate(objects[a]);
}
else//normal movement
{
objects[a].x += cos(radians(objects[a].direction)) * objects[a].speed;
objects[a].y -= sin(radians(objects[a].direction)) * objects[a].speed;//Flip y-axis so that degrees increase counter-clockwise
}
int collidedIndex = checkCollision(objects[a]);
if (collidedIndex != -1) {
if (objects[collidedIndex].solid == true) {//If solid, jump back to previous position, set speed to zero
objects[a].x -= cos(radians(objects[a].direction)) * objects[a].speed;
objects[a].y += sin(radians(objects[a].direction)) * objects[a].speed;
objects[a].speed = 0;
}
collision(objects[a], objects[collidedIndex]);//Call the user-defined collision event
}
if (objects[a].alarmTime > 0 && millis() - objects[a].startTime > objects[a].alarmTime) {
alarm(objects[a]);
}
}
}
void enemyUpdate(GameObject enemy)
{
for(i = 0; i < listMax; i++)
{
if(enemy == enemyList[i])
{
if(enemyCoord[i] != coordMax)
{
float xdiff = (xCoord[enemyCoord[i]] - enemy.x);
float ydiff = (yCoord[enemyCoord[i]] - enemy.y);
if(abs(xdiff) > abs(ydiff))
{
if(abs(xdiff) <= enemy.speed)
{
enemy.x = xCoord[enemyCoord[i]];
enemy.y = yCoord[enemyCoord[i]];
enemyCoord[i]++;
}
else
{
enemy.y += (ydiff / abs(xdiff)) * enemy.speed;
enemy.x += (xdiff / abs(xdiff)) * enemy.speed;
}
}
else
{
if(abs(ydiff) <= enemy.speed)
{
enemy.x = xCoord[enemyCoord[i]];
enemy.y = yCoord[enemyCoord[i]];
enemyCoord[i]++;
}
else
{
enemy.x += (xdiff / abs(ydiff)) * enemy.speed;
enemy.y += (ydiff / abs(ydiff)) * enemy.speed;
}
}
}
}
}
}
boolean damageEnemy(GameObject enemy)
{
for (int a = 0; a < listMax; a++)
{
if (enemy == enemyList[a])
{
if(enemyHealth[a] > 1)
{
enemyHealth[a]--;
return(false);
}
else
{
return(true);
}
}
}
}
void clearEnemy(GameObject enemy)
{
for (int a = 0; a < listMax; a++)
{
if (enemy == enemyList[a])
{
for(int b = a; b < listMax - 1; b++)
{
enemyList[b] = enemyList[b+1];
enemyCoord[b] = enemyCoord[b+1];
enemyHealth[b] = enemyHealth[b+1];
}
enemyList[listMax] = null;
enemyCoord[listMax] = 0;
enemyHealth[listMax] = 0;
listMax--;
a--;
if(enemy.idTag == "enemy2")
{
money += 40;
}
else
{
money += 20;
}
}
}
}
void clearObjects() { //Removes an object from the object list, effectively destroying it
for (int a = 0; a < arrayIndex; a++) {
if (objects[a].toDestroy == true) {
for (int b = a; b < arrayIndex - 1; b++) { //move all the objects past this one down one
objects[b] = objects[b + 1];
}
objects[arrayIndex] = null; //set the last one to null
arrayIndex--;
a--;
}
}
}
class GameObject {
float x, y; //x and y position of object
float speed, direction; //speed in pixels per cycle, direction in degrees (0 is right, 90 is up, 180 is left, 270 is down)
int spriteWidth = 16; //width of sprite, rectangle if image is null
int spriteHeight = 16; //height of sprite, rectangle if image is null
PImage sprite = null; //sprite to be drawn
color objectColor = color(255, 255, 255); //object color
int id; //unique object id
String classTag = "", idTag = ""; //Tags for identifying objects by group or by id
boolean solid = false; //if solid is true, cannot be moved into
int startTime, alarmTime = -1; //Used for setting up timers
boolean toDestroy = false; //Flag for destroying object
GameObject() { //Default constructor
if (arrayIndex >= objectArraySize) { //if our object array isn't big enough
objectArraySize *= 2;
GameObject[] bigger = new GameObject[objectArraySize];
for (int a = 0; a < arrayIndex; a++) { //create an array twice the size, transfer all the objects over
bigger[a] = objects[a];
}
objects = bigger;
}
objects[arrayIndex++] = this; //add the object to the array
id = objectId++; //give the object a unique id
}
GameObject(float nx, float ny) { //Constructor takes x and y position
this();
x = nx;
y = ny;
}
GameObject(float nx, float ny, String imagePath) { //Constructor takes x and y position, and sprite path
this();
x = nx;
y = ny;
setSprite(imagePath);
}
GameObject(float nx, float ny, int sW, int sH) { //Constructor takes x and y position, sprite width, and sprite height
this(nx, ny);
spriteWidth = sW;
spriteHeight = sH;
}
GameObject(float nx, float ny, int sW, int sH, color c) { //Constructor takes x and y position, sprite width, sprite height, and object color
this(nx, ny, sW, sH);
objectColor = c;
}
void destroy() { //Sets an object to be removed from the game.
toDestroy = true;
}
void drawObject() { //Draw all objects in the object list
if (sprite == null) {
fill(objectColor);
rect(x, y, spriteWidth, spriteHeight);
}
else {
image(sprite, x, y);
}
}
void move(float newSpeed, float newDirection) { //Set speed and direction of object
this.speed = newSpeed;
this.direction = newDirection;
}
void setAlarm(int timeUntilAlarm) { //Set an alarm to happen after a certain amount of milliseconds
startTime = millis();
alarmTime = timeUntilAlarm;
}
void setSprite(String imagePath) { //Set the sprite to the one at the imagePath location
sprite = loadImage(imagePath);
if (sprite != null) {
spriteWidth = sprite.width;
spriteHeight = sprite.height;
}
}
void setTowards(GameObject destination) { //Set the current object angle to be the destination point
if (getIndex(destination) != -1) {
direction = degrees(atan2(-destination.y+y, destination.x-x)); //Note: y is negative due to flipped axis, atan2 due to quadrant issues
}
}
}
See More Shortcuts
Please verify your email to comment
Verify Email