大家好,我已经学习Java几周了,因此决定制作自己的Breakout版本。除了应该根据球拍速度(GRect)修改球(GOval)速度的方法外,游戏运行良好。无论如何,这是方法:
private double paddleVelocity(){
double paddleTracker=(paddleXTracker+paddleXTrackerTwo+paddleXTrackerThree)/3; //get the average x-coordinate for the paddle
if(loopCounter>3){ //if the game has been through the loop more than three times (to ensure three x-coordinates have been stored)
paddleVelocity=(paddle.getX()-paddleTracker)/50; //set paddleVelocity to the current paddle x-coordinate minus the average of the previous three paddle x-coordinates (which therefore finds the velocity and direction)
}else{
paddleVelocity=(paddle.getX()-paddleXTracker)/50; //takes the previous paddle x-coordinate and subtracts it from the current x-coordinate (which therefore finds the velocity and direction)
}
return (paddleVelocity);
}
这是调用它的方法:
private void detectCollisionPaddle(){;
double ballY = ball.getY()+(BALL_RADIUS*2);
if(paddle.getY()<=ballY && ball.getX()+BALL_RADIUS>=paddle.getX() && ball.getX()+BALL_RADIUS/2<=(paddle.getX()+PADDLE_WIDTH)){ //a mess of code which ensures the ball is within the bounds of the paddle
yVel=-yVel; //make the ball bounce off
bounceClip.play(); //play the bounce clip
xVel=xVel+paddleVelocity(); //increase the velocity of the ball in the direction and speed of the paddle
} }
实例变量包括:
private int loopCounter; //how many loops of the game code have been executed
private double paddleVelocity; //velocity of the paddle
private double paddleXTracker = 1; //value of the x-coordinate of the paddle one loop previous
private double paddleXTrackerTwo; //value of the x-coordinate of the paddle two loops previous
private double paddleXTrackerThree; //value of the x-coordinate of the paddle three loops previous
private double yVel = -1; //y velocity of the ball
private double xVel = 1; //x velocity of the ball
基本上,我希望paddleVelocity能够获取paddle的三个连续x坐标,并将它们与当前位置进行比较(paddle.getX())。这使我可以确定用户移动桨叶的速度和方向。然后,我返回结果(paddleVelocity),该结果应该添加到球的当前速度(xVel)中。但是,这似乎无法正常运行。删除对paddleVelocity()的调用后,代码将正常运行(尽管没有加速)。因此,我知道paddleVelocity方法存在问题。它似乎偏向右侧。毕竟,也许你们可以提供帮助,这是我第一次认真尝试编程。这是游戏atm:http://cyb3rglitch.com/games/Glitchout_0.1/
是的,砖头还没有破裂。 :P干杯!
编辑:为了更容易,这是它凌乱的荣耀中的所有代码:
/*
* File: Breakout.java
* -------------------
* Name: Vito Cassisi
*/
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;
/**Ball speed in milliseonds*/
private static final int DELAY = 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;
private static final int COUNTDOWN = 200;
private GOval ball;
private GRect paddle;
private GRect block;
private GLabel life;
private GLabel countDown;
private GObject getObject;
AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
/* Method: run() */
/** Runs the Breakout program. */
public void run() {
drawBlocks();
drawPaddle();
drawBall();
drawLife();
addMouseListeners();
while(!lose) {
detectCollisionWall();
detectCollisionBlock();
detectCollisionPaddle();
decelerate();
assignPaddleTrackers();
pause(DELAY);
loopCounter++;
}
GLabel youLose = new GLabel("You lose!",APPLICATION_WIDTH/2,APPLICATION_HEIGHT/2);
youLose.setLocation((getWidth()-youLose.getWidth())/2, (getHeight()-youLose.getAscent())/2);
add(youLose);
}
private void drawBlocks(){
/**Draw as many rows as defined in the constant 'NBRICKS_PER_ROW'*/
for(int i = 0;i<NBRICK_ROWS;i++){
blockX=(APPLICATION_WIDTH-((BRICK_WIDTH+BRICK_SEP)*NBRICKS_PER_ROW))/2; //center the blocks horizontally
drawRow(); //draw the row
blockY+=BRICK_HEIGHT+BRICK_SEP; //move the row down
}
}
private void drawRow(){
/**Draw as many bricks as defined in the constant 'NBRICKS_PER_ROW'*/
for(int i = 0;i<NBRICKS_PER_ROW;i++){
block = new GRect(blockX,blockY,BRICK_WIDTH,BRICK_HEIGHT); //initialise the blocks
add(block); //draw the blocks
blockX+=BRICK_WIDTH+BRICK_SEP; //move x co-ordinate across
}
}
private void drawPaddle(){
paddle = new GRect((APPLICATION_WIDTH-PADDLE_WIDTH)/2,(APPLICATION_HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT),PADDLE_WIDTH, PADDLE_HEIGHT);
add(paddle);
}
private void drawBall(){
double diameter = BALL_RADIUS*2;
ball = new GOval((APPLICATION_WIDTH-diameter)/2,APPLICATION_HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT*2-diameter,diameter,diameter);
add(ball);
}
private void moveBall(){
ball.move(xVel, yVel);
}
private void detectCollisionWall(){
if(ball.getY()+(BALL_RADIUS*2)<getHeight() && ball.getY()>0 && ball.getX()+(BALL_RADIUS*2)<getWidth() && ball.getX()>0){
moveBall();
}
if(ball.getY()+(BALL_RADIUS*2)>getHeight()){
yVel=-yVel;
turnsLeft--;
testForLose();
bounceClip.play();
}
if(ball.getY()<0){
yVel=-yVel;
bounceClip.play();
}
if(ball.getX()+(BALL_RADIUS*2)>=getWidth()){
xVel=-xVel;
bounceClip.play();
}
if(ball.getX()<=0){
xVel=-xVel;
bounceClip.play();
}
moveBall();
}
private void testForLose(){
if(turnsLeft==0){
lose=true;
life.setLabel("You have "+turnsLeft+" lives!");
return;
}
life.setLabel("You have "+turnsLeft+" lives!");
remove(ball);
drawBall();
xVel=1;
yVel=-1;
drawCountDown();
}
private void drawCountDown(){
GLabel countDown = new GLabel("",APPLICATION_WIDTH/2,APPLICATION_HEIGHT/2);
for(int i=3;i>0;i--){
countDown.setLabel("Respawn in: "+i);
countDown.setLocation((getWidth()-countDown.getWidth())/2, (getHeight()-countDown.getAscent())/2);
add(countDown);
pause(COUNTDOWN);
}
countDown.setLabel("Go");
countDown.setLocation((getWidth()-countDown.getWidth())/2, (getHeight()-countDown.getAscent())/2);
pause(COUNTDOWN);
remove(countDown);
}
private void drawLife(){
life = new GLabel("You have "+turnsLeft+" lives!", APPLICATION_WIDTH/2,15);
life.setLocation((getWidth()-life.getWidth())/2, 20);
add(life);
}
private void detectCollisionBlock(){
GObject collisionObject = detectObject(ball, BALL_RADIUS*2, BALL_RADIUS*2);
if(collisionObject==block){
remove(collisionObject);
yVel=-yVel;
}
}
private double paddleVelocity(){
double paddleTracker=(paddleXTracker+paddleXTrackerTwo+paddleXTrackerThree)/3;
if(loopCounter>3){
paddleVelocity=(paddle.getX()-paddleTracker)/50;
//paddleXTracker=paddle.getX();
}else{
paddleVelocity=(paddle.getX()-paddleXTracker)/50;
}
return (paddleVelocity);
}
private void detectCollisionPaddle(){;
double ballY = ball.getY()+(BALL_RADIUS*2);
//double ballX = ball.getX()+(BALL_RADIUS*2);
/*if(ball.getY()+BALL_RADIUS*2>=paddle.getY() && ball.getX()+BALL_RADIUS*2>=paddle.getX() && ball.getX()<=paddle.getX()){
ball.setLocation(ball.getX(),paddle.getY()-BALL_RADIUS*2-1);
}*/
if(paddle.getY()<=ballY && ball.getX()+BALL_RADIUS>=paddle.getX() && ball.getX()+BALL_RADIUS/2<=(paddle.getX()+PADDLE_WIDTH)){
yVel=-yVel;
bounceClip.play();
xVel=xVel+paddleVelocity();
/*
int x = 4;
int center = PADDLE_WIDTH/2;
double parts=center/x;
int location=0;
if(ball.getX()+BALL_RADIUS*2>paddle.getX()&& ball.getX()<paddle.getX()+PADDLE_WIDTH){
for(int i = 0;i<x;i++){
if (ball.getX()<center+paddle.getX() && ball.getX()>(parts*i)+paddle.getX()){
}else{
location = i;
break;
}
}
xVel=x/location;
}
}
//
if(ball.getX()+BALL_RADIUS<paddle.getX()+PADDLE_WIDTH/4){
if(xVel!=1){
if(xVel<0)
{
xVel=xVel-1;
}else{
xVel=-xVel-1;
}
}
}
if (ball.getX()>paddle.getX()+(PADDLE_WIDTH-PADDLE_WIDTH/4)){
if(xVel>0){
xVel=xVel+1;
}else{
xVel=-xVel+1;
}
}
if (ball.getX()<paddle.getX()+(PADDLE_WIDTH-PADDLE_WIDTH/4) && ball.getX()+BALL_RADIUS>paddle.getX()+PADDLE_WIDTH/4){
if(xVel>1){
xVel=xVel-1;
}
if(xVel<-1){
xVel=xVel+1;
}
}
*/
}
}
public void mouseMoved(MouseEvent e){
paddle.move((e.getX()-paddle.getX()-PADDLE_WIDTH/2),0);
}
/*Passes in an object to be checked, i.e. the ball, and asks for it's height and width. It returns the object if one is present, otherwise it retuns null.**/
private GObject detectObject(GObject object, int width, int height){
double right = object.getX()+width;
double bottom = object.getY()+height;
getObject = getElementAt(object.getX(),object.getY());
if(getObject==null){
getObject = getElementAt(right,object.getY());
}else{
return getObject;
}
if(getObject==null){
getObject = getElementAt(object.getX(),bottom);
}else{
return getObject;
}
if(getObject==null){
getObject = getElementAt(right,bottom);
}else{
return getObject;
}
if(getObject==null){
return null;
}else{
return getObject;
}
}
private void decelerate(){
if(xVel<-1){
xVel=xVel+0.01;
}
if(xVel>1){
xVel=xVel-0.01;
}
}
private void assignPaddleTrackers(){
paddleXTrackerThree=paddleXTrackerTwo;
paddleXTrackerTwo=paddleXTrackerThree;
paddleXTracker=paddle.getX();
}
private int loopCounter;
private double paddleVelocity;
private double paddleXTracker = 1;
private double paddleXTrackerTwo;
private double paddleXTrackerThree;
private double yVel = -1;
private double xVel = 1;
private int turnsLeft = NTURNS;
private int blockY = BRICK_Y_OFFSET;
private int blockX = 0;
private static boolean lose = false;
private RandomGenerator rgen = RandomGenerator.getInstance();
}
最佳答案
当桨速度更新时,我看不到您在哪里设置paddleXTracker
和朋友。因此,这里有一些可能的修复:
您不会连续不断地更新过去的值,仅在开始时一次。
您从未更新过paddleXTrackerTwo
和paddleXTrackerThree
值,因此如果原点在左侧(可能),则平均值在左侧(由于被3除)。
两者都会说明您所看到的正确偏见。
此外,您似乎正在执行moving average的临时版本。还有其他变化可能更适合您要尝试做的事情。
编辑
现在您可以使用完整的代码了,我看不出有什么问题。我只能建议您使用调试技术,例如在游戏运行时打印出paddle*Tracker*
,以查看会产生什么值以及它们是否符合预期。
编辑2
只是为了使这个答案成为答案,根据我们的OOB讨论,您的分配跟踪器功能是错误的,并且没有用paddleXTrackerTwo
更新paddleXTracker
。