I am currently developing a game on Java and I am required to have GUI elements for it (using JavaFX). The game will be similar to Snakes And Ladders. There must a zig zag board.During the game play, after a dice roll, a player will have to move. I am not sure what would be the best solution when it comes to having a grid (board) on the game. What I need is to have separate indexes on every squares, so I could tell game that this is just a regular square, or the square that you stepped on has a specific function to do (for example go back few steps). Would it be the best solution to implement this game using GridPane to draw the board? Thank you.
Well, the best thing for you would be to go with GridPane of a size for example 16x16 with setting your own ColumnConstraints and RowConstraints for it which will resemble a board on which snake can move.
As for game logic, you can implement that each index of a board( GridPane ) will have a child of a special object( call it Field ) which will hold Rectangle, shape with width and height of that board index, and some specific logic regarding feature for that field.
Bare in mind that gridpane and buttons resembling logic of a game, draw dice and similar, should be children of AnchorPane.
Link:
ColumnConstraints, RowConstraints
Related
I'm working on a card game and I'm stuck on how to proceed with giving a simple visual feedback when the players are interacting with the cards that are placed on board.
As you can see in the image I'd like to implement drawing arrows between the cards that interact with different object on the board (for example when the player attacks with a starship a arrow will appear between the ship and its target). When the player commits his move, the arrow needs to be drawn on the opponents gui aswell when the opponent starts his defending phase.
The whole client gui aswell as the cards are coded with swing library and are JPanels with various layouts. The cards is one class and the board gui is composed of several gui classes (handGui, heroGui, unitFieldGui etc) added together in a mainWindow gui class.
I don't really know how to proceed to achieve this kind of visual feedback and would appreciate if you could point me in the right direction, what kind of technique would help me solve this problem?
Check out the section from the Swing tutorial on Glass Panes. a Glass Pane is a component that is painted over top of the frame and you can do custom painting.
You can use the SwingUtilities.convertPoint(...) method to convert the two card components location relative to the Glass Pane. Then you do your custom painting to draw the arrow from one component to the other.
So I am trying to create a 2D side-scrolling javafx game.
So far I used AnimationTimer to control movement of my character. But now I am kind of stuck trying to make the stage move.
I can move non-interactive elements using AnimationTimer again. But I am lacking an idea for how should I generate interactive elements in game.
For example, lets say player walks a lot of steps and reaches to take a pickup. Now how do I put this pickup in stage so it is somewhere later in game. To try explain my problem better, consider this pesky image I drew in paint:
Initially, only the screen between green bounds is visible to player. The player must walk forward (and hence the screen must walk forward too) and should find that pickup between two walls. How do I place pickup outside scene's visible view so it comes into view only when player reaches it?
The easy way: You add everything to the scene and give it absolute coordinates. You move the player by changing its coordinates in the scene. Depending on the position of the player you start scrolling. While you scroll the background you also move all other objects about the same x and y coordinates. The visible view has a given width and height. Depending on the player's position, view width/height and object range the objects become visible during scrolling.
So I'm creating a 2D game platform that works with Tiles. Currently it won't let the player go through the any tiles and works fine. Though rather then stopping the player at a solid tile. I would like to stop the player at an actual object. Pretend the triangle is in a tile.
Whats Happening:
What I want:
I would like the player to be able to walk through the tile until their is no more transparents. Basically walk on the triangle.
Player Class http://pastebin.com/SJrzSvVV
Tile Class http://pastebin.com/V3nqxh61]
TileMap Class http://pastebin.com/fuj8dR5K
You should check out the intersects() method provided by the Java2D API.
You can define the two sprites as shapes and adjust the coordinates when the two shapes intersect. I'm guessing you are splitting up a BufferedImage to create the animation frames.
You can draw a Rectangle or Ellipse frame around your sprites and check those for collisions. The frames do not have to be visible but making them visible helps when debugging.
Figured it out, essentially what I did was I was creating this game using tiles. I created something to scan a tile and find out where it's transparent, and allow players to go through it or not depending on whether the transparency is already inside something in the tile or not.
I'm creating a civilization game for a project just wondering what the best way to do it when it reads in a .map file with each line having characters inside of it representing a terrian
so for example t000="aaaaaaaaaaaaaaaaaaaaaaa"
would be the terrain for the arctic.
I'm just asking what the best method to place this into a view in java SWING, to view the map because what I'm doing right now is creating 2d array of jbuttons and placing the terrain image in those, but I'm thinking it would mess me up when I start adding sprites to the map.
Can anyone help?
Consider using ImageIcons for each terrain's background, and then perhaps creating a grid of JLabel, each holding the ImageIcon that corresponds to the correct terrain.
Edit
If your sprite is to remain on a tile, then add it to the tile as a component (just be sure to give the JLabel a decent layout manager). If the sprite is to move over the tiles, then it should be drawn in a different layer, either on the top level window's glasspane or in a JLayeredPane.
I'd like some advice regarding structure of a game I'm working on. Specifically where to place painting methods.
Currently there is a applet wrapper class for a Jpanel which runs the game loop.
The game itself is meant to simulate a very large area. objects will have x&y values which themselves will part of a larger x&y grid.
i.e. object1 position is 150000x30000 in grid block 1,5.
objects will need to be able to move into neighbouring grids, however I'd rather not run each grid block until needed as 99% of them will be empty.
Currently the UI is a Jpanel with a few buttons + listeners, a large drawing pane is needed to display the objects.
my question is:
what class should this internal drawing pane be based on? I'd like to have control to zoom and pan around the grid. it only needs to draw what is visible, but object movements will continue in the game loop.
what painting strategy would be applicable for simple (icons really when zoomed out) moving around vast areas, I'm guessing relying on the EDT to repaint isn't going to be good enough?
I'm not really after specific code, I want to learn myself how to do this, I just need pointing in the right direction, as most things I read don't seam to quite cover what I'm after, or don't make use of JRE6+ features.
Many Thanks
Rather than paint each grid cell in your drawing pane, why don't you have each object repaint itself onto the grid?