Created
November 24, 2018 18:37
-
-
Save GravenilvecTV/9ee31bc10ba662f6b04774cecdd7003f to your computer and use it in GitHub Desktop.
Apprendre le Java #7 : Les Objets (corrigé du tp)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from model.player import Player | |
from model.weapon import Weapon | |
knife = Weapon("Couteau", 3) | |
player1 = Player("Graven", 20, 3) | |
# donne un couteau faisant 3 dégats au joueur ! | |
player1.set_weapon(knife) | |
player2 = Player("Bob", 20, 5) | |
player1.attack_player(player2) | |
print(player1.get_pseudo(), "attaque", player2.get_pseudo()) | |
print("Bienvenue au joueur", player1.get_pseudo(), "/ Points de vie:", player1.get_health(), "/ Attaque:", player1.get_attack_value()) | |
print("Bienvenue au joueur", player2.get_pseudo(), "/ Points de vie:", player2.get_health(), "/ Attaque:", player2.get_attack_value()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Player: | |
def __init__(self, pseudo, health, attack): | |
self.pseudo = pseudo | |
self.health = health | |
self.attack = attack | |
self.weapon = None | |
print("Bienvenue au joueur", pseudo, "/Points de vie: ", health, "/ Attaque", attack) | |
def get_pseudo(self): | |
return self.pseudo | |
def get_health(self): | |
return self.health | |
def get_attack_value(self): | |
return self.attack | |
def damage(self, damage): | |
self.health -= damage | |
def attack_player(self, target_player): | |
damage = self.attack | |
# si le joueur a une arme | |
if self.has_weapon(): | |
# ajoute les dégats de l'arme au point d'attaque du joueur | |
damage += self.weapon.get_damage_amount() | |
target_player.damage(damage) | |
# méthode pour changer l'arme du joueur | |
def set_weapon(self, weapon): | |
self.weapon = weapon | |
# méthode pour verifier si le joueur a une arme | |
def has_weapon(self): | |
return self.weapon is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Weapon: | |
def __init__(self, name, damage): | |
self.name = name | |
self.damage = damage | |
def get_name(self): | |
return self.name | |
def get_damage_amount(self): | |
return self.damage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bonjour à tous, voici ma version :
Weapon.py
Player.py
Main.py