下午看了慕课网上的五子棋游戏教学,编写代码如下:
layout:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.jowang.wuziqi.MainActivity"> <com.example.jowang.wuziqi.wuzi android:id="@+id/wuziqi" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true"/> </RelativeLayout>五子棋代码:
public class wuzi extends View { private int panelwidth; private float lineheight; private int MAX_LINE=10; private Paint paint=new Paint(); private Bitmap white; private Bitmap black; private float ratio=3*1.0f/4; private boolean iswhite=true; private ArrayList<Point> whitearray=new ArrayList<>(); private ArrayList<Point> blackarray=new ArrayList<>(); private boolean gameover; private boolean whitewinner; private int MAXCOUNTLINE=5; public wuzi(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint.setColor(0x88000000); paint.setAntiAlias(true); paint.setDither(true); paint.setStyle(Paint.Style.STROKE); white=BitmapFactory.decodeResource(getResources(),R.drawable.qi1); black=BitmapFactory.decodeResource(getResources(),R.drawable.qi2); } @Override protected v