Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2013 1:01 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sat Jun 09, 2012 6:03 pm 
Offline

Joined: Sat Jul 30, 2011 11:33 am
Posts: 60
Hey i am making a little game where the user can place blocks (hehe mc :)) so here i am i can create and remove blocks with the mouse but it removes the block that was last placed but i want to remove the block where the mouse is hovering over.
So i was thinking maybe if i made an array of polygons and draw a 1x1 poly at the mouse's position and polygons around the blocks and when the mouse collides with the block i want to remove that certain block but how would i select that certain block in the array ?
here is my source :)

Code:

import java.awt.Point;
import java.util.ArrayList;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class MainGame extends BasicGame{
   
   ArrayList<Image> stone = new ArrayList<Image>();
   ArrayList<Point> posi = new ArrayList<Point>();   
   Input input = null;
   int gx = 50;
   int gy = 50;
   Image ghost = null;
   Image stoneBlock = null;
   boolean pick = false;
   boolean build = false;
   
   

    public MainGame()
    {
        super("Slick2DPath2Glory - SimpleGame");
    }

    @Override
    public void init(GameContainer gc)
         throws SlickException {
       ghost = new Image("data/stone.png");
       stoneBlock = new Image("data/stone.png");
       ghost.setAlpha(0.5f);

    }

    @Override
    public void update(GameContainer gc, int delta)
         throws SlickException     
    {
       input = gc.getInput();
       gx = input.getMouseX()-ghost.getWidth()/2;
       gy = input.getMouseY()-ghost.getHeight()/2;
       
       
       if(input.isKeyDown(Input.KEY_B) && input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
          posi.add(new Point(gx,gy));
          stone.add(stoneBlock);
       }
       
       if(input.isKeyDown(Input.KEY_P) && input.isMousePressed(Input.MOUSE_LEFT_BUTTON) && stone.size() > 0) {
          stone.remove(stoneBlock);
          posi.remove(posi.size()-1);
       }

    }

    public void render(GameContainer gc, Graphics g)
         throws SlickException
    {
       ghost.draw(gx,gy,1);
       
       for(int i = 0; i < stone.size(); i++) {
          for(Point pos: posi) {
             
             String a = String.valueOf(pos.getX());
             float posx = Float.valueOf(a);
             
             String b = String.valueOf(pos.getY());
             float posy = Float.valueOf(b);
          
          stone.get(i).draw(posx,posy,1);
          }
       }

    }

    public static void main(String[] args)
         throws SlickException
    {
         AppGameContainer app =
         new AppGameContainer(new MainGame());

         app.setDisplayMode(800, 600, false);
         app.setVSync(true);
         app.start();
    }
}


Top
 Profile  
 
PostPosted: Sat Jun 09, 2012 10:46 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
In your code, you have the line "posi.remove(posi.size()-1);", and as you state, this will always remove the last stone position added. To remove a specific object in that array, you can use posi.remove(the position I want to remove). This is the code you use for "stone.remove(stoneBlock);".

This brings me to something that you didn't ask, but I think I should point out anyway. Currently, you have an array list for images, and an array list for their positions. Handling two different arrays could prove inconvinient, and will get quite frustrating if you add more "stone block"s to your game. I suggest you make a separate class that stores both the image, and the position of each stone block. Then, you can create a single array in which each object will be able to give you both the position and the image that it relates to.

consider (may have syntax errors):

public class Block(){
public Image blockImage;
public Point position;

public Block(Image img, Point pos){
this.blockImage=img;
this.position=pos;
}

}

ArrayList<Block> blocks = new ArrayList<Block>();

public void update(GameContainer gc, int delta)
throws SlickException
{

input = gc.getInput();
gx = input.getMouseX()-ghost.getWidth()/2;
gy = input.getMouseY()-ghost.getHeight()/2;


if(input.isKeyDown(Input.KEY_B) && input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {

blocks.add(new Block(stoneBlock, new Point(gx,gy)));
}

if(input.isKeyDown(Input.KEY_P) &&
input.isMousePressed(Input.MOUSE_LEFT_BUTTON) &&
stone.size() > 0)
{
block target;

//insert code that figures out which block you want to remove
//and sets target to that block;

blocks.remove(target);

}

}

_________________
"Artificial intelligence will never be a match for human stupidity" - "Jamos Kennedynos"


Top
 Profile  
 
PostPosted: Sat Jun 09, 2012 11:09 pm 
Offline

Joined: Sat Jul 30, 2011 11:33 am
Posts: 60
yeah this was more of a quick thingy but how would i know the index of the block the mouse is over thats like my main question :)


Top
 Profile  
 
PostPosted: Sun Jun 10, 2012 8:58 pm 
Offline
Oldbie
User avatar

Joined: Thu Jan 13, 2011 4:42 pm
Posts: 349
Iterate through the list and perform collision detection between each element and the mouse.

_________________
"Artificial intelligence will never be a match for human stupidity" - "Jamos Kennedynos"


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group