gpt4 book ai didi

java - Java RPG 的对象层

转载 作者:行者123 更新时间:2023-11-30 11:53:28 25 4
gpt4 key购买 nike

我正在尝试用 Java 制作一个基本的角色扮演游戏。我认为我掌握了 Java 的基础知识,但是说到这些东西我绝对是初学者。不管怎样,我认为最好创建一个有两层的 map :一个地形层有水、岩石、草地等等……还有一个对象层有树木、房屋、元素——玩家可以与之互动的东西。到目前为止,我有一个地形层,使用 2-Dim 数组生成。我的问题是:我怎样才能制作一个对象层,更具体地说,我怎样才能制作一个包含可以与之坐标交互的对象的图层?

以下是我目前所拥有的。抱歉,我的代码组织起来一定很奇怪——就像我说的,我是这方面的初学者。谢谢你的时间。

-马库斯

import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import javax.imageio.ImageIO.*;
import java.awt.image.*;
import java.awt.image.BufferedImage.*;
import java.io.File.*;
import java.io.*;
import java.awt.Graphics2D.*;

public class mapStoreII extends JFrame implements KeyListener{


BufferedImage pic;
int width = 32;
int height = 32;
int px = 656;
int py = 656;

int[][] map_1 = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,10,5,11,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,3,14,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,13,6,12,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,16,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,1,1,1}
};

public mapStoreII(){
setSize(875,750);
setLocation(800,150);
setTitle("Test_Organics_v.1");
setResizable(true);
setBackground(Color.black);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addKeyListener(this);
setVisible(true);
}

public void paint(Graphics g){
Graphics2D graphics = (Graphics2D) g;
for(int y = 0; y<20; y++){
for(int x = 0; x<24; x++){

try {
pic = ImageIO.read(new File(getImage(getMap(y,x))));
} catch (IOException e) {}

graphics.drawImage(pic, (x*width)+50, (y*height)+50, null);
graphics.setPaint(Color.red);
graphics.fill(new Ellipse2D.Double(px,py,32,32)); //Currently the player.
}
}
}

public int getMap(int y, int x){
return map_1[y][x];
}

public String getImage(int a){
if(a==2){
return "shoreline_1.png";
}
else if(a==1){
return "water_1.png";
}
else if(a==9){
return "player_on_dirt_1.png";
}
else if(a==3){
return "island_left.png";
}
else if(a==4){
return "island_right.png";
}
else if(a==5){
return "island_up.png";
}
else if(a==6){
return "island_down.png";
}
else if(a==7){
return "chemical_1.png";
}
else if(a==10){
return "island_topLC.png";
}
else if(a==11){
return "island_topRC.png";
}
else if(a==12){
return "island_botRC.png";
}
else if(a==13){
return "island_botLC.png";
}
else if(a==14){
return "island_center.png";
}
else if(a==15){
return "shoreline_2.png";
}
else if(a==16){
return "shoreline_3.png";
}
else{
return "grassydirt_1.png";
}
}

public void keyPressed(KeyEvent event){
int code = event.getKeyCode();
if(code == KeyEvent.VK_LEFT || code == KeyEvent.VK_A){
px-=8;
repaint();
}
else if(code == KeyEvent.VK_RIGHT || code == KeyEvent.VK_D){
px+=8;
repaint();
}
else if(code == KeyEvent.VK_UP || code == KeyEvent.VK_W){
py-=8;
repaint();
}
else if(code == KeyEvent.VK_DOWN || code == KeyEvent.VK_S){
py+=8;
repaint();
}
else if(code == KeyEvent.VK_I){
lineDraw inventory = new lineDraw(); //Opens an inventory.
}
}

public void keyReleased(KeyEvent event){
}

public void keyTyped(KeyEvent event){
}

public static void main(String[] args){
JFrame frame = new mapStoreII();
}

}

最佳答案

听起来是个很棒的项目!我在编写名为 Tyrant 的 Java RPG/roguelike 游戏时玩得很开心几年前。这一切都是开源的,所以请随意探索!

您可能会觉得有帮助的几点:

  • 我有一个名为 Map ( Map.java source ) 的对象
  • map 存储了图 block (在 int[] 数组中)和对象(在 Thing[] 数组中)。我使用一维数组并使用 (x+y*mapWidth) 计算它们的偏移量,但您同样可以使用二维数组
  • Thing 是一个单独的类,它描述了一个对象的所有属性和行为。它还包含一个指向另一个事物的“下一个”指针,这样您就可以在一个方 block 中将事物列表链接在一起。
  • 最好将图形代码与游戏逻辑和游戏数据结构分开。这有助于保持代码的可维护性和灵 active ——例如,当您想要存储多个不可见的 map 时。就我而言,我使用了 MapPanel GUI component它扩展了 JPanel 并知道如何绘制 map ,但 map 对象本身不包含 GUI 或绘图相关代码。

无论如何希望这对您有所帮助。祝你好运!

关于java - Java RPG 的对象层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6254111/

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