Here's the main code I have for my java space invaders clone:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.*;
public class Game extends JPanel implements Runnable, KeyListener{
public static void main (String args[]) { Game game = new Game(); game.construirJanela(); game.inicializarInimigos(); } public void construirJanela() { JFrame janela = new JFrame ("Game"); setBackground(Color.black); setFocusable(true); setDoubleBuffered(true); addKeyListener(this); janela.setSize(800, 600); janela.setResizable(false); janela.setLocationRelativeTo(null); janela.add(this); janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); janela.setVisible(true); }
private Thread animador; private final int VELOCIDADE = 5; private final int ESPACO = 50; private int velInimigo; private Jogador jogador; private Inimigo[] inimigos; private Tiro[] tiros; private boolean inGame; public Game() { jogador = new Jogador(385, 500); tiros = new Tiro[5]; inimigos = new Inimigo[12]; inGame = true; } public void inicializarInimigos() { for (int i = 0; i < inimigos.length; i++) { inimigos[i] = new Inimigo(ESPACO * i, 20); } } public void addNotify() { super.addNotify(); animador = new Thread(this); animador.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; if (inGame == true) { if (jogador.getJVisivel() == true) { jogador.desenharJogador(g2d); } for(int t = 0; t< tiros.length; t++) { if(tiros[t] != null) { tiros[t].desenharTiro(g2d); } } for (int i = 0; i < inimigos.length; i++) { if (inimigos[i].getIVisivel() == true) { inimigos[i].desenharInimigo(g2d); } } } else { Font fonte = new Font("Tahoma", Font.BOLD, 35); g2d.setFont(fonte); g2d.setColor(Color.white); g2d.drawString("Game Over", 320, 300); } }
public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { jogador.moverJogadorX(-VELOCIDADE); } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { jogador.moverJogadorX(VELOCIDADE); } if (e.getKeyCode() == KeyEvent.VK_SPACE) { for(int i = 0; i < tiros.length; i++) { if(tiros[i] == null) { tiros[i] = jogador.atirar(); break; } } } if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { inGame = false; } }
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { } public void checarColisao() { Rectangle rJ = jogador.getLimites();
for (int a = 0; a < inimigos.length; a++) { [color=red]Rectangle rI = inimigos[a].getLimites();[/color]
if (rJ.intersects(rI)) { jogador = null; inimigos[a] = null; inGame = false; } }
for (int b = 0; b < tiros.length; b++) { Rectangle rT= tiros[b].getLimites(); for (int c = 0; c < inimigos.length; c++) { Rectangle rI = inimigos[c].getLimites(); if (rT.intersects(rI)) { tiros[b] = null; inimigos[c] = null; } } } } public void run() { while (true) { for(int d = 0; d < tiros.length; d++) { if(tiros[d] != null) { tiros[d].moverTiro(-VELOCIDADE); if(tiros[d].getTiroY() < 0) { tiros[d] = null; } } } for (int e = 0; e < inimigos.length; e++) { if (inimigos[e] != null) { inimigos[e].moverInimigo(velInimigo); if (inimigos[e].getInimigoX() + 20 == 800) { velInimigo = - VELOCIDADE; } if (inimigos[e].getInimigoX() == 0) { velInimigo = VELOCIDADE; } } } [color=red] checarColisao(); [/color] repaint(); try { Thread.sleep(25); } catch (InterruptedException e) { } } } } |
Here's the problem:
Exception in thread "Thread-3" java.lang.NullPointerException
at Game.checarColisao(Game.java:150) at Game.run(Game.java:211) at java.lang.Thread.run(Thread.java:619)
"checarColisao()" is a method that retrieves a Rectangle from every entity in the game and then checks if they intersect each other. When they do, the method should get rid of the entity.
There's also a bug at the enemy's movement that I'll try to fix later, never mind.
I would be glad if someone could help me here. Thanks in advance.