Skocz do zawartości

Allegro problem z klawiaturą


Rekomendowane odpowiedzi

Opublikowano

Wiam niedawno zaczelem uczyc sie bibliotek "Allegro" i chcialem zrobic gre gdzie porusza sie kwadracikiem xD

lecz gdy zrobie

if(key[KEY_RIGHT])

to ta instrukcja dziala na czas wcisniecia. Jak potrzymam 1sek to kwadrat ucieka poza ekran, jak zrobic by program czekal na zwolnienie guzika i wcisniecie go ponownie? plx help :D

Opublikowano

Moim zdaniem to powinno być:

 

if(key[VK_RIGHT])

 

lub

 

if(key[i tu jakiś numer odpowidający strzałce w prawo])

 

Ale nie przypominam sobie, żebym kiedyś korzystał z TEJ biblioteki. :(

Opublikowano
Ale nie przypominam sobie, żebym kiedyś korzystał z TEJ biblioteki. :(

Jedyna sensowna biblioteka do programowania gier 2D w C++ ;[.

 

 

@KiPPer:

Tutaj masz przykład ponga więc możesz sprawdzić jak zostało rozwiązane sterowanie paletką:

#include <allegro.h>
#include <cstdlib>
#include <time.h>


int ball_x = 320;
int ball_y = 240;

int ball_tempX = 320;
int ball_tempY = 240;

int p1_x = 20;
int p1_y = 210;

int p1_tempX = 20;
int p1_tempY = 210;

int p2_x = 620;
int p2_y = 210;

int p2_tempX = 620;
int p2_tempY = 210;

time_t secs;    //The seconds on the system clock will be stored here
               //this will be used as the seed for srand()

int dir;     //This will keep track of the circles direction
           //1= up and left, 2 = down and left, 3 = up and right, 4 = down and right

BITMAP *buffer; //This will be our temporary bitmap for double buffering

void moveBall(){

   ball_tempX = ball_x;
   ball_tempY = ball_y;

   if (dir == 1 && ball_x > 5 && ball_y > 5){

        if( ball_x == p1_x + 15 && ball_y >= p1_y && ball_y <= p1_y + 60){
                 dir = rand()% 2 + 3;
        }else{    
                --ball_x;
                --ball_y;
        }    

   } else if (dir == 2 && ball_x > 5 && ball_y < 475){

        if( ball_x == p1_x + 15 && ball_y >= p1_y && ball_y <= p1_y + 60){
                 dir = rand()% 2 + 3;
        }else{    
                --ball_x;
                ++ball_y;
        }

   } else if (dir == 3 && ball_x < 635 && ball_y > 5){

        if( ball_x + 5 == p2_x && ball_y >= p2_y && ball_y <= p2_y + 60){
                 dir = rand()% 2 + 1;
        }else{    
                ++ball_x;
                --ball_y;
        }

   } else if (dir == 4 && ball_x < 635 && ball_y < 475){

        if( ball_x + 5 == p2_x && ball_y >= p2_y && ball_y <= p2_y + 60){
                 dir = rand()% 2 + 1;
        }else{    
                ++ball_x;
                ++ball_y;
        }

   } else { 

       if (dir == 1 || dir == 3)    ++dir;
       else if (dir == 2 || dir == 4)    --dir;

   }    

   acquire_screen();
   circlefill ( buffer, ball_tempX, ball_tempY, 5, makecol( 0, 0, 0));
   circlefill ( buffer, ball_x, ball_y, 5, makecol( 128, 255, 0));
   draw_sprite( screen, buffer, 0, 0);
   release_screen();

   rest(5);

}    

void p1Move(){

   p1_tempY = p1_y;

   if( key[KEY_W] && p1_y > 0){

       --p1_y;

   } else if( key[KEY_S] && p1_y < 420){

       ++p1_y;

   }     

   acquire_screen();
   rectfill( buffer, p1_tempX, p1_tempY, p1_tempX + 10, p1_tempY + 60, makecol ( 0, 0, 0));
   rectfill( buffer, p1_x, p1_y, p1_x + 10, p1_y + 60, makecol ( 0, 0, 255));
   release_screen();

}  

void p2Move(){

   p2_tempY = p2_y;

   if( key[KEY_UP] && p2_y > 0){

       --p2_y;

   } else if( key[KEY_DOWN] && p2_y < 420){

       ++p2_y;

   }     

   acquire_screen();
   rectfill( buffer, p2_tempX, p2_tempY, p2_tempX + 10, p2_tempY + 60, makecol ( 0, 0, 0));
   rectfill( buffer, p2_x, p2_y, p2_x + 10, p2_y + 60, makecol ( 0, 0, 255));
   release_screen();

}    

void startNew(){

   clear_keybuf();
   readkey();
   clear_to_color( buffer, makecol( 0, 0, 0));
   ball_x = 320;
   ball_y = 240;

   p1_x = 20;
   p1_y = 210;

   p2_x = 620;
   p2_y = 210;

}    

void checkWin(){

   if ( ball_x < p1_x){
       textout_ex( screen, font, "Player 2 Wins!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0)); 
       startNew();
   } else if ( ball_x > p2_x){
       textout_ex( screen, font, "Player 1 Wins!", 320, 240, makecol( 255, 0, 0), makecol( 0, 0, 0)); 
       startNew();
   }    

}    

void setupGame(){

   acquire_screen();
   rectfill( buffer, p1_x, p1_y, p1_x + 10, p1_y + 60, makecol ( 0, 0, 255));
   rectfill( buffer, p2_x, p2_y, p2_x + 10, p2_y + 60, makecol ( 0, 0, 255));  
   circlefill ( buffer, ball_x, ball_y, 5, makecol( 128, 255, 0));
   draw_sprite( screen, buffer, 0, 0);
   release_screen();

   time(&secs);
   srand( (unsigned int)secs);
   dir = rand() % 4 + 1;

}    

int main(){

   allegro_init();
   install_keyboard();
   set_color_depth(16);
   set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

   buffer = create_bitmap( 640, 480); 

   setupGame();

   while( !key[KEY_ESC]){

       p1Move();
       p2Move();
       moveBall();
       checkWin();

   }    

   return 0;

}
END_OF_MAIN();

  • Filar Społeczności
Opublikowano
Jedyna sensowna biblioteka do programowania gier 2D w C++ ;[.
No jak się innych nie zna, to rzeczywiście, można wysnuć takie twierdzenie. :)
Opublikowano

Dzieki ziomki za odpowiedzi juz se poradzilem ;d poprostu dodalem zeby po kadym kliknieciu czytal klawisz i w ten sposob zwolnilem ruszanie kwadratu ;d Gra jest gotowa mozna sciagnac ja z https://gmclan.org/up1667.html

:D zapraszam do sciagania ! i prosze ocenic ;d pamietajcie to moja 1 gra ;p

Opublikowano

Jest to twoja pierwsza "gra" (jeśli można to tak nazwać). Nie ma co oceniać - chodzenie kwadracikiem. Rozumiem nie jest prosto zrobić nawet to, ale nie nadaje się to do oceny ;/ .

  • 1 rok później...
Opublikowano

Sam miałem ten problem.

ROZWIĄZANIE:

#include <allegro.h>
#define TICKMAX 19
#define ACW 24
#define ACH 24
void init();
void deinit();
int makegsc(int lvl){
    return makecol(lvl,lvl,lvl);
}
int main() {
init();
int x,y,t;
BITMAP *buf,*acs;
int bgr;
x=30;
y=60;
   bgr=makegsc(255);
buf=create_bitmap(SCREEN_W,SCREEN_H);
set_color_conversion(COLORCONV_KEEP_TRANS);
acs=load_bitmap("aktor_n.tga",NULL);
t=0;
while(!key[KEY_ESC]){
   if(t==TICKMAX){t=0;}else{t++;}
   clear_to_color(buf,makegsc(255));
   int col1;
col1=makecol(0,255,0);
   rectfill(buf,5,5,30,30,col1);
   ellipsefill(buf,90,90,30,30,makegsc(75));
   draw_sprite(buf,acs,x,y);
   draw_sprite(screen,buf,0,0);
   if(t==5){
   if(key[KEY_UP] && y>4){
                   y-=2;
                   }
   if(key[KEY_DOWN] && y<216){
                   y+=2;
                   }
                   if(key[KEY_LEFT] && x>4){
                   x-=2;
                   }
   if(key[KEY_RIGHT] && x<300){
                   x+=2;
                   }
                   }    
}
deinit();
return 0;
}
END_OF_MAIN()

void init() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 320, 240, 0, 0);
if (res != 0) {
	allegro_message(allegro_error);
	exit(-1);
}

install_timer();
install_keyboard();
install_mouse();
/* add other initializations here */
}

void deinit() {
clear_keybuf();
/* add other deinitializations here */
}

Główne elementy to:

Stała TICKMAX i if(t=TICKMAX){t=0;}else{t++;} i if(t==5){ KOD KLAWIATURY }

 

Nie no, teraz przesadziłeś. Warn.

Yoda

Opublikowano

Hehe koleś rox, odswierzacz tematów FAN --> ja oczywiscie :D. Wracając do tematu jak ktoś by potrzebował wystarczy walnąć przy dodawaniu np player_x+=1 trzeba dodac rest(100) te 100 mozna zmienić jak kto chce. Dziekuje. Ups ja też troszke odswiezylem, ale ważne ze pomoglem :D

Jeśli chcesz dodać odpowiedź, zaloguj się lub zarejestruj nowe konto

Jedynie zarejestrowani użytkownicy mogą komentować zawartość tej strony.

Zarejestruj nowe konto

Załóż nowe konto. To bardzo proste!

Zarejestruj się

Zaloguj się

Posiadasz już konto? Zaloguj się poniżej.

Zaloguj się
  • Ostatnio przeglądający   0 użytkowników

    • Brak zarejestrowanych użytkowników przeglądających tę stronę.
×
×
  • Dodaj nową pozycję...