package com.example.tanqou;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import org.cocos2d.nodes.CCSprite;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
/**
* 小弹球游戏
* @author WangKunjie
*
*/
public class MainActivity extends Activity {
//随机数。每次进入游戏初始不一样
Random rand = new Random();
private double xyRate = rand.nextDouble() - 0.5; //一个-0.5~0.5的比率,控制小球的方向
//屏幕宽,高
private int tablewidth;
private int tableheight;
//球拍的水平位置,垂直位置,宽度,高度
private int racketx = rand.nextInt(200);//***每次进入游戏得到随机球拍初始位置
private int rackety ;
private int RACKET_WIDTH = 100;
private int RACKET_HEIGHT = 15;
//球的大小,x和Y坐标
private int BALL_SIZE = 12;
private int ballx = rand.nextInt(200) 20;//***每次进入游戏得到随机小球初始位置
private int bally = rand.nextInt(10) 20;//***
//球的纵向速度,横向速度
private int yspeed = 20;
private int xspped = (int) (yspeed*xyRate*2); //***每次进入游戏得到一个随机的横向速度
//游戏是否结束的标志
private boolean isLose = false;
//球拍的旧xy坐标
int oldx;
int oldy;
int r =(int)(Math.random()*220);
int g =(int)(Math.random()*220);
int b =(int)(Math.random()*220);
int a =(int)(Math.random()*220);
int score =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GameView gameView = new GameView(this);
setContentView(gameView);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN);
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
tablewidth = metrics.widthPixels;
tableheight = metrics.heightPixels;
Log.i("mydate" , tablewidth " " tableheight);
rackety = tableheight - 80;
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 1){
gameView.invalidate();
}
}
};
gameView.setOnKeyListener(new View.OnKeyListener() { //按键
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (event.getKeyCode()){
case KeyEvent.KEYCODE_A:
if (racketx > 0) racketx -= 10;
break;
case KeyEvent.KEYCODE_D:
if (racketx < tablewidth - RACKET_WIDTH) racketx = 10;
break;
}
gameView.invalidate();//控制球拍左右移动
return true;
}
});
gameView.setOnTouchListener(new View.OnTouchListener() { //触摸
@Override
public boolean onTouch(View v, MotionEvent event) {
int nowx = (int) event.getX();
//int nowy = (int) event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
oldx = nowx;
//oldy = nowy;
break;
case MotionEvent.ACTION_MOVE:
if (nowx > oldx){ //向右移动
racketx = 10;
} else if (nowx < oldx) {//向左移动
racketx -= 10;
}
oldx = nowx;
break;
case MotionEvent.ACTION_UP:
oldx = nowx;
break;
}
gameView.invalidate();
return true;
}
});
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//小球碰到游戏界面左右边框时
if (ballx <= 0 || ballx >= tablewidth - BALL_SIZE){
xspped = -xspped; //横向速度反向
}
//如果小球的高度超出了球拍的位置,且横向不在球拍的范围内,则GAME OVER
if (bally >= rackety - BALL_SIZE && (ballx < racketx || ballx > racketx RACKET_WIDTH)){
timer.cancel();
isLose = true; //游戏结束标志
} //如果小球在拍的范围内或碰到顶部,小球反弹
else if (bally <= 0 ){
yspeed = -yspeed; //纵向速度反向
}
else if(bally >= rackety-BALL_SIZE && ballx > racketx && ballx < racketx RACKET_WIDTH){
yspeed = -yspeed;
r =(int)(Math.random()*220);
g =(int)(Math.random()*220);
b =(int)(Math.random()*220);
a =(int)(Math.random()*220);
score =5;
}
//小球坐标不停的改变
bally = yspeed;
ballx = xspped;
handler.sendEmptyMessage(1);
}
} , 0 , 100); //每0.1秒执行刷新
}
class GameView extends View{
Paint paint = new Paint();
public GameView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
if (isLose){
paint.setColor(Color.RED);
paint.setTextSize(40);
canvas.drawText("GAME OVER" , tablewidth/2-100 , 200 , paint);
} else {
paint.setARGB(a, r, g, b);
canvas.drawCircle(ballx , bally , BALL_SIZE , paint); //绘制小球
paint.setColor(Color.BLUE);
canvas.drawRect(racketx , rackety , racketx RACKET_WIDTH , rackety RACKET_HEIGHT , paint);//绘制球拍
paint.setColor(Color.RED);
canvas.drawText("分数:" score, 50, 50, paint);
}
}
}
}
资源文件列表
tanqou/.classpath , 475
tanqou/.project , 842
tanqou/.settings/org.eclipse.jdt.core.prefs , 177
tanqou/AndroidManifest.xml , 878
tanqou/assets/image/timg.jpg , 24987
tanqou/bin/AndroidManifest.xml , 878
tanqou/bin/classes/android/support/v7/appcompat/R$anim.class , 629
tanqou/bin/classes/android/support/v7/appcompat/R$attr.class , 5136
tanqou/bin/classes/android/support/v7/appcompat/R$bool.class , 763
tanqou/bin/classes/android/support/v7/appcompat/R$color.class , 594
tanqou/bin/classes/android/support/v7/appcompat/R$dimen.class , 1530
tanqou/bin/classes/android/support/v7/appcompat/R$drawable.class , 5191
tanqou/bin/classes/android/support/v7/appcompat/R$id.class , 2556
tanqou/bin/classes/android/support/v7/appcompat/R$integer.class , 445
tanqou/bin/classes/android/support/v7/appcompat/R$layout.class , 1629
tanqou/bin/classes/android/support/v7/appcompat/R$string.class , 1141
tanqou/bin/classes/android/support/v7/appcompat/R$style.class , 9582
tanqou/bin/classes/android/support/v7/appcompat/R$styleable.class , 6541
tanqou/bin/classes/android/support/v7/appcompat/R.class , 1015
tanqou/bin/classes/com/example/tanqou/BuildConfig.class , 345
tanqou/bin/classes/com/example/tanqou/MainActivity$1.class , 997
tanqou/bin/classes/com/example/tanqou/MainActivity$2.class , 1445
tanqou/bin/classes/com/example/tanqou/MainActivity$3.class , 1503
tanqou/bin/classes/com/example/tanqou/MainActivity$4.class , 1896
tanqou/bin/classes/com/example/tanqou/MainActivity$GameView.class , 2295
tanqou/bin/classes/com/example/tanqou/MainActivity.class , 5021
tanqou/bin/classes/com/example/tanqou/R$anim.class , 599
tanqou/bin/classes/com/example/tanqou/R$attr.class , 5106
tanqou/bin/classes/com/example/tanqou/R$bool.class , 733
tanqou/bin/classes/com/example/tanqou/R$color.class , 564
tanqou/bin/classes/com/example/tanqou/R$dimen.class , 1598
tanqou/bin/classes/com/example/tanqou/R$drawable.class , 5196
tanqou/bin/classes/com/example/tanqou/R$id.class , 2565
tanqou/bin/classes/com/example/tanqou/R$integer.class , 415
tanqou/bin/classes/com/example/tanqou/R$layout.class , 1636
tanqou/bin/classes/com/example/tanqou/R$menu.class , 388
tanqou/bin/classes/com/example/tanqou/R$string.class , 1217
tanqou/bin/classes/com/example/tanqou/R$style.class , 9620
tanqou/bin/classes/com/example/tanqou/R$styleable.class , 6695
tanqou/bin/classes/com/example/tanqou/R.class , 921
tanqou/bin/classes.dex , 1542224
tanqou/bin/dexedLibs/android-support-v4-c348eaacef55daab6299f0530e8cf635.jar , 232610
tanqou/bin/dexedLibs/android-support-v7-appcompat-1ab12f9ea715548570fad83604eec90b.jar , 166
tanqou/bin/dexedLibs/android-support-v7-appcompat-55b500938bb53bcdf445050b046afb67.jar , 150574
tanqou/bin/dexedLibs/cocos2d-android-aa53488d5524377eb4f2c5c236b335f0.jar , 283556
tanqou/bin/jarlist.cache , 120
tanqou/bin/R.txt , 30746
tanqou/bin/res/crunch/drawable-hdpi/ic_launcher.png , 5964
tanqou/bin/res/crunch/drawable-mdpi/ic_launcher.png , 3112
tanqou/bin/res/crunch/drawable-xhdpi/ic_launcher.png , 9355
tanqou/bin/res/crunch/drawable-xxhdpi/ic_launcher.png , 17889
tanqou/bin/resources.ap_ , 411262
tanqou/bin/tanqou.apk , 1095166
tanqou/gen/android/support/v7/appcompat/R.java , 41086
tanqou/gen/com/example/tanqou/BuildConfig.java , 160
tanqou/gen/com/example/tanqou/R.java , 180751
tanqou/ic_launcher-web.png , 51394
tanqou/libs/android-support-v4.jar , 621451
tanqou/libs/cocos2d-android.jar , 660980
tanqou/proguard-project.txt , 781
tanqou/project.properties , 623
tanqou/res/drawable-hdpi/ic_launcher.png , 7658
tanqou/res/drawable-mdpi/ic_launcher.png , 3777
tanqou/res/drawable-xhdpi/ic_launcher.png , 12516
tanqou/res/drawable-xxhdpi/ic_launcher.png , 24777
tanqou/res/layout/activity_main.xml , 666
tanqou/res/menu/main.xml , 263
tanqou/res/values/dimens.xml , 220
tanqou/res/values/strings.xml , 223
tanqou/res/values/styles.xml , 697
tanqou/res/values-sw600dp/dimens.xml , 203
tanqou/res/values-sw720dp-land/dimens.xml , 277
tanqou/res/values-v11/styles.xml , 334
tanqou/res/values-v14/styles.xml , 391
tanqou/src/com/example/tanqou/MainActivity.java , 7081
