Hallo,
ich schreibe seit kurzem an einem multiplayer shooter, bei dem ich anfangs Weise einfach nur über ein 20 * 20 Feld laufen kann! Alles soll multiplayer gehen mit einem einfachen Echo-Server (das hat mein Lehrer vorgegeben)! Ich habe jeetzt aber das große Problem, dass ich das mit dem Canvas und den Graphics nicht ordentlich hinbekomme (beim auführen kommt eine null pointer execption)!
Vielleicht kann mir ja jemand helfen!
Würde mich auch über andere Tipps freuen, da ich in Java noch keinen wirklich eleganten Stil habe.
JAVA-CODE (GUI-CLASS):
JAVA-CODE (GAME-CLASS):
Danke schon mal im Voraus
Anton
ich schreibe seit kurzem an einem multiplayer shooter, bei dem ich anfangs Weise einfach nur über ein 20 * 20 Feld laufen kann! Alles soll multiplayer gehen mit einem einfachen Echo-Server (das hat mein Lehrer vorgegeben)! Ich habe jeetzt aber das große Problem, dass ich das mit dem Canvas und den Graphics nicht ordentlich hinbekomme (beim auführen kommt eine null pointer execption)!
Vielleicht kann mir ja jemand helfen!
Würde mich auch über andere Tipps freuen, da ich in Java noch keinen wirklich eleganten Stil habe.
JAVA-CODE (GUI-CLASS):
Java:
...
Game game = new Game(gui);
JPanel pgame = new JPanel();
pgame.add(game);
game.addKeyListener(this);
...
JAVA-CODE (GAME-CLASS):
Java:
package Game;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import Main.GUI;
public class Game extends Canvas {
private static final long serialVersionUID = 1L;
static GUI gui = null;
static int[][] ls_game = new int[20][20];
static int[] ls_myposi = new int[2];
static Graphics gr;
public Game(GUI _gui) {
if (_gui != null) {
gui = _gui;
gr = this.getGraphics();
setBounds(10, 10, 500, 500);
setBackground(Color.LIGHT_GRAY);
setGameField();
}
}
public void paint(Graphics _g) {
gr = _g;
gr.setColor(Color.GREEN);
gr.fillRect(10, 10, 20, 20);
this.repaint();
}
public void setGameField() {
init();
Random rnd = new Random();
int x = rnd.nextInt(20);
int y = rnd.nextInt(20);
ls_myposi[0] = x;
ls_myposi[1] = y;
updateGUI(3, x, y);
}
public void init() {
for (int i = 0; i < ls_game.length; i++) {
for (int j = 0; j < ls_game.length; j++) {
ls_game[i][j] = 0;
}
}
for (int i = 0; i < ls_myposi.length; i++) {
ls_myposi[i] = 0;
}
}
public void updateGUI(int type, int x, int y) {
System.out.println(type + " " + x + " " + y);
switch (type) {
case 0:
gr.setColor(Color.magenta);
gr.fillRect(x, y, 20, 20);
break;
case 1:
gr.setColor(Color.BLUE);
gr.fillRect(x, y, 20, 20);
break;
case 2:
gr.setColor(Color.CYAN);
gr.fillRect(x, y, 20, 20);
break;
case 3:
gr.setColor(Color.GREEN);
gr.fillRect(x, y, 20, 20);
break;
}
}
public void setnewPosi(int type, int x, int y) {
ls_game[x][y] = type;
updateGUI(type, x, y);
}
public void go(int go) {
int x = ls_myposi[0];
int y = ls_myposi[1];
switch (go) {
case 0: // up
y++;
break;
case 1: // down
y--;
break;
case 2: // left
x--;
break;
case 3: // right
x--;
break;
}
System.out.println(cango(x, y));
if (cango(x, y)) {
ls_myposi[0] = x;
ls_myposi[1] = y;
setnewPosi(30, x, y);
}
}
public boolean cango(int x, int y) {
if (x < 0) {
return false;
}
if (y < 0) {
return false;
}
if (x > 20) {
return false;
}
if (y > 20) {
return false;
}
return true;
}
// types::::
// 0:leeres Feld
// 1:blockade
// 2:
// 30:ich
// 31:Player 1
// 32:Player 2
// 33:Player 3
}
Danke schon mal im Voraus
Anton