Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6359377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:30:36+00:00 2026-05-24T23:30:36+00:00

Right now when i start my game am making in C++ i walk left

  • 0

Right now when i start my game am making in C++ i walk left right up or down.. But the character just slides doesn’t look like he’s walking.. And i have all the pictures already loaded into my game and they are working.. But i don’t know how i would solve it.. Thing i can’t figure out is how to make the picture change when u hold the button..

This is in allegro by the way..

Here is my code for the drawing player:

void Player::Draw(BITMAP *Buffer){
    draw_sprite(Buffer, Oskar[picNumber], x, y);
}

Oskar[] is the name of the Array with all the pictures..

Here is what changes the picture for the character when u press the buttons:

void Player::Controls(){

if(key[KEY_RIGHT]){
    velocityX = speed;
    picNumber = 6;
}

else if(key [KEY_LEFT]){
    velocityX = -speed;
    picNumber = 9;
}

else{
    velocityX = 0;
}

if(key [KEY_UP]){
    velocityY = -speed;
    picNumber = 3;
}
else if(key [KEY_DOWN]){
    velocityY = speed;
    picNumber = 0;
}
else{
    velocityY = 0;
}

x += velocityX;
y += velocityY;
}

Its all about the variable i created picNumber.. All pictures i have is in an Array and the picNumber is represents what picture to be drawn..
Would be nice to get some help on this.. I’ve been thinking all day about this..

EDIT

#include "Player.h"
#include "Global.h"
#include <allegro.h>


Player::Player(){

}


Player::~Player(){

}

void Player::Init(){
        x = 10;
        y = 10;
        velocityX = 0;
        velocityY = 0;
        speed = 1;
        picNumber = x % MAXPICS;

        OskarFront[0] = load_bitmap("Character\\OskarFront.bmp", NULL);
        OskarFront[1] = load_bitmap("Character\\OskarStanding.bmp", NULL);
        OskarFront[2] = load_bitmap("Character\\OskarFront2.bmp", NULL);

        OskarBack[0] = load_bitmap("Character\\OskarBack.bmp", NULL);
        OskarBack[1] = load_bitmap("Character\\OskarStandingBack.bmp", NULL);
        OskarBack[2] = load_bitmap("Character\\OskarBack2.bmp", NULL);

        OskarRight[0] = load_bitmap("Character\\Oskar1.bmp", NULL);
        OskarRight[1] = load_bitmap("Character\\Oskar.bmp", NULL);
        OskarRight[2] = load_bitmap("Character\\Oskar2.bmp", NULL);

        OskarLeft[0] = load_bitmap("Character\\OskarLeft.bmp", NULL);
        OskarLeft[1] = load_bitmap("Character\\OskarLeftStand.bmp", NULL);
        OskarLeft[2] = load_bitmap("Character\\OskarLeft2.bmp", NULL);
}

void Player::Update(){
        Player::Controls();
}

void Player::Draw(BITMAP *Buffer){

        if(walkingRight == true){
                draw_sprite(Buffer, OskarRight[picNumber], x, y);
        }

        else if(walkingLeft == true){
                draw_sprite(Buffer, OskarLeft[picNumber], x, y);
        }

        else if(walkingFront == true){
                draw_sprite(Buffer, OskarFront[picNumber], x, y);
        }

        else if(walkingBack == true){
                draw_sprite(Buffer, OskarBack[picNumber], x, y);
        }

        else{
                draw_sprite(Buffer, OskarFront[1], x, y);
        }
}

void Player::Controls(){

        if(key[KEY_RIGHT]){
                velocityX = speed;
                walkingRight = true;
        }

        else if(key [KEY_LEFT]){
                velocityX = -speed;
                walkingLeft = true;
        }

        else{
                walkingRight = false;
                walkingLeft = false;
                velocityX = 0;
        }

        if(key [KEY_UP]){
                velocityY = -speed;
                walkingFront = true;
        }
        else if(key [KEY_DOWN]){
                velocityY = speed;
                walkingBack = true;
        }
        else{
                velocityY = 0;
                walkingFront = false;
                walkingBack = false;
        }

        x += velocityX;
        y += velocityY;
}

here is now the new full code i typed after getting help here.. Its now not working when am walking up its showing the front picture and am walking down the up picture is showing.. But left and right works.. Also its not changing picture like animation..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:30:37+00:00Added an answer on May 24, 2026 at 11:30 pm

    You only have one index for each direction of movement!

    Assuming you have drawn about 3 (left leg forward, overlap, right leg forward) you would need to increment the index with time or movement as the march progresses.

    Interesting how far Allegro has progressed- a decade ago it was mainly DOS graphics (I used it with DJGPP) and had a note from the author living in a London flat. How austere I thought, only now do I see how far off of London prices we provincials are.

    In answer to your comment, try something like:

    #define MAXPICS 3
    BITMAP *OskarWalksLeft[MAXPICS];
    BITMAP *OskarWalksRight[MAXPICS];
    picNumber = x % MAXPICS;
    if (facingLeft) {
        draw_sprite(Buffer, OskarWalksLeft[picNumber], x, y);
    } else //facingRight
        draw_sprite(Buffer, OskarWalksRight[picNumber], x, y);
    

    or if you don’t want a new frame per pixel but every 3 or so try replacing

    picNumber = x % MAXPICS;
    

    with

    picNumber = (x / 3)  % MAXPICS;
    

    I’ve tried tidying up your code a little and come up with this, I don’t have allegro so couldn’t test it:

    #include "Player.h"
    #include "Global.h"
    #include <allegro.h>
    
    Player::Player(){}
    
    Player::~Player(){}
    
    enum playerDir {left, right, front, back, stationary} direction;
    BITMAP *Oskar[stationary][3];
    
    void Player::Init(){
        x = 10;
        y = 10;
        velocityX = 0;
        velocityY = 0;
        speed = 1;
        picNumber = x % MAXPICS;
    
        char filename[256];
        const char *dirStrs[] = {"left","right","front","back"};
        for (playerDir i = left; i < stationary; (int)i = (int)i + 1)
            for (j = 0; j < 3; ++j) {
                strcpy(filename, "Character\\Oskar");
                strcat(filename, dirStrs[i]);
                strcat(filename, itoa(j));
                strcat(filename, ".bmp");
                Oskar[i][j] = load_bitmap(filename, NULL);
            }
    }
    
    void Player::Update(){Player::Controls();}
    void Player::Draw(BITMAP *Buffer){
        switch (direction) {
            case left :
                    draw_sprite(Buffer, OskarLeft[picNumber], x, y);
                    break;
            case right :
                    draw_sprite(Buffer, OskarRight[picNumber], x, y);
                    break;
            case front :
                    draw_sprite(Buffer, OskarFront[picNumber], x, y);
                    break;
            case back :
                    draw_sprite(Buffer, OskarBack[picNumber], x, y);
                    break;
            default:
                    draw_sprite(Buffer, OskarFront[1], x, y);
            }
    }
    
    void Player::Controls(){
        direction = stationary;
        if(key[KEY_RIGHT]){
            velocityX = speed;
            direction = right;
        } else if(key [KEY_LEFT]){
            velocityX = -speed;
            direction = left;
        } else velocityX = 0;
    
        if(key [KEY_UP]){
            velocityY = -speed;
            direction = front;
        } else if(key [KEY_DOWN]){
            velocityY = speed;
            direction = back;
        } else velocityY = 0;
    
        x += velocityX;
        y += velocityY;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a game, so I want the JFrame to start maximized, but
Right now I'm trying make some browser based game. But I have little questions.
I'm learned php as functional and procedure language. Right now try to start learn
Right now in sublime text 2 when I start an if statement in Coldfusion
Right now I have this SQL query which is valid but always times out:
I use this constructor to make objects for a game. Right now, the series
Right now the setup for my javascript chat works so it's like function getNewMessage()
I came up with a problem right now when my game is almost finish.
I am working on a game for android right now. It is a game
I'm working on a Pong game for my portfolio right now in Java, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.