西南交通大學java課程設(shè)計_第1頁
西南交通大學java課程設(shè)計_第2頁
西南交通大學java課程設(shè)計_第3頁
西南交通大學java課程設(shè)計_第4頁
西南交通大學java課程設(shè)計_第5頁
已閱讀5頁,還剩18頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、JAVA綜合實驗:滑板反射小球游戲?qū)I(yè):電子科學與技術(shù)(微電子方向)學號:20132116姓名:李瑞婷2014-2015第二學期源代碼:ball.javapackage ;import ;import ;import ;import ;public class Ball extends BallComponent / 定義球的豎向速度private int speedY = 10;/ 定義彈球的橫向速度private int speedX = 8;/ 定義是否在運動private boolean started = false;/ 定義是否結(jié)束運動private boolean stop =

2、false;/* * m 有參數(shù)構(gòu)造器 * * param panelWidth * int 畫板寬度 * param panelHeight * int 畫板高度 * param offset * int 位移 * param path * String 圖片路徑 */public Ball(int panelWidth, int panelHeight, int offset, String path)throws IOException / 調(diào)用父構(gòu)造器super(panelWidth, panelHeight, path);/ 設(shè)置y坐標this.setY(panelHeight -

3、super.getImage().getHeight(null) - offset);/* * 設(shè)置橫向速度 * * param speed * int 速度 * return void */public void setSpeedX(int speed) this.speedX = speed;/* * 設(shè)置豎向速度 * * param speed * int 速度 * return void */public void setSpeedY(int speed) this.speedY = speed;/* * 設(shè)置是否在運動 * * param b * boolean * return v

4、oid */public void setStarted(boolean b) this.started = b;/* * 設(shè)置是否結(jié)束運動 * * param b * boolean * return void */public void setStop(boolean b) this.stop = b;/* * 返回橫向速度 * * return int 速度 */public int getSpeedX() return this.speedX;/* * 返回豎向速度 * * return int 速度 */public int getSpeedY() return this.speed

5、Y;/* * 是否在運動 * * return boolean 是否在運動 */public boolean isStarted() return this.started;/* * 是否已經(jīng)結(jié)束運動 * * return boolean 是否已經(jīng)結(jié)束運動 */public boolean isStop() return this.stop;BallComponentpackage ;import ;import ;import ;import ;public class BallComponent / 設(shè)置x坐標private int x = -1;/ 設(shè)置y坐標private int y

6、= -1;/ 設(shè)置圖片private Image image = null;/ 設(shè)置圖片速度private int speed = 5;public BallComponent(String path) throws IOException super();this.image = ImageIO.read(new File(path);/* * 構(gòu)造器 * * param panelWidth * int 畫板寬度 * param panelHeight * int 畫板高度 * param path * String 圖片路徑 */public BallComponent(int pane

7、lWidth, int panelHeight, String path)throws IOException super();/ 讀取圖片this.image = ImageIO.read(new File(path);/ 設(shè)置x坐標this.x = (int) (panelWidth - image.getWidth(null) / 2);/* * 構(gòu)造器 * * param x * int 圖像的x坐標 * param y * int 圖像的y坐標 * param path * String 圖片路徑 */public BallComponent(String path, int x,

8、int y) throws IOException super();/ 讀取圖片this.image = ImageIO.read(new File(path);this.x = x;this.y = y;/* * 獲取x坐標 * * return int x坐標 */public int getX() return this.x;/* * 獲取y坐標 * * return int y坐標 */public int getY() return this.y;/* * 獲取圖片速度 * * return int 圖片速度 */public int getSpeed() return this.s

9、peed;/* * 設(shè)置x坐標 * * param x * int x坐標 * return void */public void setX(int x) this.x = x;/* * 設(shè)置y坐標 * * param y * int y坐標 * return void */public void setY(int y) this.y = y;/* * 返回圖片 * * return Image 圖片 */public Image getImage() return this.image;BallFramepackage ;import ;import ;import ;import ;imp

10、ort ;import ;import ;import ;import ;import ;import ;import ;import ;public class BallFrame extends JFrame / 定義JPanel的寬度private final int BALLPANEL_WIDTH = 307;/ 定義JPanel的高度private final int BALLPANEL_HEIGHT = 400;/ 定義畫板private BallPanel ballPanel = null;/ 定義檔板/ private Image stick = null;/ 設(shè)置檔板x坐標p

11、rivate int stickX = -1;/ 創(chuàng)建一個BallService實例private BallService service = null;/ 定義一個timerTimer timer = null;/* * 默認構(gòu)造器 */public BallFrame() throws IOException super();/ 初始化initialize();/* * 初始化界面 * * return void */public void initialize() throws IOException / 設(shè)置窗口的標題this.setTitle("彈球");/ 設(shè)置

12、為不可改變大小this.setResizable(false);/ 設(shè)置背景為黑色this.setBackground(Color.BLACK);/ 獲取畫板ballPanel = getBallPanel();/ 創(chuàng)建BallService實例service = new BallService(this, BALLPANEL_WIDTH, BALLPANEL_HEIGHT);/ 定義每0.1秒執(zhí)行一次監(jiān)聽器ActionListener task = new ActionListener() public void actionPerformed(ActionEvent e) / 開始改變位置

13、service.run();/ 刷新畫板ballPanel.repaint();/ 如果timer不為空if (timer != null) / 重新開始timertimer.restart(); else / 新建一個timertimer = new Timer(100, task);/ 開始timertimer.start();this.add(ballPanel);/ 增加事件監(jiān)聽器KeyListener klarr = this.getKeyListeners();if (klarr.length = 0) / 定義鍵盤監(jiān)聽適配器KeyListener keyAdapter = new

14、 KeyAdapter() public void keyPressed(KeyEvent ke) / 改變檔板的坐標service.setStickPos(ke);this.addKeyListener(keyAdapter);/* * 獲取畫板 * * return BallPanel 返回BallPanle */public BallPanel getBallPanel() if (ballPanel = null) / 新建一個畫板ballPanel = new BallPanel();/ 設(shè)置畫板的大小ballPanel.setPreferredSize(new Dimension(

15、BALLPANEL_WIDTH,BALLPANEL_HEIGHT);return ballPanel;/ 定義一個JPanel內(nèi)部類來完成畫圖功能public class BallPanel extends JPanel /* * 重寫void paint( Graphics g )方法 * * param g * Graphics * return void */public void paint(Graphics g) / 畫圖service.draw(g);BallGamepackage ;import ;import ;public class BallGame /* * 開始游戲 *

16、 * return void */public static void main(String args) throws IOException BallFrame ballFrame = new BallFrame();ballFrame.pack();ballFrame.setVisible(true);ballFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);BallServicepackage ;import ;import ;import ;import ;public class BallService / 定義一個Stick

17、(檔板)private Stick stick = null;/ 定義一個彈球private Ball ball = null;/ 定義一個游戲結(jié)束圖片private BallComponent gameOver = null;/ 定義一個贏了游戲的圖片private BallComponent won = null;/ 定義一個磚塊圖片數(shù)組private Brick bricks = null;private int width;private int height;BallFrame ballFrame = null;/* * 私有空構(gòu)造器 */private BallService()

18、super();/* * * param frame * JFrame JFrame實例 * param width * int 寬 * param height * int 高 * return BallService */public BallService(BallFrame frame, int width, int height)throws IOException / 初始化變量this.width = width;this.height = height;this.ballFrame = frame;/ 創(chuàng)建一個Stick(檔板)實例stick = new Stick(width

19、, height, "img/stick.jpg");/ 創(chuàng)建一個彈球的實例ball = new Ball(width, height, stick.getImage().getHeight(null),"img/ball.gif");/ 游戲結(jié)束圖片gameOver = new BallComponent("img/over.gif");/ 贏圖片won = new BallComponent("img/win.gif");/ 磚塊圖片數(shù)組bricks = createBrickArr("img/bri

20、ck.gif", 11, 6);/* * run * * return void */public void run() / 彈球坐標改變setBallPos();/ 道具坐標改改變setMagicPos();/* * 設(shè)置檔板圖片的位置 * * param ke * KeyEvent 鍵盤事件 * return void */public void setStickPos(KeyEvent ke) / 把彈球的運動狀態(tài)設(shè)為trueball.setStarted(true);/ 如果是左方向鍵if (ke.getKeyCode() = KeyEvent.VK_LEFT) if (st

21、ick.getX() - stick.SPEED > 0) / x坐標向左移動stick.setX(stick.getX() - stick.SPEED);/ 如果是右方向鍵if (ke.getKeyCode() = KeyEvent.VK_RIGHT) if (stick.getX() + stick.SPEED < width - stick.getPreWidth() / x坐標向右移動stick.setX(stick.getX() + stick.SPEED);/ ballFrame.getBallGame().reStart( ballFrame );/ 如果是F2鍵if

22、 (ke.getKeyCode() = KeyEvent.VK_F2) / 初始化ballFrametry ballFrame.initialize(); catch (IOException e) System.out.println(e.getMessage();/* * 設(shè)置小球圖片的位置 * * return void */public void setBallPos() / 正數(shù)的數(shù)度int absSpeedX = Math.abs(ball.getSpeedX();int absSpeedY = Math.abs(ball.getSpeedY();/ 如果游戲已經(jīng)開始而且沒有結(jié)束i

23、f (ball.isStarted() / 如果小球碰到左邊界if (ball.getX() - absSpeedX < 0) / 重新設(shè)置x坐標ball.setX(ball.getImage().getWidth(null);/ 把x方向的速度設(shè)為反方向ball.setSpeedX(-ball.getSpeedX();/ 如果小球碰到右邊界if (ball.getX() + absSpeedX > width- ball.getImage().getWidth(null) / 重新設(shè)置x坐標ball.setX(width - ball.getImage().getWidth(nu

24、ll) * 2);/ 把x方向的速度設(shè)為反方向ball.setSpeedX(-ball.getSpeedX();/ 如果小球碰到上邊界if (ball.getY() - absSpeedY < 0) / 重新設(shè)置y坐標ball.setY(ball.getImage().getWidth(null);/ 把y方向的速度設(shè)為反方向ball.setSpeedY(-ball.getSpeedY();/ 如果小球碰到下邊界if (ball.getY() + absSpeedY > height- stick.getImage().getHeight(null) / 如果小球與檔板有碰撞if

25、(isHitStick(ball) / 重新設(shè)置y坐標ball.setY(height - ball.getImage().getHeight(null) * 2);/ 把y方向的速度設(shè)為反方向ball.setSpeedY(-ball.getSpeedY();/ 與磚塊碰撞后的運動for (int i = bricks.length - 1; i > -1; i-) for (int j = bricksi.length - 1; j > -1; j-) / 如果小球與磚塊有碰撞if (isHitBrick(bricksij) if (ball.getSpeedY() > 0

26、) ball.setSpeedY(-ball.getSpeedY();/ 結(jié)束游戲if (ball.getY() > height) ball.setStop(true);/ 設(shè)置x坐標ball.setX(ball.getX() - (int) (Math.random() * 2)- ball.getSpeedX();/ 設(shè)置y坐標ball.setY(ball.getY() - (int) (Math.random() * 2)- ball.getSpeedY();/* * 小球與磚塊是否有碰撞 * * return boolean */public boolean isHitBric

27、k(Brick brick) if (brick.isDisable() return false;/ ball的圓心x坐標double ballX = ball.getX() + ball.getImage().getWidth(null) / 2;/ ball的圓心y坐標double ballY = ball.getY() + ball.getImage().getHeight(null) / 2;/ brick的中心x坐標double brickX = brick.getX() + brick.getImage().getWidth(null) / 2;/ brick的中心y坐標doub

28、le brickY = brick.getY() + brick.getImage().getHeight(null) / 2;/ 兩個坐標點的距離double distance = Math.sqrt(Math.pow(ballX - brickX, 2)+ Math.pow(ballY - brickY, 2);/ 如果兩個圖形重疊,返回true;if (distance < (ball.getImage().getWidth(null) + brick.getImage().getWidth(null) / 2) / 使brick無效brick.setDisable(true);r

29、eturn true;return false;/* * BallComponent是否與檔板有碰撞 * * param image * BallComponent 圖像 * return boolean */public boolean isHitStick(BallComponent bc) / 獲取圖片對象Image tempImage = bc.getImage();/ 如果與檔板有碰撞if (bc.getX() + tempImage.getWidth(null) > stick.getX()&& bc.getX() < stick.getX() + st

30、ick.getPreWidth()&& bc.getY() + tempImage.getHeight(null) > stick.getY() return true;return false;/* * 設(shè)置道具的位置 * * return void */public void setMagicPos() for (int i = 0; i < bricks.length; i+) for (int j = 0; j < bricksi.length; j+) / 獲取magicMagic magic = bricksij.getMagic();if (ma

31、gic != null) / 如果這個brick的狀態(tài)是無效的if (bricksij.isDisable() && magic.getY() < height) / 設(shè)置magic的y坐標向下增加magic.setY(magic.getY() + magic.getSpeed();/ 設(shè)置檔板的寬度setStickWidth(magic);/* * 設(shè)置檔板的長度 * * param magic * Magic 道具 * return void */public void setStickWidth(Magic magic) if (isHitStick(magic)

32、/ 道具的作用magic.magicDo(stick);/* * 判斷是否贏了 * * return boolean */public boolean isWon() / 如果消了全部磚塊,則為贏for (int i = 0; i < bricks.length; i+) for (int j = 0; j < bricksi.length; j+) if (!bricksij.isDisable() return false;return true;/* * 創(chuàng)建一個類型為Brick的數(shù)組 * * param path * String 圖像路徑 * param xSize *

33、int * param ySize * int * return Brick */public Brick createBrickArr(String path, int xSize, int ySize)throws IOException / 創(chuàng)建一個BrickBrick bricks = new BrickxSizeySize;int x = 0;int y = 0;int random = 0;int imageSize = 28;boolean isDisable = false;/ 迭代初始化數(shù)組for (int i = 0; i < xSize; i+) for (int

34、j = 0; j < ySize; j+) / 創(chuàng)建一個新的磚塊random = (int) (Math.random() * 3);x = i * imageSize;y = j * imageSize;/ 一定機率沒有磚塊isDisable = Math.random() > 0.8 ? true : false;if (isDisable) random = 0;Brick brick = new Brick(path, random, x, y);brick.setDisable(isDisable);/ 設(shè)置x坐標brick.setX(x);/ 設(shè)置y坐標brick.se

35、tY(y);bricksij = brick;return bricks;/* * 畫圖 * * param g * Graphics 用來畫圖的對象 * return void */public void draw(Graphics g) / 如果贏了if (isWon() / 繪制贏的圖片g.drawImage(won.getImage(), won.getX(), won.getY(), width,height - 10, null); else if (ball.isStop() / 繪制游戲結(jié)束圖像g.drawImage(gameOver.getImage(), gameOver.

36、getX(), gameOver.getY(),width, height - 10, null); else / 清除原來的圖像g.clearRect(0, 0, width, height);/ 繪制檔板圖像g.drawImage(stick.getImage(), stick.getX(), stick.getY(), stick.getPreWidth(), stick.getImage().getHeight(null), null);/ 繪制彈球圖像g.drawImage(ball.getImage(), ball.getX(), ball.getY(), null);/ 迭代繪制磚塊圖像for (int i = 0; i < bricks.length; i+) for (int j = 0; j < bricksi.length; j+) BallComponent magic = bricksij.getMagic();/ 如果這個磚塊圖像對像是有效的if (!bricksij.isDisable() / 里面的數(shù)字1為磚塊圖像間的間隙g.drawImage(bricksij.getImage(), bricksij.getX(), bri

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論