Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 434 times)
0 Members and 1 Guest are viewing this topic.
Offline Reck

Senior Newbie





« Posted 2012-11-29 21: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

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #1 - Posted 2012-11-29 22:37:24 »

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

Senior Newbie





« Reply #2 - Posted 2012-11-29 22: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

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #3 - Posted 2012-11-29 22: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.

 

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

The first screenshot will be displayed as a thumbnail.

BurntPizza (22 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (90 views)
2014-09-10 06:39:09

Tekkerue (44 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (48 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (37 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!