Java Video Tutorial 60

import java.awt.*;

public class SpaceShip extends Polygon{

	// Determines the speed the ship moves
	private double xVelocity = 0, yVelocity = 0;
	// Get the board width and height
	int gBWidth = GameBoard.boardWidth;
	int gBHeight = GameBoard.boardHeight;
	// Center of space ship
	private double centerX = gBWidth/2, centerY = gBHeight/2;
	// Will hold the x & y coordinates for the ship
	// Everything is based on coordinates from the center
	// It is done this way so that rotation works properly
	public static int[] polyXArray = {-13,14,-13,-5,-13};
	public static int[] polyYArray = {-15,0,15,0,-15};
	// Width and height of ship
	private int shipWidth = 27, shipHeight = 30;
	// Upper left hand corner of space ship
	private double uLeftXPos = getXCenter() + this.polyXArray[0];
	private double uLeftYPos = getYCenter() + this.polyYArray[0];
	// Defines if the ship should rotate
	private double rotationAngle = 0, movingAngle = 0;
	// Keep track of whether SpaceShip is on screen
	public boolean onScreen = true;
	// Creates a new space ship
	public SpaceShip(){
			// Creates a Polygon by calling the super or parent class Polygon
			super(polyXArray, polyYArray, 5);
	// Gets & sets the values for the x & y center of ship
	public double getXCenter(){ return centerX; }
	public double getYCenter(){ return centerY; }
	public void setXCenter(double xCent){ this.centerX = xCent; }
	public void setYCenter(double yCent){ this.centerY = yCent; }
	public void increaseXPos(double incAmt) { this.centerX += incAmt; }
	public void increaseYPos(double incAmt) { this.centerY += incAmt; }	
	// Gets & sets the x & y for upper left hand corner of ship
	public double getuLeftXPos(){ return uLeftXPos; }
	public double getuLeftYPos(){ return uLeftYPos; }
	public void setuLeftXPos(double xULPos){ this.uLeftXPos = xULPos; }
	public void setuLeftYPos(double yULPos){ this.uLeftYPos = yULPos; }
	// Gets & sets the x & y for upper left hand corner of ship
	public int getShipWidth(){ return shipWidth; }
	public int getShipHeight(){ return shipHeight; }
	// Gets, sets, increases and decreases the ship velocity
	public double getXVelocity(){ return xVelocity; }
	public double getYVelocity(){ return yVelocity; }
	public void setXVelocity(double xVel){ this.xVelocity = xVel; }
	public void setYVelocity(double yVel){ this.yVelocity = yVel; }
	public void increaseXVelocity(double xVelInc){ this.xVelocity += xVelInc; }
	public void increaseYVelocity(double yVelInc){ this.yVelocity += yVelInc; }
	public void decreaseXVelocity(double xVelDec){ this.xVelocity -= xVelDec; }
	public void decreaseYVelocity(double yVelDec){ this.yVelocity -= yVelDec; }
	// Set and increase the ship movement angle
	public void setMovingAngle(double moveAngle){ this.movingAngle = moveAngle; }
	public double getMovingAngle(){ return movingAngle; }
	public void increaseMovingAngle(double moveAngle){ this.movingAngle += moveAngle; }
	// Calculate the ship angle of movement
	// This is trigonometry at work
	// By taking the cos of the angle I get the opposite value of x
	// Angle * Math.PI / 180 converts degrees into radians
	public double shipXMoveAngle(double xMoveAngle){
		return (double) (Math.cos(xMoveAngle * Math.PI / 180));
	// By taking the sin of the angle I get the adjacent value of y
	// Angle * Math.PI / 180 converts degrees into radians 
	public double shipYMoveAngle(double yMoveAngle){
		return (double) (Math.sin(yMoveAngle * Math.PI / 180));
	// Gets & increases or decreases the rotation angle of the ship
	// Increase the rotation angle by 5, but don't let it go above 360
	public double getRotationAngle(){ return rotationAngle; }
	public void increaseRotationAngle(){ 
		if(getRotationAngle() >= 355) { rotationAngle = 0; }
		else { rotationAngle += 5; }
	// Decrease the rotation angle by 5, but don't let it go below 0
	public void decreaseRotationAngle(){ 
		if(getRotationAngle() < 0) { rotationAngle = 355; }
		else { rotationAngle -= 5; }
	// NEW FIX Artificial rectangle that is used for collision detection
	public Rectangle getBounds(){
		return new Rectangle((int) getXCenter() - 14, (int) getYCenter() - 14, getShipWidth(), getShipHeight());
	// Gets the spaceships nose X vector -------------------
	public double getShipNoseX(){
		// x2 = this.getXCenter() + cos(rotationAngle) * 14
		return this.getXCenter() + Math.cos(rotationAngle) * 14;
	// Gets the spaceships nose Y vector --------------------
	public double getShipNoseY(){
		// x2 = this.getXCenter() + cos(rotationAngle) * 14
		// y2 = this.getYCenter() + sin(rotationAngle) * 14
		return this.getYCenter() + Math.sin(rotationAngle) * 14;
	public void move(){
		// Increase the x origin value based on current velocity 
		// If the ship goes off the board flip it to the other side of the board
		if(this.getXCenter() < 0){
		} else
			if (this.getXCenter() > gBWidth){
		// Increase the x origin value based on current velocity 
		// If the ship goes off the board flip it to the other side of the board
		if(this.getYCenter() < 0){
		} else
			if (this.getYCenter() > gBHeight){

Pages: 1 2 3 4 5

26 Responses to “Java Video Tutorial 60”

  1. mr.roshik says:

    Hi do u have any plan to make tutorial on magento or drupal for developing ecommerce site ? i think advanced step of this type tutorials are not available. so please …..

    • admin says:

      Not at this time because I made a free wordpress ecommerce tutorial that has worked perfectly for me for almost a year with a single problem. I don’t know of an iron clad free ecommerce solution other than what I covered in Create an Online Store. I don’t know if you’ve seen that tutorial or not?

  2. mr.roshik says:

    display none and visibility hidden , whts the difference of this two css property ? help me to know.

    • admin says:

      visibility:hidden hides an element, but it will still take up the same space as before and still affect the layout.

      display:none hides an element, it will not take up any space and the page will be displayed as if the element is not there

  3. mr.roshik says:

    Thanks a lot.

  4. Bilou says:

    Look man you did a great job ^^ I am glad because there is people like you alive :p I’ll be happier if you make more Java tutorials

    • admin says:

      More pure Java tutorials are right around the bend. My last tutorials on design patterns and UML are technically Java tutorials. You have to learn a bunch of things to be a great programmer. Refactoring using java is coming next. Thanks for stopping by 🙂

  5. MADLY says:

    Is there a .zip archive for the noises…

  6. bob says:

    I also do games long time ago using Java and J2ME. I just do simple box collission detection. I think circles are much easier because you just compute the distance between the 2 centers.

  7. Samuel Pius says:

    Hey Derek 🙂

  8. Umair says:

    Hi Derek,

    I liked this series of video game. I just wanted to ask is this include MVC(Model View Controller) pattern. My question is what is MVC and how did you achieved in this video game.

  9. Umair says:


    Is it true that C,C++ supports more graphics option than Java. This is what you told in one of the game video series.

  10. Umair says:

    Thank you in advance…

  11. Prakash says:

    Hi.. I am your great fan. I learnt java from your tutorial.. You are really awesome.. Keep going.

  12. Alex says:

    Woooooooooo!!!!!! first video game down!

    Just wanted to say thank you. I have learned so much through your tutorials. They may have also played a part in me getting a job, so congratulations you are feeding me now too.

Leave a Reply

Your email address will not be published.