1 / 10

國立北門高中 電腦遊戲程式設計

國立北門高中 電腦遊戲程式設計. 遊戲案例: 打磚塊. 本案例出自 http://www.greenfoot.org/scenarios/60 ( 南港高中高慧君老師 ). 遊戲情境. 用下方的桿子接球,打完所有磚塊就過關 。. 場景與演員. 新增場景 BrickWorld 類別,設定圖像 space.jpg 新增演員 Ball 類別,設定圖像 ball.png 新增演員 Brick 類別,設定圖像 brick.png 新增演員 Paddle 類別,設定圖像 paddle.png. 編輯 BrickWorld 類別. public BrickWorld()

Télécharger la présentation

國立北門高中 電腦遊戲程式設計

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 國立北門高中 電腦遊戲程式設計

  2. 遊戲案例: 打磚塊 本案例出自http://www.greenfoot.org/scenarios/60 (南港高中高慧君老師)

  3. 遊戲情境 • 用下方的桿子接球,打完所有磚塊就過關 。

  4. 場景與演員 • 新增場景BrickWorld類別,設定圖像space.jpg • 新增演員Ball類別,設定圖像ball.png • 新增演員Brick類別,設定圖像brick.png • 新增演員Paddle類別,設定圖像paddle.png

  5. 編輯BrickWorld類別 public BrickWorld() { super(400, 300, 1); addObject(new Ball(), 200, 150); addObject(new Paddle(), 200, 280); for (int i=0; i< 8; i++) { for (int j=0; j< 4; j++) { addObject(new Brick(), 50+i*40, 20+j*30); } } }

  6. 讓球會移動,遇到邊緣會反彈 import greenfoot.*; public class Ball extends Actor { private int motionX=2; private int motionY=2; public void act() { int newX; int newY; newX = getX() + motionX; newY = getY() + motionY; if (newX > 400) { motionX = -2; } else if (newX < 0) { motionX = 2; } if (newY > 300) { motionY = -2; } else if (newY < 0) { motionY = 2; } setLocation(newX, newY); } }

  7. 讓球碰到磚塊會打掉磚塊 • 新增Ball的act()程式碼 Actor brick = getOneIntersectingObject(Brick.class); if (brick != null) { motionY = -motionY; getWorld().removeObject(brick); }

  8. 讓板子能跟著滑鼠左右移動 • 修改Paddle的act()程式碼 public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse!= null) { setLocation(mouse.getX(), getY()); } }

  9. 球碰到板子會反彈 • 新增Ball的act()程式碼 Actor paddle = getOneIntersectingObject(Paddle.class); if (paddle != null) { motionY = -motionY; }

  10. 若打完所有磚塊,則遊戲結束 • 增加brickHit變數來記錄打下的磚塊數目。 • 修改Ball的act()程式碼 private int brickHit=0; Actor brick = getOneIntersectingObject(Brick.class); if (brick != null) { motionY = -motionY; brickHit++; getWorld().removeObject(brick); } if (brickHit==32) { Greenfoot.stop(); }

More Related