在评论THE BALL下,代码刚从另一个程序中取出。另一个程序只是在窗户周围反弹。这个程序是一个与砖破坏者相同的游戏。我需要让球移动,然后我可以在其中一个砖块中开始碰撞的代码(它们的所有坐标都存储在数组中)。请提供一些支持,或诊断出问题所在,这样我就可以找到一些解决方案。感谢所有帮助,谢谢。
以下是我的计划:
// The "FinalProgram" class.
import java.awt.*;
import hsa.LASSConsole;
public class FinalProgram
{
static LASSConsole c; // The output console
//these variables can be used everywhere!!! even in the methods
static int paddleX = 250; //the paddle x cordinate
static int paddleY = 489; //paddle y cordinate
static char keypressed = ' '; //variable created (character) for they key that will be pressed
static final char left = 'j'; //character variable that will remain the same, left is j
static final char right = 'k'; //character variable that will remain the same, right is k
static final int paddleSize = 210; //the size of the paddle
static final int paddleSpeed = 25; //speed the paddle will move at
static final int thinness = 15; //how thin the paddle is
static final char again = 'y'; //character for YES if they want to play, and play again
static final char done = 'n'; //character for NO if they do not want to play, and are done playing after they lose/win
static int totalBricks = 18;
public static void main (String[] args) throws java.lang.InterruptedException
{
c = new LASSConsole ();
// Place your program here. 'c' is the output console
//setting the instructions screen
c.setTextBackgroundColor (Color.black); //black background
c.clear (); //clearing the screen
c.setColor (Color.white); //set the color to white(for the font)
//new font object
Font TitleFont;
//font, style and sizet
TitleFont = new Font ("Rockwell", Font.BOLD, 60); //the font will be in bold, size 60, rockewell font
//Change the current font to TitleFont
c.setFont (TitleFont);
//Use our new font to display BREAKER OF BRICKS
c.drawString ("BREAKER", 180, 210); //must draw the string, and the cordinates after ' '
c.drawString ("OF", 240, 260);
c.drawString ("BRICKS", 180, 310);
//new font object
Font AgainFont;
//font, style and sizet
AgainFont = new Font ("Candara", Font.BOLD, 30);
//ACTUAL INSTRUCTIONS
c.setCursor (20, 1); //setting the cursor
c.setTextColor (Color.white); //text color to white
c.println ("Objective: eliminate all of the bricks with the ball by moving the paddle."); //output info
c.println ("You can move the paddle left and right using the keys J and K on your keyboard. The game will be over once you miss the ball.");
c.println ("");
c.setTextColor (Color.red); //color to red
c.println ("Would you like to play? Y/N"); //output the question
//note the while loop for the entire program is based upon this question. if they enter Y, the game will begin
char againKey;
//clear the screen
//Check to see if the user is pressing a key
c.isCharAvail ();
againKey = c.getChar ();
while (againKey == 'y')
{
//IF THE USER SAYS THEY WANNA PLAY THEN ALL THIS beLow
c.setTextBackgroundColor (Color.black); //background color of game to black
c.clear (); //clear the screen
c.setColor (Color.white); //set the color to white for drawing the paddle
c.fillRect (paddleX, paddleY, paddleSize, thinness); //draw the paddle
//BRIKS BRICKS
int brickCount = 0; //counts the bricks!
int bricksize = 30; //size of each brick
Color brickColor; //color for bricks
brickColor = new Color (RandInt (0, 255), RandInt (0, 255), RandInt (0, 255));
//^ brick color will be a combination of red, blue, and green. These values will be assigned randomly
//using the randInt method.
//first brick, since i have to start from the second
int firstBrickX = 0; //first bricks x cordinate
int firstBrickY = 0; //first bricks y cordinate
brick (firstBrickX, firstBrickY, bricksize, brickColor); //drawing the brick using the method brick
//xbrick (x cordinate of brick)
int[] xbrick; //variable, array created x cordinates of every brick
xbrick = new int [totalBricks]; //setting up array to have 18(num. of total bricks) "boxes"
//ybrick (y cordinate of brick)
int[] ybrick; //variable to store y cordinate of every brick
ybrick = new int [totalBricks]; //similar to the one for xbrick
xbrick [0] = firstBrickX; //the first box in the xbrick array will have the value of 0
ybrick [0] = firstBrickY; //the first box in the ybrick array will have the value of 0
//this loop will be used for creating the bricks
for (int i = 1 ; i < totalBricks ; i++) //it will repeat until the number of total bricks
{
brickColor = new Color (RandInt (0, 255), RandInt (0, 255), RandInt (0, 255)); //color for every brick(random)
xbrick [i] = xbrick [i - 1] + 120; //where the brick will be
brick (xbrick [i], ybrick [i], bricksize, brickColor); //drawing brick
brickCount += 1; //will add 1 to the brick counter variable
if ((brickCount > 5) && (brickCount < 12)) //if the bricks reach 6(max in the row) (brick count aids this)
{
ybrick [i] = ybrick [i - 6] + 30; //add 30 to their value (moving it down)
xbrick [i] = xbrick [i - 6] - 20; //change the x cordinate to the left -20 (off Set)
brickColor = new Color (RandInt (0, 255), RandInt (0, 255), RandInt (0, 255));
brick (xbrick [i], ybrick [i], bricksize, brickColor); //drawing brick
}
if ((brickCount > 11) && (brickCount < totalBricks))
{
ybrick [i] = ybrick [i - 6] + 30; //add 30 to their value (moving it down)
xbrick [i] = xbrick [i - 6] - 20; //change the x cordinate to the left -20 (off Set)
brickColor = new Color (RandInt (0, 255), RandInt (0, 255), RandInt (0, 255));
brick (xbrick [i], ybrick [i], bricksize, brickColor); //drawing brick
}
}
//see if they are pressing a key
if (c.isCharAvail ())
{
//get the key
keypressed = c.getChar ();
if (keypressed == 'j') //if it equals j, that means jump to the moveleft method
{
moveLeft (); //method will make the paddle appear to move left and right
}
if (keypressed == 'k') //similar to one above, except opposite direction
{
moveRight ();
}
}
***//THE BALL
//draw the ball
c.setColor (Color.red);
c.fillOval (x, y, diameter, diameter);
//wait
Thread.sleep (100);
//erase the ball
c.setColor (Color.black);
c.fillOval (x, y, diameter, diameter);
//update cordinates ******IMPORTANT***********
//ORDER OF THIS LOOP IS IMPORTANT
x += xspeed;
y += yspeed; //y equals y + the speed (it will go down & right)
//to make it bounce u need if statements
//bounce if the ball hits the paddleX cordinate + width
if ((y + diameter >= paddleX) && (y + diameter <= paddleX + paddleSize))
{
//change direction
yspeed = -yspeed; //if it is going down 5, now it will go up5
}
//ball goes under the paddle
//this if statement will check if the cordinates of the ball (y+diameter of the ball) is
//equal to the space starting from the paddle's x cordinate but less than paddle's x cordinate+paddlelength
if (y + diameter > paddleY)
{
againKey = done; //basically it will break the loop
}
//make ball bounce from the right side
if (x + diameter >= c.maxx ()) //if the x cordinate plus the diameter of the ball (gets to the edge), touches x.maxx
{
//put ball back into the window
x = c.maxx () - diameter;
//change direction
xspeed = -xspeed;
}
//bounce if the ball hits the top
if (y - diameter <= 0)
{
//put ball back into the window
y = 0 + diameter;
//change direction
**yspeed = -yspeed; //if it is going down 5, now it will go up5
}
//make ball bounce from the left side
if (x - diameter <= 0)
{
//put ball back into the window
x = diameter;
//change direction
xspeed = -xspeed;
}*****
/*//hitting the bricks
for (int i = totalBricks ; i > 0 ; i--)
{
if ((x == xbrick [0]) && (x == xbrick [0] + brickSize))
{
c.setColor (Color.black);
brick (xbrick [element], ybrick [0], bricksize, black);
}
}*/
} //loop
} // main method
//method for creating the bricks
public static void brick (int x, int y, int size, Color col)
{
// make it the same size everytime
int height = size;
int width = 4 * size;
//rectangle of da flag
int MainRectangleX = x;
int MainRectangleY = y;
int MainRectangleW = width;
int MainRectangleH = height;
//DRAW IT
//color of main rectangle (white)
c.setColor (col);
//draw the white one + fill (red)
c.fillRect (MainRectangleX, MainRectangleY, MainRectangleW, MainRectangleH);
}
//method for random integer
public static int RandInt (int low, int high)
{
//figure out the range of values between "low"
//and "high"
int range = high - low + 1;
//choose number between 0 and 1, mulitply by the
//range and then shift it up so "low" is the minimum value
int r = (int) (Math.random () * range + low);
//finishhhhh
return r;
}
public static void moveLeft ()
{
c.setColor (Color.black); //same color as background so i can erase it
c.fillRect (paddleX, paddleY, paddleSize, thinness);
paddleX -= paddleSpeed; //update cordinate
c.setColor (Color.white);
c.fillRect (paddleX, paddleY, paddleSize, thinness); //redraw
}
public static void moveRight ()
{
c.setColor (Color.black);
c.fillRect (paddleX, paddleY, paddleSize, thinness);
paddleX += paddleSpeed;
c.setColor (Color.white);
c.fillRect (paddleX, paddleY, paddleSize, thinness);
}
} // FinalProgram class