老师说,我们要做个弹球游戏,好,我就做了个传说中的弹球游戏。
先看效果:
好吧,的确是简陋了点,不过基本功能还是实现了的。
先看主界面
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/ballbg1" > <ImageButton android:id="@+id/play" android:layout_width="120dp" android:layout_height="60dp" android:layout_above="@+id/exit" android:layout_centerHorizontal="true" android:layout_marginBottom="37dp" android:background="@drawable/play1" /> <ImageButton android:id="@+id/exit" android:layout_width="120dp" android:layout_height="60dp" android:layout_alignLeft="@+id/play" android:layout_alignParentBottom="true" android:layout_marginBottom="56dp" android:background="@drawable/exit1" /> </RelativeLayout>
ballbg1.png
play.png
exit.png
图片都是一手p出来的,水平虽然差了点,不过是血和泪的结晶啊。
再看主界面.java
package com.PinballGame.pinball;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
//屏幕宽
public static int tableWidth;
//屏幕高
public static int tableHeight;
//常亮属性
PowerManager powerManager = null;
WakeLock wakeLock = null;
MediaPlayer media = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//音频
// media = MediaPlayer.create(this, R.raw.start);
// media.start();
//设置常亮
this.powerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
this.wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
//获取窗口管理器
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
//获取屏幕宽和高
tableWidth = metrics.widthPixels;
tableHeight = metrics.heightPixels;
//按钮宽和高
int botwidth = (int) ((int) tableWidth/2.5);
int botheight = (int) ((int) tableHeight/8);
//设置play
ImageButton playbot =(ImageButton) findViewById(R.id.play);
ViewGroup.LayoutParams playbotn = (ViewGroup.LayoutParams) playbot.getLayoutParams();
playbotn.width = botwidth;
playbotn.height = botheight;
playbot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View source) {
Intent intent = new Intent(MainActivity.this, PlayActivity.class);
startActivity(intent);
}
});
//设置 exit
ImageButton exitbot =(ImageButton) findViewById(R.id.exit);
ViewGroup.LayoutParams exitbotn = (ViewGroup.LayoutParams) exitbot.getLayoutParams();
exitbotn.width = botwidth;
exitbotn.height = botheight;
exitbot.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View source) {
/** 弹出对话框*/
new AlertDialog.Builder(MainActivity.this)
//设置标题
.setTitle("天王盖地虎")
//设置图标
.setIcon(R.drawable.goodbay)
//设置内容
.setMessage("你确定要离开?")
//添加确定按钮
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
System.exit(0);
}
})
//添加取消按钮
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.create()
.show();
}
});
}
//唤醒
@Override
protected void onResume(){
super.onResume();
// media.start();
this.wakeLock.acquire();
}
//暂停
@Override
protected void onPause(){
super.onPause();
// media.stop();
this.wakeLock.release();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
好吧,里面有屏幕常亮啦,唤醒啦,bababab~,好吧。其他好像没什么
再看play_activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/playRoot" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout>
平淡无奇,好继续。
看看 PlayActivity.java
package com.PinballGame.pinball;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class PlayActivity extends Activity implements OnTouchListener, OnGestureListener {
//常亮属性
PowerManager powerManager = null;
WakeLock wakeLock = null;
//playView
PlayView playView = null;
/**手势*/
private GestureDetector mGestureDetector;
public PlayActivity(){
mGestureDetector = new GestureDetector( (OnGestureListener) this );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play_activity);
//设置常亮
this.powerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
this.wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
//创建PlayView组件
playView = new PlayView(this, null);
setContentView(playView);
playView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
});
//handler
final Handler handler = new Handler(){
public void handleMessage(Message msg){
if( msg.what == 0x123){
playView.invalidate();
}
}
};
//定时器
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//结束判断
if( (playView.circleY + playView.radius) > (playView.paiY + playView.paiHeight) ){
timer.cancel();
playView.isOver = true;
}else{
//碰到墙壁时X的改变
if( playView.circleX <= playView.radius
|| playView.circleX >= playView.screenWidth - playView.radius ){
playView.speedX = -playView.speedX;
}
//碰到墙壁时Y的改变
if( playView.circleY <= playView.radius
|| ( playView.circleX >= playView.paiX
&& playView.circleX <= (playView.paiX + playView.paiWidth)
&& (playView.circleY + playView.radius) >= playView.paiY
&& (playView.circleY + playView.radius) <= playView.paiY + playView.paiHeight) ){
playView.speedY = -playView.speedY;
}
//初始化后才判断
if( playView.baffleX != null ){
//碰到障碍时X的改变
for( int i = 0; i < playView.baffleLev; i++ ){
int tag = 0;
if( playView.circleY >= playView.baffleY[i]
&& playView.circleY <= (playView.baffleY[i] + playView.perHeight) ){
for( int j = 0; j < playView.baffleNum; j++ ){
if( playView.baffleArr[j][i] == 0 ){
if( playView.circleX >= (playView.baffleX[j] - playView.radius)
&& playView.circleX <= (playView.baffleX[j] + playView.perWidth + playView.radius) ){
playView.speedX = -playView.speedX;
playView.baffleArr[j][i] = 1;
playView.baffleTotle--;
tag = 1;
break;
}
}
}
if( tag == 1 ) break;
}
}
//碰到障碍时Y的改变
for( int i = 0; i < playView.baffleNum; i++ ){
int tag = 0;
if( playView.circleX >= playView.baffleX[i]
&& playView.circleX <= ( playView.baffleX[i] + playView.perWidth ) ){
for( int j = 0; j < playView.baffleLev; j++ ){
if( playView.baffleArr[i][j] == 0 ){
if( playView.circleY >= (playView.baffleY[j] - playView.radius)
&& playView.circleY <= (playView.baffleY[j] + playView.perHeight+ playView.radius) ){
playView.speedY = -playView.speedY;
playView.baffleArr[i][j] = 1;
playView.baffleTotle--;
tag = 1;
break;
}
}
}
if( tag == 1) break;
}
}
}
playView.circleX += playView.speedX;
playView.circleY += playView.speedY;
}
handler.sendEmptyMessage(0x123);
}
}, 0, 10);
}
//唤醒
@Override
protected void onResume(){
super.onResume();
this.wakeLock.acquire();
}
//暂停
@Override
protected void onPause(){
super.onPause();
this.wakeLock.release();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**手势中的按下*/
public boolean onDown(MotionEvent e){
return true;
}
/**手势中的拖动*/
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
float tempX = playView.paiX - distanceX;
if( tempX < 0 ) tempX = 0;
if( tempX > playView.screenWidth - playView.paiWidth ) tempX = playView.screenWidth - playView.paiWidth;
playView.paiX = tempX;
playView.invalidate();
return true;
}
@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
里面有手势,不过不是很懂。
在看PlayView.java
package com.PinballGame.pinball;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class PlayView extends View{
//屏幕宽高
int screenWidth = 0;
int screenHeight = 0;
//障碍挡板宽
public int baffleWith;
//每份宽
int perWidth = 0;
//间隔宽
int InterWidth = 0;
//每份高
int perHeight = 0;
//间隔高
int InterHeight = 0;
//挡板层数
public int baffleLev = 8;
//每层挡板数
public int baffleNum = 5;
//挡板颜色
public int[] colors = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW};
//挡板总数
public int baffleTotle = baffleLev * baffleNum;
//障碍物X坐标
int baffleX [] = null;
//障碍物Y坐标
int baffleY [] = null;
//障碍物标签
int baffleArr[][] = null;
//画笔
Paint paint = null;
//弹球的X坐标
float circleX = -1;
//弹球的Y坐标
float circleY = -1;
//弹球的半径
int radius = 0;
//弹球的X初速度
int speedX = 1;
//弹球的Y初速度
int speedY = 1;
//球拍X坐标
float paiX = -1;
//球拍Y坐标
float paiY = -1;
//球拍宽
int paiWidth = 0;
//球拍高
int paiHeight = 0;
//游戏是否结束
boolean isOver = false;
//游戏得分
int scroe = 0;
public PlayView(Context context, AttributeSet set) {
super(context, set);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
if( isOver ){
paint = paint != null ? paint : new Paint();
paint.setColor(Color.RED);
paint.setTextSize(40);
canvas.drawText("游戏结束啦", 50, 200, paint);
}else if( baffleTotle == 0 ){
paint.setColor(Color.RED);
paint.setTextSize(40);
canvas.drawText("你胜利啦", 50, 200, paint);
}else{
//把画布变成黑色
canvas.drawColor(Color.BLACK);
//画障碍物
drawBaffle(canvas);
//画球
drawCircle(canvas);
//画拍子
drawPai(canvas);
}
}
/**画障碍物*/
public void drawBaffle(Canvas canvas){
//屏幕宽高
screenWidth = screenWidth != 0 ? screenWidth : MainActivity.tableWidth;
screenHeight = screenHeight != 0 ? screenHeight: MainActivity.tableHeight;
//每份宽
perWidth = perWidth != 0 ? perWidth : screenWidth/(baffleNum+2);
//间隔宽
InterWidth = InterWidth != 0 ? InterWidth : perWidth*2/(baffleNum+1);
int tempLev = baffleLev/2;
//每份高
perHeight = perHeight != 0 ? perHeight : screenHeight/2/(colors.length+tempLev+2);
//间隔高
InterHeight = InterHeight !=0 ? InterHeight : perHeight/2;
int tempHeight = 0;
int tempH = 0;
int tempWidht = 0;
int tempW = 0;
//画笔
paint = new Paint();
//去锯齿
paint.setAntiAlias(true);
//设置画笔宽
paint.setStrokeWidth(3);
//设置填充
paint.setStyle(Paint.Style.FILL);
//初始化障碍物X坐标
baffleX = baffleX != null ? baffleX : new int[baffleNum];
//初始化障碍物Y坐标
baffleY = baffleY != null ? baffleY : new int[baffleLev];
//初始化障碍物标签
baffleArr = baffleArr != null ? baffleArr : new int[baffleNum][baffleLev];
for(int i = 0; i < baffleLev; i++ ){
//设置颜色
paint.setColor(colors[i]);
if ( i == 0 ) tempHeight = InterHeight + perHeight;
else tempHeight = tempHeight + InterHeight + perHeight;
tempH = tempHeight + perHeight;
//保存Y坐标
if( baffleY[i] == 0 ) baffleY[i] = tempHeight;
for(int j = 0; j < baffleNum; j++ ){
tempWidht = (j+1)*InterWidth + j*perWidth;
tempW = tempWidht + perWidth;
//保存X坐标
if( baffleX[j] == 0 ) baffleX[j] = tempWidht;
//判断是否已消除
if(baffleArr[j][i] == 0 ){
canvas.drawRect(tempWidht, tempHeight ,tempW ,tempH, paint);
}
}
}
}
/**画弹球*/
public void drawCircle(Canvas canvas){
paint.setColor(Color.WHITE);
circleX = circleX != -1 ? circleX : screenWidth/8;
circleY = circleY != -1 ? circleY : screenHeight/2 + perHeight;
radius = radius != 0 ? radius : perHeight/2;
canvas.drawCircle(circleX, circleY, radius, paint);
}
/**画拍子*/
public void drawPai(Canvas canvas){
paiWidth = paiWidth != 0 ? paiWidth : screenWidth/5;
paiHeight = paiHeight != 0 ? paiHeight : perHeight;
paiX = paiX != -1 ? paiX : (screenWidth-paiWidth)/2;
paiY = paiY != -1 ? paiY : screenHeight - perHeight*3;
canvas.drawRect(paiX, paiY,paiX+paiWidth ,paiY+paiHeight ,paint);
}
}
然后展示给老师看,老师说,勉强给个及格。我操###,写了不少时间好么。
还说view不好,要改回surferView,不过太懒了,不想改了,凑合用着吧。
源码奉上,欢迎改造!
http://pan.baidu.com/s/1xhtxt
apk下载:
http://pan.baidu.com/share/link?shareid=3381226173&uk=1444496286