Author Topic: INTERMEDIATE JAVA GAME PROGRAMMING SOURCE CODE  (Read 11013 times)

Ollie

  • Administrator
  • Legendary Member
  • *****
  • Posts: 1541
    • View Profile
    • TheJavaHub
  • Programming Languages: Java, (X)HTML, CSS & PHP
INTERMEDIATE JAVA GAME PROGRAMMING SOURCE CODE
« on: October 27, 2011, 08:53:52 PM »
Here is the source code for the new series! Enjoy :)

Ollie

  • Administrator
  • Legendary Member
  • *****
  • Posts: 1541
    • View Profile
    • TheJavaHub
  • Programming Languages: Java, (X)HTML, CSS & PHP
Main.java:
Code: [Select]
package thejavahub;

import javax.swing.JFrame;

public class Main extends JFrame {
   
    GamePanel gp;
   
    public Main(){
        gp = new GamePanel();
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        add(gp);
    }
   
    public static void main(String[] args) {
        Main m = new Main();
    }
   
}

GamePanel.java
Code: [Select]
package thejavahub;

import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable {
    //Double buffering
    private Image dbImage;
    private Graphics dbg;
    //JPanel variables
    static final int GWIDTH = 500, GHEIGHT = 400;
    static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);
    //Game variables
    private Thread game;
    private volatile boolean running = false;
    private long period = 6*1000000; //ms -> nano
    //Game Objects
    World world;
   
    public GamePanel(){
        world = new World();
       
        setPreferredSize(gameDim);
        setBackground(Color.WHITE);
        setFocusable(true);
        requestFocus();
        //Handle all key inputs from user
        addKeyListener(new KeyAdapter(){
            @Override
            public void keyPressed(KeyEvent e){
                if(e.getKeyCode() == KeyEvent.VK_LEFT){
                    world.navigateMap(World.PAN_LEFT);
                }
                if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                    world.navigateMap(World.PAN_RIGHT);
                }
                if(e.getKeyCode() == KeyEvent.VK_UP){
                    world.navigateMap(World.PAN_UP);
                }
                if(e.getKeyCode() == KeyEvent.VK_DOWN){
                    world.navigateMap(World.PAN_DOWN);
                }
            }
            @Override
            public void keyReleased(KeyEvent e){
                world.stopMoveMap();
            }
            @Override
            public void keyTyped(KeyEvent e){
               
            }
        });
       
    }
   
    public void run(){
        while(running){
            gameUpdate();
            gameRender();
            paintScreen();
           
        }
    }
   
    private void gameUpdate(){
        if(running && game != null){
            world.moveMap();
        }
    }
   
    private void gameRender(){
        if(dbImage == null){ // Create the buffer
            dbImage = createImage(GWIDTH, GHEIGHT);
            if(dbImage == null){
                System.err.println("dbImage is still null!");
                return;
            }else{
                dbg = dbImage.getGraphics();
            }
        }
        //Clear the screen
        dbg.setColor(Color.WHITE);
        dbg.fillRect(0, 0, GWIDTH, GHEIGHT);
        //Draw Game elements
        draw(dbg);
    }
   
    /* Draw all game content in this method */
    public void draw(Graphics g){
        world.draw(g);
    }
   
    private void paintScreen(){
        Graphics g;
        try{
            g = this.getGraphics();
            if(dbImage != null && g != null){
                g.drawImage(dbImage, 0, 0, null);
            }
            Toolkit.getDefaultToolkit().sync(); //For some operating systems
            g.dispose();
        }catch(Exception e){
            System.err.println(e);
        }
    }
   
    public void addNotify(){
        super.addNotify();
        startGame();
    }
   
    private void startGame(){
        if(game == null || !running){
            game = new Thread(this);
            game.start();
            running = true;
        }
    }
   
    public void stopGame(){
        if(running){
            running = false;
        }
    }
   
    private void log(String s){
        System.out.println(s);
    }
}

World.java
Code: [Select]
package thejavahub;

import java.awt.*;
import javax.swing.ImageIcon;

public class World {
   
    private Rectangle[] blocks;
    private Image[] blockImg;
    private final int arrayNum = 500;
    //Block images
    private Image BLOCK_DIRT_TOP, BLOCK_DIRT, BLOCK_STONE, BLOCK_SKY;
   
    private int x, y, xDirection, yDirection;
   
    //Map navigation
    static final int PAN_UP = 0, PAN_DOWN = 1, PAN_LEFT = 2, PAN_RIGHT = 3;
   
    public World(){
        BLOCK_DIRT_TOP = new ImageIcon("C:/Users/Oliver/Documents/NetBeansProjects/TheJavaHub/src/thejavahub/images/tile_topDirt1.png").getImage();
        BLOCK_DIRT = new ImageIcon("C:/Users/Oliver/Documents/NetBeansProjects/TheJavaHub/src/thejavahub/images/tile_dirt1.png").getImage();
        BLOCK_STONE = new ImageIcon("C:/Users/Oliver/Documents/NetBeansProjects/TheJavaHub/src/thejavahub/images/tile_stone.png").getImage();
        BLOCK_SKY = new ImageIcon("C:/Users/Oliver/Documents/NetBeansProjects/TheJavaHub/src/thejavahub/images/tile_sky.png").getImage();
        blocks = new Rectangle[500];
        blockImg = new Image[500];
       
        loadArrays();
    }
   
    private void loadArrays(){
        for(int i = 0; i < arrayNum; i++){
            if(x >= 500){
                x = 0;
                y += 20;
            }
            if(i >= 0 && i < 100){
                blockImg[i] = BLOCK_SKY;
                blocks[i] = new Rectangle(x, y, 20, 20);
            }
            if(i >= 100 && i < 125){
                blockImg[i] = BLOCK_DIRT_TOP;
                blocks[i] = new Rectangle(x, y, 20, 20);
            }
            if(i >= 125 && i < 225){
                blockImg[i] = BLOCK_DIRT;
                blocks[i] = new Rectangle(x, y, 20, 20);
            }
            if(i >= 225 && i < 500){
                blockImg[i] = BLOCK_STONE;
                blocks[i] = new Rectangle(x, y, 20, 20);
            }
            x += 20;
        }
    }
   
    public void draw(Graphics g){
        for(int i = 0; i < arrayNum; i++){
            g.drawImage(blockImg[i], blocks[i].x, blocks[i].y, null);
        }
    }
   
    public void moveMap(){
        for(Rectangle r : blocks){
            r.x += xDirection;
            r.y += yDirection;
        }
    }
    public void stopMoveMap(){
        setXDirection(0);
        setYDirection(0);
    }
    private void setXDirection(int dir){
        xDirection = dir;
    }
    private void setYDirection(int dir){
        yDirection = dir;
    }
    public void navigateMap(int nav){
        switch(nav){
            default:
                System.out.println("default case entered... Doing nothing.");
                break;
            case PAN_UP:
                setYDirection(-1);
                break;
            case PAN_DOWN:
                setYDirection(1);
                break;
            case PAN_LEFT:
                setXDirection(-1);
                break;
            case PAN_RIGHT:
                setXDirection(1);
                break;
        }
    }
   
}
« Last Edit: May 23, 2012, 12:22:32 PM by Ollie »

Ollie

  • Administrator
  • Legendary Member
  • *****
  • Posts: 1541
    • View Profile
    • TheJavaHub
  • Programming Languages: Java, (X)HTML, CSS & PHP
Re: INTERMEDIATE JAVA GAME PROGRAMMING SOURCE CODE
« Reply #2 on: May 23, 2012, 12:23:52 PM »
Completed source code and images for the intermediate series is attached. Enjoy :)

[attachment deleted by admin]
« Last Edit: December 23, 2012, 11:33:09 AM by Ollie »