Thread: Pong, Need help with paddles and pause

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    16

    Pong, Need help with paddles and pause

    Hello all. First off i just wanna say thank you to anyone who can help, as this is my first big code undertaking. I have the code below and the coding has gone fine except I need it bounce off if it hits the paddle (right now it goes right through), and then do block++ if it does. Im thinking i need to pass some functions by reference to accomplish this, but im not sure. Also anyone know how i can implement that pause function with the printLogo one? I want it to do printLogo, then pause for like 5 seconds, than start the game. Ive tried putting it every which way and I cant seem to get it to work. Thanks again!

    Code:
    #include <iostream>
    #include <curses.h>
    using namespace std;
    
    /*********************** Global Variables *********************/
    
    char paddle = '|';
    char ball = 'O';
    char str[1000];
    double posx = 12;
    double posy = 12;
    double speedx = 0.004;
    double speedy = 0.004;
    int maxX, maxY;
    int blocks = 0;
    int misses = 0;
    int x = 12;
    int y = 12;
    
    
    /*************** Function Prototypes *******************/
    
    void initialize();
    void quit();
    void displayPaddle(int y, int x, char paddle);
    void moveBall();
    void movePaddle();
    void displayScreen();
    void pause(int x);
    void printLogo();
    
    
    /**************** Main ************************/
    
    int main(){	
    initialize();
    movePaddle();
    
    
    return 0;
    }
    
    
    /******************** Initialize Function *********************/
    void initialize(){
    	initscr();      /* initialize the curses library */
        keypad(stdscr, TRUE);  /* enable keyboard mapping */
        nonl();         /* tell curses not to do NL->CR/NL on output */
        cbreak();       /* take input chars one at a time, no wait for \n */
        noecho();       /* don't echo input */	
        nodelay(stdscr, TRUE);  /* allows no delay for speed on ball*/  
    
    }
    
    
    /***************** displayPaddle function ****************/
    
    
    void displayPaddle(int y, int x, char paddle){
    	mvaddch(y, x, paddle);
    	mvaddch(y+1, x, paddle);
    	mvaddch(y+2, x, paddle);
    	mvaddch(y-1, x, paddle);
    	mvaddch(y-2, x, paddle);
    	
    
    	
    }
    /******************* moveBall function ******************/
    
    void moveBall(){
    	displayScreen();
    	getmaxyx(stdscr, maxY, maxX);
    		mvaddch((int) posy, (int) posx, ' ');
    		posx = posx + speedx;
    		posy = posy + speedy;
    		if(posx <= 0){
    			speedx = -1.0 * speedx;
    			misses++;
    			}
    		if(posx >= maxX){
    			speedx = -1.0 * speedx;
    			
    		}
    		if(posy <= 0){
    			speedy = -1.0 * speedy;
    			
    		}
    		if(posy >= maxY){
    			speedy = -1.0 * speedy;
    	
    		}
    		
    		
    		
    		
    		
    		
    		mvaddch((int) posy, (int) posx, ball);
    	
    }
    
    
    /**************** movePaddle function *********************/
    
    void movePaddle(){
    
    	getmaxyx(stdscr, maxY, maxX);
    	
    for (;;){
       int c = getch();
           if(c == 'q' || c == 'Q'){
           	endwin(); 
            	break;
       } 
        
       moveBall();
       displayPaddle(y,x,' ');
       
     
       
     	
     	if(y<=(maxY-4) && y>=3){  
        switch (c){
           	case 'w':
           	y--;
           	break;
           	case 'W':
           	y--;
           	break;
           	case 's':
           	y++;
           	break;
           	case 'S':
           	y++;
           	break;
           	default:
           	break;
         } 
     	}else if(y>(maxY-4)){
     		 switch (c){
           	case 'w':
           	y--;
           	break;
           	case 'W':
           	y--;
           	break;
           	case 's':
           	break;
           	default:
           	break;
     		 }
     	}else if(y<3){
     		 switch (c){
           	case 'w':
           	break;
           	case 's':
           	y++;
           	break;
           	case 'S':
           	y++;
           	break;
           	default:
           	break;
     		 }
     	}
       
     displayPaddle(y,x,paddle); 
     
    
    }
    }
    
    /************** displayScreen function********************/
    
    void displayScreen(){
    	sprintf(str, "Blocks: %2i", blocks);
    	mvaddstr(0, 60, str);
      	sprintf(str, "Misses: %2i", misses);
    	mvaddstr(1, 60, str);
    	sprintf(str, "Press Q to quit");
    	mvaddstr(0, 30, str);
    	
    }
    
    
    /****************** pause function **********************/
    
    void pause(int x){
    	for(int i=0;i<x*1000000;i++){
    		//pause
    	}
    }
    
    
    /****************** printLogo function *******************/
    
    void printLogo(){
    	sprintf(str, "P O N G P O N G P O N G P O N G P O N G");
    	mvaddstr(10, 19, str);
    	mvaddstr(11, 20, str);
    	mvaddstr(12, 19, str);
    
    	
    
    	
    }

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I'll give you a hint, you need to use 4 IF statements, all of which include the ball's and paddle's position.

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Basically, you need a very rudimentary collision detection. Suppose the balls speed is represented by (Vx, Vy) and his position by (x,y), you need to check if the ball passed through the paddle between (x,y) and (x+Vx, y+Vy).

    Imagine the paddle as moving along a line. First, check if the line has been crossed by checking if the new position is behind the line. Then check if the paddle has been crossed by taking the outer dimensions of the paddle (the upper and lower sides) and compare the Y-position of the ball with that of the paddle. If your framerate is too low and the ball moved a pretty far distance, you have not only to check his new position but also if the line, the ball traveled on, went through the paddle.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I'd love to see the whole source code for the program when your done, looks pretty neat! haha good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Quote Originally Posted by Junior89 View Post
    I'd love to see the whole source code for the program when your done, looks pretty neat! haha good luck!
    Yeah, I've never seen a console Pong game. That's tough.

    Hey trev, I would recommend a OOP approach, rather then the procedural approach you are doing. It may be more code, but it's MUCH easier to read and follow. Since a console Pong game is quite difficult, and you are not using an API, I think OOP would be perfect. The code could also be reusable if you code it correctly.
    Last edited by Sentral; 04-14-2007 at 01:23 PM.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Thanks for all the help everyone =) Im gonna try to figure it all out today and i think i get what my if statements need to do, but well see! And whats OOP stand for?

  7. #7
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    OOP (Object Oriented Programming), it's only the heart of C++.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    hehe, im still in my first semester of learning C++ =) Havent learned about that yet.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Heres the if statements I wrote that seems logical, but still nothing. Also the posx,posy,intx,inty values are all passed by reference, so they should update. I actually can get the ball to bounce back if it hits anywhere on the x axis the paddle lies, but when i go to have it check the y coords to see if it actually hit the paddle, it still goes right through. Thanks for anymore help! Been trying all day to no avail.

    Code:
    void paddleHit(){
    	 if(posx<=x && posy==y){
     	 	speedx = -1.0 * speedx;
     }
     	 if(posx<=x && posy==y+1){
     	 	speedx = -1.0 * speedx;
     }
     	 if(posx<=x && posy==y+2){
     	 	speedx = -1.0 * speedx;
     }
     	 if(posx<=x && posy==y-1){
     	 	speedx = -1.0 * speedx;
     }
     	 if(posx<=x && posy==y-2){
     	 	speedx = -1.0 * speedx;
     }
    
    }

Popular pages Recent additions subscribe to a feed