We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey guys I was wondering how to program collisions (I'm new to programming). How do I make it so that when Pac man hits any of the ellipses something will happen. For example if I touch an ellipse I want a "game over" sign to appear. I know how to program the sign but how do I program that interaction between pacman and my shapes when they touch each other. This is what I have so far:
PImage face;
PImage apple;
int y = 0;
int x = 0;
int speed = 3;
int positionX=700;
int positionY=500;
int diameter=70;
float y = 0;
float yspeed = 3;
float f = random(100,700);
void setup() {
size(700,500);
smooth();
face=loadImage("face.png");
apple=loadImage("apple.png");
frameRate(300);
}
void draw() {
background(255);
x = x + speed;
//left to right bounce
if ((x > width) || (x < 0)) {
speed = speed * -1;
}
stroke(0);
fill(175);
ellipse(x,100,70,70);
ellipse(x,300,70,70);
ellipse(x,600,70,70);
//up down bounce
y = y + yspeed;
if ((y > height) || (y < 0)) {
yspeed = yspeed * -1;
}
stroke(0);
fill(175);
ellipse(100,y,70,70);
ellipse(300,y,70,70);
ellipse(500,y,70,70);
//Diagnal bounce below
if(positionX>=width-diameter/2){
Switch=1;
}
if(positionX==diameter/2){
Switch=0;
}
if(Switch==0){
positionX++;
}
else if(Switch==1){;
positionX--;
}
if(positionY>=height-diameter/2){
Switch=1;
}
if(positionY==diameter/2){
Switch=0;
}
if(Switch==0){
positionY++;
}
else if(Switch==1){;
positionY--;
}
ellipse(positionX, positionY, diameter, diameter);
//Player image
imageMode(CENTER);
face.resize(50,50);
image(face, mouseX, mouseY);
//random image
apple.resize(40,40);
image (apple, f, f);
}
Answers
When posting code, please use the code button (or ctrl+k) to preserve your formatting.
The Processing examples contains several examples on collision detection, and google is your friend.
Here is a tutorial I wrote on collision detection in Processing: http://staticvoidgames.com/tutorials/intermediateConcepts/collisionDetection