![flappy mario exe flappy mario exe](https://m.gjcdn.net/game-thumbnail/500/614094-ujh6p6rn-v4.png)
Public void Draw() // here you don't need to pass the bird, app, etc, as a parameter This.app = app // the same for the RenderWindow This.bird = bird // assign the Bird reference to a class var through you can access that object Public Obstacle(int x, int y, Bird bird, RenderWindow app) pass the Bird and RenderWindow references so you can use them directly in the class you pass the var as a parameter (in this case) That's because you just pass that var (app, bird, etc) ONCE, and then you can simply access it from anywhere in the class without extra parameter adding in every internal class function. Anyway, as the RenderWindow object (app) is the same, independently of what function it is passed to, I think it would be better to pass it as a parameter in the constructor (the function that creates the object - I don't know exactly how it is in C++). Well, I didn't see (or realize) you were implementing 2 views. Usually, while more you group the game components into objects, better and tidier will be the code. You also could create a class for the group of Obstacles (with a List or Vector to store them), to make it more efficient, and check the collision bird-obstacle one by one (in a loop, not to pass the 3 obstacles and the bird to a same function). That is, for the main() function isn't so untidy. That would be better performance and tidier code.Ģ) I think it would be better to define a Game class which declares the bird and obstacle objects (and letters and all other stuff), with a RunGame() method (or function). But a couple of things now:ġ) the app object (of type RenderWindow) should also be passed as an argument to the constructors of all objects that need it instead of being passed to all functions. In a while I'll try to see all the project files in a C++ IDE when I'm at home. Actually, how much complicated is C++! How courageous have to be programmers who use it! You also can see this mechanism in this game, my smallest one (it's written in C#, but the concept is the same)
#Flappy mario exe code
Remember that in C++ the syntax for references and pointers is a bit complicated, it's not as in C#, so the code I write here is not C++, but the idea is OK. The attributes of the Bird that need to be accessed have to be public or as a property (or the way that C++ implements for a variable to be accessed outside its class). You don't need to pass it to the enemy class internal methods because it is a class global variable, visible in all the class. Then you can simply read from or write to that variable. In the constructor you add a parameter of that type, and when you create an enemy object you pass as last argument a reference to the Bird object. You just need to add a class attribute of the type Bird in all the enemies and every class that needs to access it.
![flappy mario exe flappy mario exe](https://i.redd.it/8kgvxfwzvvn71.jpg)
You can see the code below that shows how an object can access (read and write) another object. (*) Remember that in C++ the subject of references and pointers and all that stuff is not simple as it is in C#. That's why it is better to pass a reference of the player to the other objects and not on the contrary. Because the bird's control depends on you (even if you crash against an enemy and die), not on the enemies, but what they do (attack you, die if you kill them, etc) does depend on the bird (and on other instruction set or behavior you program them to do that are independent of anything). So they can read and modify the player's attributes, and theirs, in function of it. In your game class you create a Bird object, and then when you create the enemies and other objects, you pass them a reference to the Bird. If the bird is your game main character, I think it would be better that the other objects have a reference to it. Usually in my games, every enemy or animated object (such as blocks in Super Mario games) has a reference of the player (that is passed in its constructor) and so they are able to process both their atacks to the player and player's attacks to them. If an object needs to know some data of another object (that is very common), the only way (at least what my experience tells me) is to pass a reference of the 2nd to the 1st(*).