查看: 7595| 回复: 33
跳转到指定楼层
上一主题 下一主题
收起左侧

Stanford CS106A #3 Assignment3讨论贴

全局:
本帖最后由 yiyiyaya 于 2012-8-4 19:43 编辑

时间:2012.07.31-2012.8.5

课程:
9.  Stylistic expectations engineering for reuse using javadoc
10.The acm.graphics package
11.Event driven programming

作业:
Assignment 3 Breakout.java
due: 2012.8.5

建议:
课程是可以很快看完的,关键是这次的作业,算是比较大型的了,要求设计的是一个我们小时候都玩过的弹球的游戏。
具体作业要求见pdf,一步一步写的很详细,可以分步实现。建议一天实现一部分
官网上还给出了一个demo,大家可以先去运行一下以便更直观的了解设计的内容:http://www.stanford.edu/class/cs106a/进入Assignments就可以看到了
加油!这个作业完成了还是挺有成就感的!
可以参见bouncingball和UFO的程序
单选投票, 共有 20 人参与投票
您所在的用户组没有投票权限

评分

参与人数 1大米 +100 学分 +1 收起 理由
midfirefly + 100 + 1 组织学习辛苦了!大家加油!

查看全部评分


上一篇:[Udacity CS 262] Programming Languages (Week #1)
下一篇:完成了Stanford算法课 第二期的筒子进来领取积分
🔗
zhuolinliu8 2012-7-30 23:22:20 | 只看该作者
全局:
上次发的代码怎么没有缩进。。。这次的刚编好。。四个判边界点的误差有时会出现问题,球好像有点太大了。总之编的比较粗糙。。
  1. /*
  2. * File: Breakout.java
  3. * -------------------
  4. * Name:
  5. * Section Leader:
  6. *
  7. * This file will eventually implement the game of Breakout.
  8. */

  9. import acm.graphics.*;
  10. import acm.program.*;
  11. import acm.util.*;

  12. import java.applet.*;
  13. import java.awt.*;
  14. import java.awt.event.*;

  15. public class Breakout extends GraphicsProgram {

  16.         /** Width and height of application window in pixels */
  17.         public static final int APPLICATION_WIDTH = 400;
  18.         public static final int APPLICATION_HEIGHT = 600;

  19.         /** Dimensions of game board (usually the same) */
  20.         private static final int WIDTH = APPLICATION_WIDTH;
  21.         private static final int HEIGHT = APPLICATION_HEIGHT;

  22.         /** Dimensions of the paddle */
  23.         private static final int PADDLE_WIDTH = 60;
  24.         private static final int PADDLE_HEIGHT = 10;

  25.         /** Offset of the paddle up from the bottom */
  26.         private static final int PADDLE_Y_OFFSET = 30;

  27.         /** Number of bricks per row */
  28.         private static final int NBRICKS_PER_ROW = 10;

  29.         /** Number of rows of bricks */
  30.         private static final int NBRICK_ROWS = 10;

  31.         /** Separation between bricks */
  32.         private static final int BRICK_SEP = 4;

  33.         /** Width of a brick */
  34.         private static final int BRICK_WIDTH =
  35.           (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;

  36.         /** Height of a brick */
  37.         private static final int BRICK_HEIGHT = 8;

  38.         /** Radius of the ball in pixels */
  39.         private static final int BALL_RADIUS = 8;

  40.         /** Offset of the top brick row from the top */
  41.         private static final int BRICK_Y_OFFSET = 70;

  42.         /** Number of turns */
  43.         private static final int NTURNS = 3;
  44.        
  45.         /**Time for ball action delay*/
  46.         private static final int DELAY = 10;
  47.        
  48.         private GRect paddle;
  49.         /** Track the mouse's position*/
  50.         private int mousex;
  51.         private GObject gpaddle;
  52.         private RandomGenerator rgen = RandomGenerator.getInstance();
  53.         /** The velocity  along each coordinate*/
  54.         private double vx,vy;
  55.         private GOval ball;
  56.         private int num_brick = NBRICKS_PER_ROW * NBRICK_ROWS;
  57.         /* Method: init() */
  58.         /** Sets up the Breakout program. */
  59.         public void init() {
  60.                 addMouseListeners();
  61.         }
  62.        
  63.         /* Method: run() */
  64.         /** Runs the Breakout program. */
  65.         public void run() {
  66.                
  67.                 setupBricks();
  68.                 int t = NTURNS;
  69.                 while(t > 0 && num_brick > 0){
  70.                         createPaddle();
  71.                         createBall();
  72.                         while(ball.getY() < getHeight()+2*BALL_RADIUS && num_brick > 0){
  73.                                 moveBall();
  74.                                 checkforCollision();
  75.                                 pause(DELAY);
  76.                         }
  77.                         if (num_brick > 0 && t > 1) remove(paddle);
  78.                         t--;
  79.                 }
  80.         }
  81.        
  82.         /** Set up Bricks */
  83.         private void setupBricks(){
  84.                 for(int i = 1, yy = BRICK_Y_OFFSET; i<=NBRICK_ROWS; i++, yy += BRICK_HEIGHT + BRICK_SEP){
  85.                         Color c = Color.RED;
  86.                         switch((i-1)/2%5){
  87.                                 case 0: c = Color.RED; break;
  88.                                 case 1: c = Color.ORANGE; break;
  89.                                 case 2: c = Color.YELLOW; break;
  90.                                 case 3: c = Color.GREEN; break;
  91.                                 case 4: c = Color.CYAN; break;
  92.                         }
  93.                         for(int j = 1, xx = 0; j<=NBRICKS_PER_ROW; j++,xx+= BRICK_WIDTH + BRICK_SEP ){
  94.                                 GRect brick = new GRect(xx,yy,BRICK_WIDTH,PADDLE_HEIGHT);
  95.                                 brick.setFilled(true);
  96.                                 brick.setFillColor(c);
  97.                                 add(brick);
  98.                         }
  99.                 }       
  100.         }
  101.        
  102.         /** Create the Paddle */
  103.         private void createPaddle(){
  104.                 paddle = new GRect(getWidth()/2-PADDLE_WIDTH/2,getHeight()-PADDLE_Y_OFFSET,PADDLE_WIDTH,PADDLE_HEIGHT);
  105.                 paddle.setFilled(true);
  106.                 add(paddle);       
  107.         }
  108.         /** Create the Ball */
  109.         private void createBall(){
  110.                 ball = new GOval(getWidth()/2-BALL_RADIUS,getHeight()-PADDLE_Y_OFFSET-BALL_RADIUS*2,BALL_RADIUS*2,BALL_RADIUS*2);
  111.                 ball.setFilled(true);
  112.                 add(ball);
  113.                 vx = rgen.nextDouble(1.0,3.0);
  114.                 if (rgen.nextBoolean(0.5)) vx = -vx;
  115.                 vy = -3;
  116.         }
  117.         /** Move Ball */
  118.         private void moveBall(){
  119.                 ball.move(vx, vy);
  120.         }
  121.         /** Check if the collision happens */
  122.         private void checkforCollision(){
  123.                 double by = ball.getY();
  124.                 double bx = ball.getX();
  125.                 /** Check top */
  126.                 if(by < 0){
  127.                         vy = -vy;
  128.                         ball.move(0, -2*by);       
  129.                 }               
  130.                 /** Check left */
  131.                 if (bx < 0){
  132.                         vx = -vx;
  133.                         ball.move(-2*bx, 0);
  134.                 }else if(bx > getWidth()-2*BALL_RADIUS){
  135.                         /** Check right */
  136.                         vx = -vx;
  137.                         double diff = bx -(getWidth()-2*BALL_RADIUS);
  138.                         ball.move(-2*diff , 0);
  139.                 }
  140.                
  141.                 /** Check if collide the paddle or collide bricks from top*/
  142.                 GObject gobj = getElementAt(bx+2*BALL_RADIUS, by+2*BALL_RADIUS);
  143.                 if( gobj != null){
  144.                         vy = -vy;
  145.                         double diff = by-(gobj.getY()-2*BALL_RADIUS);
  146.                         ball.move(0, -2*diff);
  147.                         if (gobj != paddle) {
  148.                                 remove(gobj);
  149.                                 num_brick--;
  150.                         }
  151.                         return;
  152.                 }
  153.                 /** Check if collide the blocks from Bottom*/
  154.                 gobj = getElementAt(bx,by);
  155.                 if( gobj != null && gobj != paddle){
  156.                         vy = -vy;
  157.                         double diff = by-(gobj.getY()+BRICK_HEIGHT+BALL_RADIUS);
  158.                         ball.move(0,-2*diff);
  159.                         remove(gobj);
  160.                         num_brick--;
  161.                         return;
  162.                 }               
  163.                 /** Check if collide the blocks from Left*/
  164.                 gobj = getElementAt(bx+2*BALL_RADIUS,by);
  165.                 if( gobj != null){
  166.                         vx = -vx;
  167.                         double diff = bx-(gobj.getX()-2*BALL_RADIUS);
  168.                         ball.move(-2*diff, 0);
  169.                         remove(gobj);
  170.                         num_brick--;
  171.                         return;
  172.                 }
  173.                 /** Check if collide the blocks from Right*/
  174.                 gobj = getElementAt(bx,by+2*BALL_RADIUS);
  175.                 if( gobj != null){
  176.                         vx = -vx;
  177.                         double diff = bx-(gobj.getX()+BRICK_WIDTH+BALL_RADIUS);
  178.                         ball.move(-2*diff,0);
  179.                         remove(gobj);
  180.                         num_brick--;
  181.                         return;
  182.                 }
  183.         }
  184.        
  185.         public void mousePressed(MouseEvent e){
  186.                 mousex = e.getX();
  187.                 gpaddle = getElementAt(mousex,getHeight()-PADDLE_Y_OFFSET);
  188.         }
  189.        
  190.         public void mouseDragged(MouseEvent e){
  191.                 if (gpaddle == paddle && mousex >=0 && mousex <=WIDTH-PADDLE_WIDTH)
  192.                         paddle.move(e.getX()-paddle.getX(), 0);
  193.                 mousex = e.getX();
  194.         }
  195.                
  196. }
复制代码
回复

使用道具 举报

🔗
nprotect 2012-8-3 12:55:46 | 只看该作者
全局:
领导能稍微把due的时间调后1-2天? 到这个周末可以吗?想把前面学过的东西稍微整理一下,总结一下,ASSIGMENT3后面就开始全新的内容了,Assig3也有不少问题。
回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-8-4 19:42:45 | 只看该作者
全局:
nprotect 发表于 2012-8-3 12:55
领导能稍微把due的时间调后1-2天? 到这个周末可以吗?想把前面学过的东西稍微整理一下,总结一下,ASSIGME ...

好,这次的作业挺难得,due的时间改到8月5号吧
回复

使用道具 举报

🔗
 楼主| yiyiyaya 2012-8-4 19:47:02 | 只看该作者
全局:
  1. import acm.graphics.*;
  2. import acm.program.*;
  3. import acm.util.*;

  4. import java.applet.*;
  5. import java.awt.*;
  6. import java.awt.event.*;

  7. public class Breakout extends GraphicsProgram {

  8. /** Width and height of application window in pixels */
  9.         public static final int APPLICATION_WIDTH = 400;
  10.         public static final int APPLICATION_HEIGHT = 600;

  11. /** Dimensions of game board (usually the same) */
  12.         private static final int WIDTH = APPLICATION_WIDTH;
  13.         private static final int HEIGHT = APPLICATION_HEIGHT;

  14. /** Dimensions of the paddle */
  15.         private static final int PADDLE_WIDTH = 60;
  16.         private static final int PADDLE_HEIGHT = 10;

  17. /** Offset of the paddle up from the bottom */
  18.         private static final int PADDLE_Y_OFFSET = 30;

  19. /** Number of bricks per row */
  20.         private static final int NBRICKS_PER_ROW = 10;

  21. /** Number of rows of bricks */
  22.         private static final int NBRICK_ROWS = 10;

  23. /** Separation between bricks */
  24.         private static final int BRICK_SEP = 4;

  25. /** Width of a brick */
  26.         private static final int BRICK_WIDTH =
  27.           (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;

  28. /** Height of a brick */
  29.         private static final int BRICK_HEIGHT = 8;

  30. /** Radius of the ball in pixels */
  31.         private static final int BALL_RADIUS = 10;

  32. /** Offset of the top brick row from the top */
  33.         private static final int BRICK_Y_OFFSET = 70;

  34. /** Number of turns */
  35.         private static final int NTURNS = 3;

  36. /* Method: run() */
  37. /** Runs the Breakout program. */
  38.         public void run() {
  39.                 setup();
  40.                 while(!gameOver){       
  41.                       moveBall();
  42.                 }
  43.         }
  44.     private void setup(){
  45.             createBricks();
  46.             createPaddle();   
  47.             createBall();
  48.             addMouseListeners();
  49.     }
  50.     private void createBricks() {
  51.             int x0=(WIDTH-BRICK_WIDTH*NBRICKS_PER_ROW-BRICK_SEP*(NBRICKS_PER_ROW-1))/2; //the starting x coordinate for the first brick in a row
  52.             int y0= BRICK_Y_OFFSET;
  53.            
  54.             for(int i=0;i<NBRICKS_PER_ROW;i++) {
  55.                     for(int j=0;j<NBRICK_ROWS;j++) {
  56.                             int x1 = (BRICK_WIDTH+BRICK_SEP)*i+x0;
  57.                         int y1 = (BRICK_HEIGHT+BRICK_SEP)*j+y0;
  58.                         GRect rect = new GRect(x1,y1,BRICK_WIDTH,BRICK_HEIGHT);
  59.                         add(rect);
  60.                         rect.setFilled(true);
  61.                         switch(j){
  62.                         case 0:
  63.                         case 1:
  64.                                 rect.setFillColor(Color.RED);
  65.                                 break;
  66.                         case 2:
  67.                         case 3:
  68.                                 rect.setFillColor(Color.ORANGE);
  69.                                 break;
  70.                         case 4:
  71.                         case 5:
  72.                                 rect.setFillColor(Color.YELLOW);
  73.                                 break;
  74.                         case 6:
  75.                         case 7:
  76.                                 rect.setFillColor(Color.GREEN);
  77.                                 break;
  78.                         case 8:
  79.                         case 9:
  80.                                 rect.setFillColor(Color.CYAN);
  81.                                 break;
  82.                         }
  83.                     }
  84.             }
  85.     }
  86.     private void createPaddle(){
  87.             int x = (WIDTH-PADDLE_WIDTH)/2;
  88.             int y = HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT;
  89.             paddle = new GRect(x,y,PADDLE_WIDTH,PADDLE_HEIGHT);
  90.             paddle.setFilled(true);
  91.             add(paddle);
  92.     }
  93.     private void createBall(){
  94.             int x0=WIDTH/2-BALL_RADIUS;
  95.             int y0=HEIGHT/2-BALL_RADIUS;
  96.             ball = new GOval(x0,y0,BALL_RADIUS*2,BALL_RADIUS*2);
  97.             ball.setFilled(true);
  98.             add(ball);
  99.             vy = 3.0;
  100.                 vx = rgen.nextDouble(1.0, 3.0);
  101.                 if(rgen.nextBoolean(0.5)){vx = -vx;}
  102.     }
  103.    
  104.     public void mouseMoved(MouseEvent e){
  105.                 int xLocation = (e.getX() - (PADDLE_WIDTH / 2));
  106.                
  107.                 if((xLocation) > 0 && xLocation < WIDTH - PADDLE_WIDTH){
  108.                         paddle.setLocation(xLocation, (HEIGHT - (PADDLE_Y_OFFSET + PADDLE_HEIGHT)));
  109.                 }//why it does not need to consider the situation that xLocation is larger than WIDTH-PADDLE_WIDTH
  110.         }

  111.         private void checkGameState(){
  112.                 if(lives == 0){
  113.                         gameOver = true;
  114.                         GLabel label = new GLabel("Game Over. Score:"+score, 0, 300);
  115.                         label.setFont("Serif-36");
  116.                         label.setColor(Color.RED);
  117.                         add(label);
  118.                 }
  119.                 if(brickAmount == 0){
  120.                         gameOver = true;
  121.                         GLabel label = new GLabel("Congratulations.", 200, 200);
  122.                         label.setFont("Serif");
  123.                         add(label);
  124.                 }
  125.         }
  126.    
  127.     private void moveBall(){
  128.             ball.move(vx, vy);
  129.             x = ball.getX();
  130.                 y = ball.getY();
  131.                 checkForCollisions(x,y);
  132.                 checkGameState();
  133.                 if(ball.getX() < 0 || ball.getX() > (WIDTH - BALL_RADIUS*2)){vx = -vx;}
  134.                 if(ball.getY() < 0 ){vy = -vy;}
  135.                 if(ball.getY() > (HEIGHT - (BALL_RADIUS*2))){
  136.                         newLife();
  137.                 }
  138.                 pause(10);
  139.     }
  140.     private void newLife(){
  141.             remove(ball);
  142.             lives--;
  143.             GLabel label = new GLabel(lives+" Lives Remained", 200, 200);
  144.             label.setFont("Serif");
  145.             add(label);
  146.             pause(1);
  147.             createBall();
  148.             moveBall();
  149.     }
  150.     private void checkForCollisions(double x, double y){
  151.                 if(getElementAt(x, y) != null){
  152.                         collider = getElementAt(x, y);
  153.                         if(collider == paddle) {
  154.                                 newLife();
  155.                         } else {
  156.                         vy=-vy;
  157.                         remove(collider);
  158.                         brickAmount--;
  159.                         score+=10;
  160.                         playAudio();
  161.                         }
  162.                 }
  163.                 else if(getElementAt(x + BALL_RADIUS*2, y) != null){
  164.                         collider = getElementAt(x + BALL_RADIUS*2, y);
  165.                         if(collider == paddle) {
  166.                                 newLife();
  167.                         } else {
  168.                         vy=-vy;
  169.                         remove(collider);
  170.                         brickAmount--;
  171.                         score+=10;
  172.                         playAudio();
  173.                         }
  174.                        
  175.                 }
  176.                 else if(getElementAt(x, y + BALL_RADIUS*2) != null){
  177.                         collider = getElementAt(x, y + BALL_RADIUS*2);
  178.                         removeBrick(collider);
  179.                 }
  180.                 else if(getElementAt(x + BALL_RADIUS*2, y + BALL_RADIUS*2) != null){
  181.                         collider = getElementAt(x + BALL_RADIUS*2, y + BALL_RADIUS*2);
  182.                         removeBrick(collider);
  183.                 }
  184.     }

  185.        
  186. /** Removes the appropriate brick */
  187.         private void removeBrick(GObject collider){
  188.                 vy = -vy;
  189.                 if(collider != paddle){
  190.                         remove(collider);
  191.                         brickAmount--;
  192.                         score+=10;
  193.                         playAudio();
  194.                 }
  195.         }

  196.     private void playAudio(){
  197.             AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
  198.             bounceClip.play();
  199.     }

  200.     private GRect paddle;
  201.     private GOval ball;
  202.     private GObject collider;
  203.     private double vx;
  204.     private double vy;
  205.     private double x, y;
  206.     private RandomGenerator rgen = RandomGenerator.getInstance();
  207.     private int brickAmount = NBRICKS_PER_ROW * NBRICK_ROWS;
  208.     private int lives = NTURNS;
  209.     private boolean gameOver = false;
  210.     private int score=0;
  211. }
复制代码

回复

使用道具 举报

🔗
nprotect 2012-8-5 22:10:55 | 只看该作者
全局:
本帖最后由 nprotect 于 2012-8-5 22:13 编辑

略蛋疼,写了2天多。
回复

使用道具 举报

🔗
zhuolinliu8 2012-8-5 22:15:17 | 只看该作者
全局:
nprotect 发表于 2012-8-5 22:10
略蛋疼,写了2天多。

加油~~~~这个应该是前六个assignment里面最麻烦的了
回复

使用道具 举报

🔗
nprotect 2012-8-5 22:16:41 | 只看该作者
全局:
  1. /*
  2. * File: Breakout.java
  3. * -------------------
  4. * Name:
  5. * Section Leader:
  6. *
  7. * This file will eventually implement the game of Breakout.
  8. */

  9. import acm.graphics.*;
  10. import acm.program.*;
  11. import acm.util.*;

  12. import java.applet.*;
  13. import java.awt.*;
  14. import java.awt.event.*;

  15. public class Breakout extends GraphicsProgram {

  16. /** Width and height of application window in pixels */
  17.         public static final int APPLICATION_WIDTH = 400;
  18.         public static final int APPLICATION_HEIGHT = 500;

  19. /** Dimensions of game board (usually the same) */
  20.         private static final int WIDTH = APPLICATION_WIDTH;
  21.         private static final int HEIGHT = APPLICATION_HEIGHT;

  22. /** Dimensions of the paddle */
  23.         private static final int PADDLE_WIDTH = 130;
  24.         private static final int PADDLE_HEIGHT = 10;

  25. /** Offset of the paddle up from the bottom */
  26.         private static final int PADDLE_Y_OFFSET = 80;

  27. /** Number of bricks per row */
  28.         private static final int NBRICKS_PER_ROW = 10;

  29. /** Number of rows of bricks */
  30.         private static final int NBRICK_ROWS = 10;

  31. /** Separation between bricks */
  32.         private static final int BRICK_SEP = 4;

  33. /** Width of a brick */
  34.         private static final int BRICK_WIDTH =(WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;

  35. /** Height of a brick */
  36.         private static final int BRICK_HEIGHT = 8;

  37. /** Radius of the ball in pixels */
  38.         private static final int BALL_RADIUS = 7;

  39. /** Offset of the top brick row from the top */
  40.         private static final int BRICK_Y_OFFSET = 70;

  41. /** Number of turns */
  42.         private static final int NTURNS = 3;

  43. /** Next are my static constant*/
  44.         private static final int DELAY = 10; // animation delay or pause time between ball moves
  45.         private static final int x_START_VELOCITY = 3; // starting x-coordinate velocity
  46.         private static final int y_START_VELOCITY = 5; // starting y-coordinate velocity

  47. /** Next are my own instance variables */
  48.         private GPoint last;  // variable "last" (x.getX(),y.getY()) stores the position of mouse
  49.         private GRect paddle; // create paddle
  50.         private GOval ball;   // create bouncing ball
  51.         private GRect brick;  // create bricks
  52.         private GLabel Label; // create label to show "win" or "lose"
  53.         private int yVELOCITY = y_START_VELOCITY; // y-coordinate velocity (variable)
  54.         private int xVELOCITY = x_START_VELOCITY; // x-coordinate velocity (setting to constant)
  55.         private int amount = 100; // amount of bricks

  56. /** Breakout program begins */
  57.         public void init(){    // event driven program
  58.                 addMouseListeners();
  59.         }
  60.         public void mouseEntered(MouseEvent e){  // listen to the mouse when enter the canvas
  61.                 last = new GPoint(e.getPoint());     // last store the mouse position
  62.         }
  63.         public void mouseMoved(MouseEvent e){
  64.                 if ((e.getX()>=PADDLE_WIDTH/2)&&(e.getX()<=APPLICATION_WIDTH-PADDLE_WIDTH/2)){
  65.                 paddle.setLocation(e.getX()-PADDLE_WIDTH/2, APPLICATION_HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT);
  66.                 paddle.move(e.getX()-last.getX(), 0);  
  67.                 last = new GPoint(e.getPoint());  // store last coordinate in variable "last"
  68.                 }
  69.                 else {
  70.                 paddle.move(0, 0);
  71.                 }
  72.         }
  73.        
  74.         public void run() {  // main method
  75.                 createBricks();
  76.                 createPaddle();
  77.                 createBall();
  78.                 gameCircle();
  79.                 }

  80.         private void createBricks(){  // method
  81.                 for (int i=0; i<NBRICKS_PER_ROW; i++){  // each row (for loop create 100 bricks)
  82.                         for (int j=0; j<NBRICK_ROWS; j++){  // each column
  83.                                 int x =(BRICK_WIDTH+BRICK_SEP)*i; // bricks' X-coordinates
  84.                                 int y = BRICK_Y_OFFSET+(BRICK_HEIGHT+BRICK_SEP)*j; // bricks' Y-coordinates
  85.                                 brick = new GRect (x, y, BRICK_WIDTH, BRICK_HEIGHT);
  86.                                 if ((j>=0)&&(j<=1)){
  87.                                 brick.setColor(Color.RED);
  88.                                 brick.setFilled(true);
  89.                                 add(brick);
  90.                                 }
  91.                                 else if((j>=2)&&(j<=3)){
  92.                                 brick.setColor(Color.ORANGE);
  93.                                 brick.setFilled(true);
  94.                                 add(brick);
  95.                                 }
  96.                                 else if((j>=4)&&(j<=5)){
  97.                                 brick.setColor(Color.YELLOW);
  98.                                 brick.setFilled(true);
  99.                                 add(brick);
  100.                                 }
  101.                                 else if((j>=6)&&(j<=7)){
  102.                                 brick.setColor(Color.GREEN);
  103.                                 brick.setFilled(true);
  104.                                 add(brick);
  105.                                 }
  106.                                 else if((j>=8)&&(j<=9)){
  107.                                 brick.setColor(Color.CYAN);
  108.                                 brick.setFilled(true);
  109.                                 add(brick);
  110.                                 }
  111.                         }
  112.                 }
  113.         }
  114.        
  115.         private void createPaddle(){ // method
  116.                 paddle = new GRect ((APPLICATION_WIDTH-PADDLE_WIDTH)/2, APPLICATION_HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT); //paddle's start position
  117.                 paddle.setFilled(true);
  118.                 paddle.setColor(Color.BLACK);
  119.                 add(paddle);
  120.         }
  121.        
  122.         private void createBall(){  // method
  123.                 ball = new GOval (2, APPLICATION_HEIGHT/2, BALL_RADIUS*2, BALL_RADIUS*2);
  124.                 ball.setFilled(true);
  125.                 add(ball);
  126.         }
  127.        
  128.         private void gameCircle(){ // method
  129.                 while (amount>0){
  130.                         moveBall();
  131.                         checkForSideCollision(); // this method contains "lose" condition
  132.                         checkForBrickCollision();
  133.                         pause(DELAY);
  134.                 }
  135.                 Label= new GLabel("You Win!", 180, 250); // "win" condition
  136.                 Label.setColor(Color.RED);
  137.                 add(Label);
  138.         }  

  139.         private void moveBall(){   // method
  140.                 ball.move(xVELOCITY, yVELOCITY);
  141.         }
  142.        
  143.         private void checkForSideCollision(){ // method
  144.                 if (ball.getY()+BALL_RADIUS*2>=getHeight()){ //bottom
  145.                         Label = new GLabel ("You Lose!", 180, 250);
  146.                         Label.setColor(Color.RED);
  147.                         add(Label);
  148.                 }
  149.                 else if (ball.getX()+BALL_RADIUS*2>=getWidth()){ //right side
  150.                         xVELOCITY = -xVELOCITY;                 
  151.                 }
  152.                 else if (0>=ball.getY()){ //top
  153.                         yVELOCITY = -yVELOCITY;
  154.                 }
  155.                 else if (0>=ball.getX()){ //left side
  156.                         xVELOCITY = -xVELOCITY;
  157.                 }
  158.         }
  159.        
  160.         private void checkForBrickCollision(){  // method
  161.                 GObject collObj1 = getElementAt(ball.getX()+BALL_RADIUS, ball.getY()-1); // upper point
  162.                 GObject collObj2 = getElementAt(ball.getX()+BALL_RADIUS*2+1, ball.getY()+BALL_RADIUS); //right point
  163.                 GObject collObj3 = getElementAt(ball.getX()+BALL_RADIUS, ball.getY()+BALL_RADIUS*2+1); //bottom point
  164.                 GObject collObj4 = getElementAt(ball.getX()-1, ball.getY()+BALL_RADIUS); //left point
  165.                 if (collObj3 == paddle){
  166.                         yVELOCITY = -yVELOCITY;
  167.                 }
  168.                 else if ((collObj1!=null)&&(collObj1!= paddle)){
  169.                         remove(collObj1);
  170.                         amount--;
  171.                         yVELOCITY = -yVELOCITY;
  172.                 }       
  173.                 else if ((collObj2!=null)&&(collObj2!= paddle)){
  174.                         remove(collObj2);
  175.                         amount--;
  176.                         xVELOCITY = -xVELOCITY;
  177.                 }
  178.                 else if ((collObj3!=null)&&(collObj3!= paddle)){
  179.                         remove(collObj3);
  180.                         amount--;
  181.                         yVELOCITY = -yVELOCITY;
  182.                 }
  183.                 else if ((collObj4!=null)&&(collObj4!= paddle)){
  184.                         remove(collObj4);
  185.                         amount--;
  186.                         xVELOCITY = -xVELOCITY;
  187.                 }
  188.         }
  189. } // main class ends

复制代码
回复

使用道具 举报

🔗
gotoo 2012-8-5 23:15:41 | 只看该作者
全局:
提个建议 大家以后不要一道题发一个帖子 一次作业所有题都发在一个帖子里 看起来比较清楚。
回复

使用道具 举报

🔗
nprotect 2012-8-6 01:22:48 | 只看该作者
全局:
gotoo 发表于 2012-8-5 23:15
提个建议 大家以后不要一道题发一个帖子 一次作业所有题都发在一个帖子里 看起来比较清楚。

好的哦,不过这次只有一题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表