这篇文章主要介绍了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。