//main---------------------------------------------------------------------
//jugador.h---------------------------------------------------------------------
//jugador.cpp---------------------------------------------------------------------
//enemigo1.h-------------------------------------------------------------------
//enemigo1.cpp-------------------------------------------------------------------
//fondo1.h--------------------------------------------------------------------
//fondo1.cpp---------------------------------------------------------------------
I would like to create a simplified version of space invader with some changes to make it easier for me to program and keep learning.
I have three objects at the moment, the player's ship moving from left to right, the enemy moving down, and the background.
I want to organize the code correctly from the beginning and I am going to create two levels to see how it goes from one to the other when a certain condition is met, what I would like to do now is create level 1 and add those objects.
So how do I create the first level and a structure to manage the levels of the game.
a little feedback.
// no need '== true', when ejcecutar is a boolean which is already true or false. //while(ejecutar == true) while(ejecutar)
// '#pragma once' o '#ifndef JUGADOR_H'. No los dos. #pragma once #ifndef JUGADOR_H #define JUGADOR_H
So how do I create the first level and a structure to manage the levels of the game.
What is it you want it to look like? In the original game, there are 5 rows of aliens. No variations. So I don't know what you mean by level if it's all the same. Unless you want variation in aliens or different placement.
I played a lot of TI Invaders as a kid from the TI4/99a. That was my first experience with a space invaders game. They had the same 5 rows, but a variation of enemies. Each 2 rows was a different alien. Every time you cleared a level, the rows would shift. Meaning, every 2 levels you got a new alien. And they got harder to hit.
Later levels, they would flash, increase/decrease size. Also, each level they got just a bit faster.
Sorry, I made a mistake in the game. I mean the galaga, I usually play the nes version on an emulator, it's the one I like the most in this style, I don't like the space invader very much.
Regarding the levels, I mean the stages, I think that's how it is said in English, so I want to create two stages, the first with the different enemies and the second level where the enemies appear from different parts of the screen moving along a route or something So.
Well that, I want to know how the code can be structured for two stages, I create the first one and when I eliminate all the enemies it goes to the second stage. I don't know if I explained myself well, it's complicated with the translator.
I have tried to make the player shoot but it gives me an error, I am creating it in the player's shoot method.
Every key_down you are loading a bitmap from file.
// jugador.h if(al_key_down(&m_estado_de_teclas,ALLEGRO_KEY_Z)){ m_disparo = Disparo{32.0f,400.0f}; // disparo.h Disparo::Disparo(float x,float y): m_imagen{al_load_bitmap("disparo.png")}
How many times per second is mover called? How many bitmaps are you loading?
Do not load/unload bitmaps in the middle of the game or you will get delays.
Two ways:
1. Make an array somewhere else in the game of all needed bitmaps. Load/unload at the start/end of the program. Supply array to Disparo for use.
2. Make the bitmap static. Also, load/unload at the start/end of the program.
Here is a static example.
Lastly, check to see if current bullet is active/not active before creating a new one.
I'm going to try to do it but I have some questions. Why do you use static. The Shooting class is created in the player class so when I load and unload the graphics and where, I load them in the player constructor and download it in the destructor or in the main file .
When you say an array of shots you mean this -> Shot shot[];
Or to this -> std::vector<Shot> shot;
this confuses me -> return (!(Disparo::m_imagen = al_load_bitmap("disparo.png")));
There is no clearer way to write it.
I was more conncerened with this function:
void Jugador::disparar(){ al_get_keyboard_state(&m_estado_de_teclas); if(al_key_down(&m_estado_de_teclas,ALLEGRO_KEY_Z)){ m_disparo = Disparo{32.0f,400.0f}; } }
How many times a second are you trying to call Disparo? Every time you call the Disparo constructor, you are also calling al_load_bitmap.
Instead, load the bitmap once. either outside your class or inside your class. Static allows you to keep it in the class share between every Disparo.
You told me to use an array for the shot class but I have not achieved anything, and since I have never made an array of objects either, I don't really know how to do it and I have done what occurred to me but it is clear that it is wrong and it is not I can think of how to do it.
1. What is the problem? What is happening or not happening that we should focus on. You're not saying much. I can try to guess and see what might be a problem. Are you getting errors or is it compiling? Is the game crashing?
2. I said to create an array of IMAGES for all your images of the game. Not an array of shots. Remove the array indexing on m_disparo.
3. In your Jugador constructor, you call this function to load the image for m_disparo. That is valid code.
m_disparo.m_imagen = al_load_bitmap("disparo.png");
4. However, this line in Jugador::disparar is a problem
m_disparo = Disparo{32.0f,400.0f};
Try to understand what you are doing with this line.
5. Timing needs adjusted. In the event handling, jugador.pintar is called 60 times a second based on your timing speed. You have not taken that into consideration. In that function, it calls pintar. Pintar ONLY checks if a key is down and immediately increases or decrease velocity.
this confuses me -> return (!(Disparo::m_imagen = al_load_bitmap("disparo.png")));
The ! should not be there. Removing the ! returns true if loaded or false if not.
ALLEGRO_BITMAP* imagenes[10]; std::vector<ALLEGRO_BITMAP*> v_imagenes; //recorrer array y vector de imagenes for(int iterar{0};iterar < v_imagenes.size();++iterar){ v_imagenes[iterar] = al_load_bitmap("graficos/"+std::to_string(iterar)+".png"); } for(int iterar{0};iterar < 10;++iterar){ imagenes[iterar] = al_load_bitmap("graficos/"+std::to_string(iterar)+".png"); }
I have added the images to a folder and I have changed the name by numbers so that it is easier to go through with a loop. I have created an array and a vector of images to see which one is easier to use and when going through the loop I add it the images to the array or vector but when I convert to string it gives me an error. I got this idea by searching the internet because since I don't know how to do it, I have to look for answers somewhere and this is the result, which is not knowing what I am doing
You can append a char string to the end of a std::string because std::string has that functionality.
However, char string does not have the functionality to append a std::string to it.
std::string() + "blah" // good
"blah" + std::string() // bad
Get around that by making "graficos/" a std::string. Then you're appending a std::string to std::string.
std::string path = "graficos/"; //recorrer array y vector de imagenes for(int iterar{0};iterar < v_imagenes.size();++iterar){ v_imagenes[iterar] = al_load_bitmap(path+std::to_string(iterar)+".png"); } for(int iterar{0};iterar < 10;++iterar){ imagenes[iterar] = al_load_bitmap(path+std::to_string(iterar)+".png"); }
Also, you need to convert std::string to char* for Allegro functions.
use the .cstr() method function of std::string;
DanielH, don't bother what I'm going to tell you, but every time I ask a question, instead of giving me a solution explaining it to me, you just give me little clues and in the end I have no choice but to look for the solution outside the forum When I find something, I show it to you and it gives me a little clue again. As I already said, I recently finished a c++ course and I'm starting to practice with allegro, so I'm very green with c++ and totally green with allegro and I need a lot of help and that each thing be explained to me step by step because I don't know. I appreciate you bothering to answer me but instead of helping me I ended up quite dizzy and without knowing where to go so I don't know whether to continue with this or start with something else.
DanielH has been very patient and given you a lot of help. Just slow down and try to understand what he's said so far. He's given you far more than clues.
Sorry, but I don't want to tell you everything. Then I'd be writing your game and not you. And the way I write my programs might not be the way you write yours.
All I can do, without more info, is look at your code and tell you what I see is wrong or needs to be changed.
If you want a direct answer:
m_disparo = Disparo{32.0f,400.0f};
In this line, you are overwriting the values in m_disparo with the values in the constructor. This means, m_image is now invalid because it wasn't defined.
// This is what you get when you call Disparo{32.0f,400.0f} m_x = 32.0f; // set to 32.0f; m_y = 400.0f; // set to 400.0f; m_velocidad = 1.0f; // set to 1.0f from constructor initialization activo = true; // set to true from constructor initialization m_imagen = ?????; // undefined // then you copy those values, even the undefined m_imagen, to m_disparo; m_disparo = Disparo{32.0f,400.0f};
One way around this is to not call the constructor. Just change the member variables (x,y, active, etc) of m_disparo.
Thanks for all the help DanielH, and I know you don't want to tell me everything so that I'm the one to do it, but if I don't know how to do it and I've never done it, I can't get information from where there isn't any. It's as if I wanted to drive a car without Nobody would have taught me before, that's impossible. I need them to teach me how to create games before I create them myself, that's why I'm going to look for another library and video tutorials on YouTube to learn, if I don't find anything then I'll learn how to use unity or unreal engine that there are many tutorials. Thank you for your help and for your time, greetings.
Sorry that you decide to leave. In my opinion, the projects you picked were not suitable for beginners. I would recommend smaller targets. Make a tic-tac-toe game. Get the basics down with drawing graphics/primitives with Allegro, handling user input, etc.
Yes, we need more tutorials. However, not necessarily the tutorials on game design or game concepts. There are many many out there. They might not use Allegro, but it's the concepts beginners need.
The tutorials needed are how to use Allegro.
Please remember that Allegro is a low level library. Meaning, it doesn't have all the bells and whistles like Unity or others. That saying, you have more control with Allegro, but have to put in more effort.
Higher abstraction <--> less control
Lower abstraction <--> hard work
I think the problems you are having with writing games are really just inexperience with C and C++. Try tutorials on C++ before you start game making tutorials. There are many, many example programs that come with allegro, including 3 1/2 demos. Take a look at their source code, and see how much you understand.
Like DanielH said, we're not going to give you a full tutorial on a full game, but we are more than willing to help you when you get stuck and can't progress. If you give up now on Allegro, you'll probably give up when you get to other libraries. Keep trying.
Edgar
I know that I lack experience in these languages and surely the approach that I have made in my questions is not the correct one but the help that I need is too much and I know that everyone has their job, family, etc. And they are not going to dedicate their time to teach another person from scratch, for me it is not enough that they correct my code because what I need is that they teach me how to do things because I don't know how to do anything. That's why I think it would be better to watch video tutorials from other libraries and learn the basics and maybe then go back to allegro, I don't need to be shown a complete tutorial of a game either, what I should have done is create a topic for each question, ask a single thing that someone teach me how to do it and then copy, replicate, practice until you understand or even ask questions until you master it and then move on to the next thing, maybe I was also wrong with the language and I should have used c instead of c++ or maybe another simpler language. I like allegro and I want to learn how to use it but right now I don't know how to do anything and I need someone to take me by the hand and since that is asking a lot, that's why the best thing would be the video tutorials, the language doesn't help much either because I don't know much English. I'm sorry I made you waste your time, regards
We don't consider helping you a waste of time.
Try out cplusplus.com . Their tutorials are VERY good.
https://cplusplus.com/
https://cplusplus.com/doc/tutorial/
And if you're not ready for C++ and you want to stick with C, try these sites :
https://en.cppreference.com/w/ (Has both C and C++ reference)
https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html (Very detailed)
Thank you.