Hello guys :) What a great and usefull forum m8s :)
I have made a program in snake, but i have remade it, because c++ is quite new for me, but on tursday i have to do a presentation(examination) for my teacher explaining the code, i understand 80-90 % of the code. But i would like to have, a pro explaining each section function if possible :)
The code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")
int iSleepTime=125;
void screen(char main[][75], int score);
void reset(char main[][75]);
bool exitgame(int score);
void dot(int &dots, char main[][75]);
void control(int &direction);
void move(char main[][75], int &pieces, int pastCounter, int past[][2], int &dots, int &score, int &exit);
void directionn(int direction, int &pastCounter, int past[][2]);
int main()
{
int past[1000][2];
int pieces = 3;
char main[25][75];
int pastCounter = 6;
int direction = 0;
int dots = 0;
int score = 0;
int exit = 0;
int playAgain = 1;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),31); // Farve
for (int x = 0; x < 1000; x ++)
{
for (int y = 0; y < 2; y ++)
{
past [x][y] = 0;
}
}
past[pastCounter][0] = 1; //Væg
past[pastCounter][1] = 1;
while(exit == 0)
{
screen(main, score);
Sleep(iSleepTime); //For normalt hastighed
control(direction);
directionn(direction, pastCounter, past);
reset(main);
move(main, pieces, pastCounter, past, dots, score, exit);
if (dots == 0)
{ //Nye æbler
dot(dots, main);
}
}
exitgame(score);
Sleep (10000);
}
char *strAddChar(char *s1, char c)
{
char *s;
s = s1;
s = s + strlen(s1); // Slangens position
*s = c; // placering null char first
// was.
s++;
*s = 0; // sætte ekstra dele til slangen, new null string
return(s1);
} // strAddChar
void screen(char main[][75], int score)
{//Tegner skærmen
char s[2000]="";
int i=0;
i=sprintf(s,"Score : %d | Speed : %d\n", score, (int)1000/iSleepTime);
// printf ("Score : %d\n", score);
for (int x = 0; x < 23; x ++)
{
for (int y = 0; y < 75; y ++)
{
strAddChar(s,main[x][y]);
}
strcat(s,"\n");
}
system("cls");
printf("%s",s);
Sleep(10);
}
void reset(char main[][75])
{// resetter arrayen, sådan at slangen ikke får dele af slangen steder, hvor der ikke brude værer dele efter slangens bevægelse.
for (int x = 0; x < 23; x++)
{
for (int y = 0; y < 75; y++)
{
if (main[x][y] == '@')
{ //resetter ikke æble
main[x][y] == '@';
}
else
{
if (x == 0 || x == 22 || y == 0 || y == 74)
{//resetter ikke rammen
main[x][y] = 178;
}
else
{
main[x][y] = ' ';
}
}
}
}
}
//
void move(char main[][75], int &pieces, int pastCounter, int past[][2], int &dots, int &score, int &exit)
{//Bevæger slangens hovede, og dens krop følger efter
if (past[pastCounter][0] == 22 || past[pastCounter][0] == 0)
{ //Når slangen rammer rammen, så dør slangen
PlaySound("start.wav", NULL, SND_FILENAME | SND_ASYNC);
exit = 1;
}
if (past[pastCounter][1] == 74 || past[pastCounter][1] == 0)
{
PlaySound("start.wav", NULL, SND_FILENAME | SND_ASYNC);
exit = 1;
}
for (int x = 0; x < pieces; x++)
{
if (main[past[pastCounter - x][0]][past[pastCounter - x][1]] == '@')
{ //Når slangen spiset et æbel
dots--;
pieces++;
score += 10;
PlaySound("eat.wav", NULL, SND_FILENAME | SND_ASYNC);
iSleepTime=max(iSleepTime-5,25);
}
if (main[past[pastCounter - x][0]][past[pastCounter - x][1]] == 'o')
{ //Hvis slangen spiser sig selv.
PlaySound("dead.wav", NULL, SND_FILENAME | SND_ASYNC);
exit = 1;
}
else
{ //Slangens position bestemmes
main[past[pastCounter - x][0]][past[pastCounter - x][1]] = 'o';
}
}
}
void control (int &direction)
{//kontrollere brugeres indput
int key = 0;
if (kbhit())
{
switch (getch())
{
case 72:
direction = 2;
break; //up
case 77:
direction = 0;
break; //right
case 80:
direction = 3;
break; //down
case 75:
direction = 1;
break; //left
}
}
}
void directionn(int direction, int &pastCounter, int past[][2])
{ //Gælder spillerens bevægelse, som bestemmer slangens retning
int right;//adder til array
int down;
right = past[pastCounter][1];
down = past[pastCounter][0];
switch (direction)
{ //Slangens retning
case 0:
right ++;
break;
case 1:
right --;
break;
case 2:
down --;
break;
case 3:
down ++;
}
pastCounter ++;
past[pastCounter][0] = down;
past[pastCounter][1] = right;
}
void dot (int &dots, char main[][75])
{//Placere æblerne
int up = 0;
int left = 0;
dots = 1;
for (int x = 0; x < dots; x++)
{ //Placerer kun æbler, når hverken rammen eller slangens krop allerede er på dem
up = (rand() % 23)+1;
left = (rand() % 73)+1;
if (main[up][left] != ' ')
{
dot(dots, main);
}
// if (main[up][left] == '*//')
// {
// dot(dots, main);
// }
else
{
main[up][left] = '@';
}
}
}
bool exitgame (int score) // returnere enten sandt eller falskt
{//Går ud af spillet, og viser scoren
int exit = 0;
system ("cls");
printf ("Game Over\n\n");
printf ("You got a score of %d\n", score);
return (getch());
}