讲到图形处理,给学生做的一个小游戏案例!

井字棋 

MainActivity

package com.example.three_chess;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TestView tView=new TestView(this);

setContentView(tView);

//CwjView tView2=new CwjView(this,null);

//setContentView(tView2);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

}

TestView

package com.example.three_chess;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.Point;

import android.graphics.Rect;

import android.view.MotionEvent;

import android.view.View;

import android.widget.Toast;

public class TestView extends View

{

private List<Point> myWhiteArray = new ArrayList<Point>();  //白棋子位置信息

private List<Point> myBlackArray = new ArrayList<Point>();  //黑棋子位置信息

int chess[][]=new int[3][3];

private boolean isWhite = true;  //判断是否是白棋先手,或当前为白棋下子

private Bitmap Blackpic  = null;

private Bitmap Whitepic  = null;

private Context  mycontext = null;

public TestView(Context context)

{

super(context);

mycontext=context;

Blackpic=BitmapFactory.decodeResource(getResources(), R.drawable.stone_b1);

Whitepic=BitmapFactory.decodeResource(getResources(), R.drawable.stone_w2);

}

/*重写onDraw()*/

@Override

protected void onDraw(Canvas canvas)

{

/*设置背景为青色*/

canvas.drawColor(Color.CYAN);

Paint paint=new Paint();

/*设置画笔宽度*/

paint.setStrokeWidth(3);

canvas.drawLine(0, 0, 300, 0, paint);

canvas.drawLine(0, 100, 300, 100, paint);

canvas.drawLine(0, 200, 300, 200, paint);

canvas.drawLine(0, 300, 300, 300, paint);

canvas.drawLine(0, 0, 0, 300, paint);

canvas.drawLine(100, 0, 100, 300, paint);

canvas.drawLine(200, 0 , 200, 300, paint);

canvas.drawLine(300, 0 , 300, 300, paint);

paint.setStyle(Paint.Style.FILL);

//canvas.drawCircle(50,50, 50, paint);

drawPiece(canvas);

}

//画棋子

private void drawPiece(Canvas canvas) {

int n1 = myWhiteArray.size();

int n2 = myBlackArray.size();

Paint paint1=new Paint();

/*设置画笔宽度*/

paint1.setStrokeWidth(3);

paint1.setColor(Color.WHITE);

for(int i =0; i< n1 ;i++){

Point whitePoint = myWhiteArray.get(i);

canvas.drawCircle(50+100*(whitePoint.x),50+100*(whitePoint.y), 50, paint1);

//canvas.drawBitmap(Bitmap bitmap, float left, float top, Paint paint);Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置

// canvas.drawBitmap(Whitepic, 100*(whitePoint.x),100*(whitePoint.y),paint1);

}

Paint paint2=new Paint();

/*设置画笔宽度*/

paint2.setStrokeWidth(3);

paint2.setColor(Color.BLACK);

for(int i =0; i< n2 ;i++){

Point blackPoint = myBlackArray.get(i);

canvas.drawCircle(50+100*(blackPoint.x),50+100*(blackPoint.y), 50, paint2);

// 指定图片绘制区域

Rect src2 = new Rect(0,0,Blackpic.getWidth(),Blackpic.getHeight());

Rect dst2 = new Rect(100*(blackPoint.x),100*(blackPoint.y),100*(blackPoint.x)+100,100*(blackPoint.y)+100);

canvas.drawBitmap(Blackpic,src2, dst2, null);

System.out.print(Blackpic.getWidth());

}

}

protected void onDraw2(Canvas canvas)

{

/*设置背景为青色*/

canvas.drawColor(Color.CYAN);

Paint paint=new Paint();

/*设置画笔宽度*/

paint.setStrokeWidth(3);

/*设置画空心图形*/

paint.setStyle(Paint.Style.STROKE);

/*去锯齿*/

paint.setAntiAlias(true);

/*画空心矩形(正方形)*/

canvas.drawRect(10,10,70,70,paint);

/*设置画实心图形*/

paint.setStyle(Paint.Style.FILL);

/*画实心矩形(正方形)*/

canvas.drawRect(100,10,170,70,paint);

/*设置画笔颜色为蓝色*/

paint.setColor(Color.BLUE);

/*画圆心为(100,120),半径为30的实心圆*/

canvas.drawCircle(100,120,30,paint);

/*在上面的实心圆上画一个小白点*/

paint.setColor(Color.WHITE);

canvas.drawCircle(91,111,6,paint);

/*设置画笔颜色为红色*/

paint.setColor(Color.RED);

/*画三角形*/

Path path=new Path();

path.moveTo(100, 170);

path.lineTo(70, 230);

path.lineTo(130,230);

path.close();

canvas.drawPath(path,paint);

/*文字*/

paint.setTextSize(28);

paint.setColor(Color.BLUE);

canvas.drawText(getResources().getString(R.string. hello_world),

30,270,paint);

}

public boolean onTouchEvent(MotionEvent event) {

// TODO Auto-generated method stub

//Touch事件的相关细节(发生触摸的位置、时间等)被封装成MotionEvent对象

int x1,y1;

if (event.getAction() == MotionEvent.ACTION_DOWN)

{

x1 = (int) (event.getX()/100);

y1 = (int) (event.getY()/100);

Toast.makeText(mycontext,"单击了"+x1+","+y1,Toast.LENGTH_SHORT ).show();

//Point p = getVaLidPiont( (int) event.getX(), (int) event.getY());

Point p = new Point(x1,y1);

if (myWhiteArray.contains(p)|| myBlackArray.contains(p)) {

return false;

}

if (isWhite) {

myWhiteArray.add(p);

chess[y1][x1]=1;

}else {

myBlackArray.add(p);

chess[y1][x1]=2;

}

invalidate();         //invalidate()是用来刷新View的,必须在UI线程中使用

isWhite = !isWhite;

if (isGemOver()) {

Toast.makeText(mycontext,"游戏结束",Toast.LENGTH_SHORT ).show();

return false;

}

}

return true;

}

private boolean isGemOver()

{

if(chess[0][0]==chess[0][1] &&chess[0][1]==chess[0][2] &&chess[0][1]!=0)

return true;

if(chess[1][0]==chess[1][1] &&chess[1][1]==chess[0][2] &&chess[1][1]!=0)

return true;

if(chess[2][0]==chess[2][1] &&chess[2][1]==chess[2][2] &&chess[2][1]!=0)

return true;

if(chess[0][0]==chess[1][0] &&chess[1][0]==chess[2][0] &&chess[1][0]!=0)

return true;

if(chess[0][1]==chess[1][1] &&chess[1][1]==chess[2][1] &&chess[1][1]!=0)

return true;

if(chess[0][2]==chess[1][2] &&chess[1][2]==chess[2][2] &&chess[1][2]!=0)

return true;

if(chess[0][0]==chess[1][1] &&chess[1][1]==chess[2][2] &&chess[1][1]!=0)

return true;

if(chess[0][2]==chess[1][1] &&chess[1][1]==chess[2][0] &&chess[1][1]!=0)

return true;

return false;

}

private Point getVaLidPiont(int x , int y){

return new Point((int)(x/100),(int)(y/100));

}

}

手势识别

package com.example.three_chess;

import android.content.Context;

import android.util.AttributeSet;

import android.view.GestureDetector;

import android.view.MotionEvent;

import android.view.View;

import android.widget.Toast;

class CwjView extends View {

private GestureDetector mGD;              //手势识别器

int MAJOR_MOVE=20;

Context mycontext;

public CwjView(Context context, AttributeSet attrs) {

super(context, attrs);

mycontext=context;

mGD = new GestureDetector(context, new GestureDetector. OnGestureListener() {

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

int dx = (int) (e2.getX() - e1.getX()); //计算滑动的距离

if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) { //必须大于MAJOR_MOVE的动作才识别

if (velocityX > 0) {

//向右边

Toast.makeText(mycontext,"向右边",Toast.LENGTH_SHORT ).show();

} else {

//向左边

Toast.makeText(mycontext,"向左边",Toast.LENGTH_SHORT ).show();

}

return true;

} else {

return false;  //当然可以处理velocityY处理向上和向下的动作

}

}

public boolean onDown(MotionEvent e) {

// TODO Auto-generated method stub

return false;

}

public void onLongPress(MotionEvent e) {

// TODO Auto-generated method stub

}

public boolean onScroll(MotionEvent e1, MotionEvent e2,

float distanceX, float distanceY) {

// TODO Auto-generated method stub

return false;

}

public void onShowPress(MotionEvent e) {

// TODO Auto-generated method stub

}

public boolean onSingleTapUp(MotionEvent e) {

// TODO Auto-generated method stub

return false;

}

});

}

@Override

public boolean onTouchEvent(MotionEvent event) {

mGD.onTouchEvent(event);

return true;

}

}

Android井字棋相关推荐

  1. LeetCode简单题之找出井字棋的获胜者

    题目 A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: 玩家轮流将棋子放在空方格 (" ") 上. 第一个玩家 A 总是用 "X" 作 ...

  2. python井字棋_用Python做一个井字棋小游戏

    井字棋是一个经典的小游戏,在九宫格上玩家轮流画OXO,当每列或每行或是两个对角成一线时便是获胜. 今天就用Python编写一个井字棋小游戏,与电脑对战. 程序执行画面如下图所示: 程序提供了两种人工智 ...

  3. 组合游戏系列5: 井字棋、五子棋AlphaGo Zero 算法实战

    来源 | MyEncyclopedia 上一篇我们从原理层面解析了AlphaGo Zero如何改进MCTS算法,通过不断自我对弈,最终实现从零棋力开始训练直至能够打败任何高手.在本篇中,我们在已有的N ...

  4. python小游戏系列井字棋,儿时的回忆

    hello大家好,今天我又发现了个有趣的小玩意.我是专写有趣小玩意的老诗. 相信大家对于井字棋都并不陌生.现在也能找到各种各样的井字棋小游戏玩.那么你们自己是否会编写呢?接下来老诗用python教大家 ...

  5. php井字游戏,python实现井字棋游戏

    #本游戏python3.4.0下编写调试,只能在windows下运行. import random import subprocess import time #定义函数 def draw_board ...

  6. Minimax 和 Alpha-beta 剪枝算法简介,及以此实现的井字棋游戏(Tic-tac-toe)

    前段时间用 React 写了个2048 游戏来练练手,准备用来回顾下 React 相关的各种技术,以及试验一下新技术.在写这个2048的过程中,我考虑是否可以在其中加入一个 AI 算法来自动进行游戏, ...

  7. C++井字棋游戏,DOS界面版

    据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /*N字棋游戏PVP版,DOS版本 ...

  8. 用TensorFlow基于神经网络实现井字棋(含代码)

    为了展示如何应用神经网络算法模型,我们将使用神经网络来学习优化井字棋(Tic Tac Toe).明确井字棋是一种决策性游戏,并且走棋步骤优化是确定的. 开始 为了训练神经网络模型,我们有一系列优化的不 ...

  9. [C++] 井字棋游戏源码

    TicTac.h 1 #define EX 1 //该点左鼠标 2 #define OH 2 //该点右鼠标 3 4 class CMyApp : public CWinApp 5 { 6 publi ...

最新文章

  1. 应该知道的Linux技巧
  2. 使用Elasticsearch+filebeat+logstach+kibana构建日志服务平台
  3. nginx+tomcat+memcached负载均衡
  4. Mysql编码教程_mysql编码设置教程 mysql编码要怎么设置呢
  5. mips汇编计算开方_东南大学计算机系统综合设计MOOC第5期开课
  6. jQuery操作Table学习总结(转)
  7. fullcalendar自定义搜索框_高效搜索任意文件,拯救凌乱的电脑桌面!
  8. c语言图书管理信息系统源代码,C语言 图书信息管理系统 最终源代码
  9. 单片机ISP烧录原理
  10. 南大通用极速内存数据库
  11. Android 设备接入扫码枪,Android 设备接入扫码枪
  12. 数学基础知识(2) 梯度和方向向量
  13. Python爬虫—爬取京东商品信息(自动登录,换关键词,换页)
  14. linux格式化命令,Linux怎么格式化磁盘啊?
  15. 互联网诞生记: 浪成于微澜之间
  16. php制作cms视频教程下载,phpcms下载频道的模板制作
  17. RabbitMQ Management:Management API returned status code 500
  18. Image Super-Resolution via Iterative Refinement 论文解读和感想
  19. python爬取其他人微信朋友圈_python爬虫24 | 搞事情了,用 Appium 爬取你的微信朋友圈...
  20. UVALive - 2911 Maximum

热门文章

  1. 【JZOJ 省选模拟】6655.三国学者
  2. 详解js中的闭包(closure)以及闭包的使用
  3. CodeMix使用的语言和框架:PHP
  4. python中def函数的使用
  5. 华为荣耀的激活锁是什么如何解开呢卡在激活设备手机无法进系统学会几种有用的方法
  6. 从零开始系列(四):一文看懂arm架构和x86架构有什么区别
  7. java高德地图Api根据城市名称查该城市所有区域
  8. 用photoshop制作简单的作品展示页面
  9. 数据可视化原理与实例
  10. HTML中的点缀--伪类