4 Replies - 3425 Views - Last Post: 25 January 2011 - 06:20 PM Rate Topic: -----

#1 slwilson10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-January 11

SDL 2D tile based game - need help with character walking

Posted 23 January 2011 - 05:38 PM

Let me start by saying I have no formal training in programming. Im just trying to pick it up as a hobby on my free time. This program is written in C++ with SDL libraries. Im starting simple so theres no real "game" invovled yet. Im just trying to get my character walking correctly.

So far I have been able to set character,tiles, and have my collisions working. The movement of the character functions, but for some reason when hitting the left arrow key, the character switches direction from left to right over and over while moving left, instead of just facing left.

Its odd to me that it only does this when i try to move left. All my code seems identical for all directions, so maybe its within my main()? The compiler isnt reporting any bugs which leaves me to believe my syntax and logic are correct. Im really stumped on this one.

If you could help figure out this problem great, but I would also welcome any other CONSTRUCTIVE criticism as well. Here is my code:

Spoiler


*EDIT*
Added spoiler tags, click to view code.

This post has been edited by stayscrisp: 24 January 2011 - 05:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: SDL 2D tile based game - need help with character walking

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

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

Re: SDL 2D tile based game - need help with character walking

Posted 24 January 2011 - 05:01 AM

Wow! is all that code in one file!

You will find things much easier if you split some of your code into header and cpp files. I can see many ways your code could be split up but you might want to start with taking out your timer class and putting it into its own header and cpp file and also your dot class.

In case you are unfamiliar with the concept of header and cpp files here is an example, using your timer class:

Timer.h (the header file)
#ifndef TIMER_H
#define TIMER_H // remember to use include guards

//The timer
class Timer
{
    private:
    //The clock time when the timer started
    int startTicks;

    //The ticks stored when the timer was paused
    int pausedTicks;

    //The timer status
    bool paused;
    bool started;

    public:
    //Initializes variables
    Timer();

    //The various clock actions
    void start();
    void stop();
    void pause();
    void unpause();

    //Gets the timer's time
    int get_ticks();

    //Checks the status of the timer
    bool is_started();
    bool is_paused();
};

#endif



Notice that the header file includes all of the declarations for your class and its member functions.

Timer.cpp (the implementation file)
#include "Timer.h"
#include <SDL.h>

Timer::Timer()
{
    //Initialize the variables
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
}

void Timer::start()
{
    //Start the timer
    started = true;

    //Unpause the timer
    paused = false;

    //Get the current clock time
    startTicks = SDL_GetTicks();
}

void Timer::stop()
{
    //Stop the timer
    started = false;

    //Unpause the timer
    paused = false;
}

void Timer::pause()
{
    //If the timer is running and isn't already paused
    if( ( started == true ) && ( paused == false ) )
    {
        //Pause the timer
        paused = true;

        //Calculate the paused ticks
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}

void Timer::unpause()
{
    //If the timer is paused
    if( paused == true )
    {
        //Unpause the timer
        paused = false;

        //Reset the starting ticks
        startTicks = SDL_GetTicks() - pausedTicks;

        //Reset the paused ticks
        pausedTicks = 0;
    }
}

int Timer::get_ticks()
{
    //If the timer is running
    if( started == true )
    {
        //If the timer is paused
        if( paused == true )
        {
            //Return the number of ticks when the timer was paused
            return pausedTicks;
        }
        else
        {
            //Return the current time minus the start time
            return SDL_GetTicks() - startTicks;
        }
    }

    //If the timer isn't running
    return 0;
}

bool Timer::is_started()
{
    return started;
}

bool Timer::is_paused()
{
    return paused;
}



The cpp file includes the implementation of your class.

I will have a look at the issue you mentioned in your post but I would recommend that you do not put all of your code into your main.cpp file (It makes things hard to read).

Hope that helps a little :)
Was This Post Helpful? 0
  • +
  • -

#3 slwilson10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-January 11

Re: SDL 2D tile based game - need help with character walking

Posted 24 January 2011 - 04:50 PM

Thank you for the criticism. I actually have it split up but put it into one file because I thought it might be easier to post. I guess it is sort of hard to read =/
Please let me know if you have any ideas with the crazy walking deal.
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

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

Re: SDL 2D tile based game - need help with character walking

Posted 25 January 2011 - 10:46 AM

Hey, could you post the sprite sheets and maps you are using? Just so I can see what you see.

I have to ask though, how much of this code is yours? It seems like you have just directly copied from the lazy foo tutorials. Those tutorials are a great source of information but they are supposed to be used as a learning tool not a place to get source code.

If you took a little more time to write out your own version of the tutorials you would understand them a lot better and small issues, like you are experiencing, would be much easier to fix. At the very least you should be creating your own variable and class names with your own comments on how they work.

This post has been edited by stayscrisp: 25 January 2011 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 slwilson10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-January 11

Re: SDL 2D tile based game - need help with character walking

Posted 25 January 2011 - 06:20 PM

You are absolutely right. I realize I probably shouldve started from scratch, but since I am strictly using this as a tool to learn. I thought it best to use the lazy foo tutorials as a bases. Out of all the tutorials Ive seen online, the lazy foo tutorials are the most clear to me. So I figured I would take them as a basis and expand upon them with my own code. Probably not the best way to do it, but its really difficult to write my own code out of thin air. I learn best from example. And once I have a working knowledge of this code. I can use it to possibly make a game of my own in the future.

Here are the images Im using. And before you say it, I know these are rip offs too ;) Just thought Id save time instead of spending tons of time on graphics.

Attached image(s)

  • Attached Image
  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1