Results 1 to 7 of 7

Thread: first game...

  1. #1
    lricci is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default first game...

    Hello. My name is Lance and have a problem. I just started the book, Beginning Java Game Programming and had a question. The first part of the book lays down a whole game of asteroids than goes step by step through the code to explain and improve on it. It seems like a really good book but it doesnt explain one thing thoroughly enough. I understand the classes are separate files but what about the other methods? Please look at the code and tell me what needs to be in separate files or what. When I try to compile I get the error class, interface or enum expected and its pointing to line 73 public void init(). Please help.

    Java Code:
    import java.awt.Shape;
    import java.awt.Polygon;
    import java.awt.Rectangle;
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.util.*;
    
    /************************************
    *Base vector shape class for polygonal shapes
    ************************************/
    
    public class BaseVectorShape {
        //variables
        private Shape shape;
        private boolean alive;
        private double x,y;
        private double velX,velY;
        private double moveAngle,faceAngle;
    
        //accessor methods
        public Shape getShape() {return shape;}
        public boolean isAlive() {return alive;}
        public double getX() {return x;}
        public double getY() {return y;}
        public double getVelX() {return velX;}
        public double getVelY() {return velY;}
        public double getMoveAngle() {return moveAngle;}
        public double getFaceAngle() {return faceAngle;}
    
        //mutator and helper methods
        public void setShape(Shape shape) {this.shape = shape;}
        public void setAlive(boolean alive) {this.alive = alive;}
        public void setX(double x) {this.x = x;}
        public void incX(double i) {this.x += i;}
        public void setY(double y) {this.y = y;}
        public void incY(double i) {this.y += i;}
        public void setVelX(double velX) {this.velX = velX;}
        public void incVelX(double i) {this.velX += i;}
        public void setVelY(double velY) {this.velY = velY;}
        public void incVelY(double i) {this.velY += i;}
        public void setFaceAngle (double angle) {this.faceangle = angle;}
        public void incFaceAngle(double i) {this.faceAngle += i;}
        public void setMoveAngle(double angle) {this.moveAngle = angle;}
        public void incMoveAngle(double i) {this.moveAngle += i;}
    
        //default constructor
        BaseVectorShape() {
            setShape(null);
            setAlive(false);
            setX(0.0);
            setY(0.0);
            setVelX(0.0);
            setVelY(0.0);
            setMoveAngle(0.0);
            setFaceAngle(0.0);
         }
    }
    
    
    
    
    /******************
    applet init event
    ******************/
    
    public void init() {
       //create the back buffer for smooth graphics
       backbuffer = new BufferedImage(640,480, BufferedImage.TYPE_INT_RGB);
       g2d = backbuffer.createGraphics();
    
       //set up the ship
       ship.setX(320);
       ship.setY(240);
    
       //set up the bullets
       for (int n = 0; n<BULLETS; n++) {
           bullet[n] = new Bullet();
        }
    
        //create the asteroids
        for (int n = 0; n<ASTEROIDS; n++) {
            ast[n] = new Asteroid();
            ast[n].setRotationVelocity(rand.nextInt(3)+1);
            ast[n].setX((double)rand.nextInt(600)+20);
            ast[n].setY((double)rand.nextInt(440)+20);
            ast[n].setMoveAngle(rand.nextInt(360));
            double ang = ast[n].getMoveAngle() - 90;
            ast[n].setVelX(calcAngleMoveX(ang));
            ast[n].setVelY(calcAngleMoveY(ang));
         }
    
         //start the user input listener
         addKeyListener(this);
      }
    
    public void update(Graphics g) {
       //start off transforms as identity
       g2d.setTransform(identity);
    
       //erase the background
       g2d.setPaint(Color.BLACK);
       g2d.fillRect(0,0,getSize().width,getSize().height);
    
       //print some status information
       g2d.setColor(Color.WHITE);
       g2d.drawString("Ship: "+ Math.round(ship.getX())+","+
           Math.round(ship.getY()),5,10);
       g2d.drawString("Move angle: "+ Math.round(
           ship.getMoveAngle())+90,5,25);
       g2d.drawString("Face angle: "+ Math.round(
           ship.getFaceAngle()),5,40);
    
       //draw the game graphics
       drawShip();
       drawBullets();
       drawAsteroids();
    
       //repaint the applet window
       paint(g);
    }
    
    public void drawShip() {
      g2d.setTransform(identity);
      g2d.translate(ship.getX(), ship.getY());
      g2d.rotate(Math.toRadians(ship.getFaceAngle()));
      g2d.setColor(Color.ORANGE);
      g2d.fill(ship.getShape());
    }
    
    public void drawBullets() {
    
        //iterate through the array of bullets
        for (int n = 0; n < BULLETS; n++) {
    
            //is this bullet currently in use?
            if (bullet[n].isAlive()) {
                //draw the bullet
                g2d.setTransform(identity);
                g2d.translate(bullet[n].getX(), bullet[n].getY());
                g2d.setColor(Color.MAGENTA);
                g2d.draw(bullet[n].getShape());
              }
         }
     }
    
    public void drawAsteroids() {
    
        //iterate through the asteroids array
        for (int n = 0; n < ASTEROIDS; n++) {
    
             //is this asteroid being used?
             if (ast[n].isAlive()) {
    
                 //draw the asteroid g2d.setTransform(identity);
                 g2d.translate(ast[n].getX(), ast[n].getY());
                 g2d.rotate(Math.toRadians(ast[n].getMoveAngle()));
                 g2d.setColor(Color.DARK_GRAY);
                 g2d.fill(ast[n].getShape());
              }
         }
      }
    
    public void paint(Graphics g) {
    
        //draw the back buffer onto the applet window
        g.drawImage(backbuffer, 0, 0, this);
    }
    
    public void start() {
       //create the gameloop thread for real-time updates
       gameloop = new Thread(this);
       gameloop.start();
    }
    
    /*****************************************************
    thread run event (game loop)
    *****************************************************/
    
    public void run() {
        //acquire the current thread
        Thread t = Thread.currentThread();
    
        //keep going as long as the thread is alive
        while (t == gameloop) {
           try {
                //update the game loop
                gameUpdate();
    
                //target framerate is 50 fps
                Thread.sleep(20);
               } catch(InterruptedException e) {
                  e.printStackTrace();
               }
               repaint();
             }
          }
          /*****************************************************
          thread stop event
          *****************************************************/
    
          public void stop() {
             //kill the gameloop thread
             gameloop = null;
          }
    
    /*****************************************************
    move and animate the objects in the game
    *****************************************************/
    
    private void gameUpdate() {
        updateShip();
        updateBullets();
        updateAsteroids();
        checkCollisions();
    }
    
    /*****************************************************
    Update the ship position based on velocity
    *****************************************************/
    
    public void updateShip() {
       //update shipís X position
       ship.incX(ship.getVelX());
    
       //wrap around left/right
       if (ship.getX() < -10)
           ship.setX(getSize().width + 10);
       else if (ship.getX() > getSize().width + 10)
            ship.setX(-10);
    
       //update shipís Y position
       ship.incY(ship.getVelY());
    
       //wrap around top/bottom
       if (ship.getY() < -10)
           ship.setY(getSize().height + 10);
       else if (ship.getY() > getSize().height + 10)
           ship.setY(ņ10);
    
     }
    
     /*****************************************************
     Update the bullets based on velocity
     *****************************************************/
    
     public void updateBullets() {
         //move each of the bullets
         for (int n = 0; n < BULLETS; n++) {
    
             //is this bullet being used?
             if (bullet[n].isAlive()) {
    
                 //update bulletís x position
                 bullet[n].incX(bullet[n].getVelX());
    
                 //bullet disappears at left/right edge
                 if (bullet[n].getX() < 0 ||
                     bullet[n].getX() > getSize().width)
                 {
                     bullet[n].setAlive(false);
                 }
    
                 //update bulletís y position
                 bullet[n].incY(bullet[n].getVelY());
    
                 //bullet disappears at top/bottom edge
                 if (bullet[n].getY() < 0 ||
                     bullet[n].getY() > getSize().height)
                 {
                     bullet[n].setAlive(false);
                 }
             }
         }
     }
    
    
     /*****************************************************
     Update the asteroids based on velocity
     *****************************************************/
    
     public void updateAsteroids() {
        //move and rotate the asteroids
        for (int n = 0; n < ASTEROIDS; n++) {
    
            //is this asteroid being used?
            if (ast[n].isAlive()) {
    
                //update the asteroidís X value
                ast[n].incX(ast[n].getVelX());
    
                //warp the asteroid at screen edges
                if (ast[n].getX() < -20)
                    ast[n].setX(getSize().width + 20);
                else if (ast[n].getX() > getSize().width + 20)
                    ast[n].setX(-20);
    
                //update the asteroidís Y value
                ast[n].incY(ast[n].getVelY());
    
                //warp the asteroid at screen edges
                if (ast[n].getY() < -20)
                    ast[n].setY(getSize().height + 20);
                else if (ast[n].getY() > getSize().height + 20)
                    ast[n].setY(-20);
                //update the asteroidís rotation
                ast[n].incMoveAngle(ast[n].getRotationVelocity());
    
                //keep the angle within 0-359 degrees
                if (ast[n].getMoveAngle() < 0)
                    ast[n].setMoveAngle(360 - ast[n].getRotationVelocity());
                else if (ast[n].getMoveAngle() > 360)
                    ast[n].setMoveAngle(ast[n].getRotationVelocity());
    
              }
          }
     }
    
    /*****************************************************
    Test asteroids for collisions with ship or bullets
    *****************************************************/
    
    public void checkCollisions() {
    
       //iterate through the asteroids array
       for (int m = 0; m<ASTEROIDS; m++) {
    
           //is this asteroid being used?
           if (ast[m].isAlive()) {
    
               /* * check for collision with bullet */
               for (int n = 0; n < BULLETS; n++) {
    
                   //is this bullet being used?
                   if (bullet[n].isAlive()) {
    
                        //perform the collision test
                        if (ast[m].getBounds().contains(
                        bullet[n].getX(), bullet[n].getY()))
                      {
                        bullet[n].setAlive(false);
                        ast[m].setAlive(false);
                        continue;
                      }
                   }
               }
    
               /* * check for collision with ship */
               if (ast[m].getBounds().intersects(ship.getBounds())) {
                   ast[m].setAlive(false);
                   ship.setX(320);
                   ship.setY(240);
                   ship.setFaceAngle(0);
                   ship.setVelX(0);
                   ship.setVelY(0);
                   continue;
                }
             }
          }
     }
    
    /*****************************************************
    key listener events
    *****************************************************/
    
    public void keyReleased(KeyEvent k) { }
    public void keyTyped(KeyEvent k) { }
    public void keyPressed(KeyEvent k) {
       int keyCode = k.getKeyCode();
    
       switch (keyCode) {
    
       case KeyEvent.VK_LEFT:
            //left arrow rotates ship left 5 degrees
            ship.incFaceAngle(-5);
    
            if (ship.getFaceAngle() < 0) ship.setFaceAngle(360-5);
            break;
    
       case KeyEvent.VK_RIGHT:
            //right arrow rotates ship right 5 degrees
            ship.incFaceAngle(5);
    
            if (ship.getFaceAngle() > 360) ship.setFaceAngle(5);
            break;
    
       case KeyEvent.VK_UP:
            //up arrow adds thrust to ship (1/10 normal speed)
            ship.setMoveAngle(ship.getFaceAngle() - 90);
            ship.incVelX(calcAngleMoveX(ship.getMoveAngle()) * 0.1);
            ship.incVelY(calcAngleMoveY(ship.getMoveAngle()) * 0.1);
            break;
    
        //Ctrl, Enter, or Space can be used to fire weapon
        case KeyEvent.VK_CONTROL:
        case KeyEvent.VK_ENTER:
        case KeyEvent.VK_SPACE:
             //fire a bullet
             currentBullet++;
             if (currentBullet > BULLETS - 1) currentBullet = 0;
             bullet[currentBullet].setAlive(true);
    
             //point bullet in same direction ship is facing
             bullet[currentBullet].setX(ship.getX());
             bullet[currentBullet].setY(ship.getY());
             bullet[currentBullet].setMoveAngle(ship.getFaceAngle() - 90);
    
             //fire bullet at angle of the ship
             double angle = bullet[currentBullet].getMoveAngle();
             double svx = ship.getVelX();
             double svy = ship.getVelY();
             bullet[currentBullet].setVelX(svx + calcAngleMoveX(angle) * 2);
             bullet[currentBullet].setVelY(svy + calcAngleMoveY(angle) * 2);
             break;
          }
       }
    
    /*****************************************************
    calculate X movement value based on direction angle
    *****************************************************/
    
    public double calcAngleMoveX(double angle) {
       return (double) (Math.cos(angle * Math.PI / 180));
    }
    /*****************************************************
    calculate Y movement value based on direction angle }
    *****************************************************/
    
    public double calcAngleMoveY(double angle) {
       return (double) (Math.sin(angle * Math.PI / 180));
    }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by lricci View Post
    Please look at the code and tell me what needs to be in separate files or what. When I try to compile I get the error class, interface or enum expected and its pointing to line 73 public void init(). Please help.
    Whenever I get an error like that, the first place I look is the code line above the line with the error (ignoring white space). So let's do that with your code:

    Java Code:
        BaseVectorShape() {
            setShape(null);
            setAlive(false);
            setX(0.0);
            setY(0.0);
            setVelX(0.0);
            setVelY(0.0);
            setMoveAngle(0.0);
            setFaceAngle(0.0);
         }
    }  // ****** here ******
    
    
    public void init() {
    I commented the line in question, and indeed there's a problem -- you have an extra closing curly brace, one that pairs up with the first open curly brace of the class and tells the compiler that your class ends here. Well the class shouldn't end there, and you need to delete this brace. If you look at the book code, I'll bet that it doesn't have this closing brace.

    Luck!
    Last edited by Fubarable; 12-15-2010 at 11:27 PM.

  3. #3
    lricci is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Thank you for replying. That brace is in the book as it is closing the BaseVectorShape class. The first brace is closing the constructor just before it. I am not here for possible syntax errors. I just want to know if this needs to be in multiple files or not. Again, I figured out that the individual classes are separated but what about all the methods (i.e., public void)? If what I have now is supposed to be in one file like it is than fine, I will pick at syntax then, but first I need clarification.

  4. #4
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    All methods belong in classes.

    This is fundamental knowledge in java programming.

    Your beginner's book must state this somewhere.

    We can't tell you what classes get what methods
    because we do not have that book.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Quote Originally Posted by lricci View Post
    Thank you for replying. That brace is in the book as it is closing the BaseVectorShape class. The first brace is closing the constructor just before it. I am not here for possible syntax errors. I just want to know if this needs to be in multiple files or not. Again, I figured out that the individual classes are separated but what about all the methods (i.e., public void)? If what I have now is supposed to be in one file like it is than fine, I will pick at syntax then, but first I need clarification.
    You have your priorities completely wrong.

    db

  6. #6
    Controloler is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I commented the line in question, and indeed there's a problem -- you have an extra closing curly brace, one that pairs up with the first open curly brace of the class and tells the compiler that your class ends here. Well the class shouldn't end there, and you need to delete this brace. If you look at the book code, I'll bet that it doesn't have this closing brace.

    Luck!
    He is indeed correct. All of this goes in one file, because it is a single class (as far as I can tell), but with that extra brace, you are ending the class before it includes any of the methods. Methods go inside of classes, but with that extra brace, you are making a class, with all of the methods outside of it, not gonna work.

    Delete that brace, then save the whole thing as one file and compile. Your syntax IS your problem, therefore you should be worried about it. Hope this helps.

  7. #7
    lricci is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Again, thanks for the replies, I was able to discover the solution myself and by the way, that brace is correct, there were no errors in my overall syntax.

Similar Threads

  1. Hi Lo Game
    By Bgreen7887 in forum New To Java
    Replies: 5
    Last Post: 10-22-2010, 03:08 AM
  2. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  3. 2D Game
    By Elffus in forum Java 2D
    Replies: 58
    Last Post: 08-05-2010, 01:47 AM
  4. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •