The code creates a very simple/easy RPG game, with 2 classes Jedi
and Orc
. The data is visualized using turtle
. Each class has a method of attack
(lightsaber_attack
for Jedi
), which has an argument that must be either a Jedi
or Orc
instance. The .health
attribute of the attacked one will be reduced by .power
of the attacker. If .health
is not positive, then the image of the character will disappear. By design, each character can attack itself.
Simulation
luke.lightsaber_attack( orc_1 )
luke.lightsaber_attack( orc_2 )
orc_1.attack( luke )
orc_2.attack( orc_2 )
Questions
- How can I make the code to be easily understood by teenagers? (in a tutorial)
- How can I make it more compact?
- Are there any missing important features of Python's OOP that are important to be explained to students? (other than
super
and inheritance)
Image Links
Full code
import turtle
import time
jedi_gif = "/home/asus/Arief_tempo/images/random/jedi.gif"
orc_gif = "orc.gif"
darkorc_gif = "darkorc.gif"
damaged_gif = "damaged.gif"
turtle.register_shape( jedi_gif )
turtle.register_shape( orc_gif )
turtle.register_shape( darkorc_gif )
turtle.register_shape( damaged_gif )
class JediLuke:
def __init__(self):
self.power = 300
self.health = 300
self.img = turtle.Turtle( shape = jedi_gif )
self.damaged_img = turtle.Turtle( shape = damaged_gif, visible = False )
self.img.penup()
self.damaged_img.penup()
def lightsaber_attack(self, enemy):
self.img.setpos(enemy.img.pos()[0], enemy.img.pos()[1])
enemy.damaged_img.showturtle()
enemy.health += - self.power
time.sleep(1)
enemy.damaged_img.hideturtle()
if enemy.health < 0:
enemy.img.hideturtle()
self.img.setpos(200, 0)
def change_pos(self, pos):
self.img.setpos(pos[0], pos[1])
self.damaged_img.setpos(pos[0], pos[1] + 150)
class Orc:
def __init__(self, health, gif_image):
self.power = 100
self.health = health
self.img = turtle.Turtle( shape = gif_image )
self.damaged_img = turtle.Turtle( shape = damaged_gif, visible = False )
self.img.penup()
self.damaged_img.penup()
def attack(self, enemy):
current_pos = self.img.pos()
self.img.setpos(enemy.img.pos()[0], enemy.img.pos()[1])
enemy.damaged_img.showturtle()
enemy.health += - self.power
time.sleep(1)
enemy.damaged_img.hideturtle()
if enemy.health < 0:
enemy.img.hideturtle()
self.img.setpos(current_pos[0], current_pos[1])
def change_pos(self, pos):
self.img.setpos(pos[0], pos[1])
self.damaged_img.setpos(pos[0], pos[1] + 150)
luke = JediLuke()
luke.change_pos( [200, 0] )
orc_1 = Orc( health = 400 , gif_image = orc_gif)
orc_1.change_pos( [-200, 100] )
orc_2 = Orc( health = 200, gif_image = darkorc_gif )
orc_2.change_pos( [-200, -100] )