十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍了java如何实现一个桌球小游戏,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比洮南网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式洮南网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖洮南地区。费用合理售后完善,十年实体公司更值得信赖。
具体内容如下
在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登陆类:输入一个名字,成功后就直接开始游戏
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如何实现一个桌球小游戏”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
本文名称:java如何实现一个桌球小游戏
文章路径:http://6mz.cn/article/pooggc.html