android人物移动设计软件,实现在SurfaceView上移动游戏人物
首先给大家介绍一个RPG素材网站这几天一直在捣鼓这个有很多游戏人物图片可以用来实现人物动画游戏画面如下图 代码如下只实现了移动人物,对碰撞之类的还没有实现。package com.example.gamethree;import android.view.KeyEvent;import android.view.SurfaceView;import android.view.SurfaceHold
首先给大家介绍一个RPG素材网站
这几天一直在捣鼓这个
有很多游戏人物图片
可以用来实现人物动画
游戏画面如下图
代码如下
只实现了移动人物,对碰撞之类的还没有实现。
package com.example.gamethree;
import android.view.KeyEvent;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.View;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Color;
public class EightOne extends SurfaceView implements SurfaceHolder.Callback,Runnable{
private Canvas canvas;
private Bitmap background;
private Thread thread;
private SurfaceHolder holder;
public int l_id=0;
public int r_id=0;
public int u_id=0;
public int d_id=0;
//人物位置坐标
int curX=0;
int curY=0;
int Code;
//获取人物朝右的图片ID
int[] r_pic_id={R.drawable.right_a,
R.drawable.right_b,
R.drawable.right_a,
R.drawable.right_d
};
//获取人物朝左的图片ID
int[] l_pic_id={R.drawable.left_a,
R.drawable.left_b,
R.drawable.left_c,
R.drawable.left_d,
};
//获取人物朝上的图片ID
int[] u_pic_id={R.drawable.up_a,
R.drawable.up_b,
R.drawable.up_a,
R.drawable.up_d
};
//获取人物朝下的图片ID
int[] d_pic_id={R.drawable.down_a,
R.drawable.down_b,
R.drawable.down_c,
R.drawable.down_d,
};
//获取对应图片ID的Bitmap数组
Bitmap[] r_pic=new Bitmap[4];
Bitmap[] l_pic=new Bitmap[4];
Bitmap[] u_pic=new Bitmap[4];
Bitmap[] d_pic=new Bitmap[4];
public EightOne(Context content)
{
super(content);
holder=getHolder();
holder.addCallback(this);
background=BitmapFactory.decodeResource(getResources(), R.drawable.background1);
//加载图片资源
maps();
thread=new Thread(this);
//这个一定要有,否则无法监听到键盘事件
setFocusable(true);
setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v,int keyCode,KeyEvent event)
{
Code=keyCode;
if(event.getAction()==KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_DPAD_RIGHT:
if(r_id>2)
r_id=0;
r_id++;
curX+=4;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
if(l_id>2)
l_id=0;
l_id++;
curX-=4;
break;
case KeyEvent.KEYCODE_DPAD_UP:
if(u_id>2)
u_id=0;
u_id++;
curY-=4;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if(d_id>2)
d_id=0;
d_id++;
curY+=4;
break;
}
}
else
{
l_id=0;
r_id=0;
u_id=0;
d_id=2;
}
return true;
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height)
{
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
//启动一条线程
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
//分别加载人物在左、右、上、下四个方面的资源
public void maps(){
for(int i=0;i
{
r_pic[i]=BitmapFactory.decodeResource(getResources(), r_pic_id[i]);
l_pic[i]=BitmapFactory.decodeResource(getResources(), l_pic_id[i]);
u_pic[i]=BitmapFactory.decodeResource(getResources(), u_pic_id[i]);
d_pic[i]=BitmapFactory.decodeResource(getResources(), d_pic_id[i]);
}
}
@Override
public void run()
{
while(true)
{
try{
synchronized(holder)
{
//调用自定义的绘画方法
canvas=holder.lockCanvas();
myDraw();
holder.unlockCanvasAndPost(canvas);
}
}
catch(Exception e)
{
}
try{
Thread.sleep(100);
}
catch(Exception e)
{
}
}
}
//自定义一个绘画方法
public void myDraw(){
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(background,0,0,null);
if(Code==KeyEvent.KEYCODE_DPAD_RIGHT)
canvas.drawBitmap(r_pic[r_id],curX,curY,null);
else if(Code==KeyEvent.KEYCODE_DPAD_LEFT)
canvas.drawBitmap(l_pic[l_id],curX,curY,null);
else if(Code==KeyEvent.KEYCODE_DPAD_UP)
canvas.drawBitmap(u_pic[u_id],curX,curY,null);
else
canvas.drawBitmap(d_pic[d_id],curX,curY,null);
}
}
更多推荐
所有评论(0)