Simple (Very Easy to Make) RPG Game Simulation in Python and Turtle












5














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




  • jedi.gif

  • orc.gif

  • darkorc.gif


  • damaged.gif


Full code



import turtle
import time

jedi_gif = "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] )









share|improve this question




















  • 3




    As an aside: Luke Skywalker attacking orcs is a jarring mix-up of universes.
    – Reinderien
    1 hour ago






  • 2




    I upvoted @Reinderien 's comment, but I also +1 this question because Luke Skywalker. Fighting Orcs.
    – bruglesco
    54 mins ago
















5














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




  • jedi.gif

  • orc.gif

  • darkorc.gif


  • damaged.gif


Full code



import turtle
import time

jedi_gif = "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] )









share|improve this question




















  • 3




    As an aside: Luke Skywalker attacking orcs is a jarring mix-up of universes.
    – Reinderien
    1 hour ago






  • 2




    I upvoted @Reinderien 's comment, but I also +1 this question because Luke Skywalker. Fighting Orcs.
    – bruglesco
    54 mins ago














5












5








5


1





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




  • jedi.gif

  • orc.gif

  • darkorc.gif


  • damaged.gif


Full code



import turtle
import time

jedi_gif = "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] )









share|improve this question















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




  • jedi.gif

  • orc.gif

  • darkorc.gif


  • damaged.gif


Full code



import turtle
import time

jedi_gif = "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] )






python object-oriented game role-playing-game turtle-graphics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 33 mins ago

























asked 2 hours ago









Arief Anbiya

492214




492214








  • 3




    As an aside: Luke Skywalker attacking orcs is a jarring mix-up of universes.
    – Reinderien
    1 hour ago






  • 2




    I upvoted @Reinderien 's comment, but I also +1 this question because Luke Skywalker. Fighting Orcs.
    – bruglesco
    54 mins ago














  • 3




    As an aside: Luke Skywalker attacking orcs is a jarring mix-up of universes.
    – Reinderien
    1 hour ago






  • 2




    I upvoted @Reinderien 's comment, but I also +1 this question because Luke Skywalker. Fighting Orcs.
    – bruglesco
    54 mins ago








3




3




As an aside: Luke Skywalker attacking orcs is a jarring mix-up of universes.
– Reinderien
1 hour ago




As an aside: Luke Skywalker attacking orcs is a jarring mix-up of universes.
– Reinderien
1 hour ago




2




2




I upvoted @Reinderien 's comment, but I also +1 this question because Luke Skywalker. Fighting Orcs.
– bruglesco
54 mins ago




I upvoted @Reinderien 's comment, but I also +1 this question because Luke Skywalker. Fighting Orcs.
– bruglesco
54 mins ago










1 Answer
1






active

oldest

votes


















3














jedi_gif = "/home/asus/Arief_tempo/images/random/jedi.gif"


It's unclear why this image has an absolute path but no others do. They should probably all be relative, as the other three are.



Especially if this is for a tutorial, you need to add docstrings to all of your functions.



This:



self.img.setpos(enemy.img.pos()[0], enemy.img.pos()[1])


can use argument expansion, i.e.:



self.img.setpos(*enemy.img.pos())


That pattern can be used elsewhere you're indexing into the position.



This:



enemy.health += - self.power


should be



enemy.health -= self.power





share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210494%2fsimple-very-easy-to-make-rpg-game-simulation-in-python-and-turtle%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    jedi_gif = "/home/asus/Arief_tempo/images/random/jedi.gif"


    It's unclear why this image has an absolute path but no others do. They should probably all be relative, as the other three are.



    Especially if this is for a tutorial, you need to add docstrings to all of your functions.



    This:



    self.img.setpos(enemy.img.pos()[0], enemy.img.pos()[1])


    can use argument expansion, i.e.:



    self.img.setpos(*enemy.img.pos())


    That pattern can be used elsewhere you're indexing into the position.



    This:



    enemy.health += - self.power


    should be



    enemy.health -= self.power





    share|improve this answer


























      3














      jedi_gif = "/home/asus/Arief_tempo/images/random/jedi.gif"


      It's unclear why this image has an absolute path but no others do. They should probably all be relative, as the other three are.



      Especially if this is for a tutorial, you need to add docstrings to all of your functions.



      This:



      self.img.setpos(enemy.img.pos()[0], enemy.img.pos()[1])


      can use argument expansion, i.e.:



      self.img.setpos(*enemy.img.pos())


      That pattern can be used elsewhere you're indexing into the position.



      This:



      enemy.health += - self.power


      should be



      enemy.health -= self.power





      share|improve this answer
























        3












        3








        3






        jedi_gif = "/home/asus/Arief_tempo/images/random/jedi.gif"


        It's unclear why this image has an absolute path but no others do. They should probably all be relative, as the other three are.



        Especially if this is for a tutorial, you need to add docstrings to all of your functions.



        This:



        self.img.setpos(enemy.img.pos()[0], enemy.img.pos()[1])


        can use argument expansion, i.e.:



        self.img.setpos(*enemy.img.pos())


        That pattern can be used elsewhere you're indexing into the position.



        This:



        enemy.health += - self.power


        should be



        enemy.health -= self.power





        share|improve this answer












        jedi_gif = "/home/asus/Arief_tempo/images/random/jedi.gif"


        It's unclear why this image has an absolute path but no others do. They should probably all be relative, as the other three are.



        Especially if this is for a tutorial, you need to add docstrings to all of your functions.



        This:



        self.img.setpos(enemy.img.pos()[0], enemy.img.pos()[1])


        can use argument expansion, i.e.:



        self.img.setpos(*enemy.img.pos())


        That pattern can be used elsewhere you're indexing into the position.



        This:



        enemy.health += - self.power


        should be



        enemy.health -= self.power






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        Reinderien

        2,937720




        2,937720






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210494%2fsimple-very-easy-to-make-rpg-game-simulation-in-python-and-turtle%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            サソリ

            広島県道265号伴広島線

            Accessing regular linux commands in Huawei's Dopra Linux