这是我关于SO的第一篇文章!
我自己一直在通过斯坦福大学的"编程方法论"课程工作几周,该课程是用java编程的简介.到目前为止,我一直在做所有的程序,没有什么困难,以最小的难度研究我需要的东西.
现在,我所拥有的只是一组砖块和一个我能够从墙上反弹的球.目前球没有做任何事情,只是在画布中反弹并穿过砖块.
涉及很多步骤,其他我非常肯定我可以照顾.我遇到困难的是......
1)让球从砖块上弹开.
2)当球从球上反弹时,让砖块消失.
我一直在使用的一些资源 -
使用ACM图形包
斯坦福PDF和分配指南
我想我的问题是.为了能够解决上面列出的问题,我需要了解什么.在其中一个讲座中,教授谈到使用"getElementAt()"但我真的不明白这种方法是如何工作的,或者我怎么能用它来让我的球从砖块上反弹然后再让它们消失.
我到目前为止编写的代码 -
/*
* File: Breakout.java
* -------------------
* Name: Sandi
* Section Leader: I'm learning this online
*
* 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;
/* Method: run() */
/** Runs the Breakout program. */
public void run() {
addBricks();
addBall();
//velocity of the ball's x and y
ballVX = 1;
ballVY = 2;
while (true) {
moveBall();
pause(10);
}
}
private void addBricks() {
int startY = BRICK_Y_OFFSET;
GRect brick;
for (int i = 0; i < NBRICK_ROWS; i++) {
int startX = BRICK_SEP;
for (int j = 0; j < NBRICKS_PER_ROW; j++) {
brick = new GRect(startX, startY, BRICK_WIDTH, BRICK_HEIGHT);
add(brick);
colorBricks(brick, Color.RED, 0, 1);
colorBricks(brick, Color.ORANGE, 2, 3);
colorBricks(brick, Color.YELLOW, 4, 5);
colorBricks(brick, Color.GREEN, 6, 7);
colorBricks(brick, Color.CYAN, 8, 9);
startX += BRICK_WIDTH + BRICK_SEP;
}
startY += BRICK_HEIGHT + BRICK_SEP;
}
}
private void colorBricks(GRect brick, Color color, int fromRowNumber,
int toRowNumber) {
//this will make it so that if the bricks are between two y coordinates
//then the method will set it to a certain color and color it.
if (brick.getY() >= BRICK_Y_OFFSET + fromRowNumber
* (BRICK_HEIGHT + BRICK_SEP)
&& brick.getY() <= BRICK_Y_OFFSET + toRowNumber
* (BRICK_HEIGHT + BRICK_SEP)) {
brick.setColor(color);
brick.setFilled(true);
}
}
private void addBall() {
ball = new GOval(getWidth() / 2, getHeight() / 2, BALL_RADIUS,
BALL_RADIUS);
ball.setFilled(true);
add(ball);
}
private void moveBall() {
double borderX = ball.getX();
double borderY = ball.getY();
if (borderX == 0 || borderX == getWidth() - BALL_RADIUS) {
ballVX = -ballVX;
}
if (borderY == 0 || borderY == getHeight() - BALL_RADIUS) {
ballVY = -ballVY;
}
ball.move(ballVX, ballVY);
}
private GOval ball;
private double ballVX;
private double ballVY;
}
Run Code Online (Sandbox Code Playgroud)
多谢你们!
以一种可以轻松检查某个坐标是否存在砖块的方式对砖块进行索引。
当你创建一块砖块时,你将它添加到容器中,然后你就失去了它。您没有可以用来与当前球位置进行比较的未来参考。
根据您的设计,我相信您应该通过二维数组来完成此操作。创建砖块时,还将GRect对象放入二维数组中。一个维度应该是行,另一个维度应该是列。然后您将能够使用该数组通过 x 和 y 坐标访问这些砖块。(这是您需要执行的操作来检测反弹。)
当球移动时,就像您检测到它从墙壁弹起一样,检查球的位置是否使其与砖块可能所在的位置接壤。如果确实如此,从任何方向,然后通过查看阵列来检查砖块是否确实存在。如果那里有砖块,弹起球并移走砖块。还要确保将数组中的该位置设置为null指示砖块已消失。
然后你也有你的胜利条件......当数组的所有元素都被设置为空时。