Archived

This topic is now archived and is closed to further replies.

Starting on my first game!!!

This topic is 5671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone! I am about to start making my first game, and I need a little help. So far I was using ''system("clr");'' in my programs when I need to update the screen, but the problem is that it takes so long time and it is slow tough. Take a look at this code:
  
// Program		- A physical simulation of a ball in the free fall

// Date			- 6. maj 2002


#include <iostream>
#include <windows.h>
#include <winbase.h>
#include <process.h>
#include <math.h>

using namespace std;

// functions with no return

void ball(void);				// for the visual output

void funn(void);				// for intro


void free_fall(float &h_start);	// the simulation of the visual output

								// using ball() or what so ever/any function

const float g = 9.82F;

void main()
{
	float h = 0;	// height for the input

	
//loop:

	funn();			// intro

	cout << "\ng=9.82 m/s^2											" << endl;
	cout << "v = sqrt(2 * 9.82m/s^2 * h) = [m/s]					" << endl;
	cout << "The free fall is not affected by air resistance!		" << endl;
	cout << "\n\nEnter the height in the beginning of the fall:	";
	cin >> h;
		
	if (h > 0 && h <= 40)	// in this case/version h <= 40(because of the MS-DOS output)

	{
		free_fall(h);
	}
	else
	{
		cout << "h must be greater than 0 and les 40" << endl;
		cout << "\nRebooting the simulation/menu in 5 seconds..." << endl;
		for (int i = 0; i <= 4; i++)
		{
			cout << (i+1) << endl;
			Sleep(1000);
		}
		system("cls");
		// goto loop;

		main();
	}
}	

void funn(void)	// intro function

{
	for(int x = 4; x > 0; x--)
	{
		cout << "    *****************************************************" << endl;
		cout << "    \\ A physical simulation of a ball in the free fall  /" << endl;
		cout << "    *****************************************************" << endl;
		Sleep(300);	
		system("cls");
		
		cout << "    *****************************************************" << endl;
		cout << "    / A physical simulation of a ball in the free fall  \\" << endl;
		cout << "    *****************************************************" << endl;
		Sleep(300);
		system("cls");

		cout << "    *****************************************************" << endl;
		cout << "    - A physical simulation of a ball in the free fall  -" << endl;
		cout << "    *****************************************************" << endl;
		Sleep(300);
		system("cls");
	}
	cout << "    *****************************************************	" << endl;
	cout << "   ** A physical simulation of a ball in the free fall  **	" << endl;
	cout << "    *****************************************************	" << endl;
}

void ball(void)
{
	cout << "\t\t\t   /-\\" << endl;
	cout << "\t\t\t   \\-/" << endl;
}

void free_fall(float &h_start)
{
	int time_pr_meter_int = 0;
	int meter_inc_by_one = 0;
	double v_m_pr_s;
	const float meter = 1.00, kata_2 = 2.00, kata_diff = 0.50;
	
	do
	{
		ball();	// visual output


		/***********************************************************************\
		* time_pr_meter_int is the time wich takes the ball to cover a distance *
		* of one meter. However time_pr_meter_int will be smaller and smaller,  *
		* as the acceleration is constant(9.82m/s). "This is the cause of high- *
		* speed touchdown".   :)												*
		\***********************************************************************/
		Sleep(time_pr_meter_int);
		
		system("cls");
		
		for (int x = 0; x < meter_inc_by_one; x++)
		{
			cout << endl;
		}
		
		// in/de-crements, calculations

		++meter_inc_by_one;
		
		v_m_pr_s = sqrt(kata_2 * g * meter_inc_by_one);
		
		time_pr_meter_int = int( (meter/v_m_pr_s)*1000);
		
		

	}while (meter_inc_by_one < h_start);
	ball();
	cout << "---------------------------------------------------------------------" << endl;
	cout << "The final speed [m/s], four decimals:\t" << sqrt(kata_2 * g * h_start) << "\tm/s\t-> h=0"<< endl;
	float diff_h = float(sqrt(kata_2 * g * h_start)) - int(sqrt(kata_2 * g * h_start));
	if (diff_h > kata_diff)
	{
		cout << "The final speed [m/s], rounded up(~):\t" << int(sqrt(kata_2 * g * h_start))+1
			<< "\tm/s\t-> h=0" << endl;
	}
	else
	{
		cout << "The final speed [m/s], rounded up(~):\t" << int(sqrt(kata_2 * g * h_start))
			<< "\tm/s\t-> h=0" << endl;
	}

}
  
How can I make this code better; I mean, is there a better way to "update screen", what can I use instead of ''system("clr"); ?

Share this post


Link to post
Share on other sites