#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <graphics.h>
#include <process.h>
#include <dos.h>
void create(char);
void check();
void out();
char prevKey = 'd', pre = 'd', getKey = 'a';
struct node {
char ch;
int x1, y1, len, flag;
struct node *next;
};
struct node *start, *t, *temp, *temp2;
int flagOut, foodx, foody, max = 100, score = 0;
void main() {
int gd = DETECT, gm = 0, i,j;
flagOut = 0;
foodx = rand() % 620;
foody = rand() % 460;
initgraph(&gd, &gm, "c:turboc3bgi"); //according to your compiler
change the url.
start = (struct node *) malloc(sizeof (struct node));
start->next = NULL;
start->x1 = 150;
start->y1 = 200;
start->ch = 'd';
start->len = max;
start->flag = 1;
while (1) {
clrscr();
if (kbhit()) {
getKey = getch();
if (prevKey != getKey && getKey == 'w' || getKey == 'a' || getKey == 's'
|| getKey == 'd') {
if (prevKey == 'w' && getKey != 's' || prevKey == 's' && getKey != 'w'
|| prevKey == 'a' && getKey != 'd' || prevKey == 'd' && getKey != 'a') {
create(getKey);
prevKey = getKey;
}
}
}
for(i=-1;i<=1;i++)
{
for(j=-1;j<=1;j++)
{
putpixel(foodx+i, foody+j, 4);
}
}
check();
out();
t = start;
do {
if (t->ch == 'd') {
setcolor(4);
line(t->x1, t->y1, t->x1 - t->len, t->y1);
if (t->flag)
t->x1++;
if (t->x1 >= 640)
flagOut = 1;
}
if (t->ch == 'w') {
setcolor(4);
line(t->x1, t->y1, t->x1, t->y1 + t->len);
if (t->flag)
t->y1--;
if (t->y1 <= 0)
flagOut = 1;
}
if (t->ch == 'a') {
setcolor(4);
line(t->x1, t->y1, t->x1 + t->len, t->y1);
if (t->flag)
t->x1--;
if (t->x1 <= 0)
flagOut = 1;
}
if (t->ch == 's') {
setcolor(4);
line(t->x1, t->y1, t->x1, t->y1 - t->len);
if (t->flag)
t->y1++;
if (t->y1 >= 480)
flagOut = 1;
}
if (flagOut == 1) {
printf("Game OvernScore=%d", score);
delay(2000);
exit(1);
}
t = t->next;
} while (t);
delay(20);
}//while close
}
void create(char v) {
t = temp = start;
while (t) {
t->flag = 0;
temp = t;
t = t->next;
}
temp2 = (struct node *) malloc(sizeof (struct node));
temp->next = temp2;
temp2->next = NULL;
temp2->flag = 1;
temp2->x1 = temp->x1;
temp2->y1 = temp->y1;
temp2->ch = v;
temp2->len = -1;
}
void check() {
t = start;
if (t->next != NULL)
t->len--;
if (t->len <= 0) {
start = t->next;
free(t);
}
t = start;
do {
temp = t;
t = t->next;
} while (t);
if (temp->len < max)
temp->len++;
if (temp->x1 > foodx - 2 && temp->x1 < foodx + 2 && temp->y1 > foody - 2 &&
temp->y1 < foody + 2) {
max = max + max / 10;
foodx = rand() % 620;
foody = rand() % 460;
score = score + score / 10 + 10;
}
}
void out() {
t = temp2 = start;
while (t) {
temp = temp2;
temp2 = t;
t = t->next;
}
t = start;
while (t->next) {
if (temp2->ch == 'w' && t->ch != 's' && t->ch != 'w' || temp2->ch == 'a'
&& t->ch != 'd' && t->ch != 'a' || temp2->ch == 's' && t->ch != 'w' && t->ch !=
's' || temp2->ch == 'd' && t->ch != 'a' && t->ch != 'd') {
if (temp2->x1 == t->x1) {
if (t->ch == 'w') {
if (temp2->y1 >= t->y1 && temp2->y1 <= (t->y1 + t->len))
flagOut = 1;
}
if (t->ch == 's') {
if (temp2->y1 <= t->y1 && temp2->y1 >= (t->y1 - t->len))
flagOut = 1;
}
}
if (temp2->y1 == t->y1) {
if (t->ch == 'a') {
if (temp2->x1 >= t->x1 && temp2->x1 <= (t->x1 + t->len))
flagOut = 1;
}
if (t->ch == 'd') {
if (temp2->x1 <= t->x1 && temp2->x1 >= (t->x1 - t->len))
flagOut = 1;
}
}
}
if (temp2->y1 == t->y1 && temp2->x1 == t->x1)
flagOut = 0;
t = t->next;
}
}

Snake.c

  • 1.
    #include <stdio.h> #include <stdlib.h> #include<conio.h> #include <ctype.h> #include <graphics.h> #include <process.h> #include <dos.h> void create(char); void check(); void out(); char prevKey = 'd', pre = 'd', getKey = 'a'; struct node { char ch; int x1, y1, len, flag; struct node *next; }; struct node *start, *t, *temp, *temp2; int flagOut, foodx, foody, max = 100, score = 0; void main() { int gd = DETECT, gm = 0, i,j; flagOut = 0; foodx = rand() % 620; foody = rand() % 460; initgraph(&gd, &gm, "c:turboc3bgi"); //according to your compiler change the url. start = (struct node *) malloc(sizeof (struct node)); start->next = NULL; start->x1 = 150; start->y1 = 200; start->ch = 'd'; start->len = max; start->flag = 1; while (1) { clrscr(); if (kbhit()) { getKey = getch(); if (prevKey != getKey && getKey == 'w' || getKey == 'a' || getKey == 's' || getKey == 'd') { if (prevKey == 'w' && getKey != 's' || prevKey == 's' && getKey != 'w' || prevKey == 'a' && getKey != 'd' || prevKey == 'd' && getKey != 'a') { create(getKey); prevKey = getKey; } } } for(i=-1;i<=1;i++) { for(j=-1;j<=1;j++) { putpixel(foodx+i, foody+j, 4); } } check(); out(); t = start; do { if (t->ch == 'd') { setcolor(4); line(t->x1, t->y1, t->x1 - t->len, t->y1); if (t->flag) t->x1++; if (t->x1 >= 640) flagOut = 1;
  • 2.
    } if (t->ch =='w') { setcolor(4); line(t->x1, t->y1, t->x1, t->y1 + t->len); if (t->flag) t->y1--; if (t->y1 <= 0) flagOut = 1; } if (t->ch == 'a') { setcolor(4); line(t->x1, t->y1, t->x1 + t->len, t->y1); if (t->flag) t->x1--; if (t->x1 <= 0) flagOut = 1; } if (t->ch == 's') { setcolor(4); line(t->x1, t->y1, t->x1, t->y1 - t->len); if (t->flag) t->y1++; if (t->y1 >= 480) flagOut = 1; } if (flagOut == 1) { printf("Game OvernScore=%d", score); delay(2000); exit(1); } t = t->next; } while (t); delay(20); }//while close } void create(char v) { t = temp = start; while (t) { t->flag = 0; temp = t; t = t->next; } temp2 = (struct node *) malloc(sizeof (struct node)); temp->next = temp2; temp2->next = NULL; temp2->flag = 1; temp2->x1 = temp->x1; temp2->y1 = temp->y1; temp2->ch = v; temp2->len = -1; } void check() { t = start; if (t->next != NULL) t->len--; if (t->len <= 0) { start = t->next; free(t); } t = start; do {
  • 3.
    temp = t; t= t->next; } while (t); if (temp->len < max) temp->len++; if (temp->x1 > foodx - 2 && temp->x1 < foodx + 2 && temp->y1 > foody - 2 && temp->y1 < foody + 2) { max = max + max / 10; foodx = rand() % 620; foody = rand() % 460; score = score + score / 10 + 10; } } void out() { t = temp2 = start; while (t) { temp = temp2; temp2 = t; t = t->next; } t = start; while (t->next) { if (temp2->ch == 'w' && t->ch != 's' && t->ch != 'w' || temp2->ch == 'a' && t->ch != 'd' && t->ch != 'a' || temp2->ch == 's' && t->ch != 'w' && t->ch != 's' || temp2->ch == 'd' && t->ch != 'a' && t->ch != 'd') { if (temp2->x1 == t->x1) { if (t->ch == 'w') { if (temp2->y1 >= t->y1 && temp2->y1 <= (t->y1 + t->len)) flagOut = 1; } if (t->ch == 's') { if (temp2->y1 <= t->y1 && temp2->y1 >= (t->y1 - t->len)) flagOut = 1; } } if (temp2->y1 == t->y1) { if (t->ch == 'a') { if (temp2->x1 >= t->x1 && temp2->x1 <= (t->x1 + t->len)) flagOut = 1; } if (t->ch == 'd') { if (temp2->x1 <= t->x1 && temp2->x1 >= (t->x1 - t->len)) flagOut = 1; } } } if (temp2->y1 == t->y1 && temp2->x1 == t->x1) flagOut = 0; t = t->next; } }