voidcollision(GameObjectmoving, GameObjectnotMoving) { //Set up what happens when there is a collision between an object that tried to move and another object
102
if (moving==myObject){
103
if (notMoving.classTag=="diamond") {
104
notMoving.destroy();
105
score+=1;
106
}
107
if (notMoving==enemytwo){
108
lives--;
109
restartGame();
110
if (lives>0) {
111
question=CONTINUE;
112
drawQuestion("Continue Playing?");
113
} else {
114
question=PLAY_AGAIN;
115
drawQuestion("Restart Game?");
116
lives=3;
117
score=0;
118
}
119
120
}
121
}
122
123
if (moving==myObject) {
124
if (notMoving==enemy) {
125
lives--;
126
127
if (lives>0) {
128
question=CONTINUE;
129
drawQuestion("Continue Playing?");
130
} else {
131
question=PLAY_AGAIN;
132
drawQuestion("Restart Game?");
133
//lives = 3;
134
//score = 0;
135
}
136
//if (lives < 0){
137
//lives = 3;
138
//}
139
}
140
if (notMoving.classTag=="money") {
141
notMoving.destroy();
142
score+=1;
143
}
144
}
145
}
146
147
148
/*Base code for Processing Games*/
149
staticintobjectArraySize=200;
150
staticGameObject[] objects=newGameObject[objectArraySize]; //Array of Game Objects, setting an upper limit on how many objects you can run
151
staticintarrayIndex=0; //current index of the last object; effectively the size of the array of objects
if (mouseX>=questionX&&mouseX<=questionX+questionWidth/2&&mouseY>=questionY+questionHeight/2&&mouseY<=questionY+questionHeight&&askingQuestion==true) {
161
loop();
162
askingQuestion=false;
163
answer=true;
164
returnYES;
165
}
166
if (mouseX>=questionX+questionWidth/2&&mouseX<=questionX+questionWidth&&mouseY>=questionY+questionHeight/2&&mouseY<=questionY+questionHeight&&askingQuestion==true) {
167
loop();
168
askingQuestion=false;
169
answer=false;
170
returnNO;
171
}
172
returnNO_ANSWER;
173
}
174
175
intcheckCollision(GameObjecttest) { //Check to see if the object hits another object, and return it's index (-1 if no object)
176
if (getIndex(test) ==-1) {
177
return-1;
178
}
179
for (inta=0; a<arrayIndex; a++) {
180
if (test!=objects[a] &&test.x<=objects[a].x+objects[a].spriteWidth&&objects[a].x<=test.x+test.spriteWidth&&
“Catch Game” by Shreya
https://openprocessing.org/sketch/966461
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
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!
Arrow keys or WASD to move
CC Attribution ShareAlike
Catch Game
xxxxxxxxxx
/* @pjs preload="Ghost1.gif, Money.gif, Fire.gif, Diamant.gif, Bear.gif "; */
String ghostLoc = "Ghost1.gif", moneyLoc = "Money.gif", fireLoc = "Fire.gif", diamantLoc = "Diamant.gif", bearLoc = "Bear.gif";
color backgroundColor = color(50, 50, 50);
static int gameWidth = 600, gameHeight = 400;
GameObject myObject, leftWall, rightWall, topWall, bottomWall, startMoney, enemy, enemytwo, controller,controllertwo, startDiamant;
final int CONTINUE = 0;
final int PLAY_AGAIN;
int score = 0, lives = 3, question = CONTINUE;
void setup() { //Any code that has to happen at the start of the game goes here
size(600, 400);
noStroke();
myObject = new GameObject(50, 50);
myObject.speed = 2;
myObject.direction = 0;
myObject.setSprite(ghostLoc);
leftWall = new GameObject(0, 16, 16, gameHeight - 32);
leftWall.solid = true;
rightWall = new GameObject(gameWidth - 16, 16, 16, gameHeight - 32);
rightWall.solid = true;
topWall = new GameObject(0, 0, gameWidth, 16);
topWall.solid = true;
bottomWall = new GameObject(0, gameHeight - 16, gameWidth, 16);
bottomWall.solid = true;
startMoney = new GameObject(int(random(16, gameWidth - 48)), int(random(16, gameHeight - 48)), moneyLoc);
startMoney.classTag = "money";
enemy = new GameObject(gameWidth - 64, gameHeight - 64, fireLoc);
enemy.speed = 1.5;
controller = new GameObject(0, 0, 0, 0);
controller.setAlarm(2000);
startDiamant = new GameObject(int(random(16, gameWidth -48)), int(random(16, gameHeight -48)), diamantLoc);
startDiamant.classTag = "diamond";
controllertwo = new GameObject(0,0,0,0);
controllertwo.setAlarm(2000);
enemytwo = new GameObject(gameWidth - 90, gameHeight - 100, bearLoc);
enemytwo.speed = 1.5;
}
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();
enemy.setTowards(myObject);
updateObjects();
drawObjects();
stroke(0);
fill(0);
text("Score:" + score + " Lives:" + lives, 16, 15);
enemytwo.setTowards(myObject);
}
void keyPressed() { //Set up what happens when a key is pressed
if (key == CODED) {
if (keyCode == LEFT) {
myObject.move(2, 180);
} else if (keyCode == RIGHT) {
myObject.move(2, 0);
} else if (keyCode == UP) {
myObject.move(2, 90);
} else if (keyCode == DOWN) {
myObject.move(2, 270);
}
}
}
void mousePressed() {
int check = checkAnswer();
if (question == CONTINUE) {
if (check == YES) {
restartGame();
} else if (check == NO) {
println("Game Over!");
endGame();
}
} else if (question == PLAY_AGAIN) {
if (check == YES) {
score = 0;
lives = 3;
restartGame();
}
else if (check == NO) {
println("Game Over");
endGame();
}
}
}
void alarm(GameObject alarmObject) { //Fires when an object alarm counts down to zero
if (alarmObject == controller) {
GameObject newMoney = new GameObject(int(random(16, gameWidth - 48)), int(random(16, gameHeight - 48)), moneyLoc);
newMoney.classTag = "money";
controller.setAlarm(2000);
GameObject newDiamant = new GameObject(int(random(16, gameWidth - 48)), int(random(16, gameHeight - 48)), diamantLoc);
newDiamant.classTag = "diamond";
controllertwo.setAlarm(2000);
}
}
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 == myObject){
if (notMoving.classTag == "diamond") {
notMoving.destroy();
score += 1;
}
if (notMoving == enemytwo){
lives --;
restartGame();
if (lives > 0) {
question = CONTINUE;
drawQuestion("Continue Playing?");
} else {
question = PLAY_AGAIN;
drawQuestion("Restart Game?");
lives = 3;
score = 0;
}
}
}
if (moving == myObject) {
if (notMoving == enemy) {
lives--;
if (lives > 0) {
question = CONTINUE;
drawQuestion("Continue Playing?");
} else {
question = PLAY_AGAIN;
drawQuestion("Restart Game?");
//lives = 3;
//score = 0;
}
//if (lives < 0){
//lives = 3;
//}
}
if (notMoving.classTag == "money") {
notMoving.destroy();
score += 1;
}
}
}
/*Base code for Processing Games*/
static int objectArraySize = 200;
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() {
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) {
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);
textFont(createFont("Arial", 72, false), 12);
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() {
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() {
while (arrayIndex > 0) {
objects[0].destroy();
}
setup();
}
void updateObjects() { //Move all objects bassed on their speed and direction; also check for collisions
for (int a = 0; a < arrayIndex; a++) {
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]);
}
}
}
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 = "";
boolean solid = false; //if solid is true, cannot be moved into
int startTime, alarmTime = -1;
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() { //Removes an object from the object list, effectively destroying it
for (int a = 0; a < arrayIndex; a++) {
if (this == objects[a]) {
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--;
}
}
}
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) {
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