Aug 20, 2024
Player
(pemain) dan Enemy
(musuh).Player
dan Enemy
dalam konteks game sederhana.Player
:
name
(nama)health
(kesehatan)energy
(energi)Enemy
:
name
(nama)health
(kesehatan)Enemy
tidak memiliki energi.Player
dan Enemy
.Player
:
attack
(menyerang)move
(bergerak)Enemy
:
attack
(menyerang) hanya ketika diserang oleh Player
.Player
memiliki atribut:
name
health
energy
Enemy
memiliki atribut:
name
health
Player
class Player:
def __init__(self, name, health=100, energy=100):
self.name = name
self.health = health
self.energy = energy
attack
energy
berdasarkan nilai damage
yang diberikan.def attack(self, damage=1):
if self.energy >= damage:
self.energy -= damage
print(f'{self.name} attacking with {damage} damage')
Enemy
class Enemy:
def __init__(self, name, health=500):
self.name = name
self.health = health
attack
Enemy
menyerang kembali jika diserang:def attack(self, damage):
self.health -= damage
if self.health < initial_health:
print(f'{self.name} is attacking back!')
Player
dan Enemy
Player
melakukan attack
ke Enemy
dan Enemy
membalas serangan jika health
berkurang.player1 = Player('Balmond')
monster = Enemy('Dragon')
player1.attack(80)
monster.attack(10)
_
).