-4
\$\begingroup\$

I 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. It draws on every block wich the player touches a dirt block because I said that it has to draw it on r.getX() and r.getY() and it changes every time the player moves. Could someone please 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;

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);
    rectangle2 = new Rectangle(x, y, 35, 35);
    g.drawString("Jake", x, y);
    for(Rectangle r : blockList) {
        g.drawImage(dirt, (int) r.getX(), (int) r.getY(), this);
    }
    // Draw player after dirt blocks
    g.drawImage(player, x, y, 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);
        }
    }
}

@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){
        int blockX = Math.round(x / 50) * 50;
        int blockY = Math.round(y / 50) * 50;
        blockList.add(new Rectangle(x, y)); 
    }   
}

@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

}
}

Thanks.

\$\endgroup\$
  • 1
    \$\begingroup\$ What's the difference between this question and this previous question? \$\endgroup\$ – Vaillancourt Jan 12 '18 at 19:17
  • 2
    \$\begingroup\$ Also, as posting question ethic, please avoid posting code that is irrelevant to the question, avoid posting code as images, and state what you have tried, and what you're expecting to have (thanks for telling us what behaviour you currently have, though, this is helpful). Titling your question with something relevant helps get better answers too. Finally, tagging the question more appropriately will bring more relevant attention to your question. \$\endgroup\$ – Vaillancourt Jan 12 '18 at 19:21
  • 1
    \$\begingroup\$ @Charanor I think you actually proved that there's no reason for these blocks to move at all! OP, we need precisions on what exactly you're witnessing :p \$\endgroup\$ – Quentin Jan 12 '18 at 19:44
  • 2
    \$\begingroup\$ This is unrelated to Minecraft. The Minecraft tag is for questions that are related to Minecraft the game. Your question has nothing to do with the game, you just happen to be making a 2D Minecraft clone. \$\endgroup\$ – Charanor Jan 12 '18 at 19:53
  • 1
    \$\begingroup\$ @ProgrammerProgrammerIOSAN minecraft is for questions related to creating mods for Minecraft. Your question here is about your game, which is similar to Minecraft, but is not actually Minecraft; using that tag on this question is incorrect (yes, there are other questions that are using the tag incorrectly; they should get cleaned up). Please do not attempt to re-add it. \$\endgroup\$ – user1430 Jan 12 '18 at 20:35
1
\$\begingroup\$

This is exactly what you'd expect to happen. This is the code that draws your dirt blocks:

for(Rectangle r : blockList) {
    if(rectangle2.intersects(r)) {
        if(BlockD){
            g.drawImage(dirt, (int) r.getX(), (int) r.getY(), this);
        }
    }
}

This code says, for every rectangle (grass block) on the map, draw a dirt block if the player is standing on that rectangle. So of course your dirt blocks move when the player moves - that's what you're telling it to do.

I'm guessing what you want to do is create a dirt block on the current block when you press space. This should be a quite simple fix:

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) {
        e.printStackTrace();
    }

    // Remove this code!
    /* for(int first = 0; first < 10; first++){
        for(int second = 0; second < 10; second++){
            rectangle = new Rectangle(xb[first], yb[second], 50, 50);
            blockList.add(rectangle);
        }
    } */
}

public void keyPressed(KeyEvent e) {
    if (...) {
        // Movement code for player
    }
    else if(e.getKeyCode() == KeyEvent.VK_SPACE){
        // Place dirt block at current coordinates
        // Round the coordinates to closest block
        int blockX = Math.round(x / 50) * 50;
        int blockY = Math.round(y / 50) * 50;
        blockList.add(new Ractangle(blockX, blockY, 50, 50));
    }   
}

public void paintComponent(Graphics g){
    drawBlock(g);
    g.drawString("Jake", x, y);

    // Draw all dirt blocks
    for(Rectangle r : blockList) {
        g.drawImage(dirt, (int) r.getX(), (int) r.getY(), this);
    }
    // Draw player after dirt blocks
    g.drawImage(player, x, y, this);
}

Note that I have changed what blockList is used for. Before it was the rectangles for all grass blocks but now it is the rectangles for the dirt blocks.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – DMGregory Jan 14 '18 at 21:15
  • \$\begingroup\$ @ProgrammerProgrammerIOSAN Updated my answer with code. I tested it this time - it works. \$\endgroup\$ – Charanor Jan 14 '18 at 21:16
  • \$\begingroup\$ I have only one question. Do you know how to add a stone block because that's a little bit more complicated? \$\endgroup\$ – ProgrammerProgrammerIOSAN Jan 16 '18 at 18:26
  • \$\begingroup\$ @ProgrammerProgrammerIOSAN Why would adding a stone block be more complicated? \$\endgroup\$ – Charanor Jan 16 '18 at 18:42
  • \$\begingroup\$ I already tried it and it didn't work. Could you please help me? Thanks. \$\endgroup\$ – ProgrammerProgrammerIOSAN Jan 16 '18 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.