Java billiards games (interest production)

Billiards games

Two pictures are placed in the same level directory of src

Version 1. Window appears

package cn.xjion.game;
/**
 * Appearance window
 * @author xjion
 *
 */
import java.awt.*;
import javax.swing.*;
//Inherit window class
public class BallGame extends JFrame{
//	How to load windows
	void launchFrame(){
//		Window size, width and height
		setSize(300,300);
//		Position of the window, from the top left corner of the window
		setLocation(400,200);
//		Window visible
		setVisible(true);
	}
//	main method, execution entry
	public static void main(String[] args) {
//		New object
		BallGame bg = new BallGame();
//		Calling method
		bg.launchFrame();
	}
}

Version 2. Loading pictures

package cn.xjion.game;
/**
 * Loading pictures
 * @author xjion
 *
 */
import java.awt.*;
import javax.swing.*;
//Inherit window class
public class BallGame extends JFrame{
//	Create two picture objects, representing two pictures
	Image ball = Toolkit.getDefaultToolkit().getImage("image/ball.png");
	Image desk = Toolkit.getDefaultToolkit().getImage("image/desk.jpg");
//	How to draw windows
	public void paint(Graphics g){
//		Draw desktop
		g.drawImage(desk, 0, 0, null);
//		Draw ball object, coordinate 100100
		g.drawImage(ball, 100, 100, null);
	}
	
//	How to load windows
	void launchFrame(){
//		Window size, width and height
		setSize(856,500);
//		Position of the window, from the top left corner of the window
		setLocation(50,50);
//		Window visible
		setVisible(true);
	}
//	main method, execution entry
	public static void main(String[] args) {
//		New object
		BallGame bg = new BallGame();
//		Calling method
		bg.launchFrame();
	}
}

Version 3. Make the ball move

package cn.xjion.game;
/**
 * horizontal scroll
 * @author xjion
 *
 */
import java.awt.*;
import javax.swing.*;
//Inherit window class
public class BallGame extends JFrame{
//	Create two picture objects, representing two pictures
	Image ball = Toolkit.getDefaultToolkit().getImage("image/ball.png");
	Image desk = Toolkit.getDefaultToolkit().getImage("image/desk.jpg");
	
	int x = 100;
	int y = 100;
	boolean right = true;
		
//	How to draw windows
	public void paint(Graphics g){
//		Draw desktop
		g.drawImage(desk, 0, 0, null);
//		Draw ball object, coordinate 100100
		g.drawImage(ball, x, y, null);
//		If you go right, add 10, otherwise subtract 10
		if (right) {
			x = x + 10;
		}else{
			x = x - 10;
		}
//		When the ball goes to the head, it goes back
		if(x>856-40-30){
			right = false;
		}
		if(x<40){
			right = true;
		}
		
	}
//	How to load windows
	void launchFrame(){
//		Window size, width and height
		setSize(856,500);
//		Position of the window, from the top left corner of the window
		setLocation(50,50);
//		Window visible
		setVisible(true);
		
//		Redraw window, unlimited redraw
		while(true){
//			Redrawing Method
			repaint();
//			try catch throw exception
			try {
//				Repeat once in 40ms, 25 times in 1s
				Thread.sleep(40);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
//	main method, execution entry
	public static void main(String[] args) {
//		New object
		BallGame bg = new BallGame();
//		Calling method
		bg.launchFrame();
	}
}

Version 4. Roll the ball at any angle

package cn.xjion.game;
/**
 * Arbitrary rolling
 * @author xjion
 *
 */
import java.awt.*;
import javax.swing.*;
//Inherit window class
public class BallGame2 extends JFrame{
//	Create two picture objects, representing two pictures
	Image ball = Toolkit.getDefaultToolkit().getImage("image/ball.png");
	Image desk = Toolkit.getDefaultToolkit().getImage("image/desk.jpg");
//	Coordinates on the window
	double x = 100;
	double y = 100;

	double degree = 3.14 / 3;//Radian, 60 °
	
//	How to draw windows
	public void paint(Graphics g){
//		Draw desktop
		g.drawImage(desk, 0, 0, null);
//		Draw ball object, coordinate 100100
		g.drawImage(ball,(int) x,(int) y, null);
//		Changes in x and y coordinates
		x = x + 10*Math.cos(degree);
		y = y + 10*Math.sin(degree);
		
		/*Judge when to hit the boundary rebound*/
//		Rebound at the upper and lower boundary
		if (y>500-40-30 || y<40 + 40) {
			degree = -degree;
		}
//		Rebound when touching left and right boundaries
		if (x>856-40-30 || x<30) {
			degree = 3.14-degree;
		}
		
	}
//	How to load windows
	void launchFrame(){
//		Window size, width and height
		setSize(856,500);
//		Position of the window, from the top left corner of the window
		setLocation(50,50);
//		Window visible
		setVisible(true);
		
//		Redraw window, unlimited redraw
		while(true){
//			Redrawing Method
			repaint();
//			try catch throw exception
			try {
//				Repeat once in 40ms, 25 times in 1s
				Thread.sleep(40);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
//	main method, execution entry
	public static void main(String[] args) {
//		New object
		BallGame2 bg = new BallGame2();
//		Calling method
		bg.launchFrame();
	}
}

Well, even if the simplest billiard game is finished, it can realize the ball rolling. If you are interested, you can study the club, auxiliary line, hitting, and the speed gradually drops to the stop. The operation of goals and scores is wrong. Please point out!

 

 

 

 

Tags: Windows Java

Posted on Fri, 08 Nov 2019 12:54:56 -0800 by reinaldo