Reputation: 41
im creating a 2d world which has a player that is in a rectangle shape and what im trying to do is when the player press space button it draws a block depending on his direction the problem is it only sets for one block and plus it deletes it after repaiting now i know my mistake is in paint method. what i wanna know how to make it draws the block everytime i press space without removing after repainting and can be set for multiple times i know i should use loop but i dont know exactly i should write it
private static final long serialVersionUID = -1401667790158007207L;
int playerx = 450;
int playery = 450;
int playerDirection = 0; //north = 0, west = 1, south = 2, east = 3
boolean setBlock;
public platform(){
super("2D game");
JPanel panel = new JPanel();
panel.setBackground(Color.white);
addKeyListener(this);
this.setContentPane(panel);
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
Rectangle player = new Rectangle(playerx, playery, 50, 50);
g2.fill(player);
if(setBlock){
if(playerDirection == 0){
g2.fillRect(playerx, playery - 50, 50, 50);
}else if(playerDirection == 1){
g2.fillRect(playerx + 50, playery, 50, 50);
}else if(playerDirection == 2){
g2.fillRect(playerx, playery + 50, 50, 50);
}else if(playerDirection == 3){
g2.fillRect(playerx - 50, playery, 50, 50);
}
setBlock = false;
}
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
playerDirection = 0;
playery -=50;
repaint();
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
playerDirection = 2;
playery +=50;
repaint();
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
playerDirection = 1;
playerx += 50;
repaint();
}
if(e.getKeyCode() == KeyEvent.VK_LEFT){
playerDirection = 3;
playerx -=50;
repaint();
}
if(e.getKeyCode() == KeyEvent.VK_SPACE){
setBlock = true;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
Upvotes: 1
Views: 591
Reputation: 4039
You need to store your Blocks in a List. And add a new one when pressing Space Key. Then you can easily draw all of them in a for loop. Also remove that setBlock boolean because it makes no sense.
...
// introduce this class to hold a block
class Block{
public int x;
public int y;
public Block(int _x, int _y){
x = _x;
y = _y;
}
}
...
// boolean setBlock; // remove this boolean
ArrayList<Block> blocks = new ArrayList<Block>(); // list of all blocks
public platform(){...}
public void paint(Graphics g){
...
// if(setBlock){ // remove this boolean
if(playerDirection == 0){
g2.fillRect(playerx, playery, 50, 50);
}else if(playerDirection == 1){
g2.fillRect(playerx, playery, 50, 50);
}else if(playerDirection == 2){
g2.fillRect(playerx, playery, 50, 50);
}else if(playerDirection == 3){
g2.fillRect(playerx, playery, 50, 50);
}
// draw the blocks
for(Block b : blocks)
g2.fillRect(b.x, b.y, 50, 50);
// setBlock = false; // remove this boolean
//} // remove this boolean
}
public void keyPressed(KeyEvent e) {
...
if(e.getKeyCode() == KeyEvent.VK_SPACE){
// setBlock = true; // remove this boolean
// add a new block
blocks.add(new Block(playerx, playery));
repaint();
}
}
...
Upvotes: 2