Important alert: (current site time 5/23/2013 2:04:32 AM EDT)
 

VB icon

Snake Game

Email
Submitted on: 9/14/2012 11:05:03 AM
By: Victor Chavauty  
Level: Beginner
User Rating: Unrated
Compatibility: C
Views: 1728
(About the author)
 
     Trying to improve algorithm creation and development. Its my first try making a game. Even though it has lots of problems i feel it is a easy way to improve your skills. I hope people more experienced than me can send me their opinions and what they would have changed.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Snake Game
// Description:Trying to improve algorithm creation and development. Its my first try making a game. Even though it has lots of problems i feel it is a easy way to improve your skills. I hope people more experienced than me can send me their opinions and what they would have changed.
// By: Victor Chavauty
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=13847&lngWId=3//for details.//**************************************

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define TRUE 1
#define FALSE 0
char map[20][20] = {{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'}};
char map2[20][20] = {{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'}};
char map3[20][20] = {{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'}};
struct pessoa
{
char name[50];
int points;
int Size;
} person;
int difficulty = 300;
int difficulty_config = 0;
int seg = 0;
int x, y;
int Direction;
void restaura(); /*restore map*/
void crear_punto(); /*create point*/
void imprime(int ax, int bx, char MESSAGE[50]) /*show map*/
{
int a, b;
system("cls");
printf("NAME: %sPOINTS: %d\n", person.name, person.points); /* MUDA */
if(MESSAGE != NULL)
{
printf("%s5n\n", MESSAGE); /* MUDA */
}
map[ax][bx] = '0';
for(a = 0;a < 20;a++)
{
for(b = 0;b < 20;b++)
{
printf("%c", map[a][b]); /* MUDA */
}
printf("\n"); /* MUDA */
}
}
void move()
{
int stat = TRUE;
restaura();
printf("A");
if(Direction == 5)
{
if(map[x - 1][y] == '-' || map[x - 1][y] == '|')
{
system("CLS");
printf("GAME OVER 1"); /* MUDA */
exit(0);
}
if(map[x - 1][y] == 'x')
{
printf("A");
if(difficulty_config == 5)
{
printf("A");
difficulty_config = 0;
if(difficulty > 100) difficulty = difficulty - 35;
}
printf("A");
eliminar_punto(x-1, y);
person.points = person.points + 1;
difficulty_config = difficulty_config + 1;
}
printf("A");
x = x - 1;
}
else if(Direction == 2)
{
if(map[x + 1][y] == '-' || map[x + 1][y] == '|' )
{
system("CLS");
printf("GAME OVER"); /* MUDA */
exit(0);
}
if(map[x + 1][y] == 'x')
{
if(map[x + 1][y] == 'x')
{
if(difficulty_config == 5)
{
difficulty_config = 0;
if(difficulty > 100) difficulty = difficulty - 35;
}
eliminar_punto(x + 1, y);
person.points = person.points + 1;
difficulty_config = difficulty_config + 1;
}
}
x = x + 1;
}
else if(Direction == 3)
{
printf("C");
if(map[x][y + 1] == '-' || map[x][y + 1] == '|')
{
system("CLS");
printf("GAME OVER 3");
exit(0);
}
printf("C");
if(map[x][y + 1] == 'x')
{
if(difficulty_config == 5)
{
difficulty_config = 0;
if(difficulty > 100)
difficulty = difficulty - 35;
printf("C");
}
printf("C");
eliminar_punto(x, y +1 );
person.points = person.points + 1;
difficulty_config = difficulty_config + 1;
}
printf("C");
y = y + 1;
}
else if(Direction == 1)
{
if(map[x][y - 1] == '-' || map[x][y - 1] == '|')
{
system("CLS");
printf("GAME OVER 4");
exit(0);
}
if(map[x][y - 1] == 'x')
{
if(difficulty_config == 5)
{
difficulty_config = 0;
if(difficulty > 100) difficulty = difficulty - 35;
}
eliminar_punto(x, y - 1);
person.points = person.points + 1;
difficulty_config = difficulty_config + 1;
}
y = y - 1;
}
if(stat == TRUE)
{
imprime(x, y, NULL);
}
}
void relojo( int seconds ) /*clock*/
{
clock_t tempo;
tempo = clock () + seconds * 300;
while (clock() < tempo) {}
seg++;
}
int main()
{
x = 10;
y = 10;
crear_punto();
Direction = 3;
printf("Qual o seu nome: >");
scanf("%s", &person.name);
person.points = 0;
person.Size = 2;
int i;
char ch = 'a';
printf("Z1");
for(i = 0;i < 5;i++)
{
printf("Z2");
do
{
if(GetAsyncKeyState(VK_ESCAPE) & 1)
{
printf("Z3");
system("cls");
printf("JOGO TERMINADO...");
exit(0);
}
else if(GetAsyncKeyState(VK_UP) & 1)
{
printf("Z3b");
Direction = 5;
}
else if(GetAsyncKeyState(VK_LEFT) & 1)
{
printf("Z3c");
Direction = 1;
}
else if(GetAsyncKeyState(VK_DOWN) & 1)
{
printf("Z3d");
Direction = 2;
}
else if(GetAsyncKeyState(VK_RIGHT) & 1)
{
printf("Z3e");
Direction = 3;
}
printf("Z");
move();
relojo(1);
printf("Z");
i = 0;
}while(ch != 'e');
}
}
void restaura()
{
int a, b;
for(a = 0;a < 20; a++)
{
for(b = 0;b < 20;b++)
{
map[a][b] = map2[a][b];
}
}
}
void eliminar_punto(int a, int b)
{
map2[a][b] = map3[a][b];
map[a][b] = map3[a][b];
crear_punto();
}
void crear_punto()
{
int random1, random2;
random1 = rand() % 19 + 0;
random2 = rand() % 19 + 0;
map[random1][random2] = 'x';
map2[random1][random2] = 'x';
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.