jjzjj

Java Breakout 游戏过早退出

coder 2024-03-22 原文

我正在介绍在线编程。但是,我被困在一项任务上。

作业是编写一个闯关游戏。我已经成功编写了 97% 的游戏。然而,游戏在移除所有积木之前停止。有时还剩 4 block 积木,有时是 11 block 。程序设计为在计分器到达所有积木都消失的点时停止,因此它必须提前到达该点。

我做错了什么?

编辑:内联代码。和改写的问题

/*
 * File: Breakout.java
 * -------------------
 * Name:Alex Godin
 * 
 * This file will eventually implement the game of Breakout.
 */

import acm.graphics.*;
import acm.program.*;
import acm.util.*;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Breakout extends GraphicsProgram {

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

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

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

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

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

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

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

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

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

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

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

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

/**pause time*/
   private static final int PAUSE_TIME = 3;

/**THE VALUE OF EACH BRICK*/
   private static final int BRICKVAL = 10;

/** ivar holding the ball*/
   private GOval ball;

/**The current row(for setup)*/
   private static int rownum = 0;

/**The paddle*/
   private static GRect paddle = new GRect(PADDLE_WIDTH, PADDLE_HEIGHT);

/**The velocity*/
   private static double vx, vy;

/**the random generator*/
   private RandomGenerator rgen = RandomGenerator.getInstance();

/**bricks remaining*/
   private static int bricks = NBRICKS_PER_ROW * NBRICK_ROWS;

/**the score int*/
   private static int scoreINT = 0;

/**livesRemaining*/
   private static int livesINT = NTURNS;

/**score label*/
   private static GLabel score = new GLabel("Score:" + scoreINT,0,0);

/**lives label*/
   GLabel lives = new GLabel("lives :" + livesINT,0,0);

/* Method: run() */
/** Runs the Breakout program */
   public void run() {
      scoreAndLives();
      setUpBricks();
      paddle();
      addMouseListeners();
      addKeyListeners();
      vx = rgen.nextDouble(1.0, 3.0);
      ball();
      move();
   }

/**adds a score and life counter*/
   private void scoreAndLives(){
      score();
      lives();
   }

/**adds a score counter*/
   private void score(){
      score.setLocation(7,7 + score.getHeight());
      score.setColor(Color.RED);
      score.setFont(new Font("Serif", Font.BOLD, 24));
      add(score);
   }

/**adds a life counter*/
   private void lives(){
      lives.setLocation(WIDTH - lives.getWidth()*2 + 7,7 + lives.getHeight());
      lives.setColor(Color.RED);
      lives.setFont(new Font("Serif", Font.BOLD, 24));
      add(lives);
   }

/**designs the brick */
   private GRect brickDesign() {
      GRect brick = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
      brick.setFilled(true);
      switch (rownum + 1){
         case 1: brick.setColor(Color.RED); break;
         case 2: brick.setColor(Color.RED); break;
         case 3: brick.setColor(Color.ORANGE); break;
         case 4: brick.setColor(Color.ORANGE); break;
         case 5: brick.setColor(Color.YELLOW); break;
         case 6: brick.setColor(Color.YELLOW); break;
         case 7: brick.setColor(Color.GREEN); break;
         case 8: brick.setColor(Color.GREEN); break;
         case 9: brick.setColor(Color.CYAN); break;
         case 10: brick.setColor(Color.CYAN); break;
      }
      return brick; 
   }

/**sets up the bricks*/
   private void setUpBricks(){
      int x=0;
      int y=0;
      for(int i=0; i<NBRICK_ROWS; i++){
         x=0;
         y=rownum * BRICK_HEIGHT + BRICK_SEP * i + BRICK_Y_OFFSET;
         for(int j=0; j<NBRICKS_PER_ROW + 1; j++){
            add(brickDesign(), x, y);
            x=(j * BRICK_WIDTH) + (BRICK_SEP * j);
         }                                                                                                                                                                                                                                    
         rownum+=1;
      }
   }

/**initializes the paddle*/
   private void paddle(){
      int xCenter = WIDTH/2 - PADDLE_WIDTH/2;
      paddle.setFilled(true);
      add(paddle, xCenter, HEIGHT-PADDLE_Y_OFFSET);
   }

/**moves the paddle*/
   public void mouseMoved(MouseEvent e){
      int x = e.getX();
      if(x < WIDTH-PADDLE_WIDTH){
         paddle.setLocation(x, APPLICATION_HEIGHT - PADDLE_Y_OFFSET);
      }
   }

/**sets up the ball*/
   private void ball(){
      ball = new GOval( WIDTH/2 - BALL_RADIUS, HEIGHT/2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2);
      ball.setFilled(true);
      add(ball);
      vy = 3.0;
   }

/**the animation*/
   private void move(){
      if (rgen.nextBoolean(0.5)) vx = -vx;
         while(true){
            ball.move(vx, vy);
            checkWallColisions();
            checkCollisions();
            pause(PAUSE_TIME);
            if(scoreINT == bricks * BRICKVAL){
               break;
            }
         }
   }

/**Checks for colisions with the wall*/
   private void checkWallColisions(){
      if(xWallCollision() == true){
         xColide();
      }
      if(yWallCollision() == true){

         yColide();
      }           

   }

/**what to do in case of a x collision*/
   private void xColide(){
      if(vx>0){
         vx = -1 * rgen.nextDouble(1.0, 3.0);
      }else{
         vx = rgen.nextDouble(1.0, 3.0);
      }
   }

/**what to do in case of a y collision*/
   private void yColide(){
      if(vx>0){
         vx = rgen.nextDouble(1.0, 3.0);
      }else{
         vx = -1 * rgen.nextDouble(1.0, 3.0);
      }
      vy=-vy;     
   }

/**checks for an x wall colision*/
   private boolean xWallCollision(){
      if(ball.getX() + BALL_RADIUS*2 > WIDTH){
         double bally=ball.getY();
         ball.setLocation(WIDTH-BALL_RADIUS*2, bally);
         return true;
      }else if(ball.getX() < 0){
         double bally=ball.getY();
         ball.setLocation(0, bally);
         return true;
      }else{
         return false;
      }
   }

/**checks for a y wall colision*/
   private boolean yWallCollision(){
      if(ball.getY() > HEIGHT - BALL_RADIUS*2){
         return true;
      }if(ball.getY() < 0){
         return true;
      }else{
         return false;
      }
   }

/**gets coliders*/
   private GObject getColidingObject(){
      if(getElementAt(ball.getX(), ball.getY()) != null){
         return getElementAt(ball.getX(), ball.getY());
      }else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY()) != null){
         return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY());
      }else if(getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2) != null){
         return getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2);
      }else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2) != null){
         return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2);
      }else{
         return null;
      }
   }

/**checks for brick and paddle colisions*/
   private void checkCollisions(){
      GObject colider = getColidingObject();
      if(colider == paddle){
         yColide();
      }else if(colider == lives || colider == score){

      }else if(colider != null){
         yColide();
         remove(colider);
         scoreINT+=BRICKVAL;
         score.setLabel("Score:" + scoreINT);
      }
   }
}

我可以让球四处弹跳,但是在移除所有积木并且球停止弹跳之前,循环就逃脱了。当分数达到所有积木都将消失的点时,循环将退出。然而,它达到那个点还为时过早。

/**the animation*/
private void move(){
        if (rgen.nextBoolean(0.5)) vx = -vx;
                while(true){
                        checkCollisions();
                        ball.move(vx, vy);
                        checkWallColisions();
                        pause(PAUSE_TIME);
                        //where i'm having issues - the loop is set to escape when the score reaches the point at which all the bricks will be gone but the score is reaching that point too early 
                        if(scoreINT == bricks * BRICKVAL){
                                break;
                        }
                }
}

/**gets coliders*/
private GObject getColidingObject(){
        if(getElementAt(ball.getX(), ball.getY()) != null){
                return getElementAt(ball.getX(), ball.getY());
        }else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY()) != null){
                return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY());
        }else if(getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2) != null){
                return getElementAt(ball.getX(), ball.getY() + BALL_RADIUS *2);
        }else if(getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2) != null){
                return getElementAt(ball.getX() + BALL_RADIUS *2, ball.getY() + BALL_RADIUS *2);
        }else{
                return null;
        }
}

/**checks for brick and paddle colisions*/
private void checkCollisions(){
        GObject colider = getColidingObject();
        if(colider == paddle){
                yColide();
        }else if(colider == lives || colider == score){}else if(colider != null){
                remove(colider);
                yColide();
        }
}

最佳答案

在您的 setUpBricks 方法中,看起来您正在创建 NBRICK_ROWS * (NBRICKS_PER_ROW + 1) 积木。但是在您的 move 方法中,您只检查 NBRICKS_PER_ROW * NBRICK_ROWS 砖 block 。

关于Java Breakout 游戏过早退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1312812/

有关Java Breakout 游戏过早退出的更多相关文章

  1. ruby - 我需要从 facebook 游戏中抓取数据——使用 ruby - 2

    修改(澄清问题)我已经花了几天时间试图弄清楚如何从Facebook游戏中抓取特定信息;但是,我遇到了一堵又一堵砖墙。据我所知,主要问题如下。我可以使用Chrome的检查元素工具手动查找我需要的html-它似乎位于iframe中。但是,当我尝试抓取该iframe时,它​​是空的(属性除外):如果我使用浏览器的“查看页面源代码”工具,这与我看到的输出相同。我不明白为什么我看不到iframe中的数据。答案不是它是由AJAX之后添加的。(我知道这既是因为“查看页面源代码”可以读取Ajax添加的数据,也是因为我有b/c我一直等到我可以看到数据页面之后才抓取它,但它仍然不存在)。发生这种情况是因为

  2. ruby - 在 ruby​​ 中生成一个进程,捕获 stdout,stderr,获取退出状态 - 2

    我想从ruby​​rake脚本运行一个可执行文件,比如foo.exe我希望将foo.exe的STDOUT和STDERR输出直接写入我正在运行rake任务的控制台.当进程完成时,我想将退出代码捕获到一个变量中。我如何实现这一目标?我一直在玩backticks、process.spawn、system但我无法获得我想要的所有行为,只有部分更新:我在Windows上,在标准命令提示符下,而不是cygwin 最佳答案 system获取您想要的STDOUT行为。它还返回true作为零退出代码,这可能很有用。$?填充了有关最后一次system调

  3. python - Ruby 或 Python 的 3d 游戏引擎? - 2

    关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于StackOverflow来说是偏离主题的,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,describetheproblem以及迄今为止为解决该问题所做的工作。关闭9年前。Improvethisquestion是否有适用于这些的3d游戏引擎?

  4. ruby-on-rails - 如何在不退出 IRB session 的情况下退出调试器? - 2

    这是一个长期存在的挫败感来源,但也许我遗漏了什么。如果我正在调试,并且我想退出调试器并返回到IRB或Rails控制台,“退出”将不起作用,因为它将退出IRB。“完成”似乎也与继续具有相同的效果。使用“删除”删除断点然后尝试“继续”或“完成”不起作用。有什么想法吗? 最佳答案 至少在byebug中,你可以这样做:evalreturn它具有计算当前函数的return语句的净效果。这有时会奏效,具体取决于调用堆栈的外观。现在虽然这不会删除当前断点....如果您只是想收回控制权,在大多数情况下这会做到这一点,具体取决于您的代码结构。在您的代

  5. ruby - Thin::Server#daemonize 立即退出 - 2

    我试图制作一个可执行文件,它通过Thin作为守护进程启动Sinatra应用程序。我正在使用此代码通过Sinatra应用程序调用Thin:#!/usr/bin/envrubyrequire'thin'require'app.rb'server=::Thin::Server.new('127.0.0.1',9999,App)server.log_file='tmp/thin.log'server.pid_file='tmp/thin.pid'server.daemonize这是我执行脚本时得到的日志输出:>>WritingPIDtotmp/thin.pid>>Exiting!服务器正常启动

  6. ruby - 当 shelled-out 命令返回非零退出代码时,如何让 Ruby 脚本失败? - 2

    在Ruby脚本中,有variousways调用系统命令/命令行反引号:`commandarg1arg2`分隔形式,例如%x(commandarg1arg2)(可用其他分隔符)Kernel#system方法:system('commandarg1arg2')Kernel#exec方法:exec('commandarg1arg2')如果我希望Ruby脚本在调用的命令失败时失败(有异常)(具有非零退出代码),我可以检查特殊变量中的退出代码$?对于前两个变体:`commandarg1arg2`failunless$?==0或%x,commandarg1arg2,failunless$?==0如

  7. ruby - 使用 Ruby 编写 Unity 游戏 - 2

    所以我看到unity支持c#、JS和Boo。我可以学习其中一个,但我想制作一个“编译器”或类似的东西,让我可以编写ruby​​代码并输出JS代码或制作一个可以被Unity编译器读取的层。这有可能吗?我愿意在这方面投入很多时间并且有相当多的经验。 最佳答案 如果您的问题实际上是“我如何将Ruby编译为JavaScript”,那么这更容易回答:Opal:RubytoJavaScriptcompiler但是,学习其中一种受支持的语言会更好。当运行的是用另一种语言解释的代码时,很难调试“您的”代码。

  8. 【Unity游戏破解】外挂原理分析 - 2

    文章目录认识unity打包目录结构游戏逆向流程Unity游戏攻击面可被攻击原因mono的打包建议方案锁血飞天无限金币攻击力翻倍以上统称内存挂透视自瞄压枪瞬移内购破解Unity游戏防御开发时注意数据安全接入第三方反作弊系统外挂检测思路狠人自爆实战查看目录结构用il2cppdumper例子2-森林whoishe后记认识unity打包目录结构dll一般很大,因为里面是所有的游戏功能编译成的二进制码游戏逆向流程开发人员代码被编译打包到GameAssembly.dll中使用il2ppDumper工具,并借助游戏名_Data\il2cpp_data\Metadata\global-metadata.dat

  9. Unity游戏开发:背包系统的实现 - 2

    背包是游戏中经常使用的一个组件,它负责管理玩家在游戏中所获得的道具。一个完整的背包系统应当具有将物品放置进背包、对背包内物品进行管理和使用背包内物品等功能。而往往一个背包系统的逻辑关系较为复杂,如果把所有功能都放在一个脚本中实现会使代码显得十分冗杂且缺乏逻辑。所以在背包系统的设计过程中,我们常将其分解为数据、逻辑和UI三部分分别来进行完成。一、UI设计以CottonPuzzle中的背包设计为例,我们需要有物品展示栏、物品切换按键和物品提示信息等部分。在Canvas中创建ItemHolder,在ItemHolder中创建LeftButton和RightButton控制物品的左右切换、Slot来控

  10. ruby - 为什么退出 Ruby 线程会杀死我的整个程序? - 2

    我有这段代码:puts"Start"loopdoThread.startdoputs"Hellofromthread"exitendtext=getsputs"#{text}"endputs"Done"我希望看到“Start”后跟“Hellofromthread”,然后我可以输入会得到回显的输入。相反,我得到“Start”和“Hellofromthread”,然后程序退出。来自关于exit的文档:Terminatesthrandschedulesanotherthreadtoberun.Ifthisthreadisalreadymarkedtobekilled,exitreturnst

随机推荐