#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Exclamation Help Understand Space Invaders Command Propmt Game


    Hello, i'm kinda sorta new to C++ and programming in general. I know the syntax but I just don't know how to use it and why. If you can, could you please explain this code. All Help will be appreciated.

    Link to tutorial where I got this:
    (Part 1):http://www.youtube.com/watch?v=TiSAH_MNHN0&list=HL1357421953
    (Part 2):http://www.youtube.com/watch?v=J4B-5gLfIvQ

    Code:
    #include <iostream>
    #include "windows.h"
    #include <time.h>
    
    using namespace std;
    
    char Map[20][20] =
    {
    
    	"###################",
    	"#                 #",
    	"#   @ @ @ @ @ @   #",
    	"#    @ @ @ @ @    #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#        W        #",
    	"###################"
    
    };
    
    bool endGame = false;
    int gameSpeed = 100;
    int randomNum;
    int maxHealth = 200;
    int yourHealth = maxHealth;
    
    int main()
    {
    	system("color 0a");
    
    	while(!endGame)
    	{
    
    		srand(time(0));
    		system("cls");
    		for(int y = 0; y < 20; y++)
    		{
    
    			cout << Map[y] << endl;
    
    		}
    
    		cout << "Health " << yourHealth << "/" << maxHealth;
    
    	for(int y = 0; y < 20; y++)
    	{
    
    		for(int x = 0; x < 20; x++)
    		{
    
    			switch(Map[y][x])
    			{
    			case 'W':
    
    				if(GetAsyncKeyState(VK_LEFT) != 0)
    				{
    					int newX = x - 1;
    
    					switch(Map[y][newX])
    					{
    					case ' ':
    						Map[y][x] = ' ';
    						x--;
    						Map[y][newX] = 'W';
    
    					break;
    					}
    				}
    
    				if(GetAsyncKeyState(VK_RIGHT) != 0)
    				{
    					int newX = x + 1;
    
    					switch(Map[y][newX])
    					{
    					case ' ':
    						Map[y][x] = ' ';
    						x++;
    						Map[y][newX] = 'W';
    						break;
    					}
    				}
    
    				if(GetAsyncKeyState(VK_SPACE) != 0)
    				{
    					y--;
    					Map[y][x] = '^';
    				}
    
    			break;
    
    			case '^':
    
    				Map[y][x] = ' ';
    				y--;
    
    				if(Map[y-1][x] != '#' && Map[y][x] != '@')
    				{
    					Map[y][x] = '^';
    				}else if(Map[y][x] == '@')
    				{
    					Map[y][x] = ' ';
    				}
    
    			break;
    
    			case '@':
    				randomNum = rand() % 50 + 1;
    				
    				if(randomNum == 1)
    				{
    					y++;
    					Map[y][x] = '*';
    				}
    			break;
    
    			case '*':
    				Map[y][x] = ' ';
    				y++;
    
    				if(Map[y][x] != '#' && Map[y][x] != 'W')
    				{
    					Map[y][x] = '*';
    				}else if(Map[y][x] == 'W')
    				{
    					yourHealth -= 20;
    				}
    
    			break;
    			}
    		}
    	}
    
    	if(yourHealth <= 0)
    	{
    		endGame = true;
    	}
    	Sleep(gameSpeed);
    	}
    
    	system("cls");
    	cout << "You lost!" << endl;
    
    	cin.get();
    
    	return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    165
    Rep Power
    18
    This is a very simple game. I have gone through and commented the code for you as i am extremely bored. but if i may make a suggestion i would not recommend C or C++ for beginner programmers.

    With that said i hope this helps

    Code:
    #include <iostream>
    #include "stdlib.h"
    #include <time.h>
    
    using namespace std;
    /*Declare array established starting position of game objects
     Play with this to add more bad guys or change where they start!*/
    char Map[20][20] =
    {
    
    	"###################",
    	"#                 #",
    	"#   @ @ @ @ @ @   #",
    	"#    @ @ @ @ @    #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#        W        #",
    	"###################"
    
    };
    /*Declare some variables
    *is game over
     *how fast game plays
     * and your health
     */
    
    bool endGame = false;
    int gameSpeed = 100;
    int randomNum;
    int maxHealth = 200;
    int yourHealth = maxHealth;
    
    int main()
    {
    	system("color 0a");
    /*Loop through game until game ends*/
    	while(!endGame)
    	{
    
    		srand(time(0));
    		/*Pass Clear Screen to OS*/
    		system("cls");
    		/*Loop through map and print to screen*/
    		for(int y = 0; y < 20; y++)
    		{
    
    			cout << Map[y] << endl;
    
    		}
    /*Print health to screen*/
    		cout << "Health " << yourHealth << "/" << maxHealth;
    /*Loop through Y-Coords of map*/
    	for(int y = 0; y < 20; y++)
    	{
    /*Loop through X-Coords for each Y-coord of map*/
    		for(int x = 0; x < 20; x++)
    		{
    /*Switch checks which character is found
    			 in current coordinate*/
    			switch(Map[y][x])
    			{
    				/*If it is W then it is player*/
    			case 'W':
    /*Did you press left*/
    				if(GetAsyncKeyState(VK_LEFT) != 0)
    				{
    					int newX = x - 1;
    /*Move player one space left if new coord
    					 is white space and not a wall*/
    					switch(Map[y][newX])
    					{
    					case ' ':
    						Map[y][x] = ' ';
    						x--;
    						Map[y][newX] = 'W';
    
    					break;
    					}
    				}
    /*did you press right*/
    				if(GetAsyncKeyState(VK_RIGHT) != 0)
    				{
    					int newX = x + 1;
    /*Move player one space right if new coord
    					 is white space and not a wall*/
    					switch(Map[y][newX])
    					{
    					case ' ':
    						Map[y][x] = ' ';
    						x++;
    						Map[y][newX] = 'W';
    						break;
    					}
    				}
    /*Did you press space*/
    				if(GetAsyncKeyState(VK_SPACE) != 0)
    				{
    					/*Create bullet above player*/
    					y--;
    					Map[y][x] = '^';
    				}
    
    			break;
    
    					/*If it is ^ then it is a bullet*/
    			case '^':
    /*Return old coord to white space*/
    				Map[y][x] = ' ';
    				y--;
    					
    /*Check for colission with wall and bad guys*/
    				if(Map[y-1][x] != '#' && Map[y][x] != '@')
    				{
    					/*Write bullet ti bew ciird*/
    					Map[y][x] = '^';
    				}else if(Map[y][x] == '@')
    						/*Check if collission was a badguy*/
    				{
    					/*Destroy bullet and badguy*/
    					/*Want your bullet to keep going? set to '^'*/
    					Map[y][x] = ' ';
    				}
    
    			break;
    
    					/*@ is bad guy*/
    			case '@':
    					/*Generate random number between 1 and 50*/
    				randomNum = rand() % 50 + 1;
    				/*if random number is 1*/
    				if(randomNum == 1)
    				{
    					/*create bomb bellow the bad guy*/
    					y++;
    					Map[y][x] = '*';
    				}
    			break;
    /** is a bomb*/
    			case '*':
    					/*remove bomb from old position*/
    				Map[y][x] = ' ';
    				y++;
    /*Check for collision*/
    				if(Map[y][x] != '#' && Map[y][x] != 'W')
    				{
    					/*if no collision set bomb one space down*/
    					Map[y][x] = '*';
    				}else if(Map[y][x] == 'W')
    				{
    					/*if collision with player reduce health by 20*/
    					yourHealth -= 20;
    				}
    
    			break;
    			}
    		}
    	}
    /*If health is equal to or less than 0*/
    	if(yourHealth <= 0)
    	{
    		/*set endgame variable*/
    		endGame = true;
    	}
    		/*Pause for duration set as game speed*/
    	Sleep(gameSpeed);
    		
    	/*repeat loop*/}
    /*Clear the screen*/
    	system("cls");
    	/*GAME OVER*/
    	cout << "You lost!" << endl;
    /*Wait for key press to end*/
    	cin.get();
    
    	return 0;
    }


    Originally Posted by anikan1297
    Hello, i'm kinda sorta new to C++ and programming in general. I know the syntax but I just don't know how to use it and why. If you can, could you please explain this code. All Help will be appreciated.

    Link to tutorial where I got this:
    (Part 1):http://www.youtube.com/watch?v=TiSAH_MNHN0&list=HL1357421953
    (Part 2):http://www.youtube.com/watch?v=J4B-5gLfIvQ

    Code:
    #include <iostream>
    #include "windows.h"
    #include <time.h>
    
    using namespace std;
    
    char Map[20][20] =
    {
    
    	"###################",
    	"#                 #",
    	"#   @ @ @ @ @ @   #",
    	"#    @ @ @ @ @    #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#        W        #",
    	"###################"
    
    };
    
    bool endGame = false;
    int gameSpeed = 100;
    int randomNum;
    int maxHealth = 200;
    int yourHealth = maxHealth;
    
    int main()
    {
    	system("color 0a");
    
    	while(!endGame)
    	{
    
    		srand(time(0));
    		system("cls");
    		for(int y = 0; y < 20; y++)
    		{
    
    			cout << Map[y] << endl;
    
    		}
    
    		cout << "Health " << yourHealth << "/" << maxHealth;
    
    	for(int y = 0; y < 20; y++)
    	{
    
    		for(int x = 0; x < 20; x++)
    		{
    
    			switch(Map[y][x])
    			{
    			case 'W':
    
    				if(GetAsyncKeyState(VK_LEFT) != 0)
    				{
    					int newX = x - 1;
    
    					switch(Map[y][newX])
    					{
    					case ' ':
    						Map[y][x] = ' ';
    						x--;
    						Map[y][newX] = 'W';
    
    					break;
    					}
    				}
    
    				if(GetAsyncKeyState(VK_RIGHT) != 0)
    				{
    					int newX = x + 1;
    
    					switch(Map[y][newX])
    					{
    					case ' ':
    						Map[y][x] = ' ';
    						x++;
    						Map[y][newX] = 'W';
    						break;
    					}
    				}
    
    				if(GetAsyncKeyState(VK_SPACE) != 0)
    				{
    					y--;
    					Map[y][x] = '^';
    				}
    
    			break;
    
    			case '^':
    
    				Map[y][x] = ' ';
    				y--;
    
    				if(Map[y-1][x] != '#' && Map[y][x] != '@')
    				{
    					Map[y][x] = '^';
    				}else if(Map[y][x] == '@')
    				{
    					Map[y][x] = ' ';
    				}
    
    			break;
    
    			case '@':
    				randomNum = rand() % 50 + 1;
    				
    				if(randomNum == 1)
    				{
    					y++;
    					Map[y][x] = '*';
    				}
    			break;
    
    			case '*':
    				Map[y][x] = ' ';
    				y++;
    
    				if(Map[y][x] != '#' && Map[y][x] != 'W')
    				{
    					Map[y][x] = '*';
    				}else if(Map[y][x] == 'W')
    				{
    					yourHealth -= 20;
    				}
    
    			break;
    			}
    		}
    	}
    
    	if(yourHealth <= 0)
    	{
    		endGame = true;
    	}
    	Sleep(gameSpeed);
    	}
    
    	system("cls");
    	cout << "You lost!" << endl;
    
    	cin.get();
    
    	return 0;
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by portcitysoftwar
    This is a very simple game. I have gone through and commented the code for you as i am extremely bored. but if i may make a suggestion i would not recommend C or C++ for beginner programmers.

    With that said i hope this helps

    Code:
    #include <iostream>
    #include "stdlib.h"
    #include <time.h>
    
    using namespace std;
    /*Declare array established starting position of game objects
     Play with this to add more bad guys or change where they start!*/
    char Map[20][20] =
    {
    
    	"###################",
    	"#                 #",
    	"#   @ @ @ @ @ @   #",
    	"#    @ @ @ @ @    #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#                 #",
    	"#        W        #",
    	"###################"
    
    };
    /*Declare some variables
    *is game over
     *how fast game plays
     * and your health
     */
    
    bool endGame = false;
    int gameSpeed = 100;
    int randomNum;
    int maxHealth = 200;
    int yourHealth = maxHealth;
    
    int main()
    {
    	system("color 0a");
    /*Loop through game until game ends*/
    	while(!endGame)
    	{
    
    		srand(time(0));
    		/*Pass Clear Screen to OS*/
    		system("cls");
    		/*Loop through map and print to screen*/
    		for(int y = 0; y < 20; y++)
    		{
    
    			cout << Map[y] << endl;
    
    		}
    /*Print health to screen*/
    		cout << "Health " << yourHealth << "/" << maxHealth;
    /*Loop through Y-Coords of map*/
    	for(int y = 0; y < 20; y++)
    	{
    /*Loop through X-Coords for each Y-coord of map*/
    		for(int x = 0; x < 20; x++)
    		{
    /*Switch checks which character is found
    			 in current coordinate*/
    			switch(Map[y][x])
    			{
    				/*If it is W then it is player*/
    			case 'W':
    /*Did you press left*/
    				if(GetAsyncKeyState(VK_LEFT) != 0)
    				{
    					int newX = x - 1;
    /*Move player one space left if new coord
    					 is white space and not a wall*/
    					switch(Map[y][newX])
    					{
    					case ' ':
    						Map[y][x] = ' ';
    						x--;
    						Map[y][newX] = 'W';
    
    					break;
    					}
    				}
    /*did you press right*/
    				if(GetAsyncKeyState(VK_RIGHT) != 0)
    				{
    					int newX = x + 1;
    /*Move player one space right if new coord
    					 is white space and not a wall*/
    					switch(Map[y][newX])
    					{
    					case ' ':
    						Map[y][x] = ' ';
    						x++;
    						Map[y][newX] = 'W';
    						break;
    					}
    				}
    /*Did you press space*/
    				if(GetAsyncKeyState(VK_SPACE) != 0)
    				{
    					/*Create bullet above player*/
    					y--;
    					Map[y][x] = '^';
    				}
    
    			break;
    
    					/*If it is ^ then it is a bullet*/
    			case '^':
    /*Return old coord to white space*/
    				Map[y][x] = ' ';
    				y--;
    					
    /*Check for colission with wall and bad guys*/
    				if(Map[y-1][x] != '#' && Map[y][x] != '@')
    				{
    					/*Write bullet ti bew ciird*/
    					Map[y][x] = '^';
    				}else if(Map[y][x] == '@')
    						/*Check if collission was a badguy*/
    				{
    					/*Destroy bullet and badguy*/
    					/*Want your bullet to keep going? set to '^'*/
    					Map[y][x] = ' ';
    				}
    
    			break;
    
    					/*@ is bad guy*/
    			case '@':
    					/*Generate random number between 1 and 50*/
    				randomNum = rand() % 50 + 1;
    				/*if random number is 1*/
    				if(randomNum == 1)
    				{
    					/*create bomb bellow the bad guy*/
    					y++;
    					Map[y][x] = '*';
    				}
    			break;
    /** is a bomb*/
    			case '*':
    					/*remove bomb from old position*/
    				Map[y][x] = ' ';
    				y++;
    /*Check for collision*/
    				if(Map[y][x] != '#' && Map[y][x] != 'W')
    				{
    					/*if no collision set bomb one space down*/
    					Map[y][x] = '*';
    				}else if(Map[y][x] == 'W')
    				{
    					/*if collision with player reduce health by 20*/
    					yourHealth -= 20;
    				}
    
    			break;
    			}
    		}
    	}
    /*If health is equal to or less than 0*/
    	if(yourHealth <= 0)
    	{
    		/*set endgame variable*/
    		endGame = true;
    	}
    		/*Pause for duration set as game speed*/
    	Sleep(gameSpeed);
    		
    	/*repeat loop*/}
    /*Clear the screen*/
    	system("cls");
    	/*GAME OVER*/
    	cout << "You lost!" << endl;
    /*Wait for key press to end*/
    	cin.get();
    
    	return 0;
    }
    Thanks!!!!!!!!!!
    Also what language would you recommend?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    165
    Rep Power
    18
    Well nobody can tell you what language to use without knowing what you are trying to program. C and c++ are notably some of the best when performance speeds are Necessity as they are highly optimized but they are not good for beginners and can be tedious to code complex applications with. Also can be dangerous for new programmers due to buffer overflow and the levels of access to hardware and OS it allows. Remember powerful languages are dangerous languages.

    VB.net it's great for beginners with the rapid application development and simple English like commands but it's not aS powerful.

    I typically recommend a language derived from FORTRAN to begin. Or php if stateside development is what you prefer

    Originally Posted by anikan1297
    Thanks!!!!!!!!!!
    Also what language would you recommend?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0
    Well you see i've already pretty much got the basics down, im just looking for someone that has the same mind-set as I can learn how to use it!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    165
    Rep Power
    18
    What kind of programming you trying to do? I primarily work on business and accounting software. Therefore i use alot if higher level languages however when i did game development i used more lower level languages.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0
    Well I haven't really established what Language i want to work with, mainly because i'm not sure as to which one is the best for game development. I've worked with both C++ and Java, i've also done some Python but everytime I try to learn more about a certain language, after a little while I just get bored with the tutorials and then I go do something else(Gaming). But when I do understand and know how to use the syntax I find it really fun. Did you ever run into these similar things whenever you started learning to program?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    165
    Rep Power
    18
    Not really i never really got into gaming untill i started developing games. I learned my first programming language when i was about 12 which was FORTRAN95.

    My recomendation with game development is to stick with low level object oriented languages. I wouldnt really recommend this untill you have a VERY good understanding of programming and logic.

    Originally Posted by anikan1297
    Well I haven't really established what Language i want to work with, mainly because i'm not sure as to which one is the best for game development. I've worked with both C++ and Java, i've also done some Python but everytime I try to learn more about a certain language, after a little while I just get bored with the tutorials and then I go do something else(Gaming). But when I do understand and know how to use the syntax I find it really fun. Did you ever run into these similar things whenever you started learning to program?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0
    Ok. Just curious, what game(s) did you help make?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    165
    Rep Power
    18
    No big titles that i know of. Much of the games i did where in college for courses or messing around on my free time. Then i did freelance work where we are rarely told what the final product is. Just told to code a component that will take such and such parameters and return a certain output.

    I am currently working on a title that i can not give much details on which is my first console game for the new OUYA gaming console coming out in april. I am super stoked for this.


    Originally Posted by anikan1297
    Ok. Just curious, what game(s) did you help make?

IMN logo majestic logo threadwatch logo seochat tools logo