Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 More Efficient way to do code - Allegro
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DrummaBoyFB




PostPosted: Wed May 05, 2010 4:44 pm   Post subject: More Efficient way to do code - Allegro

hi i am making a game in allegro but it seems to be getting a little out of control, I wanted to know if you could give me a more efficient way to code this. I know I could use functions for all the blitting and stuff, but what else could I do, especially for inputting
the sprites (pictures). I have created an illusion of screen scrolling by moving the background, but I wanted to know another way to do it (the proper way). My teacher said it could be done with 2D arrays but i can't figured out how you can do it with 2d arrays . anywho, I just want to kno how the professionals and stuff do screen scrolling or any better way to do it. The last thing I want to know it about collision detection, i know how to make things collide and all but as you can see in my code all the walking and stuff is done with a loop therefore the collision doesn't happen until after the loop, will I have to continuously add the collision to the loop or is there a better and easier way?
Here's my code: (thanks in advance)
c++:

//Character.h
#pragma once
#include <allegro.h>
#include "game.h"

class character{
public:
character ();
int a,aa;
int c;
void boundary ();
void superattacks();
void attacking ();
void intro ();
void walkR ();
void finishwalkR();
void walkL ();
void walking();
void dashing();
void solid ();
void healthbar ();
void enemyattack ();

BITMAP *background;
BITMAP *buffer;
BITMAP *walk [12];
BITMAP *walkl [12];
BITMAP *attack [10];
BITMAP *attackl [10];
BITMAP *superattack [10];
BITMAP *dash [10];
BITMAP *dashl[10];
BITMAP *buffer2;
BITMAP *face;

int eay,eax,eax2,eay2;
bool reset;
int startw,screenw,screenh;
int yy;
bool movebackground;
int moving;
int dir ;
int cl;
int x;
int y;
bool t ;
int i ;
bool stepcheck;
};

// character.cpp (its long)
        #pragma once
        #include <allegro.h>
        #include "character.h"
        #include "game.h"


void character::healthbar (){
BITMAP *face = load_bitmap ("F:\\sprites\\face.bmp",NULL);
circle (screen,500,50,40,makecol (0,184,239));
circle (screen,500,50,30,makecol (0,0,255));
line (screen,460,50,469,50,makecol (0,184,239));
line (screen,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (screen,470,30,makecol (0,255,0));
draw_sprite (screen,face,478,30);
}


        character::character (){
        eay =0;
        eax =0;
        eax2=0;
        eay2=0;
                face = load_bitmap ("F:\\sprites\\face.bmp",NULL);
                character::buffer2 = create_bitmap (640,480);
                reset = false;
                startw = 0;
                screenw = 640;
                screenw = 480;
                moving = 0;
        dir = 0;
        x =     10;
        y = 325;
        t = false;
        i = 0;
        a = -1;
        aa = -1;
        stepcheck = false;
        movebackground = false;
        // walking right sprites
        background = load_bitmap ("F:\\sprites\\background2.bmp",NULL);
        walk[0] = load_bitmap ("F:\\sprites\\walkR.bmp",NULL);
        walk[1] = load_bitmap ("F:\\sprites\\walkR2.bmp",NULL);
        walk[2] = load_bitmap ("F:\\sprites\\walkR3.bmp",NULL);
        walk[3] = load_bitmap ("F:\\sprites\\walkR4.bmp",NULL);
        walk[4] = load_bitmap ("F:\\sprites\\walkR5.bmp",NULL);
        walk[5] = load_bitmap ("F:\\sprites\\walkR6.bmp",NULL);
        walk[6] = load_bitmap ("F:\\sprites\\walkR7.bmp",NULL);
        walk[7] = load_bitmap ("F:\\sprites\\walkR8.bmp",NULL);
        walk[8] = load_bitmap ("F:\\sprites\\walkR9.bmp",NULL);
        walk[9] = load_bitmap ("F:\\sprites\\walkR10.bmp",NULL);
        walk[10] = load_bitmap ("F:\\sprites\\walkR11.bmp",NULL);
        walk[11] =load_bitmap ("F:\\sprites\\standR.bmp",NULL);
        c= -1;
        cl = -1;
        // walking left sprites
        walkl[0] = load_bitmap ("F:\\sprites\\walkL.bmp",NULL);
        walkl[1] = load_bitmap ("F:\\sprites\\walkL2.bmp",NULL);
        walkl[2] = load_bitmap ("F:\\sprites\\walkL3.bmp",NULL);
        walkl[3] = load_bitmap ("F:\\sprites\\walkL4.bmp",NULL);
        walkl[4] = load_bitmap ("F:\\sprites\\walkL5.bmp",NULL);
        walkl[5] = load_bitmap ("F:\\sprites\\walkL6.bmp",NULL);
        walkl[6] = load_bitmap ("F:\\sprites\\walkL7.bmp",NULL);
        walkl[7] = load_bitmap ("F:\\sprites\\walkL8.bmp",NULL);
        walkl[8] = load_bitmap ("F:\\sprites\\walkL9.bmp",NULL);
        walkl[9] = load_bitmap ("F:\\sprites\\walkL10.bmp",NULL);
        walkl[10] = load_bitmap ("F:\\sprites\\walkL11.bmp",NULL);
        walkl[11] = load_bitmap ("F:\\sprites\\standL.bmp",NULL);

        // attacking right sprites
        attack[0] = load_bitmap ("F:\\sprites\\attackR.bmp",NULL);
        attack[1] = load_bitmap ("F:\\sprites\\attackR2.bmp",NULL);
        attack[2] = load_bitmap ("F:\\sprites\\attackR3.bmp",NULL);
        attack[3] = load_bitmap ("F:\\sprites\\attackR4.bmp",NULL);
        attack[4] = load_bitmap ("F:\\sprites\\attackR5.bmp",NULL);
        attack[5] = load_bitmap ("F:\\sprites\\attackR6.bmp",NULL);
        attack[6] = load_bitmap ("F:\\sprites\\attackR7.bmp",NULL);
        attack[7] = load_bitmap ("F:\\sprites\\attackR8.bmp",NULL);
        attack[8] = load_bitmap ("F:\\sprites\\attackR9.bmp",NULL);
        attack[9] = load_bitmap ("F:\\sprites\\attackR10.bmp",NULL);

        attackl [0] = load_bitmap ("F:\\sprites\\attackL.bmp",NULL);
        attackl [1] = load_bitmap ("F:\\sprites\\attackL2.bmp",NULL);
        attackl [2] = load_bitmap ("F:\\sprites\\attackL3.bmp",NULL);
        attackl [3] = load_bitmap ("F:\\sprites\\attackL4.bmp",NULL);
        attackl [4] = load_bitmap ("F:\\sprites\\attackL5.bmp",NULL);
        attackl [5] = load_bitmap ("F:\\sprites\\attackL6.bmp",NULL);
        attackl [6] = load_bitmap ("F:\\sprites\\attackL7.bmp",NULL);
        attackl [7] = load_bitmap ("F:\\sprites\\attackL8.bmp",NULL);
        attackl [8] = load_bitmap ("F:\\sprites\\attackL9.bmp",NULL);
        attackl [9] = load_bitmap ("F:\\sprites\\attackL10.bmp",NULL);

        // super attack sprites
        superattack [0] = load_bitmap ("F:\\sprites\\superattack1.bmp",NULL);
        superattack [1] = load_bitmap ("F:\\sprites\\superattack2.bmp",NULL);
        superattack [2] = load_bitmap ("F:\\sprites\\superattack3.bmp",NULL);
        superattack [3] = load_bitmap ("F:\\sprites\\superattack4.bmp",NULL);
        superattack [4] = load_bitmap ("F:\\sprites\\superattack5.bmp",NULL);
        superattack [5] = load_bitmap ("F:\\sprites\\superattack6.bmp",NULL);
        superattack [6] = load_bitmap ("F:\\sprites\\superattack7.bmp",NULL);
        superattack [7] = load_bitmap ("F:\\sprites\\superattack8.bmp",NULL);
        superattack [8] = load_bitmap ("F:\\sprites\\superattack9.bmp",NULL);
        superattack [9] = load_bitmap ("F:\\sprites\\superattack10.bmp",NULL);

        // dash sprite right
        dash [0] = load_bitmap ("F:\\sprites\\dashR.bmp",NULL);
        dash [1] = load_bitmap ("F:\\sprites\\dashR3.bmp",NULL);
        dash [2] = load_bitmap ("F:\\sprites\\dashR4.bmp",NULL);
        dash [3] = load_bitmap ("F:\\sprites\\dashR5.bmp",NULL);
        dash [4] = load_bitmap ("F:\\sprites\\dashR6.bmp",NULL);
        dash [5] = load_bitmap ("F:\\sprites\\dashR7.bmp",NULL);
        dash [6] = load_bitmap ("F:\\sprites\\dashR8.bmp",NULL);
        dash [7] = load_bitmap ("F:\\sprites\\dashR9.bmp",NULL);
        dash [8] = load_bitmap ("F:\\sprites\\dashR10.bmp",NULL);
        dash [9] = load_bitmap ("F:\\sprites\\dashR11.bmp",NULL);

        //dash sprite left
        dashl [0] = load_bitmap ("F:\\sprites\\dashL.bmp",NULL);
        dashl [1] = load_bitmap ("F:\\sprites\\dashL3.bmp",NULL);
        dashl [2] = load_bitmap ("F:\\sprites\\dashL4.bmp",NULL);
        dashl [3] = load_bitmap ("F:\\sprites\\dashL5.bmp",NULL);
        dashl [4] = load_bitmap ("F:\\sprites\\dashL6.bmp",NULL);
        dashl [5] = load_bitmap ("F:\\sprites\\dashL7.bmp",NULL);
        dashl [6] = load_bitmap ("F:\\sprites\\dashL8.bmp",NULL);
        dashl [7] = load_bitmap ("F:\\sprites\\dashL9.bmp",NULL);
        dashl [8] = load_bitmap ("F:\\sprites\\dashL10.bmp",NULL);
        dashl [9] = load_bitmap ("F:\\sprites\\dashL11.bmp",NULL);

        // creating the buffer
        buffer = create_bitmap (640,480);
        }

        void character::solid(){

        }

        void character::boundary () {
        if (x <= 0)
        x = 0;
        if (x >= 670)
        x = 670;
        if (y <= 0)
        y = 0;
        if (y >= 470)
                y = 470;
        }


        void character::intro ()
        {
                i ++;
                if (i == 11)
                i =0;

                if (x >= 630)
        {t = true;
        i = 0;}
        if (x <= 0)
        {t = false;
        i = 0;}

        if (t == false)
        {       x += 5;
                draw_sprite (buffer,walk[i],x,y);
        }
        if (t == true)
        {       x -= 5;
                draw_sprite (buffer,walkl[i],x,y);
        }

                blit (buffer,screen,0,0,0,0,640,480);
                rest (50);
        }

        // beginnning of hte player walking (screen scrolling included.
        void character::walking(){
                        if (key[KEY_RIGHT]){
                        bool t_f = false;
                                if (reset == false){
        screenh = 640;
        screenw = 480;
        startw = 0;
       
        reset = true;
        }
        else{

                startw = startw;
        screenh = screenh;
        screenw = screenw;}
                       
        int b = -1; // the counter
        dir = 0; // DIRECTION IS RIGHT
        while (b <5) {

                if (x >= 300)
        {
                screenw += 10;
                startw += 5;
        }
        else
        x += 5;
                b++;
                c++;
if (c == 11)
c = 0;
        rest (80);


blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
draw_sprite (buffer,walk [c],x,y);
blit (buffer,screen,0,0,0,0,640,480);
       
        // end of stepping animation
        }
                                        if (!key [KEY_RIGHT]) {

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
draw_sprite (buffer,walk [11],x,y);
        blit (buffer,screen,0,0,0,0,640,480);
        } // end of stop stepping - RIGHT
                        }
       

        if (key [KEY_LEFT]){
if (reset == false){
screenh = 640;
screenw = 480;
startw = 0;

reset = true;
}
else{

startw = startw;
screenh = screenh;
screenw = screenw;}

        int b = -1; // the counter
        dir = 180; // DIRECTION IS LEFT
       
        while (b <5) {

        if (x >= 300)
                        startw -= 10;
                else
                        x -= 5;
                if (startw <= 0 && x >= 300)
                        x = 299;
               
                b++;
                c++;
       
                if (c ==11)
                c= 0;
                if (x <= 0)
                x = 0;
                if (startw <= 0)
                startw = 0;
                rest (80);
               

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
                draw_sprite (buffer,walkl [c],x,y);
        blit (buffer,screen,0,0,0,0,640,480);
        } // end of stepping animation -left
       
        if (!key [KEY_LEFT]) {

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
        draw_sprite (buffer,walkl [11],x,y);
        blit (buffer,screen,0,0,0,0,640,480);
        } // end of stop stepping - LEFT
        }
        }

        void character::attacking (){
                        if (key [KEY_X] && dir == 0){
                                if (reset == false){
        screenh = 640;
        screenw = 480;
        startw = 0;
        reset = true;
        }
        else{
        startw = startw;
        screenh = screenh;
        screenw = screenw;}
                                a = -1
                                int d = -1;
                y -= 12;
                                while (d < 9){
                        d++;
        a ++;
        if (a == 10)
                a = 0 ;
        rest (80);

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
        draw_sprite (buffer,attack[a],x,y);
        blit (buffer,screen,0,0,0,0,640,480);
                        } // end of attack animation while loop
                y += 12;// lowering the player
                       

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);       
        draw_sprite(buffer,walk[11],x,y);
                blit (buffer,screen,0,0,0,0,640,480);
                        }
                        if (key [KEY_X]&&dir == 180){
                                a = -1;  
                                int d = -1;
                y -= 12;
                                while (d < 9){
                        d++;
        a ++;
        if (a == 10)
                a = 0 ;

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
        draw_sprite (buffer,attackl[a],x,y);
                blit (buffer,screen,0,0,0,0,640,480);
                        rest (80);
                        }
                y += 12;

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
                                draw_sprite(buffer,walkl[11],x,y);
                blit (buffer,screen,0,0,0,0,640,480);
                        }
               
        }                     

        void character::superattacks(){
        if (key [KEY_Z]){
                if (reset == false){
        screenh = 640;
        screenw = 480;
        startw = 0;
        reset = true;
        }
        else{
        startw = startw;
        screenh = screenh;
        screenw = screenw;}
                int d = 4;
                y -= 60;
                while (key[KEY_Z])
        {
        aa ++;
        if (aa == 4)
        aa = 0;

        rest (80);

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
        draw_sprite (buffer,superattack[aa],x,y);
                blit (buffer,screen,0,0,0,0,640,480);
               
                        }
                int yy = 0;
                while (d <9){
                        d ++;
               
                        y += 5;
       

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
                draw_sprite (buffer,superattack[d],x,y);
                blit (buffer,screen,0,0,0,0,640,480);
                        rest (80);
       
                        }
                y += 35;
       

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
        draw_sprite(buffer,walk[11],x,y);
                blit (buffer,screen,0,0,0,0,640,480);
        }
        }                     


        void character::dashing () {
        if (key[KEY_D]){
        dir = 0;
        if (reset == false){
        screenh = 640;
        screenw = 480;
        startw = 0;
        reset = true;
        }
        else{
        startw = startw;
        screenh = screenh;
        screenw = screenw;}

        while (key[KEY_D]){
        if (x >= 300)
        {
                screenw += 10;
                startw += 10;
        }
        else
        x += 10;

        rest (80);

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
        draw_sprite (buffer,dash [1],x,y);
        blit (buffer,screen,0,0,0,0,640,480);
        } // end dashing right while loop
        int count = 1;
        while (count < 9) {
        count ++;
        startw += 10 - count;   
       
        rest (80);

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
        draw_sprite (buffer,dash [count],x,y - 8);
        blit (buffer,screen,0,0,0,0,640,480);
        } // end of dash landing while loop
       

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
        draw_sprite (buffer,walk [11],x,y);
        blit (buffer,screen,0,0,0,0,640,480);
        } // end if statement - pressing d, dir = 0

        // beginning of dashing left
        if (key[KEY_A])
        {
        if (reset == false){
        screenh = 640;
        screenw = 480;
        startw = 0;
        dir = 180;
        reset = true;
        }
        else{
        dir = 180;
                startw = startw;
        screenh = screenh;
        screenw = screenw;}

                while (key[KEY_A]){
                if (x >= 300)
                        startw -= 10;
                else
                        x -= 10;
                if (startw <= 0 && x >= 300)
                        x = 299;
                rest (80);
                if (x <= 0)
                x = 0;
                if (startw <= 0)
                startw = 0;
               

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
                draw_sprite (buffer,dashl [1],x,y);
                blit (buffer,screen,0,0,0,0,640,480);
                } // end of while dash loop - LEFT
                int count = 1;
                while (count < 9){
                        count ++;
                        startw -= 10 - count;
                        if (startw <= 0)
                        startw = 0;
                        rest (80);

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
                        draw_sprite (buffer,dashl [count],x,y -8);
        blit (buffer,screen,0,0,0,0,640,480);
                } // end of landing loop - LEFT

blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
        draw_sprite (buffer,walkl [11],x,y);
        blit (buffer,screen,0,0,0,0,640,480);
        }       // end of if dash if statement - LEFT

        }// end of void dashing

        void character::enemyattack () {
        if (key [KEY_T]) {
        eay += 5;


        blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,255,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460-21,50,500,50,makecol (0,0,0));
line (buffer,460 -21 + eax,50 -eay,500 + eax2,50 - eay2,makecol (0,0,0));
floodfill (buffer,465,49,makecol (255,0,255));
//floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
blit (buffer,screen,0,0,0,0,640,480);
        rest (200);
//      while (key [KEY_T]) {
//      if (eay < 20)
//            eay += 5;
//      else
//            eay += 10;
//      if (eay % 5 == 0)
//      eax += 1;
//     
//      blit (background,buffer,startw,0,0,0,640,480);
//circle (buffer,500,50,40,makecol (0,184,239));
//circle (buffer,500,50,30,makecol (0,0,255));
//line (buffer,460,50,469,50,makecol (0,184,239));
//line (buffer,460+ eax,50 -eay,469 + eax2,50 - eay2,makecol (0,184,239));
////floodfill (buffer,500,50,makecol (0,0,255));
////floodfill (buffer,470,30,makecol (0,255,0));
//draw_sprite (buffer,face,478,30);
//blit (buffer,screen,0,0,0,0,640,480);
//
//     
//      }
        }
        }



//enemy.h (just started it)
#pragma once
#include <allegro.h>
#include "character.h"
#include "game.h"

class enemy {
public:
        enemy ();
        BITMAP *combo [35];
        BITMAP *combol [35];
        BITMAP *buffer;
        void moving (character &xx);
        //int distance;
int x;
        int y;
};

//enemy.cpp (just started)
#pragma once
#include <allegro.h>
#include "character.h"
#include "game.h"
#include "enemy.h"

enemy::enemy () {
combo [0] = load_bitmap ("F:\\enemysprites\\redcomboR.bmp",NULL);
combo [1] = load_bitmap ("F:\\enemysprites\\redcomboR2.bmp",NULL);
combo [2] = load_bitmap ("F:\\enemysprites\\redcomboR3.bmp",NULL);
combo [3] = load_bitmap ("F:\\enemysprites\\redcomboR4.bmp",NULL);
combo [4] = load_bitmap ("F:\\enemysprites\\redcomboR5.bmp",NULL);
combo [5] = load_bitmap ("F:\\enemysprites\\redcomboR6.bmp",NULL);
combo [6] = load_bitmap ("F:\\enemysprites\\redcomboR7.bmp",NULL);
combo [7] = load_bitmap ("F:\\enemysprites\\redcomboR8.bmp",NULL);
combo [8] = load_bitmap ("F:\\enemysprites\\redcomboR9.bmp",NULL);
combo [9] = load_bitmap ("F:\\enemysprites\\redcomboR10.bmp",NULL);
combo [10] = load_bitmap ("F:\\enemysprites\\redcomboR11.bmp",NULL);
combo [11] = load_bitmap ("F:\\enemysprites\\redcomboR12.bmp",NULL);
combo [12] = load_bitmap ("F:\\enemysprites\\redcomboR13.bmp",NULL);
combo [13] = load_bitmap ("F:\\enemysprites\\redcomboR14.bmp",NULL);
combo [14] = load_bitmap ("F:\\enemysprites\\redcomboR15.bmp",NULL);
combo [15] = load_bitmap ("F:\\enemysprites\\redcomboR16.bmp",NULL);
combo [16] = load_bitmap ("F:\\enemysprites\\redcomboR17.bmp",NULL);
combo [17] = load_bitmap ("F:\\enemysprites\\redcomboR18.bmp",NULL);
combo [18] = load_bitmap ("F:\\enemysprites\\redcomboR19.bmp",NULL);
combo [19] = load_bitmap ("F:\\enemysprites\\redcomboR20.bmp",NULL);
combo [20] = load_bitmap ("F:\\enemysprites\\redcomboR21.bmp",NULL);
combo [21] = load_bitmap ("F:\\enemysprites\\redcomboR22.bmp",NULL);
combo [22] = load_bitmap ("F:\\enemysprites\\redcomboR23.bmp",NULL);
combo [23] = load_bitmap ("F:\\enemysprites\\redcomboR24.bmp",NULL);
combo [24] = load_bitmap ("F:\\enemysprites\\redcomboR25.bmp",NULL);
combo [25] = load_bitmap ("F:\\enemysprites\\redcomboR26.bmp",NULL);
combo [26] = load_bitmap ("F:\\enemysprites\\redcomboR27.bmp",NULL);
combo [27] = load_bitmap ("F:\\enemysprites\\redcomboR28.bmp",NULL);
combo [28] = load_bitmap ("F:\\enemysprites\\redcomboR29.bmp",NULL);
combo [29] = load_bitmap ("F:\\enemysprites\\redcomboR30.bmp",NULL);
combo [30] = load_bitmap ("F:\\enemysprites\\redcomboR31.bmp",NULL);
combo [31] = load_bitmap ("F:\\enemysprites\\redcomboR32.bmp",NULL);
combo [32] = load_bitmap ("F:\\enemysprites\\redcomboR33.bmp",NULL);
combo [33] = load_bitmap ("F:\\enemysprites\\redcomboR34.bmp",NULL);
combo [34] = load_bitmap ("F:\\enemysprites\\redcomboR35.bmp",NULL);

// facing left combo
combol [0] = load_bitmap ("F:enemysprites\\redcomboL.bmp",NULL);
combol [1] = load_bitmap ("F:\\enemysprites\\redcomboL2.bmp",NULL);
combol [2] = load_bitmap ("F:\\enemysprites\\redcomboL3.bmp",NULL);
combol [3] = load_bitmap ("F:\\enemysprites\\redcomboL4.bmp",NULL);
combol [4] = load_bitmap ("F:\\enemysprites\\redcomboL5.bmp",NULL);
combol [5] = load_bitmap ("F:\\enemysprites\\redcomboL6.bmp",NULL);
combol [6] = load_bitmap ("F:\\enemysprites\\redcomboL7.bmp",NULL);
combol [7] = load_bitmap ("F:\\enemysprites\\redcomboL8.bmp",NULL);
combol [8] = load_bitmap ("F:\\enemysprites\\redcomboL9.bmp",NULL);
combol [9] = load_bitmap ("F:\\enemysprites\\redcomboL10.bmp",NULL);
combol [10] = load_bitmap ("F:\\enemysprites\\redcomboL11.bmp",NULL);
combol [11] = load_bitmap ("F:\\enemysprites\\redcomboL12.bmp",NULL);
combol [12] = load_bitmap ("F:\\enemysprites\\redcomboL13.bmp",NULL);
combol [13] = load_bitmap ("F:\\enemysprites\\redcomboL14.bmp",NULL);
combol [14] = load_bitmap ("F:\\enemysprites\\redcomboL15.bmp",NULL);
combol [15] = load_bitmap ("F:\\enemysprites\\redcomboL16.bmp",NULL);
combol [16] = load_bitmap ("F:\\enemysprites\\redcomboL17.bmp",NULL);
combol [17] = load_bitmap ("F:\\enemysprites\\redcomboL18.bmp",NULL);
combol [18] = load_bitmap ("F:\\enemysprites\\redcomboL19.bmp",NULL);
combol [19] = load_bitmap ("F:\\enemysprites\\redcomboL20.bmp",NULL);
combol [20] = load_bitmap ("F:\\enemysprites\\redcomboL21.bmp",NULL);
combol [21] = load_bitmap ("F:\\enemysprites\\redcomboL22.bmp",NULL);
combol [22] = load_bitmap ("F:\\enemysprites\\redcomboL23.bmp",NULL);
combol [23] = load_bitmap ("F:\\enemysprites\\redcomboL24.bmp",NULL);
combol [24] = load_bitmap ("F:\\enemysprites\\redcomboL25.bmp",NULL);
combol [25] = load_bitmap ("F:\\enemysprites\\redcomboL26.bmp",NULL);
combol [26] = load_bitmap ("F:\\enemysprites\\redcomboL27.bmp",NULL);
combol [27] = load_bitmap ("F:\\enemysprites\\redcomboL28.bmp",NULL);
combol [28] = load_bitmap ("F:\\enemysprites\\redcomboL29.bmp",NULL);
combol [29] = load_bitmap ("F:\\enemysprites\\redcomboL30.bmp",NULL);
combol [30] = load_bitmap ("F:\\enemysprites\\redcomboL31.bmp",NULL);
combol [31] = load_bitmap ("F:\\enemysprites\\redcomboL32.bmp",NULL);
combol [32] = load_bitmap ("F:\\enemysprites\\redcomboL33.bmp",NULL);
combol [33] = load_bitmap ("F:\\enemysprites\\redcomboL34.bmp",NULL);
combol [34] = load_bitmap ("F:\\enemysprites\\redcomboL35.bmp",NULL);

buffer = create_bitmap (640,480);
x = 600;
y = 325;

}

void enemy::moving (character &xx) {
//draw_sprite (buffer,combol[34],x,y);
       
        int distanceE;
        int distanceC;
        int counter = -1;
                distanceE = x - xx.x + xx.startw;
        distanceC = xx.x - x;
        if (xx.x >= 299){ //|| xx.x >= x && distanceC <= 20
    while (counter <34){
                distanceE = x - xx.x + xx.startw;
        distanceC = xx.x - x;
                counter += 1;
        blit (xx.background,buffer,xx.startw,0,0,0,640,480);
        circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -xx.eay,469 + xx.eax,50 - xx.eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,xx.face,478,30);
draw_sprite (buffer,combol [counter],x,y);
blit (buffer,screen,0,0,0,0,640,480);
        }
        }
}

// Main.cpp
#pragma once
#include <allegro.h>
#include <iostream>
#include "character.h"
#include "game.h"
#include "enemy.h"
using namespace std;
#define screenw 640
#define screenh 400
volatile long counter = 0;
void IncCtr() {counter ++ ;}
BITMAP *menu [4];
BITMAP *menucover;

void menus ();

int main ()
{
allegro_init ();
install_keyboard ();
install_mouse ();
set_color_depth (24);
set_gfx_mode (GFX_AUTODETECT_WINDOWED,screenw,screenh,0,0);
LOCK_VARIABLE (counter);
LOCK_FUNCTION (IncCtr);
install_int_ex (IncCtr,BPS_TO_TIMER (90));

bool quit = false;

character peter;
enemy akeel;
BITMAP *buffer = create_bitmap (640,480);
rectfill (screen,0,0,640,480,makecol(0,0,0));
blit (peter.background,screen,0,0,0,0,640,480);

//arc (screen,300,50,70,0,20,makecol (0,0,0));
draw_sprite(screen,peter.walk[11],peter.x,peter.y);
draw_sprite(screen,akeel.combol [34],akeel.x,akeel.y);
//BITMAP *background = load_bitmap ("F:\\sprites\\Map1.bmp",NULL);
//blit (background,buffer,0,0,0,0,640,480);
//draw_sprite(buffer,peter.walk[11],peter.x,peter.y);
//blit (buffer,screen,0,0,0,0,640,480);
peter.healthbar();
while (quit == false)
{

while (counter > 0)
{       
clear_keybuf();

if (key[KEY_ESC]){
quit = true;
counter = 3;}
        //rectfill(buffer,0,0,640,480,makecol (0,0,0));
        //peter.intro();



menus ();
peter.walking();
peter.dashing();
peter.attacking();
peter.superattacks();
peter.enemyattack();
akeel.moving (peter);

        if (key[KEY_ESC]){
                counter = 5;
                quit = true;}

counter -= 1;
}
}
return 0;
}
END_OF_MAIN();


void menus () {
if (key[KEY_M]){
menu [0] = load_bitmap ("F:\\sprites\\menu.bmp",NULL);
menu [1] = load_bitmap ("F:\\sprites\\menu2.bmp",NULL);
menu [2] = load_bitmap ("F:\\sprites\\menu3.bmp",NULL);
menu [3] = load_bitmap ("F:\\sprites\\menu4.bmp",NULL);
menucover = load_bitmap ("F:\\sprites\\menucover.bmp",NULL);
blit (menucover,screen,0,0,0,0,640,480);
masked_blit (menu[0],screen,0,0,0,0,640,480);

int i = 0;
while (!key[KEY_P]) {
if (key[KEY_DOWN]) {
rest (200);
i += 1;
if (i >=3)
i = 3;
blit (menucover,screen,0,0,0,0,640,480);       
masked_blit (menu[i],screen,0,0,0,0,640,480);
}
if (key[KEY_UP]) {
        rest (200);
        i -= 1;
if (i <= 0)
i = 0;
blit (menucover,screen,0,0,0,0,640,480);
masked_blit (menu [i],screen,0,0,0,0,640,480);
}
}
}
}


MOD EDIT: Remember those syntax tags!
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Wed May 05, 2010 6:55 pm   Post subject: RE:More Efficient way to do code - Allegro

The proper way to scroll the screen is to move the "camera". Moving the background image is very similar (and what you end up doing for the background), but moving the camera (the left and right components of the rectangle you draw on the window) will also "move" in-game elements.

Your teacher is probably thinking of something else with those 2D arrays. Anything that can be done with 2D arrays can be done with 1D arrays (since memory is a 1D array itself). I can't really think of a good way to apply multidimensional arrays to the problem of screen-scrolling.

You could make this better by splitting it into multiple code files. For each of your classes (such as Character), you should have a Character.h and a Character.cpp . The .h file contains all the method headers and looks something like:
c++:

class ClassName {
  public:
    void foo ();
    int bar ( int param );
  private:
    float member;
};


The .cpp file contains the implementations and looks like:
c++:

#include "ClassName.h"

void ClassName::foo () {
    // implementation of foo
}

int ClassName::bar ( int param ) {
    // implementation of bar
    return (int)(param * member);
}


You would also have a main.cpp, though you don't really need a main.h. That file will contain your int main ( int argc, char ** argv ) definition, and will use #include "ClassName.h" at the top to ensure that it has access to the class named ClassName.


Collision Detection
The standard model for a game is this:
code:

main () {

    // setup game

    bool done = false;

    while ( !done ) {

        // calculate time since we were last at the top of the loop ("interpolation" time)

        // calculate how far things have moved since then, update their positions

        // draw objects on the screen

        // if the quit button was pressed, set done to true

        // if we're locking the framerate, conditionally slow the program down (sleep() or wait() or somesuch)
    }
}


You need to refactor your code so that it follows that standard model before collision detection will be readily accessible. Make sure you save your existing work in a safe backup location in case you accidentally delete something.


Edit: And please, please, indent your code correctly. An open-brace - { - is a hint that the next line should be 1 tab or 4 spaces further in, and a close brace - } - is a hint that the line it's on should be 1 tab / 4 spaces closer to the left edge.


Edit 2, the return of Edit: You should also use for loops and string concatenation to make loading repetitive file names easier. I see at least one place where you do the exact same thing 30-odd times in a row. That should be possible in 3 lines (though I suspect C++ may require more like 7 to do it properly).
CodeMonkey2000




PostPosted: Wed May 05, 2010 7:03 pm   Post subject: RE:More Efficient way to do code - Allegro

Oh boy...

From the looks of things, it seems as though you are new to C++. For the redundant string assignments, you may want to look in C++'s string library to enclose everything in a nice for loop. Second thing, you should make an abstract character class, with functions that both a human player and an enemy player have. Then for the human class, inherit from the abstract character class, and same for the enemy class. This will make things easier to manage. Third thing, in main.cpp you have:
c++:

peter.walking();
peter.dashing();
peter.attacking();
peter.superattacks();
peter.enemyattack();
akeel.moving (peter);


This is really bad!! Condense all of these functions into a single function (like peter.think() or something), so that if you ever decide to add any more moves, you don't have to change main.cpp at all. Infact your enemy function should have its own think function.

Last thing you should make a sprite class that will handle things like drawing and loading sprites. Ideally it should work something like this, if I wanted to load the walking right images:
c++:

MySprite.load("F:\\sprites\\walkR",12);//since you have 12 sprites
CodeMonkey2000




PostPosted: Wed May 05, 2010 7:12 pm   Post subject: Re: More Efficient way to do code - Allegro

DrummaBoyFB @ Wed May 05, 2010 4:44 pm wrote:
I have created an illusion of screen scrolling by moving the background, but I wanted to know another way to do it (the proper way). My teacher said it could be done with 2D arrays but i can't figured out how you can do it with 2d arrays .


Yes this is a good way to do it (since collision between the player and map is essentially O(1) ), depending on what type of game it is. It's called 2D tile based collision, I've written a tutorial in turing, but if you understand it, it's trivial to do it in in C++. Also, since the collision is the same for players and enemies, you should do this in the abstract character class. If you don't know what I'm talking about, you should read up on inheritance and object oriented programming.
DrummaBoyFB




PostPosted: Wed May 05, 2010 7:23 pm   Post subject: Re: More Efficient way to do code - Allegro

thanks to all or you for your answers , they really helped.
DrummaBoyFB




PostPosted: Thu May 06, 2010 11:10 am   Post subject: Re: More Efficient way to do code - Allegro

I got everyting you guys said but how do I make the enemy and player move at the same time?
because I got all my animations in loops, so therefore nothing can occur until the loop is over.
I understand that I could probably fork but I really prefer not to do that.

So I wanted to know if I should make the enemy's movements in a function or if there's another way
USEC_OFFICER




PostPosted: Thu May 06, 2010 11:42 am   Post subject: RE:More Efficient way to do code - Allegro

In a word, yes.
DemonWasp




PostPosted: Thu May 06, 2010 12:17 pm   Post subject: RE:More Efficient way to do code - Allegro

Allow me to re-iterate:

code:

loop
    // update positions of objects here
    // -- update the player's position and image
    // -- update the enemy's position and image

    // draw everything
end loop


Everything happens in a loop, and each actor does something ONCE during the loop. This loop should happen several dozen times per second (hopefully at least 30+ times per second, often as much as 100/sec or more), so remember that things will only move a small distance in that time (in fact, they will move (their speed) * (interpolation time) in that time ). Change their image to be exactly what they're doing at that point.
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: Thu May 06, 2010 3:00 pm   Post subject: Re: More Efficient way to do code - Allegro

By having a look at your walking function:
c++:
void character::walking(){
   [...]
        else{

                startw = startw;
        screenh = screenh;
        screenw = screenw;}
                       
        int b = -1; // the counter
        dir = 0; // DIRECTION IS RIGHT
        while (b <5) {

                if (x >= 300)
        {
                screenw += 10;
                startw += 5;
        }
        else
        x += 5;
                b++;
                c++;
if (c == 11)
c = 0;
        rest (80);


blit (background,buffer,startw,0,0,0,640,480);
circle (buffer,500,50,40,makecol (0,184,239));
circle (buffer,500,50,30,makecol (0,0,255));
line (buffer,460,50,469,50,makecol (0,184,239));
line (buffer,460,50 -eay,469 + eax,50 - eay,makecol (0,184,239));
//floodfill (buffer,500,50,makecol (0,0,255));
floodfill (buffer,470,30,makecol (0,255,0));
draw_sprite (buffer,face,478,30);
draw_sprite (buffer,walk [c],x,y);
blit (buffer,screen,0,0,0,0,640,480);
       
        // end of stepping animation
        }
   [...]


You are doing it completely wrong. You should listen to what DemonWasp is saying. With this setup you are essentially stuck in the while loop in code above. You want to some how keep track of what frame to draw, and update it from the while loop in main.cpp. This isn't the best way to do it, but it is the easiest.
DemonWasp




PostPosted: Thu May 06, 2010 4:08 pm   Post subject: RE:More Efficient way to do code - Allegro

@CodeMonkey: What do you mean "this isn't the best way to do it"? Do you know of some other basic approach that I'm missing that's better in some way?
CodeMonkey2000




PostPosted: Thu May 06, 2010 8:03 pm   Post subject: RE:More Efficient way to do code - Allegro

Usually you want to sync it with time, not the main infinite loop. You send the time displacement to all of your functions/classes (or have it globally known), and let them handle it independently. I find that it's more consistent with different computers.
DemonWasp




PostPosted: Thu May 06, 2010 8:19 pm   Post subject: RE:More Efficient way to do code - Allegro

Ah, yes, of course. I'd listed that in my first reply, but omitted it for clarity in the second.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: