Hot File

Basic Instructions Verify Java Game programming

View: 1674    Dowload: 2   Comment: 0   Post by: hanhga  
Author: none   Category: Java Sun   Fields: Other

9 point/4 review File has been tested

Download   basic-instructions-verify-java.zip (20.99 KB)

You need to Sign In to download the file basic-instructions-verify-java.zip
If you do not have an account then Sign up for free here

Download error   Report copyright violation

I don’t know how exactly to start or make an introduction for this post. I just made in to the concept of designing a simple fighting game tutorial - from doing the character animation to its overall functionality.

Introduction

I don’t know how exactly to start or make an introduction for this post. I just made in to the concept of designing a simple fighting game tutorial - from doing the character animation to its overall functionality. This tutorial is intended for beginners who really enjoy experimenting with JAVA. No, I’m not an expert game programmer. I can’t promise to owe you a good working game engine. But this tutorial will surely give you the basic ideas on how to implement a simple game in JAVA. Please bear in mind that there are couple ways of doing this, my approach is not a standard design pattern for game programming

Before you start jiving with this tutorial, I am making assumption that you are already aware about creating basic JAVA GUI Applications. You may be asking “what’s going on in here?” Aargh!!!

Without further much ado, here we go...

Requirements:
1. Java Eclipse IDE.
2. Basic - Intermediate knowledge in Java programming.
3. Ability to think outside the box.

Part I – Let’s animate a character

The initial part of this tutorial will cover on how to animate a character (not to mention loading and displaying the image). In this part you will learn how to use Swing component classes such as “JFrame”, “JPanel”, “JLabel” and “ImageIcon”.

ANIMATECHAR

The first thing we need to do is making a game screen where we can actually design the background, load and display image, and so on. Open File ->>New ->>Java Project. Create a name for the project then finish. Start adding a Class then names it as “AnimateCharacter”.

Let’s start coding inside the class “AnimateCharacter” inheriting the “JFrame” class.

import javax.swing.JFrame;
 
public class AnimateCharacter extends JFrame {

Declare global variables for the screen height and width. These variables will initialize the constant value of the “JFrame” height and width.

final int iScreenHeight = 300;
final int iScreenWidth = 350;

Now setup the method for our game screen on how should be the close operation - will it exit on close or do nothing, the window size, is it resizable, location centered and must be visible. Note that if you want to kill a zombie from running always include “setDefaultCloseOperation()”for closing the window or “System.exit()” in exiting a program.

void SETUP_THE_WORLD(){
            setSize(iScreenWidth,iScreenHeight);
            setResizable(false);
            setLocationRelativeTo(null);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
      }

In the main method declare a new object variable naming “display” for calling the “SETUP_THE_WORLD()” method..

public static void main(String args[]){  
            AnimateCharacter display = new AnimateCharacter();   
display.SETUP_THE_WORLD();
}
}

And there we have our Game Screen...

Ok! Warm up done, time to get your hands a little dirty. Now let’s import all necessary classes needed. Having a working code is one thing but knowing how to use import classes properly is big plus.

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.BorderLayout;

Below are the variable declarations to be added on the global variables inside the “AnimateCharacter” class. This is just a continuation of what we have done earlier. This time we need a surface or canvass to hold the objects of the game which is the “JPanel”. And the “JLabel” class here is used as the container for the “ImageIcon” to be display in the screen. There’s no need to further explain these variable declarations line by line, for it is self-explanatory.

private static final long serialVersionUID = 1L;
 
JPanel BackgroundPanel = new JPanel();
      JLabel ImageHolder = new JLabel();
      ImageIcon imgLuffyLeft[] = new ImageIcon[2];
      ImageIcon imgLuffy;
 
      int iLuffyStandCounter;
      int iLuffyStandIndex;
 
final int iScreenHeight = 300;
      final int iScreenWidth = 350;
      final int iImageWidth = 75;
      final int iImageHeight = 100;
 
      int iHorizontalX = 130;
      int iVerticalY = 100;
 
      boolean bGameStart = true;

Create a constructor that will set the title for the game screen, shows a pop up message using “JOptionPane” class as an introduction and initiate an array of image to be stored in “imgLuffyLeft” variable using for-loop.

public AnimateCharacter()
      {
      super("Character Animation by Mark Cabalar");
      JOptionPane.showMessageDialog(null,"Let's animate a character...","Part I",1);
 
      for(int i=0;i<2;i++)
            imgLuffyLeft[i] = new ImageIcon(getClass().getResource("/"+(i+1)+".png"));
      }

We used “getClass.getResource()” here to search the path of the source classes included in the project. To make this work we must setup first our project properties and build a path for the class folder to be added in the libraries. Right click on the project then click on properties.

animation

Click on the “Java Build Path” and look for “Libraries”.

java build path

Then click on “Add Class Folder” go to your project folder, find and check the folder where you place the images and just click “OK”.

add class

Back to the game screen, notice that the declared “JPanel” variable “BackgroundPanel” is already added in the “JFrame”. It will hold the images and labels to be display in the game screen.

void SETUP_THE_WORLD()
      {
            ImageHolder.setBounds(iImageWidth,iImageHeight,iImageWidth,iImageHeight);
            BackgroundPanel.add(ImageHolder);
            BackgroundPanel.setLayout(null);
            BackgroundPanel.setBackground(Color.GRAY);
            add(BackgroundPanel, BorderLayout.CENTER);
            setSize(iScreenWidth,iScreenHeight);
            setResizable(false);
            setLocationRelativeTo(null);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }

Here we’re going to create a method naming CHARACTER_IMAGE_ANIMATION() which will control the count of flipping images

 void CHARACTER_IMAGE_ANIMATION()
      {
            iLuffyStandCounter +=1;
 
            if(iLuffyStandCounter> 60)
                  iLuffyStandCounter = 0;
            elseif(iLuffyStandCounter< 30)
                  iLuffyStandIndex = 0;
            elseif(iLuffyStandCounter< 60)
                  iLuffyStandIndex = 1;
            else
                  iLuffyStandCounter = 0;
 
            iLuffyStandCounter +=1;
 
            imgLuffy = imgLuffyLeft[iLuffyStandIndex];
            ImageHolder.setIcon(imgLuffy);
            ImageHolder.setLocation(iHorizontalX,iVerticalY);
      }
 
      public static void main(String args[]) throws InterruptedException
      {
                  AnimateCharacter display = new AnimateCharacter();
                  display.SETUP_THE_WORLD();
                  do{
                        display.CHARACTER_IMAGE_ANIMATION();
                        Thread.sleep(10);
                  }while(display.bGameStart);
      }
}

 

Basic Instructions Verify Java Game programming

Basic Instructions Verify Java Game programming Posted on 25-12-2015  I don’t know how exactly to start or make an introduction for this post. I just made in to the concept of designing a simple fighting game tutorial - from doing the character animation to its overall functionality. 2.25/10 1674

Comment:

To comment you must be logged in members.

Files with category

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd