中级农民
- 积分
- 114
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-5-26
- 最后登录
- 1970-1-1
|
/*
* File: Breakout.java
* -------------------
* Name:
* Section Leader:
*
* This file will eventually implement the game of Breakout.
*/
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.awt.*;
import java.awt.event.*;
public class Breakout extends GraphicsProgram {
/** Width and height of application window in pixels. On some platforms
* these may NOT actually be the dimensions of the graphics canvas. */
public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;
/** Dimensions of game board. On some platforms these may NOT actually
* be the dimensions of the graphics canvas. */
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 DELAY=50;
private double y_val=0.0;
private double x_val;
private int i=100;
/* Method: run() */
/** Runs the Breakout program. */
public void run() {
/* You fill this in, along with any subsidiary methods */
build();
setup();
addMouseListeners();
x_val=rgen.nextDouble(1.0,3.0);
if(rgen.nextBoolean(0.5)){
x_val=-x_val;
}
while(i>0){
moveBall();
checkForCollision();
pause(DELAY);
}
}
private void checkForCollision(){
gobj=getElementAt(ball.getX(),ball.getY()+BALL_RADIUS);
if(gobj!=null){
if(gobj==peddle){
y_val=-y_val;
}else{
remove(gobj);
y_val=-y_val;
}
return;
}
gobj=getElementAt(ball.getX(),ball.getY());
if(gobj!=null){
remove(gobj);
y_val=-y_val;
}
gobj=getElementAt(ball.getX()+BALL_RADIUS,ball.getY());
if(gobj!=null){
remove(gobj);
y_val=-y_val;
}
gobj=getElementAt(ball.getX()+BALL_RADIUS,ball.getY()+BALL_RADIUS);
if(gobj!=null){
if(gobj==peddle){
y_val=-y_val;
}else{
remove(gobj);
y_val=-y_val;
}
}
if(ball.getX()<0||ball.getX()>WIDTH-BALL_RADIUS){
x_val=-x_val;
}if(ball.getY()<0&&ball.getY()>HEIGHT-BALL_RADIUS){
y_val=-y_val;
}
}
private void moveBall(){
if(y_val>=0){
y_val=8;
ball.move(x_val, y_val);
}
if(y_val<0){
y_val=-8;
ball.move(x_val, y_val);
}
}
private void setup(){
peddle=new GRect((WIDTH-PADDLE_WIDTH)/2,HEIGHT-BRICK_Y_OFFSET,PADDLE_WIDTH,PADDLE_HEIGHT);
add(peddle);
peddle.setFilled(true);
peddle.setFillColor(Color.black);
ball=new GOval((WIDTH-BALL_RADIUS)/2,(HEIGHT-BALL_RADIUS)/2,BALL_RADIUS,BALL_RADIUS);
add(ball);
ball.setFilled(true);
ball.setFillColor(Color.black);
}
private void build(){
int startX=(getWidth()-((NBRICKS_PER_ROW-1)*BRICK_SEP+NBRICKS_PER_ROW*BRICK_WIDTH))/2;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
rectangular = new GRect(startX+i*(BRICK_SEP+BRICK_WIDTH),BRICK_Y_OFFSET+j*(BRICK_SEP+BRICK_HEIGHT),BRICK_WIDTH,BRICK_HEIGHT);
add(rectangular);
rectangular.setFilled(true);
if(j==1||j==0){
rectangular.setColor(Color.red);
}else if(j==3||j==2){
rectangular.setColor(Color.orange);
}else if(j==4||j==5){
rectangular.setColor(Color.yellow);
}else if(j==6||j==7){
rectangular.setColor(Color.green);
}else if(j==8||j==9){
rectangular.setColor(Color.cyan);
}
}
}
}
public void mousePressed(MouseEvent e){
last=new GPoint(e.getPoint());
ped=getElementAt(last);
}
public void mouseDragged(MouseEvent e){
if((ped.getX()+e.getX()-last.getX())<=WIDTH-PADDLE_WIDTH&&(ped.getX()+e.getX()-last.getX())>=0){
if(ped==peddle){
ped.move(e.getX()-last.getX(), 0);
last=new GPoint(e.getPoint());
}
}
}
private RandomGenerator rgen=RandomGenerator.getInstance();
private GRect rectangular;
private GRect peddle;
private GPoint last;
private GObject ped;
private GOval ball;
private static GObject gobj;
}
球上下都是一个速度,钥匙做成加速度的就好了。。。。响了好久还是弄成一个速度的了。。。。。。。。。。。。。 |
|