黑马程序员技术交流社区
标题:
ANDROID小游戏海盗船4
[打印本页]
作者:
woshiku
时间:
2015-9-13 09:44
标题:
ANDROID小游戏海盗船4
package com.testshipboat;
import xieyan.ControlInterface;
import xieyan.DataInterface;
import xieyan.GameOverListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;
import com.data.Data;
import com.gameview.GameData;
import com.gameview.GameThree;
import com.gameview.GameTwo;
import com.gameview.GameTwo.CanPassListener;
import com.gameview.GameTwo.ChangeLayout;
import com.gameview.MyData;
import com.gameview.WinGameScreen;
public class TestShip extends Activity implements ChangeLayout, GameOverListener, CanPassListener {
Context context;
private GameTwo game_two_panel;
private GameThree game_one_panel;
private TextView stage;
private TextView life;
private TextView score;
private int game_one_width;
private int game_one_height;
private int game_two_width;
private int game_two_height;
MyHandler myHandle;
private class MyHandler extends Handler{
@SuppressLint("HandlerLeak")
public void handleMessage(Message msg){
switch(msg.what){
case 1:
stage.setText(msg.getData().getString("msg"));
break;
case 2:
score.setText(msg.getData().getString("msg"));
break;
case 3:
life.setText(msg.getData().getString("msg"));
break;
}
}
}
private void sendMessage(int what,String str){
Message msg = new Message();
msg.what = what;
Bundle bd = new Bundle();
bd.putString("msg", str);
msg.setData(bd);
myHandle.sendMessage(msg);
}
private void init(){
context =TestShip.this;
ControlInterface cont=new ControlInterface();
cont.start(Data.currentStage);
stage = (TextView)findViewById(R.id.stage);
life = (TextView)findViewById(R.id.life);
score = (TextView)findViewById(R.id.score);
myHandle = new MyHandler();
game_one_width = Data.getScreenWidth(getWindowManager())/4;
game_two_width = Data.getScreenWidth(getWindowManager())/4*3;
game_one_height = game_one_width;
Data.game_one_width = game_one_height;
game_two_height = Data.getScreenHeight(getWindowManager());
Data.game_two_height = game_two_height ;
Data.game_two_width = game_two_width;
Data.minWidth = game_two_width/Data.stage_1_arrow+game_two_width/32;
Data.maxWidth = game_two_width/Data.stage_1_arrow+game_two_width/16;
DataInterface data = new DataInterface();
sendMessage(1,Data.currentStage+"");
sendMessage(2,data.getScore()+"");
sendMessage(3,data.getLife()+"");
//ControlInterface cont = new ControlInterface();
cont.addGameOverListener(TestShip.this);
}
private void init_game_one(){
Data.stage_size = Data.minWidth+(Data.maxWidth-Data.minWidth)/6;
game_one_panel = (GameThree)findViewById(R.id.game_view_one);
game_one_panel.setWidth(game_one_width);
game_one_panel.setHeight(game_one_height);
//int gap_x = Data.stage_size*Data.stage_1_arrow - game_two_width;
//int gap_y = Data.stage_size*Data.stage_1_arrow -game_two_height;
game_one_panel.calcuate( 0,0);
}
private void init_game_two(){
game_two_panel = (GameTwo)findViewById(R.id.game_view_two);
game_two_panel.setChangeLayout(this);
game_two_panel.setCanPassListener(this);
game_two_panel.init_gap();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ship_layout);
init();
init_game_one();
init_game_two();
}
@Override
public void changeLayout(GameTwo gameTwo, MyData myData,GameData gameData) {
// TODO Auto-generated method stub
if(myData!=null){
//game_one_panel.calcuate(myData.getGapWidth(), myData.getGapHeight(), myData.getXoff(),myData.getYoff());
game_one_panel.calcuate(myData.getXoff(),myData.getYoff());
}
if(gameData!=null){
sendMessage(1,gameData.getStage()+"");
sendMessage(2,gameData.getScore()+"");
sendMessage(3,gameData.getLife()+"");
}
}
@Override
public void gameOver() {
// TODO Auto-generated method stub
DataInterface data = new DataInterface();
if(data.getScore()>Data.addLifeScore){
data.setScore(data.getScore()-Data.addLifeScore);
data.setLife(data.getLife()+1);
}else{
finish();
Toast.makeText(context, "game over", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPass(GameTwo game) {
// TODO Auto-generated method stub
//进入游戏
//ControlInterface cont=new ControlInterface();
//关卡加1
Data.currentStage++;
if(Data.currentStage>Data.maxStage){
//cont.start(Data.currentStage);
Toast.makeText(context, "你已经通关了,请选择高难度!", Toast.LENGTH_SHORT).show();
finish();
}else{
Intent it = new Intent();
it.setClass(context, WinGameScreen.class);
startActivity(it);
finish();
}
}
}
作者:
往事如风555
时间:
2015-9-15 22:48
看的宝宝的头晕了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2