[英]My first time trying to make a simple tile-based java game
I'm trying to make my first little java game here and I want to make it tiled-based. 我正在尝试在这里制作我的第一个小型Java游戏,并希望使其基于图块。 I'm trying to tile a block image on a JFrame and have put together some code to fit my circumstances. 我正在尝试在JFrame上平铺块图像,并组合了一些代码以适合我的情况。 Unfortunately, it's not really working as it won't draw any of the test sprites I want, which is where I need the help. 不幸的是,它并没有真正起作用,因为它不会绘制我想要的任何测试精灵,这是我需要帮助的地方。 I'll post my code: 我将发布我的代码:
frame.java: I want to say that this is all correct... frame.java:我想说这都是正确的...
import javax.swing.JFrame;
public class frame extends JFrame
{
panel ng;
public frame()
{
ng = new panel();
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
add(ng);
}
public static void main(String [] args)
{
frame n = new frame();
}
}
panel.java: panel.java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class panel extends JPanel implements Runnable
{
private Thread game;
private boolean running = false;
private Image dbImage;
private Graphics dbGraphics;
static final int Width = 50;
static final int Height = 50;
static final Dimension dim = new Dimension(Width,Height);
map nm;
public panel()
{
nm = new map("tester.txt");
setPreferredSize(new Dimension(75,75));
setFocusable(true);
requestFocus();
}
public void run()
{
while (running)
{
update();
render();
paintScreen();
}
}
private void update()
{
if (game != null && running == true)
{
}
}
private void render()
{
if (dbImage == null)
{
dbImage = createImage(Width, Height);
dbGraphics = dbImage.getGraphics();
}
dbGraphics.setColor(Color.WHITE);
dbGraphics.fillRect(0,0,Width,Height);
draw(dbGraphics);
}
private void paintScreen()
{
Graphics g;
try
{
g=this.getGraphics();
if(dbImage != null && g != null)
{
g.drawImage(dbImage,0,0,null);
}
}
catch(Exception e)
{
}
}
public void draw(Graphics g)
{
nm.draw(g);
}
private void startGame()
{
if(game == null || running == false)
{
game = new Thread(this);
game.start();
}
}
public void addNotify()
{
super.addNotify();
startGame();
}
public void stopGame()
{
if (running == true)
{
running = false;
}
}
}
map.java: This is where I attempt to assign the sprites...not sure what I'm doing wrong. map.java:这是我尝试分配精灵的地方...不确定我在做什么错。 BTW the test text file that I use in the constructor for the map is just a file that has the width,height, and then a bunch of random 0s and 1s. 顺便说一句,我在地图的构造函数中使用的测试文本文件只是一个文件,该文件具有width,height,然后是一堆随机的0和1。 The 0s and 1s were used to show where certain blocks should go. 0和1用来显示某些块应该放在哪里。
import java.awt.Graphics;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class map
{
private int[][] tileMap;
private int height;
private int width;
private Image BLOCK;
public map(String mapFile)
{
BLOCK = new ImageIcon("path to sprite").getImage();
int[] tileNums;
try
{
BufferedReader br = new BufferedReader(new FileReader(mapFile));
width=Integer.parseInt(br.readLine());
height=Integer.parseInt(br.readLine());
tileNums= new int[width];
tileMap=new int[width][height];
for(int row = 0; row < height; row++)
{
String line=br.readLine();
//what I'm trying to do here is make each int in grid into array by converting char to int
for(int i=0; i<=line.length();i++)
{
tileNums[i]=(line.charAt(i)-48);
}
for(int col = 0; col<width; col++)
{
tileMap[row][col]=tileNums[col];
}
}
}
catch(Exception e)
{
}
}
public void draw(Graphics g)
{
int ix=0;
int iy=0;
for(int row=0;row<height;row++)
{
for(int col=0;col<height;col++)
{
if(tileMap[row][col]==0)
{
g.drawImage(BLOCK, ix, iy, null);
ix=ix+16;
}
}
}
}
}
您必须重写JPanel的paint方法,并在其中调用draw,并将图形对象g传递给它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.