Hi guys,
I have started making the classic game Snake, currently got the bog standard version of the game working using the 2d graphics library.
The problem i'm having is that the game gets very laggy as the snake begins to grow. to the point where it is unplayable.
My code is based on the work of Danuubz
anddev.org • View topic - 2D tutorial
This is the code I am using.
It currently draws a Green Square for A snake body part and a grey square for a powerup (food).
I have done this game before on C# and I had the same problem until I started using Offscreen Bitmaps like here. But it isnt working here.
Is their a better way of doing this, as I am hitting some serious Lag.
Cheers
I have started making the classic game Snake, currently got the bog standard version of the game working using the 2d graphics library.
The problem i'm having is that the game gets very laggy as the snake begins to grow. to the point where it is unplayable.
My code is based on the work of Danuubz
anddev.org • View topic - 2D tutorial
Code:
public class gamescreen extends Activity
{
public int left =0, right =0, up =0, down =0;
private Game g;
private Panel main;
private Bitmap bmap;
private Canvas offscreen;
public boolean start = true;
private volatile boolean running = true;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setOffscreenBitmap();
requestWindowFeature(Window.FEATURE_NO_TITLE);
main = new Panel(this);
setContentView(main,new ViewGroup.LayoutParams(320,480));
g = new Game();
(new Thread(new AnimationLoop())).start();
}
private void setOffscreenBitmap()
{
bmap = Bitmap.createBitmap(30,30,Bitmap.Config.ARGB_8888);
offscreen = new Canvas();
offscreen.setBitmap(bmap);
offscreen.drawColor(Color.RED);
}
private synchronized void updatePhysics()
{
if(left==1)
{
g.s.grow(0);
}
else if(right ==1)
{
g.s.grow(1);
left =0;
down =0;
up =0;
}
else if(up == 1)
{
g.s.grow(2);
left =0;
right =0;
down =0;
}
else if(down == 1)
{
g.s.grow(3);
up =0;
left =0;
right =0;
}
}
private synchronized void doDraw(Canvas canvas, Paint paint)
{
if(start)
{
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmap,10,10,paint);
start = false;
}
else
{
g.start();
canvas.restore();
Paint p = new Paint();
p.setColor(Color.rgb(0, 255, 0));
p.setShadowLayer(5, 10, 10, Color.rgb(0, 200,0));
Paint p1 = new Paint();
p1.setColor(Color.GRAY);
for(int i =0; i <32; i++)
{
for(int j =0; j <44;j++)
{
if(g.b[i][j].p instanceof Food)
{
canvas.drawRect(i*10,(j*10)+40,(i*10)+10,(j*10)+50,p1);
}
Segment seg;
int size = g.s.l.size();
for(int a =0; a < size; a++)
{
seg=g.s.l.get(a);
if(seg.getx()==i && seg.gety() ==j)
{
canvas.drawRect(i*10,(j*10)+40,(i*10)+10,(j*10)+50,p);
break;
}
}
}
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
if(running)
{
running = false;
}
else
{
running = true;
}
}
else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
{
left=1;
right =0;
up =0;
down =0;
}
else if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
{
left=0;
right =0;
up =1;
down =0;
}
else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
{
left=0;
right =1;
up =0;
down =0;
}
else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
{
left=0;
right =0;
up =0;
down =1;
}
return true;
}
class Panel extends View
{
Paint paint;
public Panel(Context context)
{
super(context);
paint = new Paint();
paint.setColor(Color.WHITE);
}
@Override
protected void onDraw(Canvas canvas)
{
doDraw(canvas,paint);
}
}
class AnimationLoop implements Runnable
{
public void run()
{
while(true)
{
while(running)
{
try
{
Thread.sleep(90);
}
catch(InterruptedException ex) {}
updatePhysics();
main.postInvalidate();
}
}
}
}
}
This is the code I am using.
It currently draws a Green Square for A snake body part and a grey square for a powerup (food).
I have done this game before on C# and I had the same problem until I started using Offscreen Bitmaps like here. But it isnt working here.
Is their a better way of doing this, as I am hitting some serious Lag.
Cheers