java實(shí)現(xiàn)一個(gè)桌球小游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)桌球小游戲的具體代碼,供大家參考,具體內(nèi)容如下


在ecplise中新建一個(gè)JAVA項(xiàng)目
建立四個(gè)class分別對(duì)應(yīng)游戲登陸界面,游戲界面,數(shù)據(jù)庫(kù)操作,和一個(gè)開(kāi)始類
代碼如下
游戲類:
package 彈球游戲;
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;
/*----------------游戲界面,彈球游戲------------------*/
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; //板子寬度
private final int TABLE_HIGH =20 ; //板子高度
private final int BALL_SIZE =20; //球的大小
Random rand = new Random();
private int TABLE_X=rand.nextInt(200); //板子的坐標(biāo)
private int TABLE_Y=400;
private int BALL_X=10; //小球的橫坐標(biāo)
private int BALL_Y=10; //小球的縱坐標(biāo)
private int SPEED = 10; //小球橫向速度
Frame f = new Frame("彈球游戲");
private Mycanvas draw = new Mycanvas();
private boolean is_X = true; //為true的時(shí)候橫坐標(biāo)+
private boolean is_Y = true; //為true的時(shí)候縱坐標(biāo)+
private int score =0; //游戲的得分
private Panel p = new Panel();
private JTextPane tex = new JTextPane(); //排行榜顯示
//定義鍵盤(pán)監(jiān)聽(tīng)器
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_X<370){
TABLE_X +=10;
}
if(e.getKeyCode()==KeyEvent.VK_UP&&TABLE_Y>0) {
TABLE_Y -=10;
}
if(e.getKeyCode()==KeyEvent.VK_DOWN&&TABLE_Y<430) {
TABLE_Y +=10;
}
}
};
//定義事件監(jiān)聽(tīng)器
ActionListener task = new ActionListener (){
public void actionPerformed(ActionEvent e) {
if(BALL_X>FRAME_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("結(jié)束了");
timer.stop();
}
if(is_X) {
BALL_X +=10;
}else {
BALL_X -=10;
}
if(BALL_X>440){
is_X=false;
}
if(BALL_X<10) {
is_X=true;
}
if(BALL_Y<0) {
is_Y =true;
}
if(is_Y) {
BALL_Y+=10;
}else {
BALL_Y-=10;
}
if(BALL_Y==TABLE_Y-10&&BALL_X>TABLE_X&&BALL_X<TABLE_X+100) {
is_Y=false;
}
score++;
draw.repaint();
}
};
Timer timer = new Timer(100,task);
public void text() {
Font f = new Font("排行榜的字體",Font.ITALIC,25);
tex.setFont(f);
String first ="名次"+"\t"+"\t"+"分?jǐn)?shù)"+"\r";
try {
DataBaseLoding d = new DataBaseLoding();
d.initParam("mysql.ini");
first +=d.select();
} catch (Exception e1) {
e1.printStackTrace();
}
tex.setText(first);
p.add(tex);
}
public void init() {
f.add(draw);
text();
f.add(p, BorderLayout.EAST);
//長(zhǎng)800,高500
f.setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH+350, FRAME_HIGH);
//關(guān)閉窗口
f.addWindowListener(new WindowAdapter() { //添加窗口監(jiān)聽(tīng)器
public void windowClosing(WindowEvent e) {
try {
DataBaseLoding d = new DataBaseLoding();
d.initParam("mysql.ini");
d.insertScore(score);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
}
});
f.addKeyListener(keyProcessor); //添加鍵盤(pán)監(jiān)聽(tīng)器
draw.addKeyListener(keyProcessor);
//畫(huà)布長(zhǎng)500,高500
draw.setPreferredSize(new Dimension(FRAME_WIDTH,FRAME_HIGH));
timer.start();
f.setVisible(true);
}
class Mycanvas extends Canvas{
public void paint(Graphics g) {
g.setColor(new Color(0, 0, 0));
g.drawLine(0, FRAME_Y, FRAME_X-20, FRAME_Y);
g.drawLine(FRAME_X-25, 0, FRAME_X-25, FRAME_Y);
g.drawLine(0, 0, 0, FRAME_Y);
g.setColor(new Color(0,0,0));
g.fillRect(TABLE_X,TABLE_Y , TABLE_WIDTH, TABLE_HIGH);
g.setColor(new Color(255,175,175));
g.fillRect(BALL_X, BALL_Y, BALL_SIZE, BALL_SIZE);
Font font = new Font("分?jǐn)?shù)",10,20);
g.setFont(font);
g.drawString("分?jǐn)?shù):"+score, 380, 20);
}
}
}
登陸類:輸入一個(gè)名字,成功后就直接開(kāi)始游戲
package 彈球游戲;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*-----------------登陸界面,輸入名字--------------------*/
public class Langding {
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 JFrame f;
private JButton ok;
private JPanel p;
private TextField name;
private TextField pass;
private Game g;
private int score=0;
/*-------------暫時(shí)還沒(méi)用到這個(gè)*/
//private String PASS_WORD ="";
private String NAME_WORD ="";
public void init() throws Exception {
f = new JFrame("登陸窗口");
p = new JPanel() {
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon("lib/01.png");
Image img = icon.getImage();
Image img1 = img.getScaledInstance(FRAME_WIDTH+300, FRAME_HIGH,Image.SCALE_DEFAULT);
icon.setImage(img1);
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
}
};
f.add(p);
ok = new JButton("登陸");
name = new TextField(20);
//pass = new TextField(20);
p.add(name);
//p.add(pass);
ok.setPreferredSize(new Dimension(100, 100));
p.add(ok);
//點(diǎn)擊按鈕的事件
Action go = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
DataBaseLoding d = new DataBaseLoding();
try {
//獲得輸入的名字
NAME_WORD = name.getText();
//將名字導(dǎo)入到數(shù)據(jù)庫(kù)
d.initParam("mysql.ini");
d.insertName(NAME_WORD);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("玩家名字是"+name.getText());
//游戲開(kāi)始
g = new Game();
g.init();
}
};
ok.addActionListener(go);
f.setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH+300, FRAME_HIGH);
f.add(p);
f.setVisible(true);
}
public static void main(String[] args) throws Exception {
Langding l = new Langding();
l.init();
}
}
數(shù)據(jù)庫(kù)操作類:包含兩個(gè)方法,插入名字和修改分?jǐn)?shù),用的數(shù)據(jù)庫(kù)分?jǐn)?shù)默認(rèn)是-1
package 彈球游戲;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
/*------------------------ 數(shù)據(jù)庫(kù)的連接和插入名字,修改分?jǐn)?shù)的方法--------------------------------------*/
public class DataBaseLoding {
private String driver;
private String url;
private String user;
private String pass;
//登陸數(shù)據(jù)庫(kù)
public void initParam(String paramFile) throws Exception {
Properties props = new Properties();
props.load(new FileInputStream(paramFile));
driver = props.getProperty("driver");
url = props.getProperty("url");
user = props.getProperty("user");
pass = props.getProperty("pass");
}
//將名字插入到數(shù)據(jù)庫(kù)中
public int insertName(String Name) throws Exception {
Class.forName(driver);
String sql ="insert into 彈球游戲(name) values("+"'"+Name+"'"+")"+";";
try (
Connection conn =DriverManager.getConnection(url,user,pass);
Statement stmt =conn.createStatement();
)
{
System.out.println(Name);
return stmt.executeUpdate(sql);
}
}
//把score插到主鍵為name的那一行
public int insertScore(int Score) throws Exception {
Class.forName(driver);
String sql ="update 彈球游戲 set score ="+Score
+" where score = -1"+";";
try (
Connection conn =DriverManager.getConnection(url,user,pass);
Statement stmt =conn.createStatement();
)
{
System.out.println(Score);
return stmt.executeUpdate(sql);
}
}
public String select() throws Exception{
Class.forName(driver);
String sql ="select * from 彈球游戲 ORDER BY score DESC ;";
String result ="";
try (
Connection conn =DriverManager.getConnection(url,user,pass);
Statement stmt =conn.createStatement();
ResultSet rs =stmt.executeQuery(sql);
)
{
int i=1;
while(rs.next()) {
result +="第"+i+"名"+ rs.getString(1)+"\t"+"\t"+rs.getInt(2)+"\r";
i++;
if(i==11) {
break;
}
}
return result;
}
}
}
主類:
package 彈球游戲;
public class Begain {
public static void main(String[] args) throws Exception {
Langding l = new Langding();
l.init()package 彈球游戲;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ssm項(xiàng)目改造spring?boot項(xiàng)目完整步驟
Spring?Boot現(xiàn)在已經(jīng)成為Java開(kāi)發(fā)領(lǐng)域的一顆璀璨明珠,它本身是包容萬(wàn)象的,可以跟各種技術(shù)集成,下面這篇文章主要給大家介紹了關(guān)于ssm項(xiàng)目改造spring?boot項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2023-04-04
java 獲取服務(wù)器真實(shí)IP的實(shí)例
這篇文章主要介紹了java 獲取服務(wù)器真實(shí)IP的實(shí)例的相關(guān)資料,這里提供實(shí)現(xiàn)方法幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
SpringBoot Devtools實(shí)現(xiàn)項(xiàng)目熱部署的方法示例
這篇文章主要介紹了SpringBoot Devtools實(shí)現(xiàn)項(xiàng)目熱部署的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
解決pageHelper分頁(yè)失效以及如何配置問(wèn)題
這篇文章主要介紹了解決pageHelper分頁(yè)失效以及如何配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
java GUI實(shí)現(xiàn)ATM機(jī)系統(tǒng)(3.0版)
這篇文章主要為大家詳細(xì)介紹了java GUI實(shí)現(xiàn)ATM機(jī)系統(tǒng)(3.0版),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
SpringBoot項(xiàng)目上高并發(fā)問(wèn)題的解決方案
本章演示在springboot項(xiàng)目中的高并發(fā)demo,演示導(dǎo)致的問(wèn)題,以及單機(jī)部署下的解決方案和集群部署下的解決方式以及分布式下的解決方案,文中通過(guò)圖文結(jié)合的方式講解的非常詳細(xì),需要的朋友可以參考下2024-06-06
java基于GUI實(shí)現(xiàn)簡(jiǎn)單畫(huà)筆小畫(huà)板
這篇文章主要為大家詳細(xì)介紹了java基于GUI實(shí)現(xiàn)簡(jiǎn)單畫(huà)筆小畫(huà)板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

