Java -- 乒乓球 乒乓彈球遊戲__Java

來源:互聯網
上載者:User

《瘋狂Java講義》 練習遊戲

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.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.Timer;public class PinBall {/** * @param args *///案頭長寬private final int TABLE_WIDTH = 300;private final int TABLE_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 f = new Frame("PinBall");Random rand = new Random();//小球縱向的運行速度private int ySpeed = 10; private double xyRate = rand.nextDouble() - 0.5;private int 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 String shape = "";Timer timer;private boolean isLose = false;private MyCanvas tableArea = new MyCanvas();public void init(){tableArea.setPreferredSize(new Dimension(TABLE_WIDTH, TABLE_HEIGHT));f.add(tableArea);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 < TABLE_WIDTH - RACKET_WIDTH )racketX += 10;}}};f.addKeyListener(keyProcessor);tableArea.addKeyListener(keyProcessor);ActionListener taskPerformer = new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif( ballX <= 0 || ballX >= TABLE_WIDTH-BALL_SIZE ){xSpeed = -xSpeed;}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();}};f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}});timer = new Timer(100, taskPerformer);timer.start();f.pack();f.setVisible(true);}public static void main(String[] args) {// TODO Auto-generated method stubnew PinBall().init();}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);}}}}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.