Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement a billiard Mini Game by java

Shulou Source: shulou.com Published: 2022-06-03 09:45:31 10月03日 Update

This article mainly introduces java how to achieve a billiards Mini Game, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The details are as follows

Create a new JAVA project in ecplise

Set up four class corresponding to the game login interface, the game interface, the database operation, and a start class

The code is as follows

Games:

Package pinball game; import java.awt.BorderLayout;import java.awt.Canvas;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.Frame;import java.awt.Graphics;import java.awt.Panel;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.util.Random;import javax.swing.JTextPane Import javax.swing.Timer;/*- game interface, pinball game-* / public class Game {private final int FRAME_X = 500; private final int FRAME_Y = 450; private final int FRAME_WIDTH = 500; private final int FRAME_HIGH = 500; private final int TABLE_WIDTH = 100; / / board width private final int TABLE_HIGH = 20; / / board height private final int BALL_SIZE = 20 / / Ball size Random rand = new Random (); private int TABLE_X=rand.nextInt (200); / / Board coordinate private int TABLE_Y=400; private int BALL_X=10; / / Ball Abscissa private int BALL_Y=10; / / Ball Abscissa private int SPEED = 10; / / Ball Lateral Velocity Frame f = new Frame ("pinball game"); private Mycanvas draw = new Mycanvas (); private boolean is_X = true / / Abscissa of true + private boolean is_Y = true; / / ordinate of true + private int score = 0; / / score of the game private Panel p = new Panel (); private JTextPane tex = new JTextPane () / / ranking shows / / defines keyboard listener KeyAdapter keyProcessor = new KeyAdapter () {public void keyPressed (KeyEvent e) {if (e.getKeyCode () = = KeyEvent.VK_LEFT&&TABLE_X > 0) {TABLE_X-= 10;} if (e.getKeyCode () = = KeyEvent.VK_RIGHT&&TABLE_X0) {TABLE_Y-= 10 } if (e.getKeyCode () = = KeyEvent.VK_DOWN&&TABLE_YFRAME_X | | BALL_Y > FRAME_Y&&BALL_Y > TABLE_Y-15) {try {DataBaseLoding d = new DataBaseLoding (); d.initParam ("mysql.ini"); d.insertScore (score);} catch (Exception E1) {e1.printStackTrace ();} System.out.println ("over"); timer.stop () } if (is_X) {BALL_X + = 10;} else {BALL_X-= 10;} if (BALL_X > 440) {is_X=false;} if (BALL_X)

Tags: Pinball name data database score login interface article ball board monitor monitor input Mini Game billiards ranking method time Abscissa game interface Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Information Apple MySQL Shulou Technology