In [16]:
import random

class Fish:
    def __init__(self, name):
        self.name = name
        self.x = random.randint(0, 800)
        self.y = random.randint(0, 600)
        self.z = random.random()
        self.speed = 0
        self.direction = "left"
        self.rotation = 0
        print("Hi, I am a fish named", self.name)
In [17]:
fish1 = Fish("Nemo")
Hi, I am a fish named Nemo
In [18]:
fish1
Out[18]:
<__builtin__.Fish instance at 0x000000000000002E>
In [19]:
fish2 = Fish("Dori")
Hi, I am a fish named Dori
In [20]:
fish2.name
Out[20]:
'Dori'
In [21]:
fish2.x, fish2.y
Out[21]:
(408, 123)
In [22]:
fish1.x, fish1.y
Out[22]:
(628, 285)
In [24]:
fish2.z
Out[24]:
0.388204738212845
In [1]:
from Graphics import Picture, Window
In [2]:
window = Window(300, 200)
In [5]:
pic = Picture("fish1.gif")
In [8]:
pic.draw(window)
pic.outline = None
In [1]:
import random
from Graphics import Picture, Window, Color

class Fish:
    def __init__(self, filename):
        self.filename = filename
        self.picture = Picture(filename)
        self.picture.outline = None
        self.x = random.randint(0, 500)
        self.y = random.randint(0, 500)
        self.z = random.random()
        self.speed = 2
        self.direction = "right"
        self.rotation = 0 
    def putInOcean(self, ocean):
        self.ocean = ocean
        self.picture.draw(ocean)
        self.picture.moveTo(self.x, self.y)
    def step(self):
        if self.direction == "left":
            self.x = self.x + self.speed
        else:
            self.x = self.x - self.speed
        if self.x > ocean.width:
            ## flip image
            self.direction = "left"
        if self.x < 0:
            ## flip image
            self.direction = "right"
        self.picture.moveTo(self.x, self.y)
        
ocean = Window(500, 500)
ocean.setBackground(Color(0, 0, 255))
fish = Fish("fish1.gif")
fish.putInOcean(ocean)

while True:
    fish.step()
    ocean.step()
Traceback (most recent call last):
  File "<string>", line 34, in <module>
  File "<string>", line 7, in __init__
Exception: Failed to open file 'fish1.gif': No such file or directory
In [85]:
fish.step()