Thread: I present to cprogramming.com: Prongject X

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    I present to cprogramming.com: Prongject X

    http://i301.photobucket.com/albums/n.../Prongject.jpg

    And the source code that makes it all work:
    Code:
    #include "Paddle.h"
    #include "Ball.h"
    #include "Textures.h"
    #include "Boundaries.h"
    #include "vector2.h"
    
    Paddle paddle1;
    Paddle paddle2;
    Ball ball;
    Boundaries border;
    
    class Mechanics
    {
    public:
    	Mechanics(){paddle1.location = Player_1_Start; paddle2.location = Player_2_Start;}
    	void check_ball_collision(){ 
    		vector2 a_min, a_max, b_min, b_max;
    		if(ball.velocity.x > 0){
    			a_min = ball.location + ball.d_min;
    			a_max = ball.location + ball.d_max;
    			b_min = paddle1.location + paddle1.d_min;
    			b_max = paddle1.location + paddle1.d_max;
    			if (collision2d(a_min, a_max, b_min, b_max))
    				{reverse_x_velocity(ball.velocity);}
    			else if(ball.location.x > 75)
    				{reverse_x_velocity(ball.velocity);}}
    		if(ball.velocity.x < 0){
    			a_min = ball.location + ball.d_min;
    			a_max = ball.location + ball.d_max;
    			b_min = paddle2.location + paddle2.d_min;
    			b_max = paddle2.location + paddle2.d_max;
    			if (collision2d(a_min, a_max, b_min, b_max))
    				{reverse_x_velocity(ball.velocity);}
    			else if(ball.location.x < -75)
    				{reverse_x_velocity(ball.velocity);}}
    		if(ball.velocity.y > 0){
    			if (ball.location.y > 75)
    				{reverse_y_velocity(ball.velocity);}}
    		if(ball.velocity.y < 0){
    			if (ball.location.y < -75)
    				{reverse_y_velocity(ball.velocity);}}}
    	void check_paddle_collision(){
    		if (paddle1.location.y == 75)
    			{reverse_y_velocity(paddle1.location);}
    		else if(paddle1.location.y == -75)
    			{reverse_y_velocity(paddle1.location);}
    		if (paddle2.location.y == 75)
    			{reverse_y_velocity(paddle2.location);}
    		else if(paddle2.location.y == -75)
    			{reverse_y_velocity(paddle2.location);}}
    private:	
    	bool collision2d(vector2 & a_min, vector2 & a_max, vector2 & b_min, vector2 & b_max) {
    	return (a_min.x <= b_max.x) && (a_min.y <= b_max.y) && (a_max.x >= b_min.x) && (a_max.y >= b_min.y);}
    	void reverse_y_velocity(vector2 & velocity)
    	{velocity.y *= -1;}
    	void reverse_x_velocity(vector2 & velocity)
    	{velocity.x *= -1;}
    };
    
    Mechanics game;
    Code:
    int DrawGLScene(GLvoid)
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	game.check_ball_collision();
    	game.check_paddle_collision();
    	border.draw();
    	ball.draw(ball.location);
    	paddle1.draw();
    	paddle2.draw();
    	glLoadIdentity();
    	return TRUE;
    }
    Last edited by Shamino; 04-01-2009 at 06:18 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cprogramming.com IRC channel
    By codec in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 05-06-2004, 10:31 AM
  2. #cprogramming.com
    By Eibro in forum Contests Board
    Replies: 5
    Last Post: 03-25-2003, 08:06 PM
  3. My birthday present to myself
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 01-25-2003, 09:49 PM
  4. Changes at CProgramming.com
    By kermi3 in forum C++ Programming
    Replies: 0
    Last Post: 01-04-2002, 10:58 AM
  5. Talk about cprogramming.com
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-02-2002, 11:07 AM