Moving ball in bitmapa
Zmienna wskaźnik nazwany smallBuffer
Większość kodu na listingu 1, powinny być znane do Ciebie teraz i nie wymaga żadnych wyjaśnień poza wbudowanych komentarzach. Jednak zmienny wskaźnik nazwany smallBuffer nie zasługują na wyjaśnienie, ponieważ jest to nowy do tego programu.
Musi skasować stary obraz piłki
We wcześniejszym wypadowa-ball programu, piłka została sporządzona w ruchu przed czarnym tle. W obu programach, gdy nadszedł czas na piłkę do przemieszczania się, istniejący obraz kuli muszą zostać usunięte przed wyciągnięciem piłki w nowej lokalizacji. W tle stały kolorów, wszystko co jest wymagane, aby usunąć kulę jest zwrócenie kolejną kulę na szczycie istniejącego piłki w kolorze tła. Jednak trochę więcej pracy jest zobowiązany do usunięcia piłkę, gdy tło jest obraz zamiast jednolitego koloru.
Zapisz niewielki fragment obrazu tła
Zmienna wskaźnik nazwany smallBuffer będą używane do wskazywania na bitmapę bufora w pamięci. Ten bufor pamięci będzie używane do zapisu mały fragment obrazu tła, koncentrującego się na nowym miejscu piłki zanim piłka została sporządzona w tym miejscu na obrazie. Mały obraz będzie następnie wykorzystywany w celu przywrócenia obszar obrazu zajmowanej przez piłkę, kiedy jest czas na bal o przeprowadzce do nowej lokalizacji. Innymi słowy, mały kwadrat obraz zostanie sporządzony w jego oryginalnej lokalizacji, skutecznie usuwanie bieżący obraz piłki i przywrócenia obrazu za piłką w tym samym czasie.
Początek głównej funkcji
Program ten zawiera dwie funkcje:
- główny
- moveBall
Zacznę moje wyjaśnienie z głównych funkcji, która zaczyna się na listingu 2.
Listing 2 . Początek głównej funkcji.
int main () { allegro_init (); install_keyboard (); set_color_depth (32); set_gfx_mode (GFX_AUTODETECT_WINDOWED, szerokość, wysokość, 0,0); / / Załaduj plik obrazu z bieżącego katalogu. buffer = load_bitmap ("starfish324x330.pcx", NULL); |
Wyjaśniłem kod podobny do kodu na listingu 2 przed (patrz Zasoby ), więc nie będę powtarzać te wyjaśnienia tutaj.
|
Przygotować bitmapę dla małego obrazka
Listing 3 nazywa create_bitmap funkcji, aby utworzyć pusty bitmapy i zapisać jej adres w zmiennej wskaźnika o nazwie smallBuffer.
Listing 3 . Przygotować bitmapę dla małego obrazka.
#include <allegro.h> //Deklaracja wskaźnika zmienna o nazwie bufor, który można // Używane do wskazywania na bitmapę. To będzie główny // Bufor pamięci zawierający obraz. BITMAP *buffer = NULL; // Deklaracja innego wskaźnika zmiennej o nazwie smallBuffer że // Może być również używane do wskazywania na bitmapę. Ta pamięć // Bufor będzie używany do zapisywania małej kwadratowej powierzchni // Duży obraz na środku piłka. Mały obrazek będzie // Użyty do przywrócenia obszaru obrazu zajmowanego przez // Kule kiedy jest czas na nia, aby przejść do // Nowej lokalizacji. BITMAP *smallBuffer = NULL; // Należy pamiętać, że rozmiar obrazu był znany zanim //Program został napisany, a rozmiar jest używany poniżej, // Ustaw rozmiar pola. int width = 324;//width of box int height = 330;//height of box int radius = 9;//radius of ball int x = 100;//initial position of ball int y = 200;//initial position of ball int tempX;//used to save the current location of the ball int tempY;//used to save the current location of the ball //Keep track of direction of motion here. //0= northwest 1 = southwest, 2 = northeast, //3 = southeast int dir; //------------------------------------------------------// void moveBall(){ //Save current location of ball. tempX = x; tempY = y; //The code in the switch statement is identical to the // code in the earlier version of the program. switch(dir){ case 0: //Direction is northwest. if((x <= radius) || (y <= radius)){ //Ball has collided with either the left wall or // the top wall. //Get a new direction. Note that if the new // direction is the same as the old one, control // will come back to here to get still another // direction the next time the function is called. dir = rand() % 4; }else{ //No collision, set new location for the ball --x; --y; }//end else break; case 1: //Direction is southwest. if(((x <= radius) || (y >= (height - radius)))){ //get a new direction dir = rand() % 4; }else{ //set new location for the ball --x; ++y; }//end else break; case 2: //Direction is northeast. if(((x >= (width - radius)) || (y <= radius))){ //get a new direction dir = rand() % 4; }else{ //set new location for the ball ++x; --y; }//end else break; case 3: //Direction is southeast if((((x >= (width - radius)) || (y >= (height - radius))))){ //get a new direction dir = rand() % 4; }else{ //set new location for the ball ++x; ++y; }//end else }//end switch //Erase the current image of the ball by drawing the // saved background over the ball. blit(smallBuffer,buffer, 0,0, tempX-radius-1,tempY-radius-1, radius*2+2,radius*2+2); //Save a square area of the background at the new // location of the ball before the ball is drawn there. // Each side of the square is two pixels larger than the // diameter of the ball to allow for the possibility // that the ball isn't perfectly centered in the square. blit(buffer,smallBuffer, x-radius-1,y-radius-1, 0,0, radius*2+2,radius*2+2); //Now draw the ball on the background at the new // location. circlefill (buffer,x,y,radius,makecol(255,0,0)); //Call the blit function to copy the off-screen buffer // contents to the screen. blit(buffer,screen,0,0,0,0,width,height); rest(5);//Delay for five milliseconds }// end moveBall function. //------------------------------------------------------// int main(){ allegro_init(); install_keyboard(); set_color_depth(32); set_gfx_mode(GFX_AUTODETECT_WINDOWED,width,height,0,0); //Load an image file from the current directory. buffer = load_bitmap("starfish324x330.pcx", NULL); //Create an empty bitmap and store its address in // smallBuffer. Make the width and height equal to the // diameter of the ball plus two pixels. smallBuffer = create_bitmap(radius*2 + 2,radius*2 + 2); //Save a square area of the background at the current // location of the ball. Each side of the square is two // pixels larger than the diameter of the ball to allow // for the possibility that the ball isn't perfectly // centered in the square. blit(buffer,smallBuffer, x-radius-1,y-radius-1, 0,0, radius*2+2,radius*2+2); //Seed the random number generator and set the initial // direction based on a random number. srand (time(NULL)); dir = rand() % 4; //Loop until the user presses the Esc key. while( !key[KEY_ESC]){ moveBall(); }//end while loop return 0; }//end main END_OF_MAIN();