Collision Detection
in
Programming Questions
•
4 months ago
Hey, I am currently making a game similar to frogger but in space! I need help with collision detection when the spaceship crashes into the other aliens and asteroids!
Any help would be appreciated. I know I need make array classes, will do that later when closer to finishing when I tidy it up and add comments :)
- PImage space;
- PImage trader;
- PImage trader2;
- PImage ship;
- PImage ufo;
- PImage ufo2;
- PImage earth;
- PImage uranaus;
- PImage mars;
- PImage venus;
- PImage work;
- PImage blackhole;
- PImage asteroid;
- PImage sun;
- PImage safe;
- float a;
- float b;
- float c;
- float d;
- float e;
- float f;
- float g;
- float h;
- float i;
- float j;
- float k;
- float l;
- float m;
- float n;
- float o;
- float p;
- float q;
- float r;
- int posx;
- int posy;
- boolean left, right, up, down;
- boolean runOnce = true;
- void setup() {
- size (1249, 768);
- smooth();
- space = loadImage("space.png");
- trader = loadImage("trader.png");
- trader2 = loadImage("trader2.png");
- ufo = loadImage("ufo.png");
- ufo2 = loadImage("ufo2.png");
- earth = loadImage("earth.png");
- uranaus = loadImage("uranaus.png");
- venus = loadImage("venus.png");
- work = loadImage("work.png");
- blackhole = loadImage("blackhole.png");
- asteroid = loadImage("astroid.png");
- sun = loadImage("sun.png");
- safe = loadImage("safe.png");
- a = 100;
- b = 200;
- c = 300;
- d = 400;
- e = 580;
- f = 670;
- g = 760;
- h = 1200;
- i = 835;
- j = 920;
- k = 1000;
- l = 1100;
- m = 1200;
- n = 110;
- o = 240;
- p = 380;
- q = 225;
- r = 625;
- posx = 600;
- posy = 725;
- }
- void draw() {
- background(space);
- imageMode(CENTER);
- // Alien Level 1
- image(ufo, a, 600);
- image(ufo, b, 550);
- image(ufo, c, 550);
- image(ufo, d, 550);
- image(ufo, e, 550);
- image(ufo, g, 550);
- image(ufo, h, 550);
- image(ufo, i, 550);
- image(ufo2, j, 450);
- image(ufo2, k, 650);
- image(ufo2, l, 450);
- image(ufo2, m, 650);
- image(ufo2, n, 450);
- image(ufo2, o, 650);
- image(ufo2, p, 450);
- // Alien Level 2
- image(ufo, a, 200);
- image(ufo, b, 250);
- image(ufo, c, 300);
- image(ufo, d, 150);
- image(asteroid, q, 200);
- image(asteroid, q, 100);
- image(asteroid, r, 250);
- image(asteroid, r, 225);
- //misc images
- image(earth, 1030, 25);
- image(uranaus, 630, 25);
- image(venus, 230, 25);
- image(blackhole, 310, 30);
- image(blackhole, 940, 30);
- image(sun, 100, 30);
- image(sun, 1150, 30);
- image(safe, 623, 380);
- image(trader2, posx, posy);
- //movement of aliens
- a +=3;
- b -=4;
- c +=5;
- d -=6;
- e +=3;
- f -=4;
- g +=5;
- h -=6;
- i +=3;
- j -=4;
- l +=5;
- k +=6;
- m +=3;
- n -=4;
- o +=5;
- p -=6;
- q -=9;
- r +=8;
- if(a>width) a=0;
- if(b<0) b=width;
- if(c>width) c=0;
- if(d<0) d=width;
- if(e>width) e=0;
- if(f<0) f=width;
- if(g>width) g=0;
- if(h<0) h=width;
- if(i>width) i=0;
- if(j<0) j=width;
- if(k>width) k=0;
- if(l<0) l=width;
- if(m>width) m=0;
- if(n<0) n=width;
- if(o>width) o=0;
- if(q<0) q=width;
- if(r>width) r=0;
- }
- void keyPressed()
- {
- if (keyCode== RIGHT)
- posx+=10;
- if (keyCode== LEFT)
- posx-=10;
- if (posx<0) posx=0;
- if (posx>width) posx=width;
- if (keyCode== DOWN)
- posy+=10;
- if (keyCode== UP)
- posy-=10;
- if (posy<0) posy=0;
- if (posy>height) posy=height;
- }
Thanks :)
1