import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class PinBall extends Activity {
private int tableWidth;
private int tableHeight;
private int racketY;
private final int RACKET_HEIGHT = 20; // 球拍高度
private final int RACKET_WIDTH = 140; // 球拍长度
private final int BALL_SIZE = 12; // 球半径
private int ySpeed =2;
Random rand = new Random();
private double xyRate = rand.nextDouble() - 0.5;
private int xSpeed = (int) (ySpeed * xyRate * 2)+1;
private int ballX = rand.nextInt(200) + 20;
private int ballY = 50;
private int racketX = rand.nextInt(200);
private boolean isLose = false;
private Bitmap bmp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
final GameView gameView = new GameView(this);
setContentView(gameView);
bmp=BitmapFactory.decodeResource(this.getResources(), R.drawable.app_brick_breaker);
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
tableWidth = display.getWidth();
tableHeight = display.getHeight();
racketY = tableHeight - 80;
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
gameView.invalidate();
}
}
};
gameView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// 如果小球碰到左边边框
if (ballX <= 0) {
System.out.println("xspeed_left=" + xSpeed); // xSpeed=-9
xSpeed = -xSpeed;
}
// 如果小球碰到右边边框
if (ballX >= tableWidth - bmp.getHeight()) {
System.out.println("xspeed_right=" + xSpeed); // xSpeed=-9
xSpeed = -xSpeed;
}
// 小球碰到上屏幕
if (ballY <= 0) {
System.out.println("yspeed_top=" + ySpeed); // xSpeed=-9
ySpeed = -ySpeed;
}
// 如果小球高度超出了球拍位置,且横向不在球拍范围之内,游戏结束。
if (ballY > racketY - bmp.getHeight()
&& ((ballX+bmp.getWidth()/2 )< racketX || ballX > racketX + RACKET_WIDTH)) {
timer.cancel();
// 设置游戏是否结束的旗标为true
isLose = true;
}
// 如果小球位于球拍之内,且到达球拍位置,小球反弹
else if ((ballY + bmp.getHeight() >= racketY && (ballX+bmp.getWidth()/2 ) >= racketX && ballX <= racketX
+ RACKET_WIDTH)) {
ySpeed = -ySpeed;
}
// 小球座标增加
ballY += ySpeed;
ballX += xSpeed;
handler.sendEmptyMessage(0x123);
}
}, 0, 10);
}
class GameView extends View {
public GameView(Context context) {
super(context);
setFocusable(true);
}
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
if (isLose) {
paint.setColor(Color.RED);
paint.setTextSize(40);
canvas.drawText("游戏ѽ结束", tableWidth/2-100, tableHeight/2, paint);
} else {
//paint.setColor(Color.rgb(255, 255, 255)); // 画出小球的颜色
//canvas.drawCircle(ballX, ballY, BALL_SIZE, paint); // 画出小球
canvas.drawBitmap(bmp, ballX, ballY,paint);
paint.setColor(Color.rgb(80, 80, 200)); // 画出挡板颜色
canvas.drawRect(racketX, racketY, racketX + RACKET_WIDTH, // 画出挡板
racketY + RACKET_HEIGHT, paint);
}
}
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
if (x <= RACKET_WIDTH / 2) {
racketX = 0;
}else if(x>=(tableWidth-RACKET_WIDTH / 2)){
racketX=tableWidth-RACKET_WIDTH;
}else{
racketX = (int) (x - RACKET_WIDTH / 2);
}
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}
}
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class PinBall extends Activity {
private int tableWidth;
private int tableHeight;
private int racketY;
private final int RACKET_HEIGHT = 20; // 球拍高度
private final int RACKET_WIDTH = 140; // 球拍长度
private final int BALL_SIZE = 12; // 球半径
private int ySpeed =2;
Random rand = new Random();
private double xyRate = rand.nextDouble() - 0.5;
private int xSpeed = (int) (ySpeed * xyRate * 2)+1;
private int ballX = rand.nextInt(200) + 20;
private int ballY = 50;
private int racketX = rand.nextInt(200);
private boolean isLose = false;
private Bitmap bmp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
final GameView gameView = new GameView(this);
setContentView(gameView);
bmp=BitmapFactory.decodeResource(this.getResources(), R.drawable.app_brick_breaker);
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
tableWidth = display.getWidth();
tableHeight = display.getHeight();
racketY = tableHeight - 80;
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
gameView.invalidate();
}
}
};
gameView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// 如果小球碰到左边边框
if (ballX <= 0) {
System.out.println("xspeed_left=" + xSpeed); // xSpeed=-9
xSpeed = -xSpeed;
}
// 如果小球碰到右边边框
if (ballX >= tableWidth - bmp.getHeight()) {
System.out.println("xspeed_right=" + xSpeed); // xSpeed=-9
xSpeed = -xSpeed;
}
// 小球碰到上屏幕
if (ballY <= 0) {
System.out.println("yspeed_top=" + ySpeed); // xSpeed=-9
ySpeed = -ySpeed;
}
// 如果小球高度超出了球拍位置,且横向不在球拍范围之内,游戏结束。
if (ballY > racketY - bmp.getHeight()
&& ((ballX+bmp.getWidth()/2 )< racketX || ballX > racketX + RACKET_WIDTH)) {
timer.cancel();
// 设置游戏是否结束的旗标为true
isLose = true;
}
// 如果小球位于球拍之内,且到达球拍位置,小球反弹
else if ((ballY + bmp.getHeight() >= racketY && (ballX+bmp.getWidth()/2 ) >= racketX && ballX <= racketX
+ RACKET_WIDTH)) {
ySpeed = -ySpeed;
}
// 小球座标增加
ballY += ySpeed;
ballX += xSpeed;
handler.sendEmptyMessage(0x123);
}
}, 0, 10);
}
class GameView extends View {
public GameView(Context context) {
super(context);
setFocusable(true);
}
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
if (isLose) {
paint.setColor(Color.RED);
paint.setTextSize(40);
canvas.drawText("游戏ѽ结束", tableWidth/2-100, tableHeight/2, paint);
} else {
//paint.setColor(Color.rgb(255, 255, 255)); // 画出小球的颜色
//canvas.drawCircle(ballX, ballY, BALL_SIZE, paint); // 画出小球
canvas.drawBitmap(bmp, ballX, ballY,paint);
paint.setColor(Color.rgb(80, 80, 200)); // 画出挡板颜色
canvas.drawRect(racketX, racketY, racketX + RACKET_WIDTH, // 画出挡板
racketY + RACKET_HEIGHT, paint);
}
}
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
if (x <= RACKET_WIDTH / 2) {
racketX = 0;
}else if(x>=(tableWidth-RACKET_WIDTH / 2)){
racketX=tableWidth-RACKET_WIDTH;
}else{
racketX = (int) (x - RACKET_WIDTH / 2);
}
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}
}