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 blockList = new ArrayList();
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.
Answer
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.
No comments:
Post a Comment