Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

Problem. Gra i strzelanie


Rekomendowane odpowiedzi

Opublikowano

A więc mam gierke, pocisk leci w stronę myszy tylko dwa problemy

 

1. Pocisk leci, leci a gdy klikniemy drugi raz to zmienia kierunek w strone myszy. Jak temu zapobiec ?

2. Chciałbym żeby leciało tyle pocisków ile ja chce, a nie tylko jeden.

 

Pomoże mi ktoś? Męczę się już trochę. Uczę się dopiero

 

 

#include <allegro.h>
#include <cmath>

class Cpostac
{
     public:
            int x,y;
            short int kierunek, klatka;
};
Cpostac ludek;

// Inicjowanie timera
volatile long speed = 0;
void increment_speed()
{
speed++;
}
END_OF_FUNCTION(increment_speed);
LOCK_VARIABLE(speed);
LOCK_FUNCTION(increment_speed);

// Zmienna
int mapa_x = 0, mapa_y = 0;
BITMAP *bufor = NULL;
BITMAP *ludekb = NULL;
BITMAP *teren = NULL;
FONT * font_pcx = NULL;
BITMAP *kursor = NULL;
BITMAP *x = NULL;
// DEFINICJA MAPY
short int map[20][20] =
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
};
//kursor
int mx = 0, my = 0, mb = 0;
void myszka()
{
   if( mx != mouse_x || my != mouse_y || mb != mouse_b )
   {
       mx = mouse_x;
       my = mouse_y;
       mb = mouse_b;
   }
};
//WYsiwetalnie mapy
void  wys_mape()
{
     int licznik_x, licznik_y;
     for (licznik_x = 0; licznik_x < 100 ; licznik_x++)
     {
     for (licznik_y = 0; licznik_y < 100 ; licznik_y++)
     {
          blit(teren,bufor,
          (map[licznik_y + mapa_y][licznik_x + mapa_x]%4) * 32,
          (map[licznik_y + mapa_y][licznik_x + mapa_x]/4) * 32,
          licznik_x * 32,licznik_y * 32,32,32);
     }
     }
};


// Funkcja G?ówna
int main()
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,640,480,0,0);
install_timer();
install_int_ex(increment_speed, BPS_TO_TIMER(80));



// Tworzenie bufora
bufor = create_bitmap(640,480);
// ?adowanie grafiki
ludekb = load_bmp("ludek1.bmp",default_palette);
teren = load_bmp("teren.bmp",default_palette);
font_pcx = load_font("arial_16.pcx",default_palette, NULL);
x = load_bmp( "strzal.bmp", default_palette );
kursor = load_bitmap( "kursor.bmp", default_palette);
//opcje myszki
install_mouse();
set_mouse_sprite( kursor );
unscare_mouse();
// Ustalanie pocz?tkowej pozycji ludzika
int frame = 0;
ludek.x = 200;
ludek.y = 200;
ludek.klatka = 0;
ludek.kierunek = 0;

double predkosc = 0.1;
double kat;
double ix = ludek.x, iy = ludek.y;
int celx = ix, cely = iy;
while( !key[KEY_ESC])
{
while( speed > 0)
{
    if( mb == 1 )
           {
               celx = mx;
               cely = my;
               double deltaX = ix - celx;
               double deltaY = iy - cely;

               if(( deltaY ) == 0 )
               {
                   kat = 0;
               } else
               {
                   kat = atan(( deltaX ) /( deltaY ) );
               } //else

               if( deltaY >= 0 )
               {
                   kat -= M_PI;
               } //if

           }


           if( iy != cely || ix != celx )
           {
               ix += sin( kat ) * predkosc;
               iy += cos( kat ) * predkosc;
           }


 if( mouse_b == 1)
                   kursor = load_bitmap( "kursor2.bmp", default_palette);
                   else{
                   kursor = load_bitmap( "kursor.bmp", default_palette);
                   }

ludek.kierunek = 0;

if( key[KEY_LEFT]) { ludek.kierunek = 4; ludek.x-=2; }
if( key[KEY_RIGHT]) { ludek.kierunek = 2; ludek.x+=2; }
if( key[KEY_UP]) { ludek.kierunek = 1; ludek.y-=2; }
if( key[KEY_DOWN]) { ludek.kierunek = 3; ludek.y+=2; }
if( key[KEY_A]) { ludek.kierunek = 4; ludek.x-=2; }
if( key[KEY_D]) { ludek.kierunek = 2; ludek.x+=2; }
if( key[KEY_W]) { ludek.kierunek = 1; ludek.y-=2; }
if( key[KEY_S]) { ludek.kierunek = 3; ludek.y+=2; }


speed--;

frame++;
if( frame > 40) frame=0;
}

// Czyszczenie bufora
clear_to_color(bufor, makecol(150,150,150));

//Wyswietl mape
wys_mape();

//strtzal
       myszka();
       masked_blit( x, bufor, 0, 0, ix, iy, 200, 200 );
       textprintf_ex( bufor, font, 40, 40, makecol( 200, 200, 200 ), - 1, "deltaX = %d, deltaY = %d !",( int ) iy - cely,( int ) ix - celx );

// Sterowanie animacj?
if( frame<20) { ludek.klatka = 0;}
else if( frame>=20 && frame<40) { ludek.klatka = 1; }

//Wy?wietlanie ludzika
masked_blit( ludekb, bufor, ludek.kierunek*32 ,ludek.klatka*32 ,ludek.x, ludek.y, 32,32);

//wyswietlanie kursora

masked_blit( kursor, bufor, 0, 0, mouse_x, mouse_y, kursor->w, kursor->h );
blit( bufor, screen, 0, 0, 0, 0, bufor->w, bufor->h );
clear_keybuf();
}
// Usuwanie wszystkiego z pami?ci.
remove_int( increment_speed);
destroy_bitmap(ludekb);
destroy_bitmap( bufor );
destroy_bitmap( kursor );
allegro_exit();
return 0;
}
END_OF_MAIN();

 

b011ae0489757.png

Opublikowano

1. Stwórz sobie klasę pocisk w której będziesz miał jego współrzędne, kąt i prędkość.

2. Stwórz sobie STL'owską listę z obiektami pocisków w którym będą wszystkie pociski

Opublikowano

Napisałem przed tym na jakimś forum dla koderów c++ i koleś mi wysłał taki kod.

Napisał że pare rzeczy musze pozmieniać żeby działał. Zmieniać tu coś czy napisać od nowa?


#include <allegro.h>
#include <cmath>

class Cpostac
{
     public:
Cpostac()
{

};
            int x,y;
            short int kierunek, klatka;
};
Cpostac ludek;
std::vector<CPostac> pocisk;

// Inicjowanie timera
volatile long speed = 0;
void increment_speed()
{
speed++;
}
END_OF_FUNCTION(increment_speed);
LOCK_VARIABLE(speed);
LOCK_FUNCTION(increment_speed);

// Zmienna
int mapa_x = 0, mapa_y = 0;
BITMAP *bufor = NULL;
BITMAP *ludekb = NULL;
BITMAP *teren = NULL;
FONT * font_pcx = NULL;
BITMAP *kursor = NULL;
BITMAP *x = NULL;
// DEFINICJA MAPY
short int map[20][20] =
{
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
};
//kursor
int mx = 0, my = 0, mb = 0;
void myszka()
{
   if( mx != mouse_x || my != mouse_y || mb != mouse_b )
   {
       mx = mouse_x;
       my = mouse_y;
       mb = mouse_b;
   }
};
//WYsiwetalnie mapy
void  wys_mape()
{
     int licznik_x, licznik_y;
     for (licznik_x = 0; licznik_x < 100 ; licznik_x++)
     {
     for (licznik_y = 0; licznik_y < 100 ; licznik_y++)
     {
          blit(teren,bufor,
          (map[licznik_y + mapa_y][licznik_x + mapa_x]%4) * 32,
          (map[licznik_y + mapa_y][licznik_x + mapa_x]/4) * 32,
          licznik_x * 32,licznik_y * 32,32,32);
     }
     }
};


// Funkcja G?ówna
int main()
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,640,480,0,0);
install_timer();
install_int_ex(increment_speed, BPS_TO_TIMER(80));



// Tworzenie bufora
bufor = create_bitmap(640,480);
// ?adowanie grafiki
ludekb = load_bmp("ludek1.bmp",default_palette);
teren = load_bmp("teren.bmp",default_palette);
font_pcx = load_font("arial_16.pcx",default_palette, NULL);
x = load_bmp( "strzal.bmp", default_palette );
kursor = load_bitmap( "kursor.bmp", default_palette);
//opcje myszki
install_mouse();
set_mouse_sprite( kursor );
unscare_mouse();
// Ustalanie pocz?tkowej pozycji ludzika
int frame = 0;
ludek.x = 200;
ludek.y = 200;
ludek.klatka = 0;
ludek.kierunek = 0;

double predkosc = 0.1;
double kat;

int count=-1;

while( !key[KEY_ESC])
{

while( speed > 0)
{
    if( mb == 1 )
           {
pocisk.push_back(Cpostac);
count++;
pocisk[count].x = ludek.x;
pocisk[count].y = ludek.y;
int celx = pocisk[count].x;
int cely =pocisk[count].y;

               celx = mx;
               cely = my;
               double deltaX = pocisk[count].x - celx;
               double deltaY = pocisk[count].y - cely;

               if(( deltaY ) == 0 )
               {
                   kat = 0;
               } else
               {
                   kat = atan(( deltaX ) /( deltaY ) );
               } //else

               if( deltaY >= 0 )
               {
                   kat -= M_PI;
               } //if

           }


           if( pocisk[count].y != cely || pocisk[count].x != celx )
           {
               pocisk[count].x += sin( kat ) * predkosc;
              pocisk[count].y += cos( kat ) * predkosc;
           }


 if( mouse_b == 1)
                   kursor = load_bitmap( "kursor2.bmp", default_palette);
                   else{
                   kursor = load_bitmap( "kursor.bmp", default_palette);
                   }

ludek.kierunek = 0;

if( key[KEY_LEFT]) { ludek.kierunek = 4; ludek.x-=2; }
if( key[KEY_RIGHT]) { ludek.kierunek = 2; ludek.x+=2; }
if( key[KEY_UP]) { ludek.kierunek = 1; ludek.y-=2; }
if( key[KEY_DOWN]) { ludek.kierunek = 3; ludek.y+=2; }
if( key[KEY_A]) { ludek.kierunek = 4; ludek.x-=2; }
if( key[KEY_D]) { ludek.kierunek = 2; ludek.x+=2; }
if( key[KEY_W]) { ludek.kierunek = 1; ludek.y-=2; }
if( key[KEY_S]) { ludek.kierunek = 3; ludek.y+=2; }


speed--;

frame++;
if( frame > 40) frame=0;
}

// Czyszczenie bufora
clear_to_color(bufor, makecol(150,150,150));

//Wyswietl mape
wys_mape();

//strtzal
       myszka();
       masked_blit( x, bufor, 0, 0, ix, iy, 200, 200 );
       textprintf_ex( bufor, font, 40, 40, makecol( 200, 200, 200 ), - 1, "deltaX = %d, deltaY = %d !",( int ) iy - cely,( int ) ix - celx );

// Sterowanie animacj?
if( frame<20) { ludek.klatka = 0;}
else if( frame>=20 && frame<40) { ludek.klatka = 1; }

//Wy?wietlanie ludzika
masked_blit( ludekb, bufor, ludek.kierunek*32 ,ludek.klatka*32 ,ludek.x, ludek.y, 32,32);

//wyswietlanie kursora

masked_blit( kursor, bufor, 0, 0, mouse_x, mouse_y, kursor->w, kursor->h );
blit( bufor, screen, 0, 0, 0, 0, bufor->w, bufor->h );
clear_keybuf();
}
// Usuwanie wszystkiego z pami?ci.
remove_int( increment_speed);
destroy_bitmap(ludekb);
destroy_bitmap( bufor );
destroy_bitmap( kursor );
allegro_exit();
return 0;
}
END_OF_MAIN();
[color=#000000]

[/color]

b011ae0489757.png

Opublikowano

Jeśli chcesz to zrozumieć to napisz sam, jeśli chcesz mieć, żeby działało, to próbuj przerobić ;]

Lista jest lepsza niż vector, bo będziesz je i tak leciał po kolei przy aktualizacji i przy wyświetlaniu, a kasowanie będziesz miał szybsze.

 

Pisząc 70% kodu w mainie szybko się pogubisz ;] Jeśli już piszesz obiektowo, to pisz metody, klasy w oddzielnych plikach, będzie Ci się łatwiej połapać w kodzie ;]

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...