Hallo,
ich habe hier ein Spiel programmiert und brauche eure Hilfe dabei, denn ich überhaupt nicht was ich hier falsch gemacht habe. Immer wieder tritt diese Fehler Meldung bei mir auf.
/*
import javax.swing.*;
public class Maze {
public Maze() {
JFrame f = new JFrame();
f.setTitle("The Maze Game");
f.add(new Board());
f.setSize(500, 400);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Maze();
}
}
*/
/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener {
private Timer timer;
private Map m;
private Player p;
private boolean win = false;
private String Message = "";
private Font font = new Font("Levent", Font.BOLD, 48);
public Board() {
m = new Map();
p = new Player();
addKeyListener(new A1());
setFocusable(true);
timer = new Timer(25, this);
timer.start();
}
public void actionPerformed(ActionEvent e) {
if(m.getMap(p.getTileX(), p.getTileY()).equals("f")) {
Message = "Winner";
win = true;
}
repaint();
}
public void paint(Graphics g) {
super.paint(g);
if(!win) {
for(int y = 0; y < 14; y++) {
for(int x = 0; x < 14; x++) {
if(m.getMap(x ,y).equals("d")) {
g.drawImage(m.getDesert(), x * 64, y* 36, null );
}
if(m.getMap(x ,y).equals("w")) {
g.drawImage(m.getWall(), x * 64, y* 36, null );
}
}
}
g.drawImage(p.getPlayer(), p.getTileX() * 64, p.getTileY() * 32, null );
}
if(win) {
g.setColor(Color.RED);
g.setFont(font);
g.drawString(Message, 150, 300);
}
}
public class A1 extends KeyAdapter{
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if(!m.getMap(p.getTileX(), p.getTileY() -1).equals("w")){
if(keycode == KeyEvent.VK_W) {
p.move(0, -1);
}
}
if(!m.getMap(p.getTileX(), p.getTileY() +1).equals("w")) {
if(keycode == KeyEvent.VK_S) {
p.move(0, 1);
}
}
if(!m.getMap(p.getTileX() - 1, p.getTileY() ).equals("w")) {
if(keycode == KeyEvent.VK_A) {
p.move( -1, 0);
}
}
if(!m.getMap(p.getTileX() + 1, p.getTileY() ).equals("w")) {
if(keycode == KeyEvent.VK_D) {
p.move(1, 0);
}
}
}
}
}
*/
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.ImageIcon;
public class Map {
private Scanner sc;
private String[] Map = new String[14];
private Image desert, finish, wall;
public Image getDesert() {
return desert;
}
public Image getFinish() {
return finish;
}
public Image getWall() {
return wall;
}
public Map() {
ImageIcon img = new ImageIcon("C://ptsd2020-papa//src-code//MazeGameUI//things//desert.jfif");
desert = img.getImage();
img = new ImageIcon("C://ptsd2020-papa//src-code//MazeGameUI//things//wall.jfif");
wall = img.getImage();
img = new ImageIcon("C://ptsd2020-papa//src-code//MazeGameUI//things//finish.png");
finish = img.getImage();
openFile();
readFile();
closeFile();
}
public String getMap(int x, int y) {
String index = Map[y].substring(x, x+1);
return index;
}
public void openFile() {
try {
sc = new Scanner(new File("C://ptsd2020-papa//src-code//MazeGameUI//things//Map.txt"));
}catch(Exception e) {
System.out.println("error");
}
}
public void readFile() {
while(sc.hasNext()) {
for(int i = 0; i<14; i++) {
Map = sc.next();
}
}
}
public void closeFile() {
sc.close();
}
}
*/
/*
import java.awt.*;
import javax.swing.ImageIcon;
public class Player {
private int tileX, tileY;
private Image player;
public Player() {
ImageIcon img = new ImageIcon("C://ptsd2020-papa//src-code//MazeGameUI//things//dummy.jfif");
player = img.getImage();
tileX = 1;
tileY = 1;
}
public Image getPlayer() {
return player;
}
public int getTileX() {
return tileX;
}
public int getTileY() {
return tileY;
}
public void move(int dx, int dy) {
tileX += dx;
tileY += dy;
}
}
*/
/*
Exception in thread "main" java.lang.NullPointerException
at MazeGameUI/de.uni_koblenz.ptsd.PAPA.mazegameui.Map.readFile(Map.java:60)
at MazeGameUI/de.uni_koblenz.ptsd.PAPA.mazegameui.Map.<init>(Map.java:40)
at MazeGameUI/de.uni_koblenz.ptsd.PAPA.mazegameui.Board.<init>(Board.java:21)
at MazeGameUI/de.uni_koblenz.ptsd.PAPA.mazegameui.Maze.<init>(Maze.java:9)
at MazeGameUI/de.uni_koblenz.ptsd.PAPA.mazegameui.Maze.main(Maze.java:16)
*/
ich habe hier ein Spiel programmiert und brauche eure Hilfe dabei, denn ich überhaupt nicht was ich hier falsch gemacht habe. Immer wieder tritt diese Fehler Meldung bei mir auf.
/*
import javax.swing.*;
public class Maze {
public Maze() {
JFrame f = new JFrame();
f.setTitle("The Maze Game");
f.add(new Board());
f.setSize(500, 400);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Maze();
}
}
*/
/*
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener {
private Timer timer;
private Map m;
private Player p;
private boolean win = false;
private String Message = "";
private Font font = new Font("Levent", Font.BOLD, 48);
public Board() {
m = new Map();
p = new Player();
addKeyListener(new A1());
setFocusable(true);
timer = new Timer(25, this);
timer.start();
}
public void actionPerformed(ActionEvent e) {
if(m.getMap(p.getTileX(), p.getTileY()).equals("f")) {
Message = "Winner";
win = true;
}
repaint();
}
public void paint(Graphics g) {
super.paint(g);
if(!win) {
for(int y = 0; y < 14; y++) {
for(int x = 0; x < 14; x++) {
if(m.getMap(x ,y).equals("d")) {
g.drawImage(m.getDesert(), x * 64, y* 36, null );
}
if(m.getMap(x ,y).equals("w")) {
g.drawImage(m.getWall(), x * 64, y* 36, null );
}
}
}
g.drawImage(p.getPlayer(), p.getTileX() * 64, p.getTileY() * 32, null );
}
if(win) {
g.setColor(Color.RED);
g.setFont(font);
g.drawString(Message, 150, 300);
}
}
public class A1 extends KeyAdapter{
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if(!m.getMap(p.getTileX(), p.getTileY() -1).equals("w")){
if(keycode == KeyEvent.VK_W) {
p.move(0, -1);
}
}
if(!m.getMap(p.getTileX(), p.getTileY() +1).equals("w")) {
if(keycode == KeyEvent.VK_S) {
p.move(0, 1);
}
}
if(!m.getMap(p.getTileX() - 1, p.getTileY() ).equals("w")) {
if(keycode == KeyEvent.VK_A) {
p.move( -1, 0);
}
}
if(!m.getMap(p.getTileX() + 1, p.getTileY() ).equals("w")) {
if(keycode == KeyEvent.VK_D) {
p.move(1, 0);
}
}
}
}
}
*/
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.ImageIcon;
public class Map {
private Scanner sc;
private String[] Map = new String[14];
private Image desert, finish, wall;
public Image getDesert() {
return desert;
}
public Image getFinish() {
return finish;
}
public Image getWall() {
return wall;
}
public Map() {
ImageIcon img = new ImageIcon("C://ptsd2020-papa//src-code//MazeGameUI//things//desert.jfif");
desert = img.getImage();
img = new ImageIcon("C://ptsd2020-papa//src-code//MazeGameUI//things//wall.jfif");
wall = img.getImage();
img = new ImageIcon("C://ptsd2020-papa//src-code//MazeGameUI//things//finish.png");
finish = img.getImage();
openFile();
readFile();
closeFile();
}
public String getMap(int x, int y) {
String index = Map[y].substring(x, x+1);
return index;
}
public void openFile() {
try {
sc = new Scanner(new File("C://ptsd2020-papa//src-code//MazeGameUI//things//Map.txt"));
}catch(Exception e) {
System.out.println("error");
}
}
public void readFile() {
while(sc.hasNext()) {
for(int i = 0; i<14; i++) {
Map = sc.next();
}
}
}
public void closeFile() {
sc.close();
}
}
*/
/*
import java.awt.*;
import javax.swing.ImageIcon;
public class Player {
private int tileX, tileY;
private Image player;
public Player() {
ImageIcon img = new ImageIcon("C://ptsd2020-papa//src-code//MazeGameUI//things//dummy.jfif");
player = img.getImage();
tileX = 1;
tileY = 1;
}
public Image getPlayer() {
return player;
}
public int getTileX() {
return tileX;
}
public int getTileY() {
return tileY;
}
public void move(int dx, int dy) {
tileX += dx;
tileY += dy;
}
}
*/
/*
Exception in thread "main" java.lang.NullPointerException
at MazeGameUI/de.uni_koblenz.ptsd.PAPA.mazegameui.Map.readFile(Map.java:60)
at MazeGameUI/de.uni_koblenz.ptsd.PAPA.mazegameui.Map.<init>(Map.java:40)
at MazeGameUI/de.uni_koblenz.ptsd.PAPA.mazegameui.Board.<init>(Board.java:21)
at MazeGameUI/de.uni_koblenz.ptsd.PAPA.mazegameui.Maze.<init>(Maze.java:9)
at MazeGameUI/de.uni_koblenz.ptsd.PAPA.mazegameui.Maze.main(Maze.java:16)
*/