Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (306)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Detecting mouseclick on field in grid  (Read 247 times)
0 Members and 1 Guest are viewing this topic.
Offline Reck

Senior Newbie





« Posted 2012-11-29 22:34:04 »

I've made this code that successfully creates a 16x12 grid by 50x50 squares on a 800x600px board.
As you can see, the player moves to the coordinates of the players mouseclick.

Each square of the grid has an object of Felt (field) on it, which can be laast (locked). If a fields lock attribute is set to 1, the player should not be moved to that position.

How do i detect the field a player tries to move on to achieve this?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
public class SimpleGame extends BasicGame{
 
    private Image plane;
    private float planeX;
    private float planeY;
   
    public SimpleGame()
    {
        super("SpilTest");
    }
 
    @Override
    public void init(GameContainer gc) throws SlickException {
        plane = new Image("figur.png");
    }
 
    @Override
    public void update(GameContainer gc, int delta) throws SlickException {
        Input input = gc.getInput();
               
        if (input.isMousePressed(input.MOUSE_LEFT_BUTTON)) {
            this.planeX = input.getMouseX() - 30;
            this.planeY = input.getMouseY() - 50;
        }
       
    }
 
    public void render(GameContainer gc, Graphics g) throws SlickException {
       
        Felt board[][] = nytGrid();
       
        int distancex = 0;
        int distancey = 0;
        int counter = 0;
       
        for (int i=0; i < board.length ; i++) {
            for (int j=0; j < board[i].length ; j++) {                
                if (board[i][j].getLaast() == 1) {
                    g.setColor(Color.red);
                    g.fillRect(distancex, distancey, 50, 50);
                }
               
                distancex += 50;
                counter++;
                if (counter == 16) {
                    distancey += 50;
                    distancex = 0;
                    counter = 0;
                }
            }
        }
       
        g.drawImage(plane, planeX, planeY);
       
    }
 
    public static void main(String[] args) throws SlickException {
       
         AppGameContainer app = new AppGameContainer(new SimpleGame());
 
         app.setDisplayMode(800, 600, false);
         app.setTargetFrameRate(60);
         app.start();
    }
   
    public Felt[][] nytGrid() {
       
        Felt [][] board = new Felt[16][12];        

        for (int i=0; i < board.length ; i++) {
            for (int j=0; j < board[i].length ; j++) {
                int x = i;
                int y = j;
                board[i][j] = new Felt(x, y);
               
                if (i == 5 && j == 5) {
                    board[i][j].setLaast(1);
                }
            }
        }
       
        return board;
    }
}
Offline RobinB
« Reply #1 - Posted 2012-11-29 23:37:24 »

With the code i suggested in your other thread Cheesy
Offline Reck

Senior Newbie





« Reply #2 - Posted 2012-11-29 23:47:46 »

I see, that got me closer :-D
1  
2  
int x = xpos - (xpos % gridsize);
int y = ypos - (ypos % gridsize);

How do i do the opposite of this? :-D
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline RobinB
« Reply #3 - Posted 2012-11-29 23:56:04 »

1  
2  
3  
4  
5  
6  
7  
//Position inside square
int rx = xpos % gridsize;
int ry = ypos % gridsize;

//square
int x = xpos - rx;
int y = ypos - ry;


rx is the position inside the square: 0 to gridsize-1
x is the selected square
if you want the index of the square you need to divide by the grid size:
1  
int x = (xpos - rx) / gridsize;


Same with y / ry
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
mrbenebob (8 views)
2013-06-19 14:55:23

BrassApparatus (15 views)
2013-06-19 08:52:37

NegativeZero (19 views)
2013-06-19 03:31:52

NegativeZero (21 views)
2013-06-19 03:24:09

Jesse_Attard (26 views)
2013-06-18 22:03:02

HeroesGraveDev (64 views)
2013-06-15 23:35:23

Vermeer (63 views)
2013-06-14 20:08:06

davedes (63 views)
2013-06-14 16:03:55

alaslipknot (57 views)
2013-06-13 07:56:31

Roquen (79 views)
2013-06-12 04:12:32
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!