What's wrong with this Space Invaders code

So i am writing a code for a project, but i can't seem to make it work. It;s supposed to transition from title screen to game area when i press any button, but the button just ends the code instead.

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
  #include <stdio.h>

#define MAPX 20		
#define MAPY 20

//function prototypes
void welcome_screen(void);   			//function to display welcome screen
void init_world(void);       			//function to initialize world map with player and enemies
void update_world(int currentScore);    //function to initialize world map with player and enemies
void player_move(void);					//function to control player's move
void victory_message(int winningScore); //function to display victory message
void defeat_message(int finalScore);    //function to display defeat message

//global variables
char world[MAPY][MAPX];   //the world consists of 20 by 20 squares
char player = 'A';		  //player
char enemy = 'E';         //enemy (needs one gunshot to destroy)
char enemyShielded = 'S'; //shielded enemy (needs two gunshots to destroy shield and enemy)
char playerGun = '+';     //player's gunshot 
char enemyGun = '-';      //enemy's gunshot
char explosion = 'X';     //explosion when enemy is shot
int GunReady = 1;         //player's gun's readiness is set to 1; needs greater than 2 to fire;reset to 0 after firing 
int enemyReady = 0;       //enemy's gun is not ready
int totalEnemies = 0;     //total number of enemies is initialized to zero

void main()
{
welcome_screen();
}
void welcome_screen(void)
{
	printf("\n \n Welcome soldier! \n \n");
	Sleep(1000);
	printf("\n \n Defeat the computer INVADERS and come back as a hero. \n \n");
	Sleep(1000);
	printf("\n \n Your computer's' life is depending on you. \n \n");
	Sleep(1000);
	printf("\n \n Good luck!!! \n \n \n ");
	Sleep(1000);
	printf("\n \n \n Direction \n Left - Press 'a' \n Right - Press 'd' \n Gun - Press 'm' \n Press any key to continue. ");
	getch();
}


void init_world(void)
{
int x,y;
 for (x = 0; x < MAPX; x ++) { //loop from left to right on the world map
 for (y = 0; y < MAPY; y ++) { //loop from top to bottom on the world map
 if (y % 2 == 1 && y < 7 && x > 4 && x < MAPX - 5 && x % 2 ==0) {
 //if x and y coordinates match those for enemies, then
 world[y][x] = enemy; // place enemy 'E’
 totalEnemies ++; //increment total number of enemies
 }
 else if (y==7 && x > 4 && x < MAPX - 5 && x % 2 ==0){
 //else if x and y coordinates match those for shielded enemies, then
 world[y][x] = enemyShielded; // place shielded enemies; first layer is shield S
 totalEnemies = totalEnemies + 2; // increment by 2 due to shield plus enemy
 }
 else {
 world[y][x] = player ; // no enemies at other coordinates, so put empty space
 }
 }
}
// Part 2: position of the player in the centre map x and last row of y
}
On line 41, where your getch() is, nothing follows it so it returns. Did you forget to call init_world() ?
I'm sorry but is the init_world() supposed to be inside the {} brackets or outside?
Hi,
Here you go :
1
2
3
4
5
int main()
 {
     init_world();
     welcome_screen();
 }
Does that help? :)
You did not include #include <windows.h> in your code. You can't use the function Sleep() when the header is not there.
Topic archived. No new replies allowed.