Android游戏开发之数独课时----4

新加了一个类:Game

package com.example.shudu_1;
public class Game {
private final String str="360000000"+
                "004230800"+
                "000004200"+
                "070460003"+
                "820000014"+
                "500013020"+
                "001900000"+
                "007048300"+
                "000000045";
private int sudoku[]=new int[9*9];
public Game(){
sudoku=fromPuzzleString(str);
}
private int getTile(int x,int y){
return sudoku[y*9+x];
}
public String getTileString(int x,int y){
int v=getTile(x, y);
if(v==0){
//当值为零的时候是不填写的
return "";
}else
return String.valueOf(v);
}
private int[] fromPuzzleString(String src) {
int []sudo=new int[src.length()];
for(int i=0;i<sudo.length;++i){
sudo[i]=src.charAt(i)-'0';
}
return sudo;
}
}

第二步:MyView

package com.example.shudu_1;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.support.v4.view.PagerAdapter;
import android.text.style.LineHeightSpan.WithDensity;
import android.view.View;
public class MyView extends View {
//设置单元格的宽度和高度
private float width;
private float height;
private Game game=new Game();
public MyView(Context context) {
super(context);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//计算当前单元格的高度和宽度
this.width=w/9f;
this.height=h/9f;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
//生成用于绘制背景色的画笔
Paint backgroundPaint=new Paint();
//设置背景的颜色
backgroundPaint.setColor(getResources().getColor(R.color.shudu_background));
//绘制背景色
canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundPaint);

Paint darkPain=new Paint();
darkPain.setColor(getResources().getColor(R.color.shudu_dark));

Paint hiliPaint=new Paint();
hiliPaint.setColor(getResources().getColor(R.color.shudu_hilite));

Paint lightPaint=new Paint();
lightPaint.setColor(getResources().getColor(R.color.shudu_light));
for(int i=0;i<9;++i){
//以下两行代码用户绘制横向的单元格
//明线和黑线才能合成一条雕刻的线
canvas.drawLine(0, i*height, getWidth(), i*height,lightPaint);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1,hiliPaint);
//纵向
canvas.drawLine(i*width, 0, i*width, getHeight(), lightPaint);
canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), hiliPaint);
}
//绘制黑线
for(int i=0;i<9;++i){
if(i%3!=0){
continue;
}
//以下两行代码用户绘制横向的单元格
//明线和黑线才能合成一条雕刻的线
canvas.drawLine(0, i*height, getWidth(), i*height,darkPain);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1,hiliPaint);
//纵向
canvas.drawLine(i*width, 0, i*width, getHeight(), darkPain);
canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), hiliPaint);
}
//绘制文字
Paint numberPain=new Paint();
numberPain.setColor(Color.BLACK);
numberPain.setStyle(Paint.Style.STROKE);
numberPain.setTextSize(height*0.75f);
numberPain.setTextAlign(Paint.Align.CENTER);

FontMetrics fm=numberPain.getFontMetrics();
float x=width/2;
float y=height/2-(fm.ascent+fm.descent)/2;
for(int i=0;i<9;++i){
for(int j=0;j<9;++j){
canvas.drawText(game.getTileString(i, j), i*width+x, j*height+y, numberPain);
}
}
   super.onDraw(canvas);
}
}

第三部:

package com.example.shudu_1;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setContentView(new MyView(this));
}


@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;
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值