Qbasic Snake Game Code
Qbasic Snake Game Code
Class Methods
Methods are functions that belongs to the class.
bool gameover;
int nTail;
eDirecton dir;
void Setup() {
gameover = false;
dir = STOP;
x = width / 2;
y = height / 2;
void Draw() {
system("cls");
if (j == 0)
if (i == y && j == x)
else {
if (j == width -1)
void Input ()
{
if (_kbhit ()) {
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN ;
break;
case 'x':
gameover = true;
break;
void algorithm()
{
tailX[0] = x;
tailY[0] = y;
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y ;
switch (dir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
score +=10;
nTail ++;
int main()
{
Setup();
while (!gameover) {
Draw ();
Input ();
algorithm ();
return 0;
}
// required header file
#include <conio.h>
#include <iostream>
#include <windows.h>
using namespace std;
switch (sDir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
}
GameInit();
while (!isGameOver) {
GameRender(playerName);
UserInput();
UpdateGame();
// creating a delay for according to the chosen
// difficulty
Sleep(dfc);
}
return 0;
}
// required header file
#include <conio.h>
#include <iostream>
#include <windows.h>
using namespace std;
switch (sDir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
}
GameInit();
while (!isGameOver) {
GameRender(playerName);
UserInput();
UpdateGame();
// creating a delay for according to the chosen
// difficulty
Sleep(dfc);
}
return 0;
}