[quote="hotfooted, post:2, topic:8627, full:true"]
if you aren't removing the asteroid it probably just catching the collision multiple times? it's hard to say without seeing the code.
[/quote]
Hi friend,
Hmm, I don´t want to remove the asteroid, basically the idea is: Having a spaceship inside the frame that have to fly around the frame without shocking with the asteroid. If the user could keep flying between shocking in 30s he moves on to the next level.
I will try to put the code here but will be a little bit large:
//THIS IS MY FRAME MODE.
//Loading font
PFont font;
//game levels
int mode; //1: intro, 2: level01, 3:level02, 4: level3, 5:gameover
//asteriodes movements
int xpos, ypos, vx, vy;
//Game features
int score;
int live;
//spaceship elements
PImage ship;
PImage ship2;
int shipsize = 100;
spaceship shiptrek;
//movement of the ship
boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;
//gif generator
PImage[] gif;
int currentPic = 0;
//Timer for the levels.
long timer = 0;
long TIMEOUT = 30000;
int time = 0;
//Asteroids
PImage asteroid;
//mouse hover for intro button:
float rectX;
float rectY;
float rectWidth;
float rectHeight;
//collision:
boolean collision = false;
float asteroidWidth = 100;
float asteroidHeight = 100;
float shipWidth = 50;
float shipHeight = 70;
void setup() {
timer = millis();
size(600,600);
frameRate(40);
mode = 1;
xpos = 100;
ypos = 300;
vx = 2;
vy = 5;
asteroid = loadImage("asteroid.png");
ship = loadImage("ship2.png");
gif = new PImage[21];
int index = 0;
while (index < 21) {
gif[index] = loadImage("starsfinal_" + index + ".jpg");
index = index + 1;
}
//ship.resize(50,90);
shiptrek = new spaceship();
imageMode(CENTER);
//mouse hover intro:
rectX = 168;
rectY = 280;
rectWidth = 270;
rectHeight = 128;
}
void draw() {
if (mode == 1) {
intro();
} else if(mode == 2) {
level01();
} else if(mode == 3) {
level02();
} else if(mode == 4) {
level03();
} else if(mode == 5){
gameOver();
} else {
background(25, 25, 25);
}
}
//THIS IS THE SPACESHIP
class spaceship {
//1. Instance variables
int lives;
PVector location;
PVector velocity;
PVector direction;
PVector stop;
//2. Constructor - same name as the class. No void (return type)
spaceship() {
lives = 3;
location = new PVector(300, 300);
direction = new PVector(0, -0.05);
velocity = new PVector(0, 0.2);
stop = new PVector(0,0);
}
//3. Behaviour functions
void show() {
pushMatrix();
translate(location.x, location.y);
rotate(direction.heading());
rotate(1.55);
fill(255, 0, 255, 100);
rect(0, 0, shipWidth, shipHeight);
image(ship, 0, 0);
popMatrix();
}
void act() {
//velocity.rotate( radians(50) );
location.add(velocity);
if (left) direction.rotate( radians(-5) );
if (right) direction.rotate( radians(5) );
if (up) velocity.add(direction);
if (down) velocity.add(stop);
if (location.x > width || location.x < 0) {
mode = 5;
}
if (location.y > height + 20 || location.y < -20) {
mode = 5;
}
}
}
//THIS IS THE LEVEL ONE
void level01() {
time = time + 1;
if (time > 1150) mode = 3;
imageMode(CORNER);
background(0, 0, 0);
image(gif[currentPic], 0, 0, 600, 600);
currentPic++;
if (currentPic >= 20) {
currentPic = 0;
}
shiptrek.show();
shiptrek.act();
if (live == 0) {
mode = 5;
}
font = loadFont("AmstoniaSans-34.vlw");
textFont(font);
//textSize(34);
fill(255);
//text("SCORE: " + score, 20, 40);
text("LIVES: " + live, 20, 80);
//noStroke();
//ellipse(xpos, ypos, 50, 50);
//imageMode(CENTER);
image(asteroid, xpos, ypos);
fill(255, 0, 0, 125);
rect(xpos, ypos, asteroidWidth, asteroidHeight);
xpos = xpos + vx;
ypos = ypos + vy;
if (ypos < -10 || ypos > height -80) {
vy = vy * -1;
}
if (xpos < 0 || xpos > width-90) {
vx = vx * -1;
}
//Defining the collision X
if (xpos + asteroidWidth + vx > shiptrek.location.x &&
xpos + vx < shiptrek.location.x + shipWidth &&
ypos + asteroidHeight > shiptrek.location.y &&
ypos < shiptrek.location.y + shipHeight) {
if (time > 1) {
live = live - 1;
}
}
//Defining the collision Y
if (xpos + asteroidWidth + vx > shiptrek.location.x &&
xpos < shiptrek.location.x + shipWidth &&
ypos + asteroidHeight + vy > shiptrek.location.y &&
ypos + vy < shiptrek.location.y + shipHeight) {
live = live - 1;
}
}
//Timer
// draw the clock's rectangle
fill(25, 25, 37, 127);
stroke(168, 153, 108);
rect(20,height-40,width-40,20);
// calculate current time
long currentTime = millis() - timer;
// choose the color, red for < 5s
//gris
//fill(197,211,211, 120);
//Azul
fill(27,72,113, 140);
if(TIMEOUT - currentTime < 5000)
fill(0,255,0);
// width of the countdown
float w = map(currentTime,0,TIMEOUT,0,width-40);
// draw if not the end of time
if(TIMEOUT - currentTime > 0)
rect(20,height-40,width-40-w,20);
}
void keyPressed() {
if (keyCode == 'W') {
up = true;
}
if (key == 's') {
down = true;
}
if (key == 'a') {
left = true;
}
if (key == 'd') {
right = true;
}
}
void keyReleased() {
if (keyCode == 'W') {
up = false;
}
if (key == 's') {
down = false;
}
if (key == 'a') {
left = false;
}
if (key == 'd') {
right = false;
}
}
//THIS IS MOUSE INTERACTION
void mouseReleased() {
if (mode == 1) {
time = 0;
image(bgintro2, 0, 0);
mode = 2;
} else if (mode == 2){
}
} else if (mode == 3){
} else if (mode == 4){
} else if (mode == 5){
setup();
mode = 1;
} else {
println("mode is broken" + mode);
}
}
//THIS IS INTROMODE
PImage bgintro;
PImage bgintro2;
void intro() {
imageMode(CORNER);
bgintro = loadImage ("button_off.jpg");
bgintro2 = loadImage ("button_on.jpg");
image(bgintro, 0, 0);
live = 5;
score = 0;
//mouse hover function
if (mouseX > rectX && mouseX < rectX + rectWidth && mouseY > rectY && mouseY < rectY + rectHeight) {
noFill();
noStroke();
image(bgintro2, 0, 0);
}
else {
noFill();
noStroke();
image(bgintro, 0, 0);
}
rect(rectX, rectY, rectWidth, rectHeight);
}
//THIS IS GAMEOVER
PImage gameover;
void gameOver() {
imageMode(CORNER);
gameover = loadImage ("gameover.jpg");
image(gameover, 0, 0);
}