弹珠小程序
来源:互联网 发布:数据挖掘与r语言 源码 编辑:程序博客网 时间:2024/05/07 07:13
弹珠小程序
import java.util.*;import java.awt.*;import java.awt.event.*;import java.awt.event.KeyAdapter.*;import java.awt.event.WindowAdapter.*;public class PinBall{private final int WIDTH = 300;private final int HEIGHT = 400;private final int RACKET_Y = 340;private final int RACKET_HEIGHT = 20;private final int RACKET_WIDTH = 60;private final int BALL_SIZE = 16;private Frame frame = new Frame("ball game");Random rand = new Random();private int ySpeed = 10;private double xyRate = rand.nextDouble() - 0.5;private double xSpeed = (int)(ySpeed * xyRate *2);private int ballX = rand.nextInt(200) + 20;private int ballY = rand.nextInt(10) + 20;private int racketX = rand.nextInt(200);private MyCanvas tableArea = new MyCanvas();private String shape = "";javax.swing.Timer timer;private boolean isLose = false;class MyCanvas extends Canvas{public void paint(Graphics g){if(isLose){g.setColor(new Color(255, 0, 0));g.setFont(new Font("Times", Font.BOLD, 30));g.drawString("game over", 50, 200);}else{g.setColor(new Color(240, 240, 80));g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);g.setColor(new Color(80, 80, 200));g.fillRect(racketX, RACKET_Y, RACKET_WIDTH, RACKET_HEIGHT);}}}public void init(){WindowAdapter windowAdapter = new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}};tableArea.setPreferredSize(new Dimension(WIDTH, HEIGHT));frame.add(tableArea);frame.addWindowListener(windowAdapter);KeyAdapter keyProcessor = new KeyAdapter(){public void keyPressed(KeyEvent ke){if(ke.getKeyCode() == KeyEvent.VK_LEFT){if(racketX > 0)racketX -= 10;}if(ke.getKeyCode() == KeyEvent.VK_RIGHT){if(racketX < WIDTH - RACKET_WIDTH)racketX += 10;}}};frame.addKeyListener(keyProcessor);tableArea.addKeyListener(keyProcessor);ActionListener taskPerformer = new ActionListener(){public void actionPerformed(ActionEvent evt){if(ballX <= 0 || ballX >= WIDTH - BALL_SIZE){xSpeed = -xSpeed;}else if(ballY >= RACKET_Y - BALL_SIZE && (ballX < racketX || ballX > racketX + RACKET_WIDTH)){timer.stop();isLose = true;tableArea.repaint();}else if(ballY <= 0 || (ballY >= RACKET_Y - BALL_SIZE && ballX > racketX && ballX <= racketX + RACKET_WIDTH)){ySpeed = -ySpeed;}ballY += ySpeed;ballX += xSpeed;tableArea.repaint();}};timer = new javax.swing.Timer(100, taskPerformer);timer.start();frame.pack();frame.setVisible(true);}public static void main(String[] args){new PinBall().init();}}
- 弹珠小程序
- Java趣味小程序:打弹珠
- Batch 批处理小弹珠
- 无尽弹珠
- bzoj4172: 弹珠
- 口袋妖怪弹珠台 是什么
- UVA10474-弹珠在哪里
- 无尽弹珠(数学题)
- bzoj-4172 弹珠
- ZZULIOJ 1790 弹珠游戏
- P2356 弹珠游戏
- BZOJ1709 超级弹珠SuperPaintball
- 小程序
- 小程序
- 小程序
- 小程序
- 小程序
- 小程序
- 2010年中兴面试题
- Driver Development Part 1: Introduction to Drivers
- 至广大Java学习者的一封信
- s3c2440A操作24C08
- u-boot-2009.08在2440上的移植详解(五)
- 弹珠小程序
- Oracle is #1 in the RDBMS Market Share Worldwide for 2012
- 设计模式之二:观察者模式
- opencv 笔记02Core_Scan
- dialog,activity 屏蔽Home键详解
- 13 Table View javafx
- 拷贝构造函数作用测试
- 在Eclipse中配置tomcat
- Direct3D初学总结