Java Games Design Patterns
YANG HU
Simple is the beginning of wisdom. This book briefly explain the concept and real practice examples in games, you will learn easy and fun.
http://en.verejava.com
Copyright 2020 Yang Hu
All rights reserved.
ISBN : 979864812538 4
CONTENTS
If you want to learn this book, you must have basic knowledge of Java,
you can learn our course << Easy Learning Java (3 Editon)>>
http://en.verejava.com
If you already have basic knowledge of Java skip it, start an exciting journey
Download GameDesignPatternImages.zip all images for this book.
http://en.verejava.com/download.jsp?id=1
Template Pattern Principle
Template Pattern : Defer the exact steps of an algorithm to a subclass. Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithms structure.
BluePlane inherit from Sprite Template for reuse.
Sprite:
1. x: x coordinate of Canvas 2. y: y coordinate of Canvas 3. Sprite(String name, int x, int y): constructor method 4. draw(): draw sprite on canvas 5. move( int distanceX, int distanceY): sprite move on canvas |
Sprite.java in package com.template.principle;
class Sprite { protected int x ; // x coordinate of Canvas protected int y ; // y coordinate of Canvas protected String name ; public Sprite(String name, int x, int y) { this . name = name; this . x = x; this . y = y; } public void draw() { String text = this . name + " draw on (" + this . x + " , " + this . y + ")" ; System. out .println(text); } // distanceX: distance moved in the x-axis // distanceX: distance moved in the y-axis public void move( int distanceX, int distanceY) { String text = this . name + " move distance (" +distanceX+ " , " + distanceY+ ")" ; System. out .println(text); } } |
BluePlane.java in package com.template.principle;
public class BluePlane extends Sprite{ public BluePlane(String name, int x, int y) { super (name, x, y); } } |
. Create a Test class : TestTemplate.java in package com.template.principle;
public class TestTemplate { public static void main(String[] args) { BluePlane bluePlane = new BluePlane( "BluePlane" , 50,150); bluePlane.draw(); bluePlane.move(5, 5); } } |
Result:
BluePlane draw on (50 , 150)
BluePlane move distance (5 , 5)
Template Pattern Game
BluePlane draw on canvas, Press the keyboard to move up, down, left, right.
2. UML Diagram BluePlane inherit from Sprite Template for reuse.
Sprite:
1. x: x coordinate of Canvas 2. y: y coordinate of Canvas 3. BufferedImage image : the image of BluePlane ( blue_plane.png ) 4. Sprite( int x, int y, BufferedImage image): constructor method 5. draw(Graphics g): draw sprite on canvas 6. move( int distanceX, int distanceY): sprite move on canvas |
Sprite.java in package com.template.game;
import java.awt.Graphics; import java.awt.image.BufferedImage; class Sprite { protected int x ; // x coordinate of Canvas protected int y ; // y coordinate of Canvas protected BufferedImage image ; public Sprite( int x, int y, BufferedImage image){ this . x = x; this . y = y; this . image = image; } public void draw(Graphics g){ g.drawImage( image , this . x , this . y , null ); } public void move( int distanceX, int distanceY){ this . x = this . x + distanceX; this . y = this . y + distanceY; } } |
BluePlane.java in package com.template.game;
import java.awt.image.BufferedImage; public class BluePlane extends Sprite{ public BluePlane( int x, int y, BufferedImage image) { super (x, y, image); } } |
Canvas:
1. loadImage(String imagePath): load image ( blue_plane.png ) to BufferedImage 2. paintComponent(Graphics g): can draw image and also be called explicitly by the repaint() method defined in Component class. The effect of calling repaint() is that Swing automatically clears the graphic on the panel and executes the paintComponent method to redraw the graphics on this panel. this .addKeyListener( new KeyAdapter(): Monitoring and receiving keyboard events to move up, down, left, right. |
Canvas.java in package com.template.game;
import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class Canvas extends JPanel { private BufferedImage blueImage ; private Sprite bluePlane ; public Canvas() { this .setLayout( null ); this .setBackground(Color. WHITE ); blueImage =loadImage( "images/blue_plane.png" ); bluePlane = new BluePlane(120, 250, blueImage ); this .addKeyListener( new KeyAdapter() { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent. VK_UP ){ bluePlane .move(0, -6); } else if (keyCode == KeyEvent. VK_DOWN ){ bluePlane .move(0, 6); } else if (keyCode == KeyEvent. VK_RIGHT ){ bluePlane .move(6, 0); } else if (keyCode == KeyEvent. VK_LEFT ){ bluePlane .move(-6, 0); } Canvas. this .repaint(); } }); } protected void paintComponent(Graphics g) { super .paintComponent(g); bluePlane .draw(g); } public BufferedImage loadImage(String imagePath) { File file = new File(imagePath); BufferedImage bufferedImage = null ; try { bufferedImage = ImageIO. read (file); } catch (IOException e) { |