Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a billiard Mini Game by java

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report