preface
Before using Java multithreading to develop games, we need to understand and use multithreading. Let's use a small ball to demonstrate the movement and change of objects in multithreading
1, Create a sports ball
1. Use Thread
The following uses Thread to create a Thread every time you click launch, and a small ball moving from left to right appears at any position
The code is as follows:
/** *Create a thread class to inherit threads *Write the method of thread work in the rewritten run method * @author Administrator * */ public class DrawThread extends Thread{ private JPanel jp; Random r = new Random(); private int radius = 40; private Graphics g; public DrawThread(JPanel jp,Graphics g) { // TODO Auto-generated constructor stub this.jp = jp; this.g = g; } //Override run method @Override public void run() { // TODO Auto-generated method stub draw(); } private void draw() { // TODO Auto-generated method stub //Initial position of the ball int x = r.nextInt(jp.getWidth()); int y = r.nextInt(jp.getHeight()); for (int i = 0; i < y; i++) { g.setColor(Color.BLACK); g.fillOval(x+i, y,radius, radius); try { Thread.sleep(10);//Unit: ms }catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //Erase after ball movement g.setColor(jp.getBackground()); g.fillRect(0, 0, jp.getWidth(), jp.getHeight()); } } }
Deficiency: every time you create a small ball, you need to add a thread. The method of erasing the small ball is incorrect, resulting in the flickering or disappearance of several small balls on the canvas at the same time
2. Use Runnable
After using Runnable, it is not necessary to create a thread every time you draw a small ball. The movement of all small balls takes place in one thread, which speeds up the running speed of the CPU.
package com.zl0809; import java.awt.Color; import java.awt.Graphics; import java.util.List; public class BallRun implements Runnable{ private Graphics g; private int radius = 20; private List<Ball> data; public BallRun(Graphics g,List<Ball> data) { // TODO Auto-generated constructor stub this.g = g; this.data = data; } @Override public void run() { System.out.println("call"); //Ball starting position int x = (int)(Math.random()*800); int y = (int)(Math.random()*600); //Draw ball while(true){ //Erase g.setColor(Color.BLACK); g.fillRect(0, 0, 800, 600); for (int i = 0; i < data.size(); i++) { Ball ball = data.get(i); ball.move(); ball.draw(g); } try { Thread.sleep(70);//Thread sleep in microseconds } catch (InterruptedException e1) { // TODO: handle exception } } } }
//Ensure that the thread is created once if(ballRun == null){ ballRun = new BallRun(g,data); new Thread(ballRun).start(); }
2, Solve the problem of multi ball flashing
When a certain number of small balls are added to the canvas, the movement of small balls will flash. At this time, we can draw small balls in the cache
//Create cache canvas to solve the problem of small ball flicker BufferedImage buffer = new BufferedImage(DrawUI.WIDTH, DrawUI.HEIGHT, BufferedImage.TYPE_INT_ARGB); Graphics bufferG = buffer.getGraphics(); //The method of writing and drawing a small ball in the middle code part uses the cached canvas to draw a small ball //To draw the cache on the interface, you need to use the canvas of the interface g.drawImage(buffer, 0, 0, null);
3, Add small balls regularly
In order to generate a small ball regularly in the canvas, you need to add a thread to generate a small ball regularly in the interface. In this thread, every time you sleep for a period of time, you will create a new small ball and put it in the small ball list
public class AutoBall implements Runnable{ private ArrayList<Ball> ballList; public AutoBall(ArrayList<Ball> ballList){ this.ballList = ballList; } public void run(){ //Add 15 balls at the beginning of the game for (int i = 0; i < 15; i++) { Ball b = new Ball(); ballList.add(b); } do{ //Thread sleep try{ Thread.sleep(3000); } catch(InterruptedException e){ e.printStackTrace(); } //Add opponent's small ball Ball ball = new Ball(); ballList.add(ball); } while(true); } }
4, Own control of the ball
If you want to make the game more interesting, you should make the characters or objects in the game obey your own command.
To make the ball follow the keyboard command or the mouse to respond, we need to get our own ball in the listener, then monitor and change the coordinates of the ball, so as to make the ball move to the direction we want.
public void keyPressed(KeyEvent e){ //getBall() is a method to get the player ball created in the thread //The return value type is Ball myball = ballRun.getBall(); if(e.getKeyCode() == KeyEvent.VK_LEFT){ myball.left(); } else if(e.getKeyCode() == KeyEvent.VK_RIGHT){ myball.right(); } else if(e.getKeyCode() == KeyEvent.VK_UP){ myball.up(); } else if(e.getKeyCode() == KeyEvent.VK_DOWN){ myball.down(); } }
summary
Tip: here is a summary of the article:
For example, the above is what we want to talk about today. This paper only briefly introduces the use of pandas, which provides a large number of functions and methods that enable us to process data quickly and conveniently.