9 Replies - 937 Views - Last Post: 19 December 2010 - 06:21 AM Rate Topic: -----

#1 Jacic  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 15-June 10

Problem with random starting positions

Posted 10 December 2010 - 03:37 PM

I decided to make a simple game to improve my knowledge of SDL. Basically, you are a black circle and use the arrow keys to avoid the blue squares. When contact with a square is made, the game ends(its supposed to at this stage). Here is the code:
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <sstream>
#include <time.h>
#include <stdlib.h>

//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces that will be used
//SDL_Surface *aVoid = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_Surface *scoreText = NULL;
TTF_Font *font = NULL;
SDL_Color textColor = {0, 0, 0};

bool running = true;
Uint32 timePlayed = 0;

class blockClass
{
    public:
        blockClass();
        ~blockClass();
        int blockX;
        int blockY;
        SDL_Surface *block;
};
blockClass block;

class aVoidClass
{
    public:
        aVoidClass();
        ~aVoidClass();
        int voidX;
        int voidY;
        SDL_Surface *aVoid;
};
aVoidClass aVoid;

blockClass::blockClass()
{
    blockX = -32;
    blockY = 100;
    block = NULL;
}

blockClass::~blockClass()
{
}

aVoidClass::aVoidClass()
{
    voidX = (SCREEN_WIDTH / 2) - 16; //sprite is 32x32
    voidY = (SCREEN_HEIGHT / 2) - 16; //sprite is 32x32
    aVoid = NULL;
}

aVoidClass::~aVoidClass()
{
}

void sdl_events()
{
    while(SDL_PollEvent(&event))
    {
        if(event.type == SDL_QUIT)
        {
            running = false;
        }
        else if(event.type == SDL_KEYDOWN)
        {
            switch(event.key.keysym.sym)
            {
                case SDLK_UP:
                    aVoid.voidY -= 3;
                    break;
                case SDLK_DOWN:
                    aVoid.voidY += 3;
                    break;
                case SDLK_LEFT:
                    aVoid.voidX -= 3;
                    break;
                case SDLK_RIGHT:
                    aVoid.voidX += 3;
                    break;
                default:
                    break;
            }
        }
    }
}

SDL_Surface *load_image(std::string filename)
{
    //Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;
    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;
    //Load the image
    loadedImage = SDL_LoadBMP(filename.c_str());
    //If nothing went wrong in loading the image
    if(loadedImage != NULL)
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat(loadedImage);
        //Free the old image
        SDL_FreeSurface(loadedImage);
    }
    //Return the optimized image
    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;
    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;
    //Blit the surface
    SDL_BlitSurface(source, NULL, destination, &offset);
}

int draw()
{
    timePlayed = SDL_GetTicks();
    SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 255, 255, 255));
    //apply image to the screen
    apply_surface(aVoid.voidX, aVoid.voidY, aVoid.aVoid, screen);
    std::stringstream timePlayedString;
    timePlayedString << "Score: " << timePlayed / 1000;
    scoreText = TTF_RenderText_Solid(font, timePlayedString.str().c_str(), textColor);
    //apply score to the screen
    apply_surface(0, 0, scoreText, screen);
    apply_surface(block.blockX, block.blockY, block.block, screen); //for debug purposes
    if(SDL_Flip(screen) == -1)
    {
        return -1;
    }
    else
    {
        return 0;
    }
}

void check_collisions()
{
    if(aVoid.voidX < 0)
    {
        aVoid.voidX = 0;
    }
    else if(aVoid.voidX > SCREEN_WIDTH - 32)
    {
        aVoid.voidX = SCREEN_WIDTH - 32; //sprite is 32x32
    }
    if(aVoid.voidY < 0)
    {
        aVoid.voidY = 0;
    }
    else if(aVoid.voidY > SCREEN_HEIGHT - 32)
    {
        aVoid.voidY = SCREEN_HEIGHT - 32; //sprite is 32x32
    }
    //check for collision with block
    if((aVoid.voidX > block.blockX && aVoid.voidX < block.blockX + 32) || (aVoid.voidX + 32 > block.blockX && aVoid.voidX + 32 < block.blockX + 32))
    {
        if((aVoid.voidY > block.blockY && aVoid.voidY < block.blockY + 32) || (aVoid.voidY + 32 > block.blockY && aVoid.voidY + 32 < block.blockY + 32))
        {
            running = false;
        }
    }
}

int random_num(int upperLimit)
{
    int randNum = rand() % upperLimit;
    return randNum;
}

int main(int argc, char* args[])
{
    int blockDir = 0;
    int blockStartX, blockStartY;
    srand(time(0));
    //Initialize all SDL subsystems
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return 1;
    }
    //Set up the screen
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    //If there was an error in setting up the screen
    if(screen == NULL)
    {
        return 1;
    }
    //initialize SDL_ttf
    if(TTF_Init() == -1)
    {
        return 1;
    }
    font = TTF_OpenFont("Herbiflora.ttf", 20);
    if(font == NULL)
    {
        return 1;
    }
    SDL_EnableKeyRepeat(250, SDL_DEFAULT_REPEAT_INTERVAL);
    //Set the window caption
    SDL_WM_SetCaption("aVoid", NULL);
    //Load the image
    aVoid.aVoid = load_image("void.bmp");
    block.block = load_image("block.bmp");
    blockDir = random_num(4);
    while(running)
    {
        sdl_events();
        check_collisions();
        //for debug purposes
        switch(blockDir)
        {
            case 0: //left to right
                blockStartX = -32;
                blockStartY = random_num(SCREEN_HEIGHT - 32);
                block.blockX++;
                break;
            case 1: //right to left
                blockStartX = SCREEN_WIDTH + 32;
                blockStartY = random_num(SCREEN_HEIGHT - 32);
                block.blockX--;
                break;
            case 2: //top to bottom
                blockStartY = -32;
                blockStartX = random_num(SCREEN_WIDTH - 32);
                block.blockY++;
                break;
            case 3: //bottom to top
                blockStartY = SCREEN_HEIGHT + 32;
                blockStartX = random_num(SCREEN_WIDTH - 32);
                block.blockY--;
                break;
            default:
                break;
        }
        if((block.blockX > SCREEN_WIDTH && blockDir == 0)||(block.blockX < SCREEN_WIDTH && blockDir == 1)||(block.blockY > SCREEN_HEIGHT && blockDir == 2)||(block.blockY < SCREEN_WIDTH && blockDir == 3))
        {
            blockDir = random_num(4);
            switch(blockDir)
            {
                case 0:
                    blockStartX = -32;
                    blockStartY = random_num(SCREEN_HEIGHT - 32);
                    break;
                case 1:
                    blockStartX = SCREEN_WIDTH + 32;
                    blockStartY = random_num(SCREEN_HEIGHT - 32);
                    break;
                case 2:
                    blockStartY = -32;
                    blockStartX = random_num(SCREEN_WIDTH - 32);
                    break;
                case 3:
                    blockStartY = SCREEN_HEIGHT + 32;
                    blockStartX = random_num(SCREEN_WIDTH - 32);
                    break;
                default:
                    break;
            }
            block.blockX = blockStartX;
            block.blockY = blockStartY;
        }
        if(draw() == -1)
        {
            return 1;
        }
    }
    //SDL_Delay(2000);
    //Free the surfaces
    SDL_FreeSurface(aVoid.aVoid);
    SDL_FreeSurface(block.block);
    SDL_FreeSurface(scoreText);
    TTF_CloseFont(font);
    TTF_Quit();
    SDL_Quit();
    return 0;
}


It compiles fine, and works almost exactly how it should, with one exception: the block never starts to the right or bottom of the screen. In other words, it only moves from left to right and from top to bottom. Could anyone help me fix this? Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with random starting positions

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Problem with random starting positions

Posted 11 December 2010 - 02:31 PM

The best way to find out would be to debug your game. Which IDE are you using?
Was This Post Helpful? 0
  • +
  • -

#3 Jacic  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 15-June 10

Re: Problem with random starting positions

Posted 12 December 2010 - 01:52 PM

Im using CodeBlocks 10.05.
Debugging the game showed that it seemed to work correctly, but when I removed the breakpoints to check it in realtime it still only moved top -> bottom and left -> right.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Problem with random starting positions

Posted 12 December 2010 - 02:36 PM

Sound to me like your control scheme isn't accurately reflected in your rendering. This is something that you really need to play with until you get it right.

Have you just been using breakpoints, or have you also been using conditional breakpoints? The reason why I ask is that it's a lot easier to spot constraint violations that might be missed simply by hopping from breakpoint to breakpoint.
Was This Post Helpful? 0
  • +
  • -

#5 Jacic  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 15-June 10

Re: Problem with random starting positions

Posted 12 December 2010 - 02:49 PM

Ive been using normal breakpoints. Could you give me some pointers on using conditional breakpoints? (Ive never used them before)
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Problem with random starting positions

Posted 12 December 2010 - 02:58 PM

Conditional breakpoints will only halt the program if the condition checked for is true and allow execution to persist otherwise. So for instance if you do not want the value of some arbitrary variable 'x' to exceed 640 at some point in the code that you have marked with the breakpoint with some condition like x > 640, if x is more than 640 at that point the execution will stop right at that breakpoint. You can also check to see if a variable has changed with the same method. These are brilliant and very powerful. :)
Was This Post Helpful? 0
  • +
  • -

#7 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Problem with random starting positions

Posted 12 December 2010 - 03:19 PM

Just to check. Put a breakpoint at the point where you set the position

random_num(SCREEN_HEIGHT - 32);



What value do you get for this? run it a few times.
Was This Post Helpful? 0
  • +
  • -

#8 Jacic  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 15-June 10

Re: Problem with random starting positions

Posted 17 December 2010 - 05:12 PM

I found out what the problem was: I had a major logic error on line 250. I should have been checking if the X or Y position is less than 0 when moving left or up. Your post helped me figure that out, stayscrisp, so thank you.
Anyway, something good came out of it: I received a crash course in conditional breakpoints courtesy of ButchDean.
Problem Solved!
Was This Post Helpful? 1
  • +
  • -

#9 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Problem with random starting positions

Posted 17 December 2010 - 05:20 PM

Awesome! :punk:
Was This Post Helpful? 0
  • +
  • -

#10 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Problem with random starting positions

Posted 19 December 2010 - 06:21 AM

Thought that might point you in the right direction :) nice work!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1