Collision Detection Images
in
Programming Questions
•
4 months ago
Hey all, I need major help with the collision detection! I need my spaceship (Trader2) to detect when it collides with the ufo's and asteroids!
- //Space Jump - Created By Ethan Keating
- //June 1st 2013
- // Would like to thank all that helped me when I struggled with programming issues
- // when creating this game!
- // All the images for the game
- Aliens aliens;
- 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;
- // The floats for the images, giving them 2 or more specific jobs.
- float enemyX;
- float enemyY;
- 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 for positions of the ufo's
- int posx;
- int posy;
- int posa;
- int posb;
- // Creates the setup code, loading all the images to be able to be put
- // into the game
- void setup() {
- size (1249, 768); // The size of the game
- smooth();
- aliens = new Aliens(random(200, 1000), random(100, 768), random(-4, 4));
- space = loadImage("space.png"); // The background
- trader = loadImage("trader.png"); // Spaceship when not mving
- trader2 = loadImage("trader2.png"); // Spaceship when moving
- ufo = loadImage("ufo.png"); // Ufo design #1
- ufo2 = loadImage("ufo2.png"); // Ufo design #2
- earth = loadImage("earth.png"); // Earth
- uranaus = loadImage("uranaus.png"); // Uranaus
- venus = loadImage("venus.png"); // Venus
- blackhole = loadImage("blackhole.png"); // Blackholes
- asteroid = loadImage("astroid.png"); // Astroids in level two
- sun = loadImage("sun.png"); // The sun
- safe = loadImage("safe.png"); // The middle of the map, safe zone for ship
- enemyX = 100;
- enemyY = 600;
- 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;
- posa = 200;
- posb = 500;
- }
- //
- void draw() {
- background(space);
- aliens.setup();
- aliens.draw();
- imageMode(CENTER);
- // Alien Level 1
- image(ufo, enemyX, enemyY);
- image(ufo, c, 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, c, 200);
- image(ufo, c, 250);
- image(ufo, c, 300);
- image(ufo, d, 150);
- image(asteroid, q, 200);
- image(asteroid, c, 100);
- image(asteroid, r, 250);
- image(asteroid, e, 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
- enemyX +=3;
- c -=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(enemyX>width) enemyX=0;
- if(enemyY<0) enemyY=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;
- }
1