snake game need clarification

Hi, Im taking programming 1 for the first time at my school this semester, and the final assignment we had to make is write a snake clone. After weeks of extensive studying, and of trial and error, and tossing many attempts to the trash, I've cranked out a snake program. Thing is, i dont know why for some reason when it compiles it doesnt print anything on screen. Could someone please have a look at the code and let me know what the problem with it is? Im desperate, i tried asking my friends students but they dont know either. The code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
#define LEVELWIDTH 28
#define LEVELHEIGHT 30

 // define the level with a 2d array.  1s are walls, 0s are the void where the snake moves
int nivel[LEVELWIDTH][LEVELHEIGHT] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,}, 
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,}, 
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,}, 
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,}, 
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,}, 
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,}, 
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,}, 
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,}, 
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
	{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,}
};

#define MAXLENGTH 30  //snake's max length
/*snake[0]['x'] //la cabeza de la snake
snake[x][0] = 0 // posicion x
snake[x][1] = 1 // posicion y   */
int snake[MAXLENGTH + 1][2];int snakelength = 0;int vX,vY; // direction where the snake moves



void resetsnake()
{
	snakelength = 0; // resets the snake to only its head

	//resets to head only at the begginning of each level
	snake[0][0] = LEVELWIDTH / 2;
	snake[0][1] = LEVELHEIGHT / 2;
}



void increasesnake(int i)
{
	if(snakelength+ i <= MAXLENGTH)
		snakelength+=i;
	else
		snakelength = MAXLENGTH;
}

void decreasesnake(int i)
{
	if(snakelength - i >= 0)
		snakelength -=i;
	else
		snakelength = 0;
}

void movesnake()
{
	int i;

	for(i = snakelength ; i > 1; i--)    //i < 0
	{
		snake[i][0] = snake[i - 1][0];
		snake[i][1] = snake[i - 1][1];
	}

	snake[0][0] += vX;
	snake[0][1] += vY;
}

int collisiondetect() // returns 1 if it detects collision with the wall or with itself, otherwise returns 0
{
	int i;
	int hit;

	hit = 0;

	//checks if it leaves the level
	if(snake[0][0] < 0 || snake[0][0] > LEVELWIDTH || snake[0][1] < 0 || snake[0][1] >LEVELHEIGHT) hit = 1;

	//checks if it reaches a wall
	if(nivel[snake[0][1]][snake[0][0]] == 1) hit = 1;

	//checks if it crashes with itself
	for(i = 1; i < snakelength; i++)
	{
		if(snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) hit = 1;
	}

	return hit;
}


#define TILE_WALL		(1)
#define POWERUP_PILL     	(2) 
#define POWERUP_FOO		(3)

void moveup()
{
	vX = 0;
	vY = -1;
}

void movedown()
{
	vX = 0;
	vY = 1;
}

void moveleft()
{
	vX = -1;
	vY = 0;
}

void moveright()
{
	vX = 1;
	vY = 0;
}
//draws the snake onscreen



void draw()
{
	int x,y;
	printf("woot");
	for(y = 0; y < LEVELHEIGHT; y++)
	{
		for(x = 0; x < LEVELWIDTH; x++)
		{
			switch(nivel[y][x])
			{
				case 	TILE_WALL:
					printf("*",x,y); // ASCII character that makes up wall
                                        break;
				case 	POWERUP_PILL:
					printf("&",x,y); // ASCII character that makes up powerup
						break;
			}
			 
		}
	}

	for(x = 0; x < snakelength; x++)
	{
		printf("*",snake[x][0],snake[x][1]); // ASCII character that makes up the snake's body
	}
}



void main()
{ 
	resetsnake();
		
	while(1)
	{
		draw();
		
		movesnake();
		
  		if(collisiondetect() != 1)
		{
			
			movesnake();
			 char key;
                         key=getch();

			//checks which key the user presses to move the snake in that direction

			if(tecla=='H') moveup();
			if(tecla=='P') movedown();

			if(tecla=='K') moveleft();
			if(tecla=='M') moveright();
		}
		else
		{
			printf("You lose!");
		}
		
	}


}


If you have any questions regarding what any line does let me know. Please, ill appreciate any help i can get

Thanks

frankie
first off, void main must be int main. that gets rid of one error.

Just after a quick glance, you are missing function prototypes for all of your functions. For all the functions you define (besides main), you need to include a function prototype. If you only have one file of code, then you can just place these prototypes at the top of the file underneath your #include's. Otherwise, it is often best to have a header file which contains your function prototypes.

Also, as the above poster said, your main function should return int. You will need to return 0 at the end of main.
You don't have to use function prototypes if you have them before they need to be used:
1
2
3
4
5
6
7
8
int func()
{
   //Function body
}
int main()
{
   func(); // ok, not prototype needed
}

1
2
3
4
5
6
7
8
9
10
11
int func();

int main()
{
   func(); // ok, but prototype needed
}

int func()
{
   //Function body
}
Last edited on
Ok, thanks for your replies, i put a int instead of voin in main, and added a return 0; i also added prototypes, although i think bazzy's right, the way the program is laid out theyre not needed. Now it compiles, and prints to the screen, problem is, it only prints many rows of asterisks. Maybe i need a newline character somewhere, i dont know. Any suggestions?
ok, so i fixed a few bits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
#define LEVELWIDTH 30
#define LEVELHEIGHT 30
#define TILE_WALL		(1)
#define POWERUP_PILL     	(2) 
#define POWERUP_FOO		(3)
#define SPACE                    0

 // define the level with a 2d array.  1s are walls, 0s are the void where the snake moves
int nivel[LEV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 0 // posicion x
snake[x][1] = 1 // posicion y   */
int snake[MAXLENGTH + 1][2];int snakelength = 0;int vX,vY; // direction where the snake moves



void resetsnake()
{
	snakelength = 0; // resets the snake to only its head

	//resets to head only at the begginning of each level
	snake[0][0] = LEVELWIDTH / 2;
	snake[0][1] = LEVELHEIGHT / 2;
}



void increasesnake(int i)
{
	if(snakelength+ i <= MAXLENGTH)
		snakelength+=i;
	else
		snakelength = MAXLENGTH;
}

void decreasesnake(int i)
{
	if(snakelength - i >= 0)
		snakelength -=i;
	else
		snakelength = 0;
}

void movesnake()
{
	int i;

	for(i = snakelength ; i > 1; i--)    //i < 0
	{
		snake[i][0] = snake[i - 1][0];
		snake[i][1] = snake[i - 1][1];
	}

	snake[0][0] += vX;
	snake[0][1] += vY;
}

int collisiondetect() // returns 1 if it detects collision with the wall or with itself, otherwise returns 0
{
	int i;
	int hit;

	hit = 0;

	//checks if it leaves the level
	if(snake[0][0] < 0 || snake[0][0] > LEVELWIDTH || snake[0][1] < 0 || snake[0][1] >LEVELHEIGHT) hit = 1;

	//checks if it reaches a wall
	if(nivel[snake[0][1]][snake[0][0]] == 1) hit = 1;

	//checks if it crashes with itself
	for(i = 1; i < snakelength; i++)
	{
		if(snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) hit = 1;
	}

	return hit;
}




void moveup()
{
	vX = 0;
	vY = -1;
}

void movedown()
{
	vX = 0;
	vY = 1;
}

void moveleft()
{
	vX = -1;
	vY = 0;
}

void moveright()
{
	vX = 1;
	vY = 0;
}
//draws the snake onscreen



void draw()
{
	int x,y;
	printf("woot");
	for(y = 0; y < LEVELHEIGHT; y++)
	{
		for(x = 0; x < LEVELWIDTH; x++)
		{
			switch(nivel[y][x])
			{
				case 	TILE_WALL:
					printf("*",x,y); // ASCII character that makes up wall
                                        break;
				case 	POWERUP_PILL:
					printf("&",x,y); // ASCII character that makes up powerup
						break;
                                case    SPACE:
                                printf(" ",x,y); // ASCII character that makes up powerup
						break;
			}
			 
		}
	}

	for(x = 0; x < snakelength; x++)
	{
		printf("*",snake[x][0],snake[x][1]); // ASCII character that makes up the snake's body
	}
}



int main()
{ 
	resetsnake();
		
	while(1)
	{
		draw();
		
		movesnake();
		
  		if(collisiondetect() != 1)
		{
			
			movesnake();
			 char key;
                         key=getch();

			//checks which key the user presses to move the snake in that direction

			if(key=='H') moveup();
			if(key=='P') movedown();

			if(key=='K') moveleft();
			if(key=='M') moveright();
		}
		else

			printf("You lose!");

		                return 0;
		
	}



}


Some friends here suggested me to add another define called SPACE or something, and add a CASE for it in the draw function, which prints out a " " empty character space. Also, to add a newline char \n so that the level isnt all printed out in one single line. But where do i add that newline char?
In the y loop (before the brace at line 169)
Topic archived. No new replies allowed.