Java Video Tutorial 50

Make a Video Game JavaI wanted to make the 50th part of my Java Video Tutorial special! In this tutorial, I answer numerous questions.

I begin showing you how to design a game like Asteroids. I start teaching OOP design principles using UML. You learn how to draw polygons on the screen and how to animate them. And, to top it all off I show you how collision detection works.

Yes, there is a lot in this 14 minute video 🙂

If you like videos like this, please tell Google [googleplusone]

I really appreciate it when you share

Code From the Video

Unified Modeling Language Rock Class Diagram

Unified Modeling Language : Class Diagram


uLeftXPos : integer
uLeftYPos : integer
xDirection : integer
yDirection : integer
boardWidth : integer
boardHeight : integer
polyXArray : integer[]
polyYArray : integer[]
sPolyXArray : integer[]
sPolyYArray : integer[]

Rock(polyXArray : integer[], polyYArray : integer[], pointsInPoly : integer, randomStartXPos : integer, randomStartYPos : integer) : void

move() : void

getpolyXArray(randomStartXPos : integer) : integer[]

getpolyYArray(randomStartYPos : integer) : integer[]


import java.awt.Polygon;

// Extending the Polygon class because I'm drawing Polygons

class Rock extends Polygon{
	// Upper left hand corner of the Polygon
	int uLeftXPos, uLeftYPos;
	// Used to change the direction of the asteroid when 
	// it hits something and determines how fast it moves
	int xDirection = 1;
	int yDirection = 1;
	// For JApplet
	// int width = ExampleBoard.WIDTH;
	// int height = ExampleBoard.HEIGHT;
	// Get the board width and height
	int width = Lesson50.boardWidth;
	int height = Lesson50.boardHeight;
	// Will hold the x & y coordinates for the Polygons
	int[] polyXArray, polyYArray;
	// x & y positions available for other methods
	// There will be more Polygon points available later
	public static int[] sPolyXArray = {10,17,26,34,27,36,26,14,8,1,5,1,10};
	public static int[] sPolyYArray = {0,5,1,8,13,20,31,28,31,22,16,7,0};
	// Creates a new asteroid 
	public Rock(int[] polyXArray, int[] polyYArray, int pointsInPoly, int randomStartXPos, int randomStartYPos){
		// Creates a Polygon by calling the super or parent class of Rock Polygon
		super(polyXArray, polyYArray, pointsInPoly);
		// Randomly generate a speed for the Polygon
		this.xDirection = (int) (Math.random() * 4 + 1);
		this.yDirection = (int) (Math.random() * 4 + 1);
		// Holds the starting x & y position for the Rock
		this.uLeftXPos = randomStartXPos;
		this.uLeftYPos = randomStartYPos;

	public void move(){
		// Get the upper left and top most point for the Polygon
		// This will be dynamic later on
		int uLeftXPos = super.xpoints[0]; 
		int uLeftYPos = super.ypoints[0];
		// If the Rock hits a wall it will go in the opposite direction
		if (uLeftXPos < 0 || (uLeftXPos + 25) > width) xDirection = -xDirection; 
		if (uLeftYPos < 0 || (uLeftYPos + 50) > height) yDirection = -yDirection; 
		// Change the values of the points for the Polygon
		for (int i = 0; i < super.xpoints.length; i++){
			super.xpoints[i] += xDirection;
			super.ypoints[i] += yDirection;
	// public method available for creating Polygon x point arrays
	public static int[] getpolyXArray(int randomStartXPos){
		// Clones the array so that the original shape isn't changed for the asteroid
		int[] tempPolyXArray = (int[])sPolyXArray.clone();
		for (int i = 0; i < tempPolyXArray.length; i++){
			tempPolyXArray[i] += randomStartXPos;
		return tempPolyXArray;
	// public method available for creating Polygon y point arrays
	public static int[] getpolyYArray(int randomStartYPos){
		// Clones the array so that the original shape isn't changed for the asteroid
		int[] tempPolyYArray = (int[])sPolyYArray.clone();
		for (int i = 0; i < tempPolyYArray.length; i++){
			tempPolyYArray[i] += randomStartYPos;
		return tempPolyYArray;

Leave a Reply

Your email address will not be published.