1. Look at video
2. ??????????
3. Profit
Oh btw its pretty ghetto, dont blame my coding style :smiley_303:
void fill( int x, int y, int w, int h, int r, int g, int b, int a ) { m_pMatSurface->DrawSetColor( r, g, b, a ); m_pMatSurface->DrawFilledRect( x, y, x + w, y + h ); } bool CheckForHit( int hitx, int hity ); bool bIsVisible = false; bool bIsActive = false; bool bReset = false; #define MAX_X 10 #define MAX_Y 10 #define MIN_X 0 // just for lulz #define MIN_Y 0 int snake_x = 300; int snake_y = 200; int score; //int MoveType; #define MOVE_RIGHT 1 #define MOVE_UP 2 #define MOVE_DOWN 3 #define MOVE_LEFT 4 unsigned int timer = 0; struct BodyPart { int x, y; int MoveType; int iUpdate; }; #define MAX_BODY_PARTS (MAX_X * MAX_Y) BodyPart Parts[MAX_BODY_PARTS]; BodyPart Head; int food_x = 0, food_y = 0; int current_body_parts; bool bInited = false; void ResetFood() { srand( (unsigned int)GetTickCount() ); food_x = rand() % MAX_X; food_y = rand() % MAX_Y; if( CheckForHit( food_x, food_y ) ) // this could lead to some problems ResetFood(); } void Reset() { bIsActive = false; current_body_parts = 5; Head.x = current_body_parts; Head.y = 0; Head.MoveType = MOVE_RIGHT; for( int i = 0; i < MAX_BODY_PARTS; i++ ) { Parts[i].x = current_body_parts - i - 1; // :> Parts[i].y = 0; Parts[i].MoveType = MOVE_RIGHT; } ResetFood(); score = 0; } bool CheckRange( BodyPart Part, int x, int y ) { for( int j = -1; j <= 1; j++ ) for( int i = -1; i <= 1; i++ ) if( Part.x == x + j && Part.y == y + i ) return true; } bool CheckForHit( int hitx, int hity ) { for( int i = 0; i < current_body_parts; i++ ) { if( Parts[i].x == hitx && Parts[i].y == hity ) return true; } return false; } void NewPart() { int oldx = Parts[current_body_parts-1].x; int oldy = Parts[current_body_parts-1].y; if( Parts[current_body_parts-1].MoveType == MOVE_RIGHT ) --oldx; else if( Parts[current_body_parts-1].MoveType == MOVE_LEFT ) ++oldx; else if( Parts[current_body_parts-1].MoveType == MOVE_UP ) ++oldy; else if( Parts[current_body_parts-1].MoveType == MOVE_DOWN ) --oldy; Parts[current_body_parts].x = oldx; Parts[current_body_parts].y = oldy; Parts[current_body_parts].MoveType = Parts[current_body_parts-1].MoveType; current_body_parts++; } void Snake() { if( GetAsyncKeyState( VK_INSERT ) & 1 ) bIsVisible = !bIsVisible; if( GetAsyncKeyState( VK_END ) & 1 ) bIsActive = !bIsActive; if( bIsVisible == false ) return; if( GetAsyncKeyState( VK_DELETE ) & 1 || bReset ) if( bReset ) { bReset = false; Reset(); } else Reset(); if( !bInited ) { Reset(); bInited = true; } stringstream Score; Score << "Your Score: " << score; gUtils.DrawString( gUtils.fBig, Score.str(), snake_x, snake_y - 20, false, 255, 255, 255 ); fill( snake_x, snake_y, (MAX_X + 1) * 20, (MAX_Y + 1) * 20, 255, 255, 255, 200 ); int y = 0, x = 0; for( int i = 0; i < MAX_X+1; i++ ) { fill( snake_x, snake_y+y, (MAX_X + 1) * 20, 1, 0, 0, 0, 255 ); y += 20; } for( int i = 0; i < MAX_Y+1; i++ ) { fill( snake_x+x, snake_y, 1, (MAX_Y + 1) * 20, 0, 0, 0, 255 ); x += 20; } fill( snake_x + Head.x * 20, snake_y + Head.y * 20, 20, 20, 255, 0, 0, 255 ); for( int i = 0; i < current_body_parts; i++ ) { int x = Parts[i].x; int y = Parts[i].y; fill( snake_x + x * 20, snake_y + y * 20, 20, 20, 0, 255, 0, 255 ); } fill( snake_x + food_x * 20, snake_y + food_y * 20, 20, 20, 0, 0, 255, 255 ); if( bIsActive == false ) return; const unsigned long TickCount = GetTickCount(); if( GetAsyncKeyState( VK_UP ) && Head.y - 1 != Parts[0].y ) Head.MoveType = MOVE_UP; if( GetAsyncKeyState( VK_LEFT ) && Head.x - 1 != Parts[0].x ) Head.MoveType = MOVE_LEFT; if( GetAsyncKeyState( VK_RIGHT ) && Head.x + 1 != Parts[0].x ) Head.MoveType = MOVE_RIGHT; if( GetAsyncKeyState( VK_DOWN ) && Head.y + 1 != Parts[0].y ) Head.MoveType = MOVE_DOWN; if( timer + 150 < TickCount ) { switch( Head.MoveType ) { case MOVE_RIGHT: Head.x++; if( Head.x > MAX_X ) bReset = true; break; case MOVE_LEFT: Head.x--; if( Head.x < MIN_X ) bReset = true; break; case MOVE_DOWN: Head.y++; if( Head.y > MAX_Y ) bReset = true; break; case MOVE_UP: Head.y--; if( Head.y < MIN_Y ) bReset = true; break; } if( CheckForHit( Head.x, Head.y ) ) Reset(); if( Head.x == food_x && Head.y == food_y ) { score += 20; // MOST IMPORTANT STUFF BOIII ResetFood(); NewPart(); } for( int i = 0; i < current_body_parts; i++ ) { if( Parts[i].iUpdate > 0 ) { Parts[i].MoveType = Parts[i].iUpdate; Parts[i].iUpdate = 0; } switch( Parts[i].MoveType ) { case MOVE_RIGHT: Parts[i].x++; if( i == 0 ) { if( Head.y > Parts[0].y ) // Head has moved down Parts[0].iUpdate = MOVE_DOWN; if( Head.y < Parts[0].y ) // Head has moved up Parts[0].iUpdate = MOVE_UP; } else { if( Parts[i-1].y > Parts[i].y ) Parts[i].iUpdate = MOVE_DOWN; if( Parts[i-1].y < Parts[i].y ) Parts[i].iUpdate = MOVE_UP; } break; case MOVE_LEFT: Parts[i].x--; if( i == 0 ) { if( Head.y > Parts[0].y ) // Head has moved down Parts[0].iUpdate = MOVE_DOWN; if( Head.y < Parts[0].y ) // Head has moved up Parts[0].iUpdate = MOVE_UP; } else { if( Parts[i-1].y > Parts[i].y ) Parts[i].iUpdate = MOVE_DOWN; if( Parts[i-1].y < Parts[i].y ) Parts[i].iUpdate = MOVE_UP; } break; case MOVE_DOWN: Parts[i].y++; if( i == 0 ) { if( Head.x > Parts[0].x ) // Head has moved right Parts[0].iUpdate = MOVE_RIGHT; if( Head.x < Parts[0].x ) // Head has moved left Parts[0].iUpdate = MOVE_LEFT; } else { if( Parts[i-1].x > Parts[i].x ) Parts[i].iUpdate = MOVE_RIGHT; if( Parts[i-1].x < Parts[i].x ) Parts[i].iUpdate = MOVE_LEFT; } break; case MOVE_UP: Parts[i].y--; if( i == 0 ) { if( Head.x > Parts[0].x ) // Head has moved right Parts[0].iUpdate = MOVE_RIGHT; if( Head.x < Parts[0].x ) // Head has moved left Parts[0].iUpdate = MOVE_LEFT; } else { if( Parts[i-1].x > Parts[i].x ) Parts[i].iUpdate = MOVE_RIGHT; if( Parts[i-1].x < Parts[i].x ) Parts[i].iUpdate = MOVE_LEFT; } break; } } timer = TickCount; } }
Comment