Android 小項目之---猜撲克牌遊戲 (附源碼)
2010-06-03 10:36 Terry_龙 阅读(3733) 评论(1) 编辑 收藏 举报本實例參考《Google Android SDK开发范例大全》。
針對上述書本,個人的一點點優化和修正的一處BUG,感興趣的不妨可以參考一下,
運行圖:
![](https://images.cnblogs.com/cnblogs_com/terryblog/3.png)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.terry;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class testGame extends Activity {
private ImageView myImageView;
private ImageView myImageView2;
private ImageView myImageView3;
private Button myButton;
private int a=0;
private int[] myImage={
R.drawable.p01,
R.drawable.p02,
R.drawable.p03,
};
private View.OnClickListener MyimageClistener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myImageView=(ImageView)findViewById(R.id.mImage01);
myImageView2=(ImageView)findViewById(R.id.mImage02);
myImageView3=(ImageView)findViewById(R.id.mImage03);
myButton=(Button)findViewById(R.id.mButton);
Randon();
MyimageClistener=new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(a==0)
{
switch (v.getId()) {
case R.id.mImage01:
SetImage(0,myImageView);
break;
case R.id.mImage02:
SetImage(1,myImageView2);
break;
case R.id.mImage03:
SetImage(2,myImageView3);
break;
default:
break;
}
}
}
};
myImageView.setOnClickListener(MyimageClistener);
myImageView2.setOnClickListener(MyimageClistener);
myImageView3.setOnClickListener(MyimageClistener);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(a==1)
{
myImageView.setImageResource(R.drawable.p04);
myImageView2.setImageResource(R.drawable.p04);
myImageView3.setImageResource(R.drawable.p04);
myImageView.setAlpha(255);
myImageView2.setAlpha(255);
myImageView3.setAlpha(255);
Randon();
a--;
}
}
});
}
private void SetImage(int temp,ImageView ss)
{
myImageView.setImageResource(myImage[0]);
myImageView2.setImageResource(myImage[1]);
myImageView3.setImageResource(myImage[2]);
myImageView.setAlpha(100);
myImageView2.setAlpha(100);
myImageView3.setAlpha(100);
ss.setAlpha(255);
if(myImage[temp]==R.drawable.p01)
{
Toast.makeText(testGame.this, "恭喜你猜對了", Toast.LENGTH_SHORT)
.show();
}
else
{
Toast.makeText(testGame.this, "對不起你猜錯了", Toast.LENGTH_SHORT)
.show();
}
a++;
}
private void Randon()
{
for(int i=0;i<myImage.length;i++)
{
int temp=myImage[i];
int s=(int)(Math.random()*2);
myImage[i]=myImage[s];
myImage[s]=temp;
}
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class testGame extends Activity {
private ImageView myImageView;
private ImageView myImageView2;
private ImageView myImageView3;
private Button myButton;
private int a=0;
private int[] myImage={
R.drawable.p01,
R.drawable.p02,
R.drawable.p03,
};
private View.OnClickListener MyimageClistener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myImageView=(ImageView)findViewById(R.id.mImage01);
myImageView2=(ImageView)findViewById(R.id.mImage02);
myImageView3=(ImageView)findViewById(R.id.mImage03);
myButton=(Button)findViewById(R.id.mButton);
Randon();
MyimageClistener=new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(a==0)
{
switch (v.getId()) {
case R.id.mImage01:
SetImage(0,myImageView);
break;
case R.id.mImage02:
SetImage(1,myImageView2);
break;
case R.id.mImage03:
SetImage(2,myImageView3);
break;
default:
break;
}
}
}
};
myImageView.setOnClickListener(MyimageClistener);
myImageView2.setOnClickListener(MyimageClistener);
myImageView3.setOnClickListener(MyimageClistener);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(a==1)
{
myImageView.setImageResource(R.drawable.p04);
myImageView2.setImageResource(R.drawable.p04);
myImageView3.setImageResource(R.drawable.p04);
myImageView.setAlpha(255);
myImageView2.setAlpha(255);
myImageView3.setAlpha(255);
Randon();
a--;
}
}
});
}
private void SetImage(int temp,ImageView ss)
{
myImageView.setImageResource(myImage[0]);
myImageView2.setImageResource(myImage[1]);
myImageView3.setImageResource(myImage[2]);
myImageView.setAlpha(100);
myImageView2.setAlpha(100);
myImageView3.setAlpha(100);
ss.setAlpha(255);
if(myImage[temp]==R.drawable.p01)
{
Toast.makeText(testGame.this, "恭喜你猜對了", Toast.LENGTH_SHORT)
.show();
}
else
{
Toast.makeText(testGame.this, "對不起你猜錯了", Toast.LENGTH_SHORT)
.show();
}
a++;
}
private void Randon()
{
for(int i=0;i<myImage.length;i++)
{
int temp=myImage[i];
int s=(int)(Math.random()*2);
myImage[i]=myImage[s];
myImage[s]=temp;
}
}
}
修改BUG一處,當用戶選擇完后,不可以再選,需要重新開始遊戲。
優化如下:
1:提取公共事件。
2:封裝相同代碼,如SetImage這個方法。
源碼下載:/Files/TerryBlog/testGame.rar
如果你有什麽疑問或建議請:QQ :285735942 或 Email:terryyhl@gmail.com