Making Minecraft In 2D(2)

by ProgrammerProgrammerIOSAN   Last Updated January 12, 2018 19:13 PM

enter image description hereI want to make a 2D Minecraft game and I have drawn the grass blocks and you can dig but when the palyer moves the dirt moves too. Could someone help me?

This is my code

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
[![enter image description here][1]][1]public class Gameplay extends JPanel implements ActionListener,    KeyListener, MouseListener{
public BufferedImage player;
public BufferedImage grass;
public BufferedImage dirt;
private BufferedImage dbImage;
private Graphics dbg;
private int x = 0;
private int y = 0;
private double velX = 8.0f;
private double velY = 8.0f;
public Rectangle rectangle;
public Rectangle rectangle2;
public Rectangle MouseRect;
public boolean BlockD = false;
public ArrayList<Rectangle> blockList = new ArrayList<Rectangle>();
private int[] xb = {0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650};
private int[] yb = {0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650};
//
public Gameplay(){
    addMouseListener(this);
    addKeyListener(this);
    setFocusable(true);
    try {
        player = ImageIO.read(getClass().getResource("Player.png"));
        grass = ImageIO.read(getClass().getResource("Grass.png"));
        dirt = ImageIO.read(getClass().getResource("Dirt.png"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void paint(Graphics g){
    dbImage = (BufferedImage) createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);
    repaint();
}

public void paintComponent(Graphics g){
    drawBlock(g);
    g.drawImage(player, x, y, this);
    rectangle2 = new Rectangle(x, y, 35, 35);
    g.drawString("Jake", x, y);
    for(Rectangle r : blockList) {
        if(rectangle2.intersects(r)) {
            if(BlockD){
                g.drawImage(dirt, (int) r.getX(), (int) r.getY(), this);
            }
        }
    }   
}

public void drawBlock(Graphics g){
    for(int first = 0; first < 10; first++){
        for(int second = 0; second < 10; second++){
            g.drawImage(grass, xb[first], yb[second], this);
            rectangle = new Rectangle(xb[first], yb[second], 50, 50);
            blockList.add(rectangle);
        }
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_UP){
        y += -velY;
    }
    else if(e.getKeyCode() == KeyEvent.VK_DOWN){
        y += velY;
    }
    else if(e.getKeyCode() == KeyEvent.VK_LEFT){
        x += -velX;
    }
    else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
        x += velX;
    }
    else if(e.getKeyCode() == KeyEvent.VK_SPACE){
        BlockD = true;  
    }   
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
    if(e.getButton() == MouseEvent.BUTTON1){

    }
}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}
}
Tags : minecraft


Related Questions


Minecraft zoomed in, how do I fix this?

Updated July 14, 2015 13:05 PM

MC PE Mini Games iOS

Updated August 07, 2015 14:05 PM