0% found this document useful (0 votes)
22 views3 pages

Snake

The document describes a snake game project including defining functions for initializing walls, drawing the screen, drawing the snake, generating apples, and detecting user input. It includes global variables to track game state like the screen space, score, snake and apple positions. Structs are used to represent points on the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Snake

The document describes a snake game project including defining functions for initializing walls, drawing the screen, drawing the snake, generating apples, and detecting user input. It includes global variables to track game state like the screen space, score, snake and apple positions. Structs are used to represent points on the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/* Project: Snake */

/* Author : Lochh2@fpt.com */
/*-------------------------*/

/* Include library */
#include <iostream>
#include <vector>
#include <conio.h>
#include <windows.h>
#include <cstring>
#include <time.h>
#include <random>

/* Declare function */
void init_wall(void);
void draw_screen(void);
void draw_snake(void);
void apple();
void interf();
void interf_start();
void select();
void gameover();
void start_game();
void gotoxy(int x, int y);
bool isAte();
bool dead();
int random(void);

/* Select Director */
enum Direction
{
UP,
DOWN,
LEFT,
RIGHT
};
Direction g_direction = Direction::RIGHT; /* Defaut direction is RIGHT */

struct point
{
int x;
int y;
};
/* Global Variable */
char g_screen[20][41] = {' '}; /* Charactor will be print in the screen */
float g_level = 6.0; /* Number to calculate delay time */
int g_score = 0; /* Score */
point g_apple{ 0,0 }; /* Storage random apple */
std::vector<point> g_snake = /* Init snake at start game */
{
point{8,10},
point{8,9},
point{8,8},
point{8,7},
};

void gotoxy(int f_x, int f_y)


{
COORD coord;
coord.X = f_x;
coord.Y = f_y;
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE),
coord
);
}

void init_wall(void) /* Creat Wall */


{
for (int i = 0; i <= 19; ++i)
{
for (int j = 0; j <=39; ++j)
{
if ((i == 0) || (i == 19))
{
g_screen[i][j] = '#'; /* Creat Row on Row 0 and Row
19 */
}
else
{
if ((j == 0) || (j == 39))
{
g_screen[i][j] = '!'; /* Creat Column on Column 0 and Column
39 */
}
else
{
g_screen[i][j] = ' '; /* Creat blank
space */
}
}
}
g_screen[i][40] = '\n'; /* Creat endline at the end
column */
}
}
void draw_screen(void) /* Put Wall and Snake into string
f_view and print f_view */
{
std::string f_view; /* Storage Wall and Snake, Prapare to
print */
init_wall(); /* Call function init_wall and
draw_snake */
draw_snake();
g_screen[g_apple.x][g_apple.y] = '@';
f_view.append(&g_screen[0][0]); /* Convert the var screen into
f_view */
std::cout << f_view ; /* Print
f_view */

}
void start_game()
{
apple(); /* Creat random
Apple */
while (true)
{
if (_kbhit()) /* Key press
detection */
{
char f_ch = _getch(); /* Read and assignment to
g_direction */
f_ch = tolower(f_ch);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy