這篇文章主要介紹了java如何實現(xiàn)一個桌球小游戲,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比洮南網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式洮南網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋洮南地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
具體內(nèi)容如下
在ecplise中新建一個JAVA項目
建立四個class分別對應(yīng)游戲登陸界面,游戲界面,數(shù)據(jù)庫操作,和一個開始類
代碼如下
游戲類:
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的時候橫坐標(biāo)+ private boolean is_Y = true; //為true的時候縱坐標(biāo)+ private int score =0; //游戲的得分 private Panel p = new Panel(); private JTextPane tex = new JTextPane(); //排行榜顯示 //定義鍵盤監(jiān)聽器 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)聽器 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"+"分數(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); //長800,高500 f.setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH+350, FRAME_HIGH); //關(guān)閉窗口 f.addWindowListener(new WindowAdapter() { //添加窗口監(jiān)聽器 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); //添加鍵盤監(jiān)聽器 draw.addKeyListener(keyProcessor); //畫布長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("分數(shù)",10,20); g.setFont(font); g.drawString("分數(shù):"+score, 380, 20); } }}
登陸類:輸入一個名字,成功后就直接開始游戲
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; /*-------------暫時還沒用到這個*/ //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); //點擊按鈕的事件 Action go = new AbstractAction() { public void actionPerformed(ActionEvent e) { DataBaseLoding d = new DataBaseLoding(); try { //獲得輸入的名字 NAME_WORD = name.getText(); //將名字導(dǎo)入到數(shù)據(jù)庫 d.initParam("mysql.ini"); d.insertName(NAME_WORD); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println("玩家名字是"+name.getText()); //游戲開始 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ù)庫操作類:包含兩個方法,插入名字和修改分數(shù),用的數(shù)據(jù)庫分數(shù)默認是-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ù)庫的連接和插入名字,修改分數(shù)的方法--------------------------------------*/public class DataBaseLoding { private String driver; private String url; private String user; private String pass; //登陸數(shù)據(jù)庫 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ù)庫中 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 彈球游戲; } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“java如何實現(xiàn)一個桌球小游戲”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
當(dāng)前題目:java如何實現(xiàn)一個桌球小游戲
路徑分享:http://vcdvsql.cn/article12/pooggc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、軟件開發(fā)、企業(yè)網(wǎng)站制作、建站公司、網(wǎng)站設(shè)計公司、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)