Java實現簡單的彈球遊戲

本文實例為大傢分享瞭Java實現簡單的彈球遊戲的具體代碼,供大傢參考,具體內容如下

該程序主要是用於對java圖形化界面編程進行聯系,程序實現全部采用的是AWT包下的類。
程序僅做參考,供學習使用。

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.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Timer;

public class PinBallGame {
 //創建窗口對象
 private Frame frame = new Frame("彈球遊戲");
 
 //桌面寬度
 private final int TABLE_WTDTH = 300;
 //桌面高度
 private final int TABLE_HEIGHT = 400;
 
 //球拍的高度和寬度
 private final int RACKET_WIDTH = 60;
 private final int RACKET_HETGHT = 20;
 
 //小球的大小
 private final int BALL_SIZE = 16;
 
 //定義變量,記錄小球的坐標
 private int ballX = 120;
 private int ballY = 20;
 
 //定義變量,記錄小球在x和y方向上分別移動的速度
 private int speedY = 10;
 private int speedX = 5;
 
 //定義變量,記錄球拍的坐標
 private int racketX = 120;
 private final int racketY = 340;
 
 //定義變量,標識當前遊戲是否已結束
 private boolean isOver = false;
 
 //聲明一個定時器
 private Timer timer;
 
 //自定義一個類,繼承Canvas,充當畫佈
 @SuppressWarnings("serial")
 private class MyCanvas extends Canvas{
 
 @Override
 public void paint(Graphics g) {
  //TODO 在這裡繪制內容
  if (isOver) {
  //遊戲結束
  g.setColor(Color.BLUE);
  g.setFont(new Font("Times",Font.BOLD,30));
  g.drawString("遊戲結束!", 50, 200);
  
  }else{
  //遊戲中
  //繪制小球
  g.setColor(Color.RED);
  g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);
  //繪制球拍
  g.setColor(Color.PINK);
  g.fillRect(racketX, racketY, RACKET_WIDTH, RACKET_HETGHT);
  }
 }
 }
 
 //創建繪畫區域
 MyCanvas drawArea = new MyCanvas();
 
 public void init() {
 //組裝視圖,遊戲邏輯的控制
 //完成球拍坐標的變化
 KeyListener listener = new KeyAdapter() {
  
  @Override
  public void keyPressed(KeyEvent e) {
  //獲取當前按下的鍵
  int keyCode = e.getKeyCode();
  if (keyCode == KeyEvent.VK_LEFT) {
   //<- 應該向左移動
   if (racketX>0) {
   racketX -= 10;
   }
  }
  
  if (keyCode == KeyEvent.VK_RIGHT) {
   //-> 應該向右移動
   if (racketX <(TABLE_WTDTH - RACKET_WIDTH)) {
   racketX += 10;
   }
  }
  }
 };
 
 //給Frame和drawArea註測監聽器
 frame.addKeyListener(listener);
 drawArea.addKeyListener(listener);
 
 //小球坐標的控制
 ActionListener task = new ActionListener() {
  
  @Override
  public void actionPerformed(ActionEvent arg0) {
  //根據邊界范圍,修正速度
  if (ballX<=0 || ballX>=(TABLE_WTDTH-BALL_SIZE)) {
   speedX = -speedX;
  }
  
  if (ballY <= 0 || (ballY > racketY-BALL_SIZE && ballX>racketX && ballX < racketX+RACKET_WIDTH)) {
   speedY = -speedY;
  }
  
  if (ballY > racketY-BALL_SIZE && (ballX < racketX || ballX > racketX+RACKET_WIDTH)) {
   //當前小球超出瞭球拍的范圍,遊戲結束
   //停止定時器
   timer.stop();
   //修改遊戲是否結束的標記
   isOver = true;
   //重繪界面
   drawArea.repaint();
  }
  
  //更新小球的坐標,重繪界面
  ballX += speedX;
  ballY += speedY;
  //重繪界面
  drawArea.repaint();
  }
 };
 
 timer = new Timer(100, task);
 timer.start();
 
 drawArea.setPreferredSize(new Dimension(TABLE_WTDTH,TABLE_HEIGHT));
 frame.add(drawArea);
 
 //設置frame最佳大小,並可視
 frame.pack();
 frame.setVisible(true);
 frame.setLocationRelativeTo(null);
 
 frame.addWindowListener(new WindowAdapter() {
  
  @Override
  public void windowClosing(WindowEvent e) {
  System.exit(0);
  }
 });
 }

 public static void main(String[] args) {
 new PinBallGame().init();
 }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: