2D Game - Java - Movement, Drawing and general help

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 12864 Views - Last Post: 28 October 2012 - 05:10 PM Rate Topic: -----

#1 NemY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-January 10

2D Game - Java - Movement, Drawing and general help

Posted 09 April 2011 - 03:36 AM

I've gotten to a point where I think I'm confident enough with Java to start making a game. I hit a problem though, in the process of messing around with Java I realized I didn't know how to use the JFrame stuff or draw my players/surroundings. I took a look into it yesterday and couldn't make any headway. This is my code currently:

Main.java
package com.herocraftonline.rightlegred.minescroller;

import java.awt.Font;
import java.awt.Graphics;

import javax.swing.*;

import com.herocraftonline.rightlegred.minescroller.player.Player;

public class Main extends JFrame{
    public Main(){
        setTitle("MineScroller");
        setSize(500,500);
        setVisible(true);
        setFont(new Font("Minecraft Regular",Font.PLAIN,20));
    }


    public void paint(Graphics g){
    }

    public static void main(String[]beans){
        Main m = new Main();
        Player p = new Player(60, 60, 3);
        m.paint(p.getGraphics());
    }
}




Player.java
package com.herocraftonline.rightlegred.minescroller.player;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JComponent;


public class Player extends JComponent{
    private int x, y;
    private int width, height = 40;
    private Image img;
    private int Health;

    public Player(int x,int y,int Health) {
        this.x = x;
        this.y = y;
        this.Health = Health;
    }
    
    public void paint(Graphics g){
        g.drawRect(x, y, width, height);
    }
}




Player is what I'm trying to draw, but I can't seem to get it to work. I get no errors, while I do get a see through screen which isn't as pleasing as the player would be.


As well as the above, I must ask another question -

Fluid movement, how can I achieve this in my game? I know doing y++; won't give you fluid movement and that I'm going to have to use velocity to create this. Would it be something like this:


int yVel;
int xVel;
y += yVel;
x += xVel;




I'm aiming to create smooth movement, which is pleasing to the eye and generally makes the game nicer to play. I've posted this thread here instead of Java because this is for a game I'm attempting to make. So I assumed it would probably be best to post this here. Sorry if this is wrong.



Please remember this is my first game. Please don't be harsh and yes, I'm attempting to copy the style of Minecraft. But in 2D side scroller. While this is a bad thing to do, this game is intended to help me get my foot in the door, nothing else.

This post has been edited by NemY: 09 April 2011 - 03:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: 2D Game - Java - Movement, Drawing and general help

#2 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: 2D Game - Java - Movement, Drawing and general help

Posted 09 April 2011 - 07:10 AM

Usually game system's use Thread/Timer to do a GameLoop and then repaint(). So you can start by making a Thread that calls some function that does your game logic, then asks the screen to repaint(), then sleeps. You'd have to overload the paint(Graphics g) function to draw all your object's.
An example:
//member variable
boolean quitGame = false;
Thread gameThread = new Thread(this);
gameThread.start();

//overloaded paint
public void paint(Graphics g)
{
  super.paint();
  p.paint(g);
}

public void updateGame()
{
  p.moveX(p.getXVel());
  p.moveY(p.getYVel());
  //Or you can do movement based on keyboard/mouse/gamepad input.
  //Lots of other stuff for a real game
}

public void run()
{
  while(!quitGame)
  {
    updateGame();
    repaint();
    try{
     gameThread.sleep(30);
    }catch(Exception e){e.printStackTrace();}
  }
}



Just an example. I personally have my entities not extend anything Swing related, and just have them draw to screen when paint is called.

I've heard countless times that Swing isn't thread-safe, but when I tried using timers for either updategame() or repaint(), the game got all choppy.

In hindsight, I'd use doubles to store x/y position x/y velocity. Using ints you can't do small increases in speed/position.

This post has been edited by Greltam: 09 April 2011 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 Deviara  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 69
  • Joined: 02-October 10

Re: 2D Game - Java - Movement, Drawing and general help

Posted 16 April 2011 - 10:15 PM

I also recently started java game development (just 2 days ago as a matter of fact), and I'm really liking my progress. I recommend having a look at the first couple of tutorials found here: http://zetcode.com/t...agamestutorial/

Afterwards it's your choice, you can read on or jump right into it and try and start playing around with things on your own (which is what I'm doing).
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: 2D Game - Java - Movement, Drawing and general help

Posted 17 April 2011 - 05:26 AM

On a side note about getting your foot in the door you will need a 3D demo far more complicated than this (unless maybe you intend to work for a mobile game dev company, in which case you should have it running on an appropriate device)! Just thought I should give you the heads-up. :)
Was This Post Helpful? 0
  • +
  • -

#5 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 06:21 AM

While ButchDean is correct, not many commercial games are done in 2D nowadays. I fiercely recommend starting game programming with 2D and not 3D graphics. If you have cash to spare, I would recommend perhaps getting this book: http://fivedots.coe.psu.ac.th/~ad/jg/
I started Java game programming with it and I found it very useful, the Java3D API itself is a little dated so I recommend (If you really enjoy game programming and are willing to put the effort in), Implement this engine: http://jmonkeyengine.com/
Its free and I find it very useful. You could also skip the book and look at tutorials for 2D Game programming with the JMonkey Engine.

I hope this helps,
v0rtex
Was This Post Helpful? 0
  • +
  • -

#6 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 09:33 AM

Making fun games is what will get your foot in the door. 2D is often a great way to start out, as you can learn alot about making games without the added complexity. I've seen it very often that beginners will bite off too much either in choosing 3D or too large of a scope for their first project. It often leads to taking far longer to finish the project, if at all in many cases.

Yesterday I booted up my xbox 360 and I saw Fancy Pants Adventures on Xbox Live. I remember first seeing that as a 2D flash game. Now it's a 2D xbox 360 game.

There are plenty of quality 2D games on the market in all areas. Fun should be your focus.

Not discouraging you from learning 3D, but rather consider what you are trying to accomplish with a first game.
Was This Post Helpful? 1
  • +
  • -

#7 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,175
  • Joined: 14-February 08

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 09:59 AM

You need to learn C++ to get into the games industry, there is no way around it. Java is a good place to start though but you should be thinking about making these games in C++ once you feel more confident with programming.

We had a guy from Aardvark Swift come to our university to give us tips on getting into the industry and they had conducted research with a lot of game development companies, asking them questions about what they look for in programmers.

You know what topped the list for almost all companies? You guessed it, C++.

I don't think your game has to be 3D it just has to be polished and written in C++ to a good standard before companies will consider you.
Was This Post Helpful? 1
  • +
  • -

#8 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 10:25 AM

Depending on what type of games you want to make, you don't need to learn C++ to get into the games industry.

While that is true for Console games, there are so many jobs in games in other parts of the market like casual games and mobile games.

Casual Games Market

Companies like EA are investing money into development of casual PC/Mobile games. Most of them are not done in C++.

Ever hear of Zynga ? They account for 10 Billion dollars of the games market and hire flash developers for as much as 150k.


Iphone/Ipad/Itouch -- Objective-C, Lua, AS3

Android ?

There is great opportunity in so many areas relating to games right now.

I'd suggest that you can make more money quicker outside of consoles, so it shouldn't be seen as the only option.

Base your decisions on what your goals are. If your goal is to make console games, you'll use a different hammer than if you were looking to make games for another area of the market.

This post has been edited by Prototypical: 23 April 2011 - 10:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,175
  • Joined: 14-February 08

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 12:24 PM

You make good points, but if you want make triple A titles some day, like I usually assume when someone says they want to get into the games industry, then you have to learn C++.
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 12:43 PM

View PostGreltam, on 09 April 2011 - 10:10 AM, said:

Usually game system's use Thread/Timer to do a GameLoop and then repaint(). So you can start by making a Thread that calls some function that does your game logic, then asks the screen to repaint(), then sleeps. You'd have to overload the paint(Graphics g) function to draw all your object's.
An example:
//member variable
boolean quitGame = false;
Thread gameThread = new Thread(this);
gameThread.start();

//overloaded paint
public void paint(Graphics g)
{
  super.paint();
  p.paint(g);
}

public void updateGame()
{
  p.moveX(p.getXVel());
  p.moveY(p.getYVel());
  //Or you can do movement based on keyboard/mouse/gamepad input.
  //Lots of other stuff for a real game
}

public void run()
{
  while(!quitGame)
  {
    updateGame();
    repaint();
    try{
     gameThread.sleep(30);
    }catch(Exception e){e.printStackTrace();}
  }
}



Just an example. I personally have my entities not extend anything Swing related, and just have them draw to screen when paint is called.

I've heard countless times that Swing isn't thread-safe, but when I tried using timers for either updategame() or repaint(), the game got all choppy.

In hindsight, I'd use doubles to store x/y position x/y velocity. Using ints you can't do small increases in speed/position.


Threads are actually one of the WORST solutions with a Swing-based architecture because Swing isn't thread safe. You need to use a javax.swing.Timer object instead to sync the threads.


View Poststayscrisp, on 23 April 2011 - 12:59 PM, said:

You need to learn C++ to get into the games industry, there is no way around it. Java is a good place to start though but you should be thinking about making these games in C++ once you feel more confident with programming.

We had a guy from Aardvark Swift come to our university to give us tips on getting into the industry and they had conducted research with a lot of game development companies, asking them questions about what they look for in programmers.

You know what topped the list for almost all companies? You guessed it, C++.

I don't think your game has to be 3D it just has to be polished and written in C++ to a good standard before companies will consider you.


I don't think you HAVE to use C++ in gaming anymore. Both Java and C# are used in gaming these days - albeit, they may not be as popular, BUT they can be and are being used. Java is not slow like it used to be:
http://www.dreaminco...ally-that-slow/

And, there are some really great Java games out there that are remarkaby successful:
http://www.dreaminco...ost__p__1149292
Was This Post Helpful? 0
  • +
  • -

#11 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 12:50 PM

Well, what defines a AAA title ?

How much money it makes ?

The graphics ?

The gameplay ?

The platform ?

This is probably the wrong thread for this discussion. But seriously, I see Angry Birds as a AAA title based on it's success. It's not a console game.

I think you are suggesting that a AAA console title is the holy grail for everyone. For a console gamer, it just might be.

I am a console gamer, but have a wider area of interest that I definitely see AAA titles as non-exclusive to any one market in the games industry.

I'm pretty sure that EA viewed Angry Birds as a AAA title when they bought Chilingo for 20 million. They didn't even get the ownership of the Angry Birds property, which I'm sure was valued much higher.

So it's really about how you define AAA.
Was This Post Helpful? 0
  • +
  • -

#12 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,175
  • Joined: 14-February 08

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 01:56 PM

Certainly the casual market is huge and should not be ignored but the point I was trying to make is that most people who ask how to get into the industry want to make the big hitters like call of duty or MMORPG's and the like. For these games you need to know C++.

Don't get me started on Angry Birds!
Was This Post Helpful? 0
  • +
  • -

#13 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 04:14 PM

Well, I think that you might be projecting your own wants on others when you say "most".

People who like Call of Duty and people who like MMORPG's want to make games of that nature. It's really all relative to your interest. It's no different as there are some people that want to be a football player or a basketball player when they grow up.

The whole "most" concept even if it was accurate, still doesn't erase the point that C++ and console games is not the only way to make it in the game industry -- which is the only reason I even commented.

There's literally hundreds of billions of dollars in the industry that have nothing to do with what you consider a AAA title. So, putting it all in that little box as C++ and/or 3D is the only way is quite misleading.
Was This Post Helpful? 0
  • +
  • -

#14 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,175
  • Joined: 14-February 08

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 04:36 PM

I don't think I'm doing anything of the sort, I will admit that I didn't take the casual games market into account with my first comment but I then went on to clarify what I meant, and I believe it to be pretty much accepted that the definition of a triple A title is a game with a big budget made by a large team of developers. It's really just a bit of a marketing/buzzword really.

I'm going to stand by what I have posted and recommend that if you want to get into mainstream development with the likes of Epic/Valve/Nintendo then learn C++.
Was This Post Helpful? 0
  • +
  • -

#15 Prototypical  Icon User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: 2D Game - Java - Movement, Drawing and general help

Posted 23 April 2011 - 06:24 PM

I can live with that. I think it's accurate to say that a AAA title is commonly seen as large budget and large development team.

That would make Waterworld a AAA movie, but I'll have to get used to that!

Lots of quality opportunities in games out there that don't involve C++, and are generating billions of dollars in revenue. There has NEVER been greater opportunity to make games without C++ and make good money than right now. That's a fact. :)

Sorry for taking this thread down this discussion path, I know it's not the topic at hand.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2