[Newb] Having problems with different things (Asteroid-Game)

edited February 2014 in Questions about Code

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.

  1. 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

  2. I don't have any clue how I can make the asteroids colide with the spaceship

Here some more details:

Unbenannt-2

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

  • edited February 2014

    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

  • edited February 2014

    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:

    3

    with this code:

          if (dist(x[i], y[i], mouseX, mouseY)<=46) {
            float movement_x = 0;
            float movement_y = 0;
            fill(0, 0, 0, 150);
            stroke (#ffffff);
            rect(5, 5, 190, 740);
            fill(#ffffff);
            textSize(26);
            text("GAME OVER", 25, 400);
          }
    

    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)

      void move_meteor_big () {
        for (int i = 0; i<meteorCount; i++) {
          x[i] += random(0.1, 0.1);
          y[i] += random(0.5, 0.6); 
    
          image (meteor_big_img, x[i], y[i]);
    
          if (y[i] >= 800) {
            y[i] = random(-1200,-10);
          }
          if (x[i] >= 200) {
            x[i] = -40;
          }
    
  • edited February 2014

    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

    final int splashScreenAtStartUp = 0;
    final int play = 1;
    final int GameOver = 2;
    
    int state = splashScreenAtStartUp;
    

    And use this in switch()

    switch (state) { 
    case splashScreenAtStartUp:
      //text( any key to start.....
      break;
    case play:
      play();
      break;
    case GameOver;
      // show meteors and text, don't move meteors 
      break; 
    } // 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:

      if (gameState == 1) {
        startscreen.draw_StartScreen();
      }
    
      else if(gameState == 2) {
        noCursor();
    
        comet.move_meteor();
        comet_big.move_meteor_big();
    
        fill(0, 0, 0, 150);
        stroke (#ffffff);
        rect (5, 750, 190, 45);
        fill(#ffffff);
        textSize(10);
        text("SCORE:", 15, 778);
        text(score, 50, 778);
      }
    
      else if (gameState == 3) {
        lostGame.draw_GameOver();
      }
    

    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..):

          if (dist(x[i], y[i], mouseX, mouseY)<=40) {
            int gameState = 2;
    

    I don't get any error when I run the programm, there is just nothing happening if an asteorid colides with the spaceship

  • edited February 2014

    to use if instead of switch is perfectly fine.

    I suggest to write after line 22

    else {
        // error - e.g. gameState is 4 
        println ("unknown value for gameState. Bad. ");
    }
    

    anyway. Vital is where you change gameState and how. But I think it is ok to change it in different places.

    your error:

    if (dist(x[i], y[i], mouseX, mouseY)<=40) {
      int gameState = 2;
    

    with int: you introduce a new local var with the same name

    try it without int

    if (dist(x[i], y[i], mouseX, mouseY)<=40) {
      gameState = 2;
    

    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 int splashScreenAtStartUp = 1;
        final int play = 2;
        final int GameOver = 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

  • I solved the problem, everthing works fine now! Thanks for your support!! =)

  • wow!

    I just posted the awt button as a new question

    ;-)

Sign In or Register to comment.