虽然Java学了很久了,但是什么也没学到,最近实习,老师在课堂授课时用这个小游戏来讲解Java中的基础知识,在这里拿出来作为学习过程的记录,希望能够每天进步。
代码如下:
package screen; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class BallFrame { public static void main(String[] args) { JFrame f = new JFrame(); BallPanel b=new BallPanel(); f.add(b); f.setTitle("PlayBall"); f.setSize( 1000, 800); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(3); f.setVisible(true); f.addMouseMotionListener(b); f.addMouseListener(b); b.move(); } } class BallPanel extends JPanel implements MouseMotionListener,MouseListener{ int px=300,bx=600,by=150; int dir=(int)(Math.random()*4+1); int sc=0,sleep=15; int state = START; public static final int START = 1; public static final int RUNNING = 2; public static final int PAUSE = 3; public static final int OVER = 4; public static final int RESTART = 5; Color c; @Override public void paint(Graphics g) { String score="分数:"+String.valueOf(sc); super.paint(g); g.setColor(c); g.fillOval(bx, by, 100, 100); g.setColor(Color.black); g.fillRect( px, 700, 200, 10); g.drawRoundRect(0, 0, 100, 30, 5, 5); g.setFont(new Font("TimesRoman",Font.BOLD,24)); g.drawString(score, 10, 20); if(state==START){ g.setColor(Color.blue); g.drawString("点击屏幕开始游戏", 300, 200); } if(state==PAUSE){ g.setColor(Color.orange); g.drawString("游戏已经暂停", 300, 200); } if(state==OVER){ g.setColor(Color.RED); g.drawString("游戏结束,点击屏幕开始新游戏", 250, 200); } } public void move(){ new Thread(){ public void run() { super.run(); while(true){ if(state==RUNNING){ switch (dir) { case 1:{ bx+=10; by+=10; break; }case 2:{ bx-=10; by+=10; break; }case 3:{ bx-=10; by-=10; break; }case 4:{ bx+=10; by-=10; break; }default: break; } //边界值 if(bx>900){ if(dir==1){dir=2;} if(dir==4){dir=3;} } if(bx<0){ if(dir==2){dir=1;} if(dir==3){dir=4;} } if(by>600){ if(px-100<bx&&bx<(px+200)){ c=new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256)); if(dir==1){dir=4;} if(dir==2){dir=3;} sc++; } else{ by=600; // JOptionPane.showMessageDialog(null, "你好,游戏已结束,你得了"+sc+"分!", "游戏结束", JOptionPane.INFORMATION_MESSAGE); state=OVER; } } if(by<0){ if(dir==4){dir=1;} if(dir==3){dir=2;} } repaint(); try { Thread.sleep(sleep-(sc/10)); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } }; }.start(); } @Override public void mouseDragged(MouseEvent e) { // TODO 自动生成的方法存根 } @Override public void mouseMoved(MouseEvent e) { if(e.getX()<770){ px=e.getX()-50; } } @Override public void mouseClicked(MouseEvent e) { if(state==START){ state=RUNNING; }else if(state==RUNNING){ state=PAUSE; }else if(state==PAUSE){ state=RUNNING; }else if(state==OVER){ px=300;bx=500;by=100; dir=(int)(Math.random()*4+1); sc=0;sleep=15; state=RUNNING; } repaint(); } @Override public void mousePressed(MouseEvent e) { // TODO 自动生成的方法存根 } @Override public void mouseReleased(MouseEvent e) { // TODO 自动生成的方法存根 } @Override public void mouseEntered(MouseEvent e) { if(state==PAUSE){ state=RUNNING; repaint(); } } @Override public void mouseExited(MouseEvent e) { if(state==RUNNING){ state=PAUSE; repaint(); } } }
游戏虽然简单,却写的很艰难,可以看到自己还有很大差距,接下来继续努力!
欢迎访问我的网站: 嘻嘻留言板