我第一次尝试制作一个简单的基于图块的java游戏

时间:2013-04-22 03:09:48

标签: java

我正在尝试在这里制作我的第一个小型java游戏,我想让它基于平铺。我正在尝试在JFrame上平铺块图像并将一些代码放在一起以适合我的情况。不幸的是,它并没有真正起作用,因为它不会绘制我想要的任何测试精灵,这是我需要帮助的地方。我会发布我的代码:

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:

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:这是我尝试分配精灵的地方......不知道我做错了什么。 BTW我在地图构造函数中使用的测试文本文件只是一个文件,其中包含宽度,高度,然后是一堆随机0和1。 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;
                    }
                }
            }
        }
}

1 个答案:

答案 0 :(得分:0)

你必须覆盖JPanel的paint方法并在那里调用draw,然后传递图形对象g。

相关问题