out of bounds problem

This is a discussion on out of bounds problem within the Game Programming forums, part of the General Programming Boards category; i am using the sdl libary to create an air hockey game, and i wish to check when the paddle ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    out of bounds problem

    i am using the sdl libary to create an air hockey game, and i wish to check when the paddle is out of bounds. the paddle is its own class, when the code is tested so when it goes it goes out of bounds. it should move several pixel's back into the screen, however this does not happen.

    can anyone help?

    this is the code for the paddle class
    Code:
    // Paddle.cpp: implementation of the CPaddle class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <SDL/SDL.h>
    #include "Paddle.h"
    
    using namespace std;
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    CPaddle::CPaddle()
    {
    	//set paddle location
    	m_locx = 150;
    	m_locy = 770;
    	//set paddle velocity
    	m_velx = 0;
    	m_vely = 0;
    }
    
    CPaddle::~CPaddle()
    {
    
    }
    //draw the images
    void CPaddle::drawImage(SDL_Surface *p_buffer)
    {
    	SDL_Rect tempRect;
    	
    	tempRect.x = m_locx;
    	tempRect.y = m_locy;
    	
    	//set transparency colour as pink. shown by RGB values
    	SDL_SetColorKey(m_image, SDL_SRCCOLORKEY,SDL_MapRGB(m_image->format, 255, 0, 255));
    	
    	SDL_BlitSurface(m_image, NULL, p_buffer, &tempRect);
    }
    void CPaddle::update()
    {
    	// update location due to velocity
    	m_locx += m_velx;
    	m_locy += m_vely;
    	// space for more update code
    
    	if (m_locx == 0)
    		m_locx = 10;
    	else if (m_locx + 100 == 400)
    		m_locx = 350;
    }
    void CPaddle::outofbounds()
    {
    	
    
    }
    this is the game loop
    Code:
    while (gameloop == true)
    	{
    
    		checkInput();
    		drawBackdrop();
    		player.update();
    		player.drawImage(buffer);
    		computer.drawImage(buffer);
    		AIpuck.drawImage(buffer);
    		SDL_Flip(buffer);
    		
    		player.outofbounds();
    		
    	}
    player is the paddle class object

    the out of bounds code has been moved to the update function from the out of bounds function

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >>if (m_locx == 0)

    your updating by a velocity each time. What happens if it skips zero and goes straight below? Try if (m_lockx <= 0). Same thing for the other bound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Going beyond bounds problem
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 01-03-2009, 09:41 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21