Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » I'm new

This thread is locked; no one can reply to it. rss feed Print
 1   2 
I'm new
amstradcpc
Member #23,651
January 2023

#SelectExpand
1class Fantasma{ 2public: 3 float x{}; 4 float y{}; 5 6 Fantasma(float x,float y){ 7 std::cout<<"iniciamos fantasma"<<"\n"; 8 this->x = x; 9 this->y = y; 10 } 11 12 void pintar(){ 13 al_draw_filled_circle(x,y,14.0f,al_map_rgb(0,180,180)); 14 } 15 16 void colocar_en_posicion_f_mapa1(float& posicionX,float& posicionY){ 17 for(int y{ 0 };y < alto_celda;++y){ 18 for(int x{ 0 };x < ancho_celda;++x){ 19 if(mapa1[y][x] == 'f'){ 20 posicionX = x*15.0f; 21 posicionY = y*29.5f; 22 return; 23 } 24 } 25 } 26 } 27 28 void colocar_en_posicion_f_mapa2(float& posicionX,float& posicionY){ 29 for(int y{ 0 };y < alto_celda;++y){ 30 for(int x{ 0 };x < ancho_celda;++x){ 31 char caracter = mapa2[x+y*ancho_celda]; 32 if(caracter == 'f'){ 33 posicionX = x*15.0f; 34 posicionY = y*29.5f; 35 return; 36 } 37 } 38 } 39 } 40 41 ~Fantasma(){ 42 std::cout<<"finalizamos fantasma"<<"\n"; 43 } 44};

This is the last question about this game.
I have created the ghost and I would like to know how to make pacman detect when it collides with the ghost and restart the game when they collide.

DanielH
Member #934
January 2001
avatar

If they overlap, then the distance between is less than zero.

Calculate the distance between. Accounting for radius of pacman and width/height of ghosts.

Also, for your map:

1. Choose mapa1 or mapa2 and remove the other from your code.
2. I would have 2 versions of the map. a constant version and a non constant version.
3. At the start of a game and when it resets after dying, copy the constant version to the non constant.

amstradcpc
Member #23,651
January 2023

I have the two maps to know how to do it in both ways, what I'm doing is just a prototype to learn. From what you've told me I haven't understood much, could you show me a bit of code to guide me.

DanielH
Member #934
January 2001
avatar

Last night, I was looking at the code for a Pacman game I made. Not quite useful for you.
It's from 2001 and uses Allegro 4.something.

I used a simple 8-bit bitmap for the master map. Another bitmap for the main map. A third bitmap with color-based directions to tell the ghosts' eyes which way to go when they died. The third bitmap was so I didn't have to program path finding to find their starting locations.

#SelectExpand
1const int map_width = 44; 2const int map_height = 17; 3 4const char master_map[map_width * map_height + 1] = 5 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 6 "x..........................................x" 7 "x..........................................x" 8 "x.. ..x" 9 "x.. xxxxxxxxxxxxx * xxxxxxxxxxxxx ..x" 10 "x.. ..x" 11 "x..........................................x" 12 "x.. ..x" 13 "x.. xxxxxxxxxxxxx xxxxxxxxxxxxx ..x" 14 "x.. ..x" 15 "x..........................................x" 16 "x.. ..x" 17 "x.. xxxxxxxxxxxxx s xxxxxxxxxxxxx ..x" 18 "x.. * ..x" 19 "x..........................................x" 20 "x..........................................x" 21 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 22 23char game_map[map_width * map_height + 1]; 24 25// During your game, you will be removing dots. 26// This will reset the map when new game is started. 27void reset_game_map() 28{ 29 int i = 0; 30 for (while i < (map_width * map_height)) 31 { 32 game_map[i] = master_map[i]; 33 ++i; 34 } 35}

In my program, Pacman and the ghosts were the same size as a map cell. To tell if they collided was to tell the distance in the x and y directions

The minimal x distance for them to not touch is 1/2 the width of pacman added to 1/2 the width of the ghost. Same goes for the y, but using height. Measure the distance between them. If the distances are less than the minimal distances in both directions then you have a collision

// something like this (NOT TESTED)
if ((abs(pacman.x - ghost.x) < (0.5f * (pacman.width + ghost.width))) && (abs(pacman.y - ghost.y) < (0.5f * (pacman.height + ghost.height))))
{
  // hit
}

amstradcpc
Member #23,651
January 2023

Thank you very much for all the help, I'm going to leave this game here and start another one. Now I want to make a version of space invader but simplified and with some changes. I'm going to start with the first lines of code and when I have the basics I'll create another thread for this game and start with new questions. :)

amarillion
Member #940
January 2001
avatar

Good luck!

 1   2 


Go to: