当前位置:操作系统 > 安卓/Android >>

Android --- 斗地主 [牌桌实现源码]

最近一直在研究关于游戏编程,人在深圳,工作不好找啊!

发一个斗地主游戏的牌桌实现。

为了节约内存资源,每张扑克牌都是剪切形成的,当然这也是当前编程的主流方法。

 

 1、主Activity


<span style="font-size:18px;color:#3333ff;">package com.bison; 
 
import android.app.Activity; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
import android.view.Window; 
import android.view.WindowManager; 
 
/**
 * 求某公司易做图
 * 
 * @author Bison
 * 
 */ 
public class PukeActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        // 这个事隐藏标题栏,不解释 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        // 隐藏状态栏,你懂的 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
        /*
         * 开始有考虑使屏幕上扑克的排列随屏幕的分辨率变动 结果貌似不好做,注释掉了 Display display =
         * getWindowManager().getDefaultDisplay(); int screenWidth =
         * display.getWidth(); int screenHeight = display.getHeight();
         */ 
 
        // 使用代码锁定横屏 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
        // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);这个是竖屏 
        setContentView(new GameView(this)); 
    } 
}</span> 
2、牌桌页面


<span style="color:#3333ff;">package com.bison; 
 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Rect; 
import android.view.MotionEvent; 
import android.view.Su易做图ceHolder; 
import android.view.Su易做图ceView; 
 
import com.bison.utils.Person; 
 
/**
 * 牌桌,会被老婆骂,最好不要上去,你懂的
 * 
 * 扑克图片来源,和牌桌背景在文章的下面。 扑克背面图等我没上传,玩家自行百度
 * 
 * @author Bison
 * 
 */ 
public class GameView extends Su易做图ceView implements Su易做图ceHolder.Callback { 
    private FlushThread thread = null;// 刷帧线程 
    private Bitmap sourceBitmap = null;// 扑克图片来源 
    private Bitmap backgroundDesk = null;// 牌桌背景 
    private Bitmap backgroundPuke = null;// 扑克背面 
 
    private final Person person; 
    private int pukeWidth = 0;// 扑克的宽 
    private int pukeHeight = 0;// 扑克的高 
    private int deskWidth = 0;// 牌桌的宽 
    private int deskHeight = 0;// 牌桌的高 
    private int left = 0;// 我自己首张牌左距离 
 
    public GameView(Context context) { 
        super(context); 
        getHolder().addCallback(this); 
        this.thread = new FlushThread(getHolder(), this);// 实例化线程 
        initBitmap();// 实例化图片 
        this.person = new Person();// 实例化Person类 
        this.left = deskWidth / 2 - (16 * 25 + pukeWidth) / 2;// 左距开始时赋值 
    } 
 
    private void initBitmap() {// 初始化图片 
        sourceBitmap = BitmapFactory.decodeResource(getResources(), 
                R.drawable.smallcard); 
        pukeWidth = sourceBitmap.getWidth() / 14;// 每张扑克的宽高 
        pukeHeight = sourceBitmap.getHeight() / 4; 
 
        backgroundDesk = BitmapFactory.decodeResource(getResources(), 
                R.drawable.gameback2); 
 
        deskWidth = backgroundDesk.getWidth();// 牌桌的宽高 
        deskHeight = backgroundDesk.getHeight(); 
 
        backgroundPuke = BitmapFactory.decodeResource(getResources(), 
                R.drawable.cardback); 
    } 
 
    @Override 
    protected void onDraw(Canvas canvas) { 
        // 绘制牌桌 
        canvas.drawBitmap(backgroundDesk, 0, 0, null); 
        personPaint(canvas, pukeWidth, pukeHeight); 
        deskthreePukes(canvas, pukeWidth, pukeHeight); 
    } 
 
    /** 绘制每个玩家手里的牌 */ 
    public void personPaint(Canvas c, int pukeWidth, int pukeHeight) { 
        Rect src = new Rect(); 
        Rect dst = new Rect(); 
 
        // 遍历数组 
        for (int i = 0; i < 3; i++) { 
            for (int j = 0; j < 17; j++) { 
                if (i == 0) {// 左手边玩家,不用绘出正面 
                    // src = person.cardRect(person.person1[j], pukeWidth, 
                    // pukeHeight); 
                    // dst.set(10, j * 20, 10 + pukeWidth, j * 20 + pukeHeight); 
 &nbs

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,