game starts nothing moves, lots of errors!!!

Hi everyone, first of all I’m going to present myself, I’m Andres Scarpone and i live in Venezuela currently trying to learn Video Game programing, and with lots of questions and problems in the tutorial I’m doing…

Well for all of you the tutorial I’m doing and where I have learned almost everything is from the book: Beginning JAVA SE 6 Game programming, I’m almost at the end of the book with the last project but instead of making it in a applet i tried to build it in the JFrame… and got lots of problems, I’ll post the code, if someone can help me out I would really appreciate it thanks.

package guerragalactica;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;

public class GuerraGalactica extends JComponent implements Runnable, KeyListener
{
    
    //Variables
    static int ancho = 800;
    static int alto = 600;
    static int centroX = ancho / 2;
    static int centroY = alto / 2;
    static int asteroidesMax = 10;
    static int balasMax = 10;
    static int velBalas = 4;
    static double aceleracion = 0.05;
    static int spriteNorm = 0;
    static int spriteChoca = 1;
    Thread loopJuego;
    BufferedImage buffer;
    Graphics2D g2d;
    boolean muestraLimites = true;
    boolean pruebaColicion = true;
    ImagenesEntidad fondo;
    Sprite nave;
    Sprite[] ast = new Sprite[asteroidesMax];
    Sprite[] balas = new Sprite[balasMax];
    int balasAct = 0;
    Random azar = new Random();
    SonidoClass shoot;
    SonidoClass explocion;
    SonidoClass acelera;
    boolean teclaAbajo, teclaArriba, teclaDer, teclaIzq, teclaDisparo;
    int cuentaMarco = 0, velMarco = 0;
    long tiempoInicio = System.currentTimeMillis();
    //Fin

    public static void main(String[] args) 
    {
        new GuerraGalactica();
    }
    
    public GuerraGalactica()
    {
        super("Guerra Galactica");
        
        buffer = new BufferedImage(ancho, alto, BufferedImage.TYPE_INT_RGB);
        g2d = buffer.createGraphics();
        
        fondo = new ImagenesEntidad(this);
        fondo.carga("\\media\\bluespace.png");
                
        nave = new Sprite(this, g2d);
        nave.cargarImg("\\media\\ship.png");
        nave.setPosicion(new Point2D(centroX, centroY));
        nave.setVivo(true);
        explocion = new SonidoClass("\\media\\explode.wav");
        shoot = new SonidoClass("\\media\\shoot.wav");
        acelera = new SonidoClass("\\media\\thrust.wav");
        
        for (int i = 0; i < balasMax; i++)
        {
            balas[i] = new Sprite(this, g2d);
            balas[i].cargarImg("\\media\\shoot.png");
        }
        
        for (int i = 0; i < asteroidesMax; i++)
        {
            ast[i] = new Sprite(this, g2d);
            ast[i].setVivo(true);
            ast[i].cargarImg("\\media\\asteroid"+(azar.nextInt(5)+1)+".png");
            ast[i].setPosicion(new Point2D(azar.nextInt(ancho), azar.nextInt(alto)));
            ast[i].setAnguloMov(azar.nextInt(360));
            ast[i].setAnguloVista(azar.nextInt(360));
            ast[i].setFacRot(azar.nextDouble());
            double ang = ast[i].anguloMov() - 90;
            ast[i].setVelocidad(new Point2D(calcMovAnguloX(ang), calcMovAnguloY(ang)));   
        }
        addKeyListener(this);
        
        loopJuego = new Thread(this);
        loopJuego.start();
        
        setSize(ancho, alto);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    
    public void paint(Graphics g)
    {
        g2d.drawImage(fondo.getImage(), 0, 0, ancho - 1, alto - 1, this);
        
        dibujaNave();
        dibujaAst();
        dibujaBalas();
        
        System.out.println("FPS: " + velMarco);
        System.out.println("Nave: " + Math.round(nave.pos.getX()) + ", " + Math.round(nave.pos.getY()));
        System.out.println("Angulo: " + Math.round(nave.anguloMov() + 90));
        System.out.println("Vista: " + Math.round(nave.anguloVista()));
        
        if (muestraLimites)
        {
            g2d.setColor(Color.GREEN);
            g2d.drawString("Cajas Limitantes", ancho - 150, 10);
        }
        
        if (pruebaColicion)
        {
            g2d.setColor(Color.GREEN);
            g2d.drawString("Prueba de Coliciones", ancho - 150, 10);
        }
                
        g.drawImage(buffer, 0, 0, this);
    }
    
    public void dibujaNave()
    {
        nave.transform();
        nave.draw();
        if (muestraLimites)
        {
            if (nave.estado() == spriteChoca)
                nave.drawLimite(Color.RED);
            else
                nave.drawLimite(Color.BLUE);
        }
    }
    
    public void dibujaAst()
    {
        for (int i = 0; i < asteroidesMax; i++)
        {
           if(ast[i].vivo())
           {
                ast[i].transform();
                ast[i].draw();
                if (muestraLimites)
                {
                    if (ast[i].estado() == spriteChoca)
                        ast[i].drawLimite(Color.RED);
                    else
                        ast[i].drawLimite(Color.BLUE);
                }
           }
        }
    }
    
    public void dibujaBalas()
    {
        for (int i = 0; i < balasMax; i++)
        {
           if(balas[i].vivo())
           {
                balas[i].transform();
                balas[i].draw();
                if (muestraLimites)
                {
                    if (balas[i].estado() == spriteChoca)
                        balas[i].drawLimite(Color.RED);
                    else
                        balas[i].drawLimite(Color.BLUE);
                }
           }
        }
    }
    
    public void run()
    {
        Thread t = Thread.currentThread();
        
        while(t == loopJuego)
        {
            try 
            {
                gameUpdate();
                Thread.sleep(20);
            } 
            catch (InterruptedException e) { e.printStackTrace(); }
            
            cuentaMarco++;
            if (System.currentTimeMillis() > tiempoInicio + 1000)
            {
                tiempoInicio = System.currentTimeMillis();
                velMarco = cuentaMarco;
                cuentaMarco = 0;
            }
            repaint();
        }
    }
    
    public void gameUpdate()
    {
        checkEntrada();
        actNave();
        actAst();
        actBalas();
        if (pruebaColicion) checkColicion();
    }
    
    public void actNave()
    {
        nave.posicion();
        double newX = nave.posicion().getX();
        double newY = nave.posicion().getY();
        
        if (nave.posicion().getX() < -10)
            newX = ancho + 10;
        else if (nave.posicion().getX() > ancho + 10)
            newX = -10;
        
        if (nave.posicion().getY() < -10)
            newY = alto + 10;
        else if (nave.posicion().getY() > alto + 10)
            newY = -10;
        
        nave.setPosicion(new Point2D(newX, newY));
        nave.setEstado(spriteNorm);
    }
            
    public void actAst()
    {
        for (int i = 0; i < asteroidesMax; i++)
        {
            if (ast[i].vivo())
            {
                ast[i].posicion();
                ast[i].actRot();
                int w = ast[i].imgAncho() - 1;
                int h = ast[i].imgAlto() - 1;
                double newX = ast[i].posicion().getX();
                double newY = ast[i].posicion().getY();
                
                if (ast[i].posicion().getX() < -w)
                    newX = ancho + w;
                else if (ast[i].posicion().getX() > ancho + w)
                    newX = -w;
                
                if (ast[i].posicion().getY() < -h)
                    newY = alto + h;
                else if (ast[i].posicion().getX() > alto + h)
                    newY = -h;
                
                ast[i].setPosicion(new Point2D(newX, newY));
                ast[i].setEstado(spriteNorm);
            }
        }
    }
    
    public void actBalas()
    {
        for (int i = 0; i < balasMax; i++)
        {
            if (balas[i].vivo())
            {
                balas[i].posicion();

                if (balas[i].posicion().getX() < 0 || balas[i].posicion().getX() > ancho)
                    balas[i].setVivo(false);

                if (balas[i].posicion().getY() < 0 || balas[i].posicion().getX() > alto)
                    balas[i].setVivo(false);

                balas[i].setEstado(spriteNorm);
            }
        }
    }
    
    public void checkColicion()
    {
        for (int i = 0; i < asteroidesMax; i++)
        {
            if (ast[i].vivo())
            {
                for (int n = 0; n < balasMax; n++)
                {
                    if (balas[n].vivo())
                    {
                        if (ast[i].chocaCon(balas[n]))
                        {
                            balas[n].setEstado(spriteChoca);
                            ast[i].setEstado(spriteChoca);
                            explocion.start();
                        }
                    }
                }
            }
        }
        
        for (int i = 0; i < asteroidesMax; i++)
        {
            if (ast[i].vivo())
            {
                if (nave.chocaCon(ast[i]))
                {
                    ast[i].setEstado(spriteChoca);
                    nave.setEstado(spriteChoca);
                    explocion.start();
                }
            }
        }
        
    }
    
    public void checkEntrada()
    {
        if (teclaIzq)
        {
            nave.setAnguloVista(nave.anguloVista() - 5);
            if (nave.anguloVista() < 0) nave.setAnguloVista(360-5);
        }
        else if (teclaDer)
        {
            nave.setAnguloVista(nave.anguloVista() + 5);
            if (nave.anguloVista() > 360) nave.setAnguloVista(5);
        }
        else if (teclaArriba)
        {
            aplicaImpulso();
        }
    }
    
    public void keyTyped(KeyEvent k) {}
    public void keyPressed(KeyEvent k)
    {
        switch (k.getKeyCode())
        {
            case KeyEvent.VK_LEFT:
                teclaIzq = true;
                break;
            case KeyEvent.VK_RIGHT:
                teclaDer = true;
                break;
            case KeyEvent.VK_UP:
                teclaArriba = true;
                break;
            case KeyEvent.VK_CONTROL:
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_SPACE:
                teclaDisparo = true;
                break;
            case KeyEvent.VK_B:
                muestraLimites = !muestraLimites;
                break;
            case KeyEvent.VK_C:
                pruebaColicion = ! pruebaColicion;
                break;
        }
    }
    
    public void keyReleased(KeyEvent k)
    {
        switch (k.getKeyCode())
        {
            case KeyEvent.VK_LEFT:
                teclaIzq = false;
                break;
            case KeyEvent.VK_RIGHT:
                teclaDer = false;
                break;
            case KeyEvent.VK_UP:
                teclaArriba = false;
                acelera.stop();
                break;
            case KeyEvent.VK_CONTROL:
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_SPACE:
                teclaDisparo = false;
                dispara();
                break;
        }
    }
    
    public void aplicaImpulso()
    {
        nave.setAnguloMov(nave.anguloVista() - 90);
        
        double velX = nave.velocidad().getX();
        velX += calcMovAnguloX(nave.anguloMov()*aceleracion);
        double velY = nave.velocidad().getY();
        velY += calcMovAnguloY(nave.anguloMov()*aceleracion);
        
        nave.setVelocidad(new Point2D(velX, velY));
        
        acelera.setLoop(true);
        acelera.start();
    }
    
    public void dispara()
    {
        balasAct++;
        if (balasAct > balasMax - 1) balasAct = 0;
        if (!balas[balasAct].vivo())
        {
            balas[balasAct].setVivo(true);
        
            int w = balas[balasAct].imgAncho();
            int h = balas[balasAct].imgAlto();

            double x = nave.centro().getX() - w / 2;
            double y = nave.centro().getY() - h / 2;
            balas[balasAct].setPosicion(new Point2D(x, y));

            balas[balasAct].setAnguloVista(nave.anguloVista());
            balas[balasAct].setAnguloMov(nave.anguloVista() - 90);

            double ang = balas[balasAct].anguloMov();
            double svX = calcMovAnguloX(ang) * velBalas;
            double svY = calcMovAnguloY(ang) * velBalas;
            balas[balasAct].setVelocidad(new Point2D(svX, svY));
            shoot.start();            
        }        
    }
        
    public double calcMovAnguloX(double angulo)
    {
        return Math.cos(angulo * Math.PI / 180);
    }
    
    public double calcMovAnguloY(double angulo)
    {
        return Math.sin(angulo * Math.PI / 180);
    }
    
}
    
    

Update: I changed the code to reflect the new changes and took out the image since that issue has been resolved.

According to the (very tiny) image, it looks like lines 96 and 124 are throwing NullPointerExceptions. This means both variables “fondo” and “nave” are null. The top exception means “fondo” was null at first, but then you made assigned it to an object which caused then caused the bottom two exceptions to say “nave” is null.

EDIT: Aha! You call setVisible(true) before you initialize anything. This means “fondo” and “nave” are both null while paint(Graphics) gets called for the first time. Try moving that line to the end of the constructor, right before you start the new Thread.

Thanks for the quick reply the errors are all gone with that just did what you told me… but the game is still frozen and nothing moves and the console is writing like if it where on fire i mean it’s like it writes 100 lines/sec telling me the data i wanted but the game is still frozen. but thanks now i Know how to avoid that error.

I didn’t look at all your code but your setup and listeners look fine.

Couple things to add:

  • Extend and draw on a JComponent instead of JFrame because you will have automatic double buffering by overriding paintComponent(Graphics) and 0,0 won’t be hidden behind the decoration (the top bar with minimize, maximize, and close buttons).
  • Don’t import java.lang.System. java.lang.* is automatically imported in every single class file…forever :stuck_out_tongue:

Ok I’ll try the changes, but how exactly do I draw on the JComponent I have never used it before. Could you please show me an example or tell me where can I find one?

This is a pretty good example using a JPanel:

http://zetcode.com/tutorials/javagamestutorial/basics/

Basically, you just do most of the coding in the JPanel, then all the JFrame does is put it on the screen.

It is best (and makes more sense) to extend a javax.swing.JComponent. All you do is create a class that extends it, override paintComponent(Graphics) and do the rendering there (don’t do your own double buffering, Swing is automatically double buffered). Also, please don’t extend JFrame, it is not meant to be extended, but to be created. You create a new instance of JFrame, set the approriate properties, add(new MyComponent()), set it to visible, and create the thread.

Don’t take a too big leap when making something new. Do small changes and check so everything is working between them.

Ok so instead of JFrame I extended to JComponent and now the lines containing this codes gave me an error, I’m guessing this part of the code only is to be used with JFrame so how exactly do I go changing this about, the override to paint still works perfectly…


super("Guerra Galactica");
fondo = new ImagenesEntidad(this);
nave = new Sprite(this, g2d);
balas[i] = new Sprite(this, g2d);
ast[i] = new Sprite(this, g2d);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

And since I moved to the JComponent class should I Still use the buffer, or should I completely eliminate it???

Well it seems I can’t really make the change to JComponent without making the other classes blowup, since they are dependent on the JFrame, I’ll try testing the JComponent in the next tutorial I take, I have some questions that maybe you guys can help me out and help me understand.

1- What is better Jpanle or Jcomponent??
2- How does one go around and implement a framework like openGL?? or more precisely what are the benefits of using any type of framework???
3- Is openGL a good framework for 2D games?

Ohh and by the way with the problem that nothing moves, the sound works fine and the bullets get shoot and they sound when they do it’s just that the sprites aren’t being moved around I think it might be on the update method the problem…

OpenGL is not a framework. OpenGL is about as low level as DirectX in that it grants access to all graphics cards supporting OpenGL with drivers. It does not SIMPLIFY making games, it simply allows you to utilize the graphics card. A framework is more like a game engine (I’d say framework = game engine), in that it handles lots of things that are related to games, like handling game objects/entities, drawing backgrounds and loading resources.

Vastly simplified: To a game engine, you can say “Draw this object for me”, “Move this object to (x ,y)” or “Fire a bullet here” and it will do it. OpenGL however doesn’t know what an object is. It doesn’t even know what a 3D model is. The only thing it knows is drawing triangles, but it is INSANELY fast at drawing those. You’d therefore build a framework OVER OpenGL, so when you tell your game engine “Draw this object for me”, your game engine in turn tells OpenGL which triangles to draw.

Concerning the benefits of OpenGL vs Java2D (what you’re using now), I recommend that you stay as far away from OpenGL as possible for now. You’ll get by fine with Java2D. OpenGL trades performance for simplicity, and you should definitely get a hang of Java2D before moving on to OpenGL when Java2D isn’t fast enough anymore.

I’ll leave the JPanel vs JComponent question to the Java2D experts. :smiley:

[quote=“WolfAlvein,post:9,topic:37897”]
A good framework for starting game development is Slick2D (http://slick.cokeandcode.com/). It uses OpenGL hardware accelleration, but it has a framework built around it which makes it a lot easier to use. However, as a starting point just using normal Java2D is probably the easiest.

JPanel vs. JComponent, as far as games are concerned, is practically a non-issue. The differences between the two are default layout, opaqueness (depending on the LAF), things like that.* If you’re painting the entire component each frame, there will be no functional difference between them. All that said, folks generally recommend JComponent since it’s “lower-level” (JPanel being a subclass of JComponent).

  • As an aside, JComponent actually isn’t double buffered by default but JPanel is; however, this is also a non-issue since the root pane of a JFrame double-buffers all of its children.

Ok so OpenGL is not a framework but basically the directives and drivers to communicate with the system hardware or more properly the video card.

I’ll keep using Java2D for now I’m not interested in doing a 3D game yet, I want to learn a lot about 2D gaming and after that advance too 3D, ill check out the framework you told me about, and what I understood about framework’s is that they are game engines made to simplify the development of some game styles, basically 2D and 3D no matter what type of game (RPG, RTS, etc).

So can anyone point me to a good tutorial about game engines??? just so I learn to make my one before using some framework jejejeje.

There aren’t really “tutorials” about game engines. A game engine is in fact the popular term for a framework or a game library. These frameworks handles things for you, they make your life easy. Since you’re a beginner I absolutely recommend you to not use a Java2D framework. In fact, I think you should read this post to understand the situation between Java2D and OpenGL.

Regarding your code, I believe your error is “setDefaultCloseOperation(…)”, since that’s not a method in JComponent, only in JFrame. Here is a bit of pseudocode to understand what to do you with JFrame and JComponent:


public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("My Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width,height);
        frame.setResizable(false);
        frame.add(new MyComponent());
        frame.setVisible(true);
    }
}

public class MyComponent extends JComponent implements Runnable, KeyListener {
    // Move everything from your original class to this class
    
    Sprite ....
    ....
    boolean isRunning;

    public MyComponent() {
        //initialize everything
        
        addKeyListener(this);
    }
    
    public void addNotify() {
        super.addNotify();
        
        isRunning = true;
        new Thread(this).start();
    }
    
    public void run() {
        //game loop
        
        while(isRunning) {
            update();
            
            repaint();
            
            //sleep
        }
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        //draw everything
    }

    public void keyPressed(KeyEvent key) {}
    public void keyReleased(KeyEvent key) {}
    public void keyTyped(KeyEvent key) {}
}

Again, move all your variables and methods into this class. Looking at your code, you are not using any of JFrame’s methods (except the ones to initialize) so you shouldn’t be having any problems.