Reedbeta, on 20 February 2012 - 05:13 PM, said:
The application is probably double-buffered, and since you're only drawing your snake once, it only gets into one of the two buffers, causing the flickering. I assume you'll eventually want your snake to be dynamic anyway, so best to just go ahead and draw it each frame.
It needs to be dynamic indeed, but the purpose of this tutorial is to minimalize the amount of drawing. What I wanted to do is as follows:
-draw the whole snake at the start
-draw the head every step
-draw a black box at the tail every step( so it isn't visible anymore )
I don't exactly know what that 'double-buffered'-thing is, but it causes a problem with the drawing. Because of its flickering, every 2 steps one tail won't
be drawn black and the head isn't drawn. This is what I see:
The right part is moving, but the left part is staying where it is and it's flickering. The code I use is this:
// Template, major revision 3
// IGAD/NHTV - Jacco Bikker - 2006-2009
#include "string.h"
#include "surface.h"
#include "stdlib.h"
#include "template.h"
#include "game.h"
using namespace Tmpl8;
int x[8] = { 5, 6, 7, 8, 9, 10, 11, 12 };
int y[8] = { 8, 8, 8, 8, 8, 8, 8, 8 };
int heading = 0, tail = 0;
void Game::Init()
{
for ( int i = 0; i < 8; i++ )
{
m_Screen->Box( x[i] * 8, y[i] * 8, x[i] * 8 + 6, y[i] * 8 + 6, 0xffffff );
start = false;
}
}
void Game::Tick( float a_DT )
{
m_Screen->Box( x[tail] * 8, y[tail] * 8, x[tail] * 8 + 6, y[tail] * 8 + 6, 0x000000 );
x[tail] = x[7];
y[tail] = y[7];
tail = ( tail + 1 ) % 7;
if ( heading == 0 ) x[7]++;
if ( heading == 1 ) y[7]++;
if ( heading == 2 ) x[7]--;
if ( heading == 3 ) y[7]--;
m_Screen->Box( x[7] * 8, y[7] * 8, x[7] * 8 + 6, y[7] * 8 + 6, 0xffffff );
if ( GetAsyncKeyState( VK_LEFT ) ) heading = ( heading + 3 ) % 4;
if ( GetAsyncKeyState( VK_RIGHT ) ) heading = ( heading + 1 ) % 4;
Sleep( 100 );
}
Rimevan