Skip to content

Instantly share code, notes, and snippets.

@hamoungh
Created November 11, 2014 15:07
Show Gist options
  • Save hamoungh/26893375e017d7ab919d to your computer and use it in GitHub Desktop.
Save hamoungh/26893375e017d7ab919d to your computer and use it in GitHub Desktop.
-the lab practice should be submitted to bb
-This program draws a spacecraft that can travel and an array of fixed asteroids.
-include the following header files:
#include <cmath>
#include <iostream>
#include <string>
#include<conio.h>
#include <Windows.h>
if adding windows.h makes your program not compile use vs2013.
-Import the std namespace
-define a global integer constant called astroids_num to store the number of astroids.
-define two global float variables xVal and yVal to store the coordinates of the spacecraft.
-define two global integer arrays arrayAsteroidsX,arrayAsteroidsY each having astroids_num elements to store the coordinates of the asteroids.
-write a void method called setup() that initializes arrayAsteroidsX with random variable between 1 and 40 and initializes arrayAsteroidsY with random numbers between 1 and 20.
- write a main funct
int main (){
setup();
for(int i=0; i<astroids_num ;i++)
cout << arrayAstroidsX[i]-xVal <<","
<< arrayAstroidsY[i]-yVal<< endl;
return 0;
}
-add a keyboard input processing function called:
void keyInputf(unsigned char key):
switch(key)
{
case 'W':
case 'w':
yVal++;
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main (){
setup();
char ch=0;
do{
for(int i=0; i<astroids_num ;i++)
cout << arrayAstroidsX[i]-xVal <<","
<< arrayAstroidsY[i]-yVal<< endl;
ch=_getch();
keyInputf(ch);
}while(ch!='q' && ch!='Q');
return 0;
}
-copy the following definition for the function gotoxy(...) if you're on Windows:
void gotoxy( int x, int y )
{
COORD p = { x, 100-y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), p );
}
this code needs windows.h
-write a method void drawScene(char ch) that prints each astroid using the following 2 statements:
gotoxy(arrayAsteroidsX[j]-xVal, arrayAsteroidsY[j]-yVal);
cout << ch;
- you need a for loop that goes over all asteroids and uses the variable asteroids_num as the upper bound.
- test your program using the following main method:
void main()
{
setup();
drawScene('o');
}
-the idea is to use the wasd keys to control the navigation. I left the code for the asd keys to you.
include the following header file:
#include<conio.h>
-change the main method to the following:
void main()
{
char ch=0;
setup();
do
{
drawScene('o');
gotoxy(20, 10 );
cout << "+";
ch = _getch();
drawScene(' ');
keyInputf(ch);
}while (ch != 'Q' && ch!='q');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment