您好,登錄后才能下訂單哦!
這篇文章主要介紹了java如何實現一個桌球小游戲,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內容如下
在ecplise中新建一個JAVA項目
建立四個class分別對應游戲登陸界面,游戲界面,數據庫操作,和一個開始類
代碼如下
游戲類:
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); //板子的坐標 private int TABLE_Y=400; private int BALL_X=10; //小球的橫坐標 private int BALL_Y=10; //小球的縱坐標 private int SPEED = 10; //小球橫向速度 Frame f = new Frame("彈球游戲"); private Mycanvas draw = new Mycanvas(); private boolean is_X = true; //為true的時候橫坐標+ private boolean is_Y = true; //為true的時候縱坐標+ private int score =0; //游戲的得分 private Panel p = new Panel(); private JTextPane tex = new JTextPane(); //排行榜顯示 //定義鍵盤監聽器 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; } } }; //定義事件監聽器 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("結束了"); 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"+"分數"+"\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); //關閉窗口 f.addWindowListener(new WindowAdapter() { //添加窗口監聽器 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); //添加鍵盤監聽器 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("分數",10,20); g.setFont(font); g.drawString("分數:"+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.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(); }}
數據庫操作類:包含兩個方法,插入名字和修改分數,用的數據庫分數默認是-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;/*------------------------ 數據庫的連接和插入名字,修改分數的方法--------------------------------------*/public class DataBaseLoding { private String driver; private String url; private String user; private String pass; //登陸數據庫 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"); } //將名字插入到數據庫中 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如何實現一個桌球小游戲”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。