Notes
Notes - notes.io |
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define WIDTH 21
#define HEIGHT 10
#define INITIAL_LIVES 3
#define MAX_SNAKE_LENGTH 100
#define MAX_LEVELS 3
char levels[MAX_LEVELS][HEIGHT][WIDTH + 1] = {
{
"#####################",
"# #",
"# #",
"# G G #",
"# #",
"# #",
"# #",
"# #",
"# #",
"#####################"
},
{
"#####################",
"#P G #",
"# #### # #",
"# #### # #",
"# ### ##### ### # #",
"# # # # # #",
"# # # # # # # # #",
"# # # #",
"# ##### #### #",
"#####################"
},
{
"#####################",
"# X X #",
"# ##### ##### ### #",
"# #",
"# ### X ### X ### #",
"# #",
"# ### #### ### #",
"# X #",
"# #",
"#####################"
}
};
char map[HEIGHT][WIDTH + 1];
int currentLevel = 0;
int playerX, playerY;
int snakeLength = 5;
int score = 0;
int highScore = 0;
int lives = INITIAL_LIVES;
int speedDelay = 150;
typedef struct {
int x, y;
} Point;
Point snake[MAX_SNAKE_LENGTH];
Point food;
char direction = 'd';
void placeFood() {
int fx, fy;
do {
fx = rand() % HEIGHT;
fy = rand() % WIDTH;
} while (map[fx][fy] != ' ');
food.x = fx;
food.y = fy;
map[food.x][food.y] = '.';
}
void loadGame() {
int i, j;
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
char ch = levels[currentLevel][i][j];
map[i][j] = (ch == '.' || ch == 'P' || ch == 'G') ? ' ' : ch;
}
}
for (i = 1; i < HEIGHT - 1; i++) {
for (j = 1; j < WIDTH - 1; j++) {
if (map[i][j] == ' ') {
playerX = i;
playerY = j;
goto found_pos;
}
}
}
found_pos:
snakeLength = 5;
for (int k = 0; k < snakeLength; k++) {
snake[k].x = playerX;
snake[k].y = playerY - k;
map[playerX][playerY - k] = 'S';
}
direction = 'd';
placeFood();
}
void drawMap() {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
printf("%c", map[i][j]);
}
printf("n");
}
printf("Score: %d | High Score: %d | Lives: %d | Level: %dn", score, highScore, lives, currentLevel + 1);
printf("Use WASD to control the snake.n");
}
void moveSnake() {
int nx = snake[0].x;
int ny = snake[0].y;
if (direction == 'w') nx--;
if (direction == 's') nx++;
if (direction == 'a') ny--;
if (direction == 'd') ny++;
if (map[nx][ny] == '#' || map[nx][ny] == 'X') {
lives--;
if (lives <= 0) {
printf("Game Over! You hit a wall or obstacle.nHigh Score: %dn", highScore);
exit(0);
}
printf("Crash! Lives left: %dn", lives);
_getch();
loadGame();
return;
}
if (map[nx][ny] == 'S') {
lives--;
if (lives <= 0) {
printf("Game Over! You ran into yourself.nHigh Score: %dn", highScore);
exit(0);
}
printf("Crashed into yourself! Lives left: %dn", lives);
_getch();
loadGame();
return;
}
Point prev = snake[0], temp;
for (int i = 1; i < snakeLength; i++) {
temp = snake[i];
snake[i] = prev;
prev = temp;
}
map[prev.x][prev.y] = ' ';
snake[0].x = nx;
snake[0].y = ny;
if (map[nx][ny] == '.') {
score++;
if (score > highScore) highScore = score;
if (snakeLength < MAX_SNAKE_LENGTH) snakeLength++;
placeFood();
if (score == 10 && currentLevel == 0) {
currentLevel = 1;
speedDelay = 100;
printf("Level 2 unlocked! Faster now.n");
_getch();
loadGame();
return;
}
if (score == 20 && currentLevel == 1) {
currentLevel = 2;
speedDelay = 75;
printf("Level 3 unlocked! Beware of X!n");
_getch();
loadGame();
return;
}
if (score == 30 && currentLevel == 2) {
printf("n🎉 YOU WIN! Final Score: %d 🎉n", score);
printf("High Score: %dn", highScore);
printf("Press any key to exit...n");
_getch();
exit(0);
}
}
map[nx][ny] = 'S';
}
int main() {
char input;
srand(time(0));
loadGame();
while (1) {
drawMap();
if (_kbhit()) {
input = _getch();
if ((input == 'w' && direction != 's') ||
(input == 's' && direction != 'w') ||
(input == 'a' && direction != 'd') ||
(input == 'd' && direction != 'a')) {
direction = input;
}
}
moveSnake();
Sleep(speedDelay);
}
return 0;
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
