We are about to switch to a new forum software. Until then we have removed the registration on this forum.
i am trying to create a space invader mini game...but i found it hard to remove the ufo when they got hited by the bullet as I am not sure about the code...can anyone here give me a help:P here is my code
int a=0;
int b=0;
int c=0;
int x = 250;
int y = 480;
int bullet_x = 250;
int bullet_y = 480;
PImage bg;
PImage flight;
PImage ufo;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
void setup()
{
size (500,500);
bg=loadImage("space.jpg");
ufo=loadImage("ufo.png");
flight=loadImage("flight.png");
minim = new Minim(this);
player = minim.loadFile("invaders.mp3",2048);
player.play();
player.loop();
ellipseMode(CENTER);
rectMode(CENTER);
smooth();
}
void draw()
{
background(bg);
bullet_y = bullet_y - 5;
fill(2,251,242);
rect(bullet_x, bullet_y, 10, 10);
image(flight,x-17,445,35,56);
a=a+2;
b=b+4;
c=c+3;
image(ufo,a,30,40,21);
image(ufo,b,70,40,21);
image(ufo,c,110,40,21);
if(a > width) {
a = 0;
}
if(b > width) {
b = 0;
}
if(c > width) {
c = 0;
}
if (keyPressed == true && key == 'd')
{
x = x+3;
}
if (keyPressed == true && key == 'a')
{
x = x-3;
}
}
void keyPressed()
{
if (key == ' ')
{
bullet_y = y;
bullet_x = x;
minim = new Minim(this);
player = minim.loadFile("gun.mp3",2048);
player.play();
}
}
void stop()
{
player.close();
minim.stop();
super.stop();
}
Answers
This very recent thread got some very simple collision detections you can use for your own UFO game:
http://forum.processing.org/two/discussion/319/help-with-this-simple-collision
You have to check that the bullet is within the bounds of the UFO rectangle.
A simple example like the Button one shows how to check if a point (the mouse position in the example, the bullet position in your example) is within a rectangle.