gpt4 book ai didi

java - 迷宫游戏编程

转载 作者:行者123 更新时间:2023-12-01 14:00:25 25 4
gpt4 key购买 nike

这是我的简单迷宫游戏代码。它正在编译,并且正在创建 MazeGame.java 和 Board.java 的类文件,但没有创建 Player.java 和 Map.java 的类文件。该代码编译无错误,但未运行。请帮帮我。

//MazeGame.java
package mygame;

import javax.swing.*;

public class MazeGame {
public static void main(String[] args) {
new MazeGame();
}

public MazeGame() {
JFrame f= new JFrame();
f.setTitle("Maze Game");
f.setSize(450,450);
f.setLocationRelativeTo(null);
f.add(new Board());
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}



//Board.java
package mygame;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.*;

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("Comic Sans",Font.BOLD,50);

public Board() {
m = new Map();
p = new Player();

addKeyListener(new Al());
setFocusable(true);

timer = new Timer(25,this);
timer.start();
}

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("f")) {
g.drawImage(m.getFinish(),x*32,y*32,null);
}
if(m.getMap(x,y).equals("g")) {
g.drawImage(m.getGrass(),x*32,y*32,null);
}
if(m.getMap(x,y).equals("w")) {
g.drawImage(m.getWall(),x*32,y*32,null);
}
}
}
g.drawImage(p.getPlayer(),p.getTileX()*32,p.getTileY()*32,null);
}

if(win)
{
g.setColor(Color.BLUE);
g.setFont(font);
g.drawString(Message,100,300);
}
}

public class Al extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if((keyCode==KeyEvent.VK_W) || (keyCode==KeyEvent.VK_UP)) {
if(!(m.getMap(p.getTileX(),p.getTileY()-1).equals("w"))) {
p.move(0,-1);
}
}
if((keyCode==KeyEvent.VK_S) || (keyCode==KeyEvent.VK_DOWN)) {
if(!(m.getMap(p.getTileX(),p.getTileY()+1).equals("w"))) {
p.move(0,1);
}
}
if((keyCode==KeyEvent.VK_A) || (keyCode==KeyEvent.VK_LEFT)) {
if(!(m.getMap(p.getTileX(),p.getTileY()-1).equals("w"))) {
p.move(-1,0);
}
}
if((keyCode==KeyEvent.VK_D) || (keyCode==KeyEvent.VK_RIGHT)) {
if(!(m.getMap(p.getTileX(),p.getTileY()-1).equals("w"))) {
p.move(1,0);
}
}
}
}

public void actionPerformed(ActionEvent e) {
if(m.getMap(p.getTileX(),p.getTileY()).equals("f")) {
Message = "WINNER!!!";
win = true;
}
repaint();
}
}



//Map.java
package mygame;

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.*;

public class Map {
private Scanner s;
private String Map[] = new String[14];
private Image grass,wall,finish;

public Map() {
ImageIcon img = new ImageIcon("G://sonali_java//mygame//grass.png");
grass = img.getImage();
img = new ImageIcon("G://sonali_java//mygame//wall.png");
wall = img.getImage();
img = new ImageIcon("G://sonali_java//mygame//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 Image getGrass() {
return grass;
}
public Image getWall() {
return wall;
}
public Image getFinish() {
return finish;
}

public void openFile() {
try {
s= new Scanner(new File("G://sonali_java//mygame//Map.txt"));
}
catch(Exception e) {
System.out.println("Error Loading File!!!!");
}
}

public void readFile() {
while(s.hasNext()) {
for(int i=0; i<14; i++) {
Map[i] = s.next();
}
}
}

public void closeFile() {
s.close();
}
}



//Player.java
package mygame;

import java.awt.*;

public class Player {
private int tileX,tileY;
private Image player;

public Player() {
tileX=1;
tileY=1;
ImageIcon img = new ImageIcon("G://sonali_java//mygane//object.png");
player = img.getImage();
}

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;
}
}

最佳答案

我不想生气,但你正在定义不同的文件夹位置

在某些地方“G://sonali_java//mygane//object.png”,检查“mygaNe”

在某些地方“G://sonali_java//mygame//Map.txt”,选中“mygaMe”

您确定加载时不会抛出 NullPointerException 吗?

关于java - 迷宫游戏编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19380871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com