Technology selection

platform :Windows development tool :Intelij IDEA

JDK environment :Java 8

UI Interface : be based on Swing Desktop programming technology .

Drawing technology :Graphics

Collection framework

IO flow

Multithreading, etc

Map configuration

Map map.txt file
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,1,0,0,0,1,1,1,1,0,0,2,2,0,0,0,0,0,0,3 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
 

Read map
package cn.tx.util; import org.junit.Test; import java.io.BufferedReader;
import java.io.FileInputStream; import java.io.InputStreamReader; import
java.util.ArrayList; import java.util.List; /** * Map configuration class * @author Ren Liang * @company
Extension salary Education * @QQ:206229531 */ public class GameMap { // Data container public List<String> list
= new ArrayList<>(); // The two-dimensional array element is also a one-dimensional array : Row column matrix public int[][] map = null; //
unit testing : verification Map Class readMap() Method does put the map configuration file map.txt // Loaded into a two-dimensional array @Test public void
testResult() throws Exception { int[][] result = readMap(); //
Content output of two-dimensional array , See if it is the configuration information of the map for(int i = 0 ; i < result.length ; i++ ){ for(int j =
0 ; j < result[i].length ; j++) { System.out.print(result[i][j]+" "); }
System.out.println(); } } public int[][] readMap() throws Exception { //
Construction file input stream FileInputStream fis = new FileInputStream("map.txt"); InputStreamReader
isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr);
// Read a row of data directly String value = br.readLine(); while (value != null) {
// Add the read row of data to the container list.add(value); value = br.readLine(); } br.close();
// How many rows and columns do you get int row = list.size(); int cloum = 0; for (int i = 0; i < 1; i++) {
String str = list.get(i); String[] values = str.split(","); cloum =
values.length; } map = new int[row][cloum]; // Converts the read character to an integer , And assign it to a binary array map for (int
i = 0; i < list.size(); i++) { String str = list.get(i); String[] values =
str.split(","); for (int j = 0; j < values.length; j++) { map[i][j] =
Integer.parseInt(values[j]); } } return map; } }
Define roles

The roles in super Mary include , Water pipe , monster , Bricks, etc . And display to the main interface .

Obstacle Abstract parent Enemy
package cn.tx.role; import java.awt.Image; /** Enemy abstract class * @author Ren Liang * @company
Extension salary Education * @QQ:206229531 */ public abstract class Enemy { // Coordinate position public int x,y;
// Width and height public int width,height; // picture public Image img; public Enemy(int x, int y,
int width, int height, Image img) { this.x = x; this.y = y; this.width = width;
this.height = height; this.img=img; } }
Water pipe
package cn.tx.role; import java.awt.Image; /** * Water pipe * @author Ren Liang * @company
Extension salary Education * @QQ:206229531 */ public class Pipe extends Enemy { public Pipe(int x,
int y, int width, int height, Image img) { super(x, y, width, height, img); } }
Bricks
package cn.tx.role; import java.awt.Image; /** * brick * * @author Ren Liang * @company
Extension salary Education * @QQ:206229531 */ public class Brick extends Enemy { public Brick(int x,
int y, int width, int height, Image img) { super(x, y, width, height, img); } }
Gold coins
package cn.tx.role; import java.awt.Image; /** Gold coins * @author Ren Liang * @company
Extension salary Education * @QQ:206229531 */ public class Coin extends Enemy { public Coin(int x,
int y, int width, int height, Image img) { super(x, y, width, height, img); } }
ZiDan
package cn.tx.role; /** ZiDan * @author Ren Liang * @company Extension salary Education * @QQ:206229531 */
public class Boom { // Bullet coordinates , size , speed public int x,y; public int width; public int
speed=1; public Boom(int x, int y, int width) { super(); this.x = x; this.y =
y; this.width = width; } }
Mario class
package cn.tx.mario; import java.awt.Image; import java.awt.Rectangle; import
javax.swing.ImageIcon; import cn.tx.ui.GameFrame; import cn.tx.role.Enemy; /**
* Mary * * @author Ren Liang * @company Extension salary Education * @QQ:206229531 */ public class Mario
extends Thread { // Form Object public GameFrame gf; // Jump public boolean jumpFlag =
true; // Mario's coordinates , The origin is the upper left corner , therefore y The younger the week, the taller Mary public int x = 50, y = 358; // Mario's speed public
int xspeed = 5, yspeed = 1; // Mario's width and height public int width = 30, height = 32;
// Mario's picture public Image img = new ImageIcon("image/mari1.png").getImage();
// Are the up, down, left and right buttons on the keyboard pressed public boolean left = false, right = false, down = false, up =
false; public String Dir_Up = "Up", Dir_Left = "Left", Dir_Right = "Right",
Dir_Down = "Down"; public Mario(GameFrame gf) { this.gf = gf; this.Gravity(); }
// The logic of Mary flying ; The logic of movement is here . public void run() { while (true) { // turn left if (left) {
// Hit if (hit(Dir_Left)) { this.xspeed = 0; } // No impact on Obstacles if (this.x >= 0) {
// Mario's location update this.x -= this.xspeed; // Change Mario's picture this.img = new
ImageIcon("image/mari_left.gif").getImage(); } this.xspeed = 5; } // turn right if
(right) { // Right side collision detection should be performed when walking to the right // Conduct collision detection : At least the protagonist ( Mary , Collision object ) if (hit(Dir_Right)) {
this.xspeed = 0; } // Let the character move to the right if (this.x < 400) { this.x += this.xspeed;
this.img = new ImageIcon("image/mari_right.gif").getImage(); }
// Mario x Axis coordinates greater than 400 What happened when I was if (this.x >= 400) { // The background moves to the left gf.bg.x -= this.xspeed;
// Obstacle item moves left for (int i = 0; i < gf.eneryList.size(); i++) { Enemy enery =
gf.eneryList.get(i); enery.x -= this.xspeed; } // Icon changes this.img = new
ImageIcon("image/mari_right.gif").getImage(); } this.xspeed = 5; } // Jump up if
(up) { if (jumpFlag && !isGravity) { jumpFlag = false; new Thread() { public
void run() { jump(); jumpFlag = true; } }.start(); } } try { this.sleep(20); }
catch (InterruptedException e) { e.printStackTrace(); } } } // Jump up function public
void jump() { // Define height value int jumpHeigh = 0; for (int i = 0; i < 150; i++) {
// Mary's y Axis position minus Mary's y Shaft speed gf.mario.y -= this.yspeed; // Mary's height is increasing jumpHeigh++;
// If you hit an obstacle and jump out if (hit(Dir_Up)) { break; } try { Thread.sleep(5); } catch
(InterruptedException e) { e.printStackTrace(); } } // Mary jumped up and fell for (int i = 0;
i < jumpHeigh; i++) { // Mary's y Shaft height plus y Shaft speed gf.mario.y += this.yspeed; // Stop if you hit an obstacle below
if (hit(Dir_Down)) { this.yspeed = 0; } try { Thread.sleep(5); } catch
(InterruptedException e) { e.printStackTrace(); } } this.yspeed = 1;// Reduction speed }
// Collision detection public boolean hit(String dir) { // Swing In technology , They have provided it !! Rectangle myrect
= new Rectangle(this.x, this.y, this.width, this.height); Rectangle rect =
null; for (int i = 0; i < gf.eneryList.size(); i++) { Enemy enery =
gf.eneryList.get(i); if (dir.equals("Left")) { rect = new Rectangle(enery.x +
2, enery.y, enery.width, enery.height); } else if (dir.equals("Right")) { //
Right side collision detection . rect = new Rectangle(enery.x - 2, enery.y, enery.width, enery.height);
} else if (dir.equals("Up")) { rect = new Rectangle(enery.x, enery.y + 1,
enery.width, enery.height); } else if (dir.equals("Down")) { rect = new
Rectangle(enery.x, enery.y - 2, enery.width, enery.height); } // collision detection if
(myrect.intersects(rect)) { return true; } } return false; } // Check whether it is attached to the ground public
boolean isGravity = false; // Gravity thread ! public void Gravity() { new Thread() {
public void run() { while (true) { try { sleep(10); } catch
(InterruptedException e) { e.printStackTrace(); } while (true) { // If you don't jump out if
(!jumpFlag) { break; } // If you hit an obstacle below, jump out if (hit(Dir_Down)) { break; } // greater than 385 Under the ground
if (y >= 358) { isGravity = false; } else { // On the ground isGravity = true;
//y It's Mario's vertical position y += yspeed; } try { sleep(10); } catch (InterruptedException e)
{ e.printStackTrace(); } } } } }.start(); } }
forms
package cn.tx.ui; import java.awt.Color; import java.awt.Graphics; import
java.awt.image.BufferedImage; import java.util.ArrayList; import
javax.swing.ImageIcon; import javax.swing.JFrame; import cn.tx.mario.Mario;
import cn.tx.role.*; import cn.tx.util.GameMap; import cn.tx.util.MusicUtil;
/** Main window interface : Show role . */ public class GameFrame extends JFrame{ // Super Marie : The interface needs a super Mary .
public Mario mario; // Separately defined : Water pipe , Gold coins and bricks public Enemy pipe ,coin , brick; // Background picture
public BackgroundImage bg ; // Define a collection container to hold enemy objects public ArrayList<Enemy> eneryList =
new ArrayList<Enemy>(); // Define a collection container to load bullets public ArrayList<Boom> boomList = new
ArrayList<Boom>(); // Bullet speed public int bspeed=0; // Map data , Make rules , yes 1 Draw bricks , yes 2 Draw gold coins , yes 3 Draw water pipe
public int[][] map = null; // Object code block : The current class is executed when it is created { // Data for initializing Map Resources in the instance code block GameMap
mp = new GameMap(); map = mp.readMap(); } // Constructor initializes the background image and Mario object public
GameFrame() throws Exception { // Create background picture bg = new BackgroundImage();
// Initialize form related attribute information data // this Represents the current main interface object . this.setSize(800,450);
this.setTitle(" Super Marie "); this.setResizable(true); // Center display window
this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true); // Create Mary object mario = new Mario(this); // Read map , And configure the map for
(int i = 0; i < map.length; i++) { for (int j = 0; j < map[0].length; j++) {
// Read is 1, Draw bricks if(map[i][j]==1){ // x brick = new Brick(j*30,i*30,30,30,new
ImageIcon("image/brick.png").getImage()); eneryList.add(brick); } // get to read 2 Draw gold coins
if(map[i][j]==2){ coin = new Coin(j*30,i*30,30,30,new
ImageIcon("image/coin_brick.png").getImage()); eneryList.add(coin); } // get to read 3 Draw water pipe
if(map[i][j]==3){ pipe = new Pipe(j*30,i*30,60,120,new
ImageIcon("image/pipe.png").getImage()); eneryList.add(pipe); } } }
mario.start(); // Start a thread responsible for the window redrawing thread of the interface new Thread(){ public void run(){
while(true){ // Redraw form repaint(); // Automatically trigger in the current window paint method // Check if the bullet is out of bounds //checkBoom();
try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace();
} } } }.start(); // Set background music new Thread(new Runnable() { @Override public void
run() { MusicUtil.playBackground(); } }).start(); } /** * Draw form * @param g */
@Override public void paint(Graphics g) { // Use double buffer to draw background pictures and Mario BufferedImage bi
=(BufferedImage)this.createImage(this.getSize().width,this.getSize().height);
// Create drawing object Graphics big = bi.getGraphics(); // Painting background big.drawImage(bg.img, bg.x,
bg.y, null); // Start drawing enemies on the interface . for (int i = 0; i < eneryList.size(); i++) { Enemy
e = eneryList.get(i); // Draw enemy big.drawImage(e.img, e.x, e.y, e.width,
e.height,null); } // Draw bullets for (int i = 0; i < boomList.size(); i++) { Boom b
=boomList.get(i); Color c =big.getColor(); big.setColor(Color.red);
big.fillOval(b.x+=b.speed, b.y, b.width, b.width); big.setColor(c); } // Painting characters
Mary herself big.drawImage(mario.img, mario.x, mario.y, mario.width,
mario.height,null); g.drawImage(bi,0,0,null); }
// Check if the bullet is out of bounds , Out of bounds is removed from the container , If not removed , Memory leaks public void checkBoom(){ for (int i = 0; i <
boomList.size(); i++) { Boom b = boomList.get(i); if(b.x<0 || b.x>800){
boomList.remove(i); } } } }
  Keyboard event monitoring
package cn.tx.util; import cn.tx.role.Boom; import cn.tx.ui.GameFrame; import
java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import
javax.swing.ImageIcon; // Keyboard press monitoring class public class KeyListener extends KeyAdapter {
// Received the current main interface : Game interface public GameFrame gf; public KeyListener(GameFrame gf) {
this.gf = gf; } // Keyboard monitor @Override public void keyPressed(KeyEvent e) {
//code Is the value corresponding to pressing the key on the keyboard int code = e.getKeyCode(); switch (code) { // turn right case 39:
gf.mario.right = true; // Signal bit break; // turn left case 37: gf.mario.left = true;
break; case 66: addBoom(); break; // Jump up case 38: gf.mario.up = true; break; } }
// Add bullets public void addBoom() { Boom b = new Boom(gf.mario.x, gf.mario.y + 5,
10); if (gf.mario.left) b.speed = -2; if (gf.mario.right) b.speed = 2;
gf.boomList.add(b); } // Keyboard release monitor @Override public void keyReleased(KeyEvent e) {
int code = e.getKeyCode(); // Release the right button if (code == 39) { gf.mario.right = false;
gf.mario.img = new ImageIcon("image/mari1.png").getImage(); } // Release the left key if (code
== 37) { gf.mario.left = false; gf.mario.img = new
ImageIcon("image/mari_left1.png").getImage(); } // Release the up key if (code == 38) {
gf.mario.up = false; } } }
 

Main method and event binding
import cn.tx.ui.GameFrame; import cn.tx.util.KeyListener; /** Super Mary startup class . */
public class Run { // Main function , Program entry public static void main(String[] args) throws
Exception { GameFrame gf = new GameFrame(); // Create listener object KeyListener kl = new
KeyListener(gf); // Add a keyboard listener to the form gf.addKeyListener(kl); } }
Complete source code and explanation QQ Additive group :685168267

Technology