We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello guys,
I'm fairly new in Java/Processing, just started last october cause I started studying. Our last homework for this course, is to programm a small game or something in that direction.
I'm making a little Asteroid-Game, where you have to avoid the asteroids coming towards you (the mousecursor is you spaceship).
However I encountered some problems with my programm.
I want to make a scoreboard that gives u like 10 point for every second you survive. Sadly I just can't find a way to make it work
I don't have any clue how I can make the asteroids colide with the spaceship
Here some more details:
Code:
Main Programm:
PImage background_img;
PImage spaceship_img;
PImage meteor_img;
PImage meteor_big_img;
void setup() {
size (200, 800);
noCursor();
// ######## BILDMATERIAL ########
background_img = loadImage("background.jpg");
spaceship_img = loadImage("spaceship.png");
meteor_img = loadImage("meteor.png");
meteor_big_img = loadImage("meteor_big.png");
loadPixels();
// ######## METEORIT ZEICHNEN ########
comet.draw_meteor();
comet_big.draw_meteor_big();
}
// ######## VOID DRAW START ########
void draw() {
frameRate(70);
updatePixels();
// ######## BILDMATERIAL ########
image (background_img, 0, 0);
image (spaceship_img, mouseX-39, mouseY-40);
// ######## METEORIT BEWEGUNG ########
comet.move_meteor();
comet_big.move_meteor_big();
// ######## SCORE-BOARD ########
fill(0, 0, 0, 150);
stroke (#ffffff);
rect (5, 750, 190, 45);
fill(#ffffff);
text("SCORE:", 15, 778);
}
Classes:
class Meteor {
int meteorCount = 13;
float [] x = new float [meteorCount];
float [] y = new float [meteorCount];
// ######## METEORITEN ZEICHNEN ########
void draw_meteor () {
for (int i=0; i<meteorCount; i++) {
x[i] = int (random(0, 200));
y[i] = int (random(-800, 0));
image (meteor_img, x[i], y[i]);
}
}
// ######## METEORITEN BEWEGEN #########
void move_meteor () {
for (int i = 0; i<meteorCount; i++) {
x[i] += random(0.4, 0.4);
y[i] += random(1.5, 2.0);
image (meteor_img, x[i], y[i]);
if (y[i] >= 800) {
y[i] = -10;
}
if (x[i] >= 200) {
x[i] = -20;
}
}
}
}
I'm looking forward to hearing from you soon! =)
Thank you guys
Answers
for the score:
use a timer:
pls see
http://wiki.processing.org/w/How_do_I_display_a_message_for_a_few_seconds?
for collision
check pos of spaceship and each meteor in a for-loop
use intersection of rectangles
Thank you for your hints. I actually found something I could use for the collision, it's almost done now!! =)
The timer thing is a bit more difficult though
ok, I have another question about the collision part:
At the moment, when the spaceship colides with one of the asteroids I made this pop up:
with this code:
Now the problem is, the rocks keep moving so the game over pop up will disappear again. Is there any why I can end the for-loop which is responsible for the asteroids movement, so the pop up will stay there till you start a new round? (Code for the moving asteroids below)
pobably states of program is the easiest
you have the states splashScreenAtStartUp, play and GameOver e.g.
When the spaceship collides with one of the asteroids set state to GameOver
in draw() use switch (state) { to determine what to do:
play and move meteors
show GameOver text and don't move meteors
you can even name the states
And use this in switch()
Thank you for this answer!
I tried a little bit different approch I guess:
I put an** int gameState = 1;** above void setup(){} and in void draw(){ i put this code:
sadly it doesn't work =( But I think it's because I try to change the varible int gameState through different classes (like Asteorid, Start, GameOver...)
This is how I'm trying it in the Class Asteorid (basicly when Spaceship hits asteorid, then it should change int gameState to 2..):
I don't get any error when I run the programm, there is just nothing happening if an asteorid colides with the spaceship
to use if instead of switch is perfectly fine.
I suggest to write after line 22
anyway. Vital is where you change gameState and how. But I think it is ok to change it in different places.
your error:
with int: you introduce a new local var with the same name
try it without int
instead. Then you really change the global var gameState as you wanted to.
You can still give the different states names instead of 1,2,3 :
(final means constant, because the names don't change, only the state does)
;-)
Ahhhh now it works! Thank you soooo much! =)
Glad to hear that!
Alright one last question here. For my homework to be perfect, I need to implement a Button using java.awt library (?) to start my game. Sadly I can't find any good tutorial on how I can achieve this =(
do you have to use awt ?
yea, I have to
ok
I solved the problem, everthing works fine now! Thanks for your support!! =)
wow!
I just posted the awt button as a new question
;-)