[ create a new paste ] login | about

Link: http://codepad.org/ehhbbPIV    [ raw code | output | fork ]

ninwa - C++, pasted on Dec 3:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <cassert>
using namespace std;

const int PADDLE_SIZE = 8;
const int SCREEN_HEIGHT = 24;
const int SCREEN_WIDTH = 80;

struct player
{
	int score;
	int paddle_loc;
	int paddle_dest;
};

struct ball
{
	int x;
	int y;
	bool going_north;
	bool going_east;
};

void draw_paddle( int pos )
{
	cout << setfill(' ') << setw(pos + PADDLE_SIZE)  << "########" << endl;
}

int main( int argc, char **argv )
{
	srand( (unsigned) time(0) ); // Seed RNG
		
	// Initialize our players
	player players[2] = {{0,30, 30},{0,30, 30}};
	ball b = {SCREEN_WIDTH/2, SCREEN_HEIGHT/2, false, true};

	bool game = true;
	while( game )
	{
		// Test point cases, did our ball hit a boundary?
		if( b.going_east )
		{
			if( b.x < SCREEN_WIDTH )
				b.x++;
			else{
				b.going_east = false;
				b.x--;
			}
		}
		else
		{
			if( b.x > 0 )
				b.x--;
			else{
				b.going_east = true;
				b.x++;
			}
		}
		
		if( b.going_north )
			b.y--;
		else
			b.y++;


		if( b.y == 0)
		{
			// This is a deflect
			if( b.x >= players[0].paddle_loc && b.x <= players[0].paddle_loc + PADDLE_SIZE )
			{
				b.going_north = false;
				b.y++;
			}
			else
			{
				players[0].score++;
				b.x = SCREEN_WIDTH/2;
				b.y = SCREEN_HEIGHT/2;	
			}
		}
		else if( b.y == SCREEN_HEIGHT )
		{
			if( b.x >= players[1].paddle_loc && b.x <= players[1].paddle_loc + PADDLE_SIZE )
			{
				b.going_north = true;
				b.y--;
			}
			{
				players[1].score++;
				b.x = SCREEN_WIDTH/2;
				b.y = SCREEN_HEIGHT/2;	
			}
		}

		for( int i = 0; i < 2; i++)
		{
			if( players[i].paddle_loc != players[i].paddle_dest )
			{
				if(players[i].paddle_loc < players[i].paddle_dest )
					players[i].paddle_loc++;
				else
					players[i].paddle_loc--;
			}
			else
			{
				// We've reached our destination, let's set a new random point
				players[i].paddle_dest = rand()%SCREEN_WIDTH-PADDLE_SIZE;
			}
		}

		// Draw our Scene
		for(int i = 0; i < SCREEN_HEIGHT; i++)
		{
			if( i == 0 ) draw_paddle( players[0].paddle_loc );
			else if( i == SCREEN_HEIGHT - 1 ) draw_paddle( players[1].paddle_loc );
			else if( i == 5 ) cout << players[0].score << " | " << players[1].score;
			else if( i == b.y )
			{
				cout << setw(b.x-1) << setfill(' ') << "o" << endl;
			}
			else cout << "\n";
		}
		
		system("cls");
	}

	return 0;
}


Output:
                              ########




0 | 0






                                       o









                              ########

Disallowed system call: SYS_fork


Create a new paste based on this one


Comments: