Game Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesGame Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 5th, 2013, 03:41 PM
anikan1297 anikan1297 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 anikan1297 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 46 sec
Reputation 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;
}

Reply With Quote
  #2  
Old January 8th, 2013, 09:48 PM
portcitysoftwar portcitysoftwar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 163 portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 13 h 18 m 54 sec
Reputation Power: 17
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;
}




Quote:
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;
}

Reply With Quote
  #3  
Old January 10th, 2013, 06:24 PM
anikan1297 anikan1297 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 anikan1297 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 46 sec
Reputation Power: 0
Quote:
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?

Reply With Quote
  #4  
Old January 10th, 2013, 09:30 PM
portcitysoftwar portcitysoftwar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 163 portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 13 h 18 m 54 sec
Reputation Power: 17
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

Quote:
Originally Posted by anikan1297
Thanks!!!!!!!!!!
Also what language would you recommend?

Reply With Quote
  #5  
Old January 10th, 2013, 09:53 PM
anikan1297 anikan1297 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 anikan1297 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 46 sec
Reputation 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!

Reply With Quote
  #6  
Old January 11th, 2013, 09:59 PM
portcitysoftwar portcitysoftwar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 163 portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 13 h 18 m 54 sec
Reputation Power: 17
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.

Reply With Quote
  #7  
Old January 11th, 2013, 11:05 PM
anikan1297 anikan1297 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 anikan1297 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 46 sec
Reputation 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?

Reply With Quote
  #8  
Old January 11th, 2013, 11:11 PM
portcitysoftwar portcitysoftwar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 163 portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 13 h 18 m 54 sec
Reputation Power: 17
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.

Quote:
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?

Reply With Quote
  #9  
Old January 11th, 2013, 11:38 PM
anikan1297 anikan1297 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 anikan1297 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 46 sec
Reputation Power: 0
Ok. Just curious, what game(s) did you help make?

Reply With Quote
  #10  
Old January 12th, 2013, 01:09 AM
portcitysoftwar portcitysoftwar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 163 portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 13 h 18 m 54 sec
Reputation Power: 17
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.


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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesGame Development > Help Understand Space Invaders Command Propmt Game

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap