Java Game Tutorial Series

Post all your tuts or request for tuts here.
Post Reply
Baseball435
Posts: 548
Joined: Sun May 30, 2010 3:49 am

Java Game Tutorial Series

Post by Baseball435 »

Hey everyone, I decided to start a series of Java tutorial videos on how to make games with Java. So far the first 3 videos have been released and am going to upload 2 or 3 more tomorrow considering I have nothing planned. So here are the first three (in order).. Oh btw, I tried to make these as easy to understand and see (video wise) as possible:

Java Game Tutorial - 1 - Setting Up
[youtube]http://www.youtube.com/watch?v=hm3kI_St6Xc[/youtube]

Java Game Tutorial - 2 - Adding A Player
[youtube]http://www.youtube.com/watch?v=b5xmxpaI4o8[/youtube]

Java Game Tutorial - 3 - Adding Movement
[youtube]http://www.youtube.com/watch?v=GCMm_O7JzR0[/youtube]

Java Game Tutorial - 4 - Adding Collision And Enemies
[youtube]http://www.youtube.com/watch?v=6PEfnkmvoj0[/youtube]

So I hope you guys find these helpful and I will be adding to these thread as I make more videos. Oh and if you could subscribe to my youtube channel, that would be awesome! :)

Thanks!
~Baseball435
Last edited by Baseball435 on Tue Jan 10, 2012 11:31 pm, edited 2 times in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Java Game Tutorial Series

Post by Jackolantern »

Very nice! I moved them to the Tutorials section, as that is the most relevant section ;)

I am actually getting into Java more myself lately (I studied it years ago, but kind of ran aground trying to think of something to use it for, but now I am aiming to make Android tablet games and apps). I am enjoying it so far, outside of the fact that I can't get Eclipse's code completion to pop-up the way I want it to (automatically and instantly the way Visual Studio does). It suggests good things, but I find it annoying to constantly be hitting Ctrl+Space to get the options up! I have fiddled with the options, and supposedly I have it set to work like VS, but it still doesn't! :x
The indelible lord of tl;dr
Baseball435
Posts: 548
Joined: Sun May 30, 2010 3:49 am

Re: Java Game Tutorial Series

Post by Baseball435 »

Oh thanks I forgot about the tutorials section xD

And yeah I love Java, great language to get into. And if eclipse isn't your type of style, try netbeans!
Baseball435
Posts: 548
Joined: Sun May 30, 2010 3:49 am

Re: Java Game Tutorial Series

Post by Baseball435 »

Video 4 is up! ^^^^^^^^^
User avatar
Zak Zillion
Posts: 112
Joined: Thu Apr 07, 2011 12:55 am

Re: Java Game Tutorial Series

Post by Zak Zillion »

I'm having a rather annoying problem. I've gone over almost every inch of videos 1-4 and I can't seem to fix this problem. I believe it is a KeyListener problem but I am unsure. The character won't move. I'll post my code below but I would really appreciate if you would post all of your source code for the tutorial series online. :)

Main.java

Code: Select all

import javax.swing.JFrame;


public class Main {
	
	
	public static void main(String[] args){
		JFrame frame = new JFrame("Test Game");
		frame.setSize(800, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.add(new GameFrame());
		frame.setVisible(true);
	}
	
}
GameFrame.java

Code: Select all

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;


public class GameFrame extends JPanel implements ActionListener {
	
	Timer mainTimer;
	Player player;
	
	int enemyCount = 5;
	
	static ArrayList<Enemy> enemies = new ArrayList<Enemy>();
	Random rand = new Random();
	
	public GameFrame() {
		setFocusable(true);
		
		player = new Player(100, 100);
		addKeyListener(new KeyAdapt(player));
		
		mainTimer = new Timer(10, this);
		mainTimer.start();
		
		for(int i=0; i < enemyCount; i++) {
			addEnemy(new Enemy(rand.nextInt(800), rand.nextInt(600)));
		}
	}
	
	public void paint(Graphics g){
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		
		player.draw(g2d);
		for(int i =0; i < enemies.size(); i++) {
			Enemy tempEnemy = enemies.get(i);
			tempEnemy.draw(g2d);
		}
	}
	
	
   @Override
	public void actionPerformed(ActionEvent arg0) {
		player.update();
		repaint();
	}
	public void addEnemy(Enemy e){
		enemies.add(e);
	}
	
	public static void removeEnemy(Enemy e) {
		enemies.remove(e);
	}
	
	public static ArrayList<Enemy> getEnemyList(){
		return enemies;
	}
}
Entity.java

Code: Select all

import java.awt.Graphics2D;


public class Entity {
	int x,y;
	public Entity(int x, int y){
		this.x = x;
		this.y = y;
		
	}
	public void update() {

	}
	
	public void draw(Graphics2D g2d){
		
	}
	
}
Player.java

Code: Select all

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Player extends Entity {

	int velX = 0, velY = 0;
	int speed = 2;
	public Player(int x, int y) {
		super(x, y);
	}
	
	@Override
	public void update() {
		x = x + velX;
		y = y + velY;
		checkCollisions();
	}
	
	@Override
	public void draw(Graphics2D g2d){
		g2d.drawImage(getPlayerImg(), x, y, null);
		//g2d.draw(getBounds());	
	}
	
	public Image getPlayerImg() {
		
		ImageIcon ic = new ImageIcon("C:/Users/Alex/Desktop/Player.png");
		return ic.getImage();
	}

	public void KeyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if(key == KeyEvent.VK_W) {
			velY = -speed;
		}
		else if(key == KeyEvent.VK_S) {
			velY = speed;
		}
		else if(key == KeyEvent.VK_A) {
			velX = -speed;
		}
		else if(key == KeyEvent.VK_D) {
			velX = speed;
		}
	}
	
	public void KeyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		
		if(key == KeyEvent.VK_U) {
			velY = 0;
		}
		else if(key == KeyEvent.VK_S) {
			velY = 0;
		}
		else if(key == KeyEvent.VK_A) {
			velX = 0;
		}
		else if(key == KeyEvent.VK_D) {
			velX = 0;
		}
	}
	
	public void checkCollisions() {
	ArrayList<Enemy> enemies = GameFrame.getEnemyList();
	
	for(int i=0; i < enemies.size(); i++){
		Enemy tempEnemy = enemies.get(i);
		
		if (getBounds().intersects(enemies.get(i).getBounds())) {
			GameFrame.removeEnemy(tempEnemy);
		}
	}
	}
	
	public Rectangle getBounds() {
		return new Rectangle(x, y, getPlayerImg().getWidth(null), getPlayerImg().getHeight(null));
	}
}
KeyAdapt.java

Code: Select all

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class KeyAdapt extends KeyAdapter{
	
	Player p;
	
	public KeyAdapt(Player player) {
		p = player;
	}
	
	public void KeyPressed(KeyEvent e) {
	p.KeyPressed(e);
	}
	
	public void KeyReleased(KeyEvent e) {
	p.KeyReleased(e);	
	}

}
Enemy.java

Code: Select all

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.ImageIcon;


public class Enemy extends Entity {

	public Enemy(int x, int y) {
		super(x, y);
	
	}
	
	public void update() {

	}
	
	public void draw(Graphics2D g2d){
	g2d.drawImage(getEnemyImg(), x, y, null);
	//g2d.draw(getBounds());
	}
	
	public Image getEnemyImg() {
		
		ImageIcon ic = new ImageIcon("C:/Users/Alex/Desktop/Player.png");
		return ic.getImage();
	}
	
	public Rectangle getBounds() {
		return new Rectangle(x, y, getEnemyImg().getWidth(null), getEnemyImg().getHeight(null));
	}

}
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - Albert Einstein

Old Project(s):
The Dark Flame
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Java Game Tutorial Series

Post by hallsofvallhalla »

I am currently working with java so i plan on watching these. Thanks for doing them.
Post Reply

Return to “Tutorials”