Coding Day 2: Classes and Virtual Pets

Today’s activity is another class assignment, one I’ve never completely finished.  I worked on this for about an hour and have decided to continue with it tomorrow.  I might come back and finish today and if I do, I’ll update.  But isn’t this the cutest thing?

virtual pet

 

Here’s the code for this so far.  I’ve got the skeleton of the methods started, but I’m going to draw buttons in another window to interact with the pet. Also, there will be hidden actions that do cool things.

from Graphics import *
from random import *
from Myro import *

win = Window(250,250)

class MyPet:
    def __init__(self):
        self.happiness = 10
        self.hunger = 0
        self.sleepiness = 0
        self.boredom = 0
        color = choice(getColorNames())
        mypet = self.drawpet(color)
        name = input("What do you want to name me?")
        text = Text((125,25),name)
        text.fill = Color(color)
        text.draw(win)

    def play(self):
        ball = Circle((20,225),20)
        ball.fill = Color("red")
        ball.draw(win)

    def drawpet(self,color):
        body = Oval((125,175),60,50)
        body.fill=Color(color)
        body.draw(win)
        head = Oval((125,125),45,35)
        head.fill=Color(color)
        head.draw(win)
        belly = Circle((0,0),40)
        belly.fill=Color("white")
        belly.draw(body)
        x = -20
        for i in range(2):
            eye = Circle((x,-20),15)
            eye.fill = Color("white")
            eye.draw(head)
            pupil = Circle((0,0),5)
            pupil.fill = Color("black")
            pupil.draw(eye)
            x = x + 40
        beak = Polygon((-10,-5),(10,-5),(0,20))
        beak.fill = Color("orange")
        beak.draw(head)
        foot = Polygon((95,215),(65,235),(110,235))
        foot.fill = Color("orange")
        foot.draw(win)
        foot2 = Polygon((150,235), (195,235), (160,215))
        foot2.fill=Color("orange")
        foot2.draw(win)
        return mypet

pet = MyPet()

2 Replies to “Coding Day 2: Classes and Virtual Pets”

  1. I have an abstract class called Team and inside this class I have a virtual function (shown below):

    class Team
    {
    public:
    Team(void);
    ~Team(void);
    virtual void SpawnUnit() = 0;
    };

    Then I created a derived class called Attacker, code below:

    class Attacker :public Team
    {
    public:

    Attacker(void);
    ~Attacker(void);
    void SpawnUnit(int, int, int);
    };

    But when I create an object of Attacker in main, for example:

    #include “Attacker.h”
    #include <iostream>

    int main()
    {
    Attacker attacker;

    (more code here but error part is shown above)
    }

    I keep getting an error telling me that I can’t create an object of an abstract class when Attacker isn’t an abstract class.

    Can anyone help me?

    Thanks

  2. Aren’t
    void SpawnUnit()
    and
    void SpawnUnit(int, int, int)
    different member functions in C++ (which I assume you are using, unlike the post, which is using Python).

    C++ cares about the types of the items passed in, so f(void) and f(int,int) are different functions, which have confusingly similar names.

Leave a Reply

Your email address will not be published. Required fields are marked *