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.