Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Game development tutorials for Java beginners

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I am Java beginner learning by myself. I know a little bit of OOP, started to use Collections, trying to code Sorting and Searching Algorythms.
Now I am continue my learning but at the same time I wanna try to create a small video game. Sure it is too early for me to do serious project all by myself.
But I really want to get how it is to make video games.  

I read a little about games in Java, and realized that it is better to try LibGDX than Swing, the last one is somewhat outdated.
So I need some advice:
1. What technologies do I need to learn first to write Java games?
2. Please, recommend good tutorials for beginners, preferably steb-by-step, to develop a game in Java.  I saw some on the Internet, and I can try some of them ... but maybe someone has already tried it and will tell you really sensible?
3. If you have any tips for a beginner, I'd love to hear from you!
Thank you very much in advance!


 
Bartender
Posts: 242
27
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, welcome to the Ranch.

Game programming is quite complex and I'm by no means an expert, though I have programmed games in my free time. Here's a high-level overview:

What I'd recommend is using JavaFX as a base, with a Canvas to display the game screen. Create a simple GameObject which can render itself on a Canvas (retrieve the GraphicsContext2D and drawImage), and a List of GameObjects which belong to the game. You can use a JavaFX based timer which asks each GameObject to render itself and then advances the game by a frame (which can mean, for example, moving an object if the player is holding an arrow key). You can add event handlers to the game canvas to detect when the user does something like press the right/left key, and then propagate that message to all game objects. If you do something like this, you'll be able to create a simple Java-based game and you can make it more complex from there - for example JSON definition files for maps, switching between sprites to make animations, etc. For importing sprites, find some freeware ones online and add them to your Resources folder. Retrieve the resource as a Stream, and then pass that to the drawImage method on GraphicsContext2D.

Though really the most important thing is to just try it out yourself. Break apart what you want to do into simple tasks, and set some realistic goals. For example when I started making a game, my tasks were like this:
1) Make a JavaFX project
2) Add a game canvas to the project
3) Import a freeware sprite into the game
4) Render a sprite on the game window
5) Detect key presses on the canvas
6) Make the arrow keys move the player character
7) Add gravity to the player character
8) Add tiles to the game
.. and just build from there.
 
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your core intention is gaming, I would suggest looking at gaming engines instead of java. A gaming engine takes care of the game loop/graphics/physics etc.. for you. You would have to do all that from scratch in java.
Having said that, yes, javaFx (or even swing) can be used to create basic 2d games.
 
salvin francis
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a simple example for perspective ...
Let's say you want to animate a ball movement from left to right in Java. Let's say it has to move a fixed length of 200 pixels in around 10 seconds. Ideally, you would think that a simple loop calling Thread.sleep would work right ?
Here's the documentation for Thread.sleep:

javadocs wrote:Two overloaded versions of sleep are provided: one that specifies the sleep time to the millisecond and one that specifies the sleep time to the nanosecond. However, these sleep times are not guaranteed to be precise, because they are limited by the facilities provided by the underlying OS.
...
In any case, you cannot assume that invoking sleep will suspend the thread for precisely the time period specified.


This will affect how your game would run on different machines configurations. To overcome this, usually a game loop is implemented that gives a "delta" value for each time it is executed. I wont go into the details here, but that's how complicated it is to simply move a ball and ensure it works on the same speed on fast as well as slow machines.

Similarly, if you want to accept user input to move the ball left/right, you have program a way to communicate with that on-going game loop.
 
Mjm Chi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:If your core intention is gaming, I would suggest looking at gaming engines instead of java. A gaming engine takes care of the game loop/graphics/physics etc.. for you. You would have to do all that from scratch in java.
Having said that, yes, javaFx (or even swing) can be used to create basic 2d games.


Thank you! But if I understand clear, JavaFX is not a game engine, it is rather user interface library?
 
Mjm Chi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zachary Griggs wrote:Hi, welcome to the Ranch.

Game programming is quite complex and I'm by no means an expert, though I have programmed games in my free time. Here's a high-level overview:

What I'd recommend is using JavaFX as a base, with a Canvas to display the game screen. Create a simple GameObject which can render itself on a Canvas (retrieve the GraphicsContext2D and drawImage), and a List of GameObjects which belong to the game. You can use a JavaFX based timer which asks each GameObject to render itself and then advances the game by a frame (which can mean, for example, moving an object if the player is holding an arrow key). You can add event handlers to the game canvas to detect when the user does something like press the right/left key, and then propagate that message to all game objects. If you do something like this, you'll be able to create a simple Java-based game and you can make it more complex from there - for example JSON definition files for maps, switching between sprites to make animations, etc. For importing sprites, find some freeware ones online and add them to your Resources folder. Retrieve the resource as a Stream, and then pass that to the drawImage method on GraphicsContext2D.

Though really the most important thing is to just try it out yourself. Break apart what you want to do into simple tasks, and set some realistic goals. For example when I started making a game, my tasks were like this:
1) Make a JavaFX project
2) Add a game canvas to the project
3) Import a freeware sprite into the game
4) Render a sprite on the game window
5) Detect key presses on the canvas
6) Make the arrow keys move the player character
7) Add gravity to the player character
8) Add tiles to the game
.. and just build from there.



THank you!
 
Sheriff
Posts: 26798
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mjm Chi wrote:Thank you! But if I understand clear, JavaFX is not a game engine, it is rather user interface library?



Yes, that's a good description of JavaFX.
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic