- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在介绍在线编程。但是,我被困在一项任务上。
作业是编写一个闯关游戏。我已经成功编写了 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/
我在网站的首页上有一个 iframe。 iframe 加载一个 .php 页面,该页面具有使用 jQuery 工具选项卡构建的旋转器。在第一个选项卡上,我使用 jQuery Tools Overlay
我正在介绍在线编程。但是,我被困在一项任务上。 作业是编写一个闯关游戏。我已经成功编写了 97% 的游戏。然而,游戏在移除所有积木之前停止。有时还剩 4 block 积木,有时是 11 block 。
breakOut 很好,但太冗长了: List(1, 2, 3).map{i => (i * 2, i / 2.0, i.toString)}(breakOut) : Array[(Int, Doub
我正在制作 Breakout 克隆游戏,但在 Racket 碰撞方面遇到了一些麻烦。我有一个矩形代表球和桨,当它们相交时,代表球速度的 Y 向量被取反(如下所示)。一切正常。问题是当桨向右移动时,我希
我正在用 JavaScript 编写一个突破游戏,并且遇到了 Racket 碰撞检测的问题。 在运行程序时,球与 Racket 的所有碰撞都没有问题,直到它第一次未命中。此时程序将球放回中心并再次将其
这是 12 年级的 OPP 作业。 目前我遇到一个问题,球偶尔会进入方 block 而不是反射。 这是我的代码: 主要如何检查球是否击中方 block : public void theBall(){
好的,我有这个永不终止的游戏循环 -- public void run() { setup(); addMouseListeners(); int Turns = NTURNS
我正在尝试使用以下 CSS 将 breakout div 居中放置在我的页面底部: #footer { clear: both; position: absolute; bottom: 0; left
如何在 this hidden html game 中作弊/自动移动 Racket ?看起来像? 有一个 ● 和一个 当移动鼠标时,桨会水平移动。如何将球的运动与 Racket 联系起来? 这个问题
This SO answer描述如何 scala.collection.breakOut可用于防止创建浪费的中间集合。例如,这里我们创建一个中间 Seq[(String,String)] : val
我正在尝试按照 DeepMind 关于 Q-learning 的论文进行游戏突破,但到目前为止,性能没有提高,即它根本没有学习任何东西。我没有体验重播,而是在运行游戏,保存一些数据和训练,然后再次运行
对于 CS 入门类(class),我正在尝试用 Java 克隆“Breakout”。游戏已经完成 99%,所以我想我应该添加一些额外内容。 我想添加的一件事是使用空格键暂停和恢复的功能。我添加了一个
我正在为我的 AP 计算机科学类(class)制作游戏 Breakout。我已经完成了大部分,但我不知道如何使用键盘上的箭头键左右移动桨。我能得到一些帮助吗?我会很感激。 这是我目前所拥有的: 主类:
在 OpenAI 健身房环境中训练时,我认为环境有时会“停止”。对于连续的许多帧,没有球可见/停止产卵。 这是健身房环境的错误吗?这是 Breakout-v0 游戏的一部分吗? 我还想知道 Break
大家好,我已经学习 Java 几周了,并决定制作我自己的 Breakout 版本。除了根据 Racket (GRect) 的速度修改球 (GOval) 速度的方法之外,游戏运行良好。不管怎样,方法是这
我的目标是让球以 Racket 为中心,即使球半径的值在游戏的 future 版本中发生变化。我唯一的问题是执行正确的数学公式为游戏球的 x 坐标。我的 y 坐标公式运行良好。 我不需要正确答案。我只
假设我有这样的数据: scala> case class Foo(a: Int, b: Int) defined class Foo scala> val data: List[Foo] = Foo(
我一直在尝试让 Breakout 在 Racket 中工作,到目前为止,球从桨上弹起(桨由鼠标控制)并且砖 block 存在 完整代码如下: (require 2htdp/image) (requir
这是我关于 SO 的第一篇文章! 我已经独自学习了几周斯坦福大学的“编程方法论”类(class),该类(class)介绍了如何使用 Java 进行编程。到目前为止,我一直在毫无困难地完成所有程序,以最
我已经得到了当球与 block 碰撞时会删除 block 的部分,但我也想知道球是否击中 block 的顶部或底部或左侧或右侧并相应地弹跳。我已经尝试过,但效果不太好。它只是吓得跳来跳去。我已从下面的
我是一名优秀的程序员,十分优秀!