¡Guerreros con espada y escudo! - POO [ESP-ENG] | Python

¡Saludos! He aquí he vuelto con otro ejercicio y como había mencionado es sobre guerreros, al estilo RPG. Esto porque los ejercicios están enfocados en ejercitar la programación orientada a objetos a través de crear una clase "Guerrero", con atributos: Vida, Estado de arma, Estado de escudo, y Estado (Ataque/Defensa).

Greetings! Here I am back with another exercise and as I mentioned it is about warriors, RPG style. This is because the exercises are focused on exercising object oriented programming by creating a "Warrior" class, with attributes: Health, Weapon State, Shield State, and State (Attack/Defense).


Portada post programacion, tech (2).gif

Python SEPARATOR PSD FILE.png

Para comenzar, el guerrero cuenta con los atributos: "vida", "estadoArma" y estadoEscudo seteados con 100 puntos, el atributo "estado" queda seteado en "Ataque". Partiendo del enunciado, si un guerrero recibe daño estando ese estado, ese daño irá directamente a su vida y será de 20 puntos, mientras que, si está en estado "Defensa", el daño irá al escudo, quitándole solo 5 puntos a este.

Con el decorador property procedí a hacer los getters y setters, esta parte ya es como "una fórmula" muy parecida a lo que se hace en Java y C++. Tan solo hubo una serie de condiciones en el estado, pues para cambiar de Ataque a Defensa es necesario que el escudo no esté roto, tampoco es posible cambiar de estado si el estado ingresado no es Ataque o Defensa. Todo esto siguiendo el enunciado.

De haber sido un proyecto más robusto habría agregado que no se debe cambiar de estado si el guerrero no tiene puntos de vida. Sigue un método que verifica si el guerrero sigue con vida. Se trata de "isalive(self)". Esto partiendo de la condición de que la vida sea superior a 0.

To begin with, the warrior has the attributes: "Life", "Weapon status" and "Shield status" set to 100 points, the "Status" attribute is set to "Attack". Based on the statement, if a warrior receives damage while in this state, this damage will go directly to his life and will be 20 points, while, if he is in the "Defense" state, the damage will go to the shield, taking only 5 points from it.

With the property decorator I proceeded to make the getters and setters, this part is already like a "formula" very similar to what is done in Java and C++. There were only a series of conditions in the state, because to change from Attack to Defense it is necessary that the shield is not broken, it is not possible to change state if the state entered is not Attack or Defense. All this following the statement.

If it had been a more robust project I would have added that it is not possible to change state if the warrior has no life points. It follows a method that checks if the warrior is still alive. This is "isalive(self)". This is based on the condition that the life is greater than 0.

Python SEPARATOR PSD FILE.png

image.png

image.png
From AzulSchool, Python Course


image.png

image.png

image.png

Python SEPARATOR PSD FILE.png

Para el método que sigue las cosas se ponen un poco más extensas y esto es porque para atacar toca verificar varias condiciones. Estas van así: si la vida es superior a 0 (está vivo o muerto), si el estado del atacante es defensivo (no se puede atacar en ese estado), si el estado del arma es superior a 0 (no se puede atacar con un arma rota), si la vida del contrincante es superior a 0 (no se pueden atacar cadáveres).

Luego llega la condición en la que finalmente hay "acción" aunque suprimida en cierta medida por la defensa, pues si el estado del contrincante es "Defensa", se ataca al escudo y este pierde 5 puntos de condición mientras que la espada del atacante pierde 2 puntos también, posteriormente se envía un mensaje informando que el ataca impacto el escudo y se informa del estado del escudo del rival.

Finalmente llega la condición donde hay más daño y es cuando el rival está en posición ofensiva, haciendo que el daño que recibe sea de 20 puntos, igualmente el arma del atacante se ve afectada en 5 puntos y se informa, en este caso, de que el ataque fue directo y de la vida actual del afectado.

Para probar el funcionamiento declaré dos objetos de tipo Guerrero, estos fueron "Pancracio y Rocky", dado que no les puse atributo nombre (el enunciado no lo pedía), me guie por el nombre de la variable para facilitar la lectura.

Luego Pancracio ataca dos veces a Rocky, Rocky cambia a estado defensivo, pero Pancracio no, sigue un apartado para informar los puntos de vida de Rocky. Continúan 5 ataques de Pancracio que acaban con Rocky, luego se llama al método "isalive" para confirmarlo y se manda otro ataque para demostrar que la condición de no atacar cadáveres está vigente.

Finalmente sale por consola el texto correspondiente a todo lo sucedido, desde los ataques, hasta los puntos de vida para acabar con la muerte de Pancracio y el detalle de que Rocky trató de atacar el cadáver de Pancracio.

For the following method things get a little more extensive and this is because to attack you have to check several conditions. These go like this: if the life is greater than 0 (alive or dead), if the state of the attacker is defensive (you can not attack in that state), if the state of the weapon is greater than 0 (you can not attack with a broken weapon), if the opponent's life is greater than 0 (you can not attack corpses).

Then comes the condition where finally there is "action" although suppressed to some extent by the defense, because if the opponent's status is "Defense", the shield is attacked and it loses 5 condition points while the attacker's sword loses 2 points as well, then a message is sent informing that the attacker attacks impact the shield and the status of the opponent's shield is reported.

Finally comes the condition where there is more damage and is when the opponent is in offensive position, making the damage received is 20 points, also the attacker's weapon is affected by 5 points and is reported, in this case, that the attack was direct and the current life of the affected.

To test the operation I declared two objects of type Warrior, these were "Pancracio and Rocky", given that I did not name them attribute (the statement did not ask for it), I was guided by the name of the variable to facilitate the reading.

Then Pancratius attacks Rocky twice, Rocky changes to defensive status, but Pancratius does not, a section follows to report Rocky's life points. Then Pancracio attacks Rocky 5 times, then the "isalive" method is called to confirm it and another attack is sent to show that the condition of not attacking corpses is in force.

Finally the text corresponding to everything that happened, from the attacks, to the life points to finish with the death of Pancracio and the detail that Rocky tried to attack the corpse of Pancracio.

Python SEPARATOR PSD FILE.png

image.png

image.png

Python SEPARATOR PSD FILE.png

image.png

Python SEPARATOR PSD FILE.png

Y bueno... ¡Eso es todo por hoy! Este ha sido otro ejercicio de POO en Python que les comparto para provecho de todo aquel que ande en su camino de aprendizaje en este mundo tan estresante y apasionante.

En lo personal te recomiendo mucho entrar también en este curso, doy fe de que es de muy buena calidad y vale cada centavo. Si quieres ingresar, clica aquí.

And well... That's all for today! This has been another Python OOP exercise that I share with you for the benefit of everyone who is on their way to learning in this stressful and exciting world.

Personally, I highly recommend you to join this course as well, I can attest that it is of very good quality and worth every penny If you want to join, click here.

Python SEPARATOR PSD FILE.png



Redes actualizada.gif


Puedes seguirme por acá si lo deseas:
You can follow me here if you want:

Cuenta secundaria
(Dibujos, edición y juegos) | Secondary account (Drawings, editing and games)



0
0
0.000
9 comments
avatar

You are very intelligent I must say 🫣
Thanks for the detailed info.

!PIZZA

0
0
0.000
avatar

Muchas gracias por compartir tu post, la verdad espero poder revivir pronto esta comunidad para que mas programadores como se animen a compartir sus conocimientos en esta comunidad

0
0
0.000
avatar

Gracias a ti por pasar por acá. También he visto activo al Sr Rafael con sus publicaciones de Linux, poco a poco puede crecer la comunidad, quizás con iniciativas o eventos similares se aviven más las cosas.

untitled.gif

0
0
0.000
avatar

dice pagar en paypal pero si no tienes tarjeta de credito como se hace con el curso??

0
0
0.000
avatar

Tengo entendido que se puede usar Paypal sin necesidad de una tarjeta de crédito, solo que se ponen un poco más estrictos en cuanto a las medidas de seguridad :D

0
0
0.000