 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Homework 6 Questions- Fall 2007
Questions?
First!
why turtles? I thought a worm was the class mascot?
and by worm I mean large scaly reptile :P
| I guess Turtles are cuter (and a little easier to draw) ;-p Student3703 |
| And pythons can't hold a pen and move at the same time. Colin Potts |
"(5) Turtles red in tooth and claw. Program a pack of killer turtles cooperating to corner a hapless herbivore victim." this made me giggle x). question: is it appropriate (or even possible...?) to introduce new turtles into the world after the simulation has started, (ie. start with 3 turtles and end with 6)? and should these loop through over and over or end when they are done? annnd if they are to end then how long are they typically? it seemed like the examples in class today (which were really entertaining btw) were all 1 min.
thank you! :)
| You can insert turtles after you start the simulation. You just create a new turtle. You can also mimic the effect by creating a turtle early on, before any delay or movement has occurred, and then use the turtle.hide() and turtle.show() commands to make it disappear and reappear (be aware that when it's hidden, and the pen is down, it will still draw if you move it). And, up to you, and up to you. As long as the assignment requirements are fulfilled, you can pretty much do whatever you like (do take that sensibly though; if it is liable to irk or offend your grading TA, don't do it...meaning I'd be wary about endless repetition. I wouldn't mind it, but someone else might). But, this assignment is intended to let you play around, and be creative. Enjoy yourself. Student5493 |
Are there any length requirements? One of the options said at least 15 seconds, otherwise I'm not seeing anything about that.
| It depends upon what you choose to do, but definately not less than 15 seconds. If you decide to have planets orbiting around, then perhaps it should be as long as it takes for the longest planet's revolution. If you are dancing, then however long it takes to complete the steps. Thanks! Brittany Duncan |
Is there any way you guys can post the examples from class today???
| We weren't planning to because they are too involved to be helpful, we didn't want people to try to use code and confuse themselves and us. You might check the Spring 2007 Slides and Code page for some turtle functions to start you off. Thanks! Brittany Duncan |
I still don't feel very comfortable doing stuff with turtles, and there's nothing in the book. Are we going to do more in class?
| Yes. Thanks! Brittany Duncan |
| I have just uploaded lots of turtle examples. These start with functions, go on to methods, and include some elaborate examples (e.g. the predator/prey chase). All of these should be intelligible if you think about what is going on. Try them out and change a few things to see what happens. There are also some object-oriented examples using subclasses and constructors (not yet covered in class) and recursive function calls (also not yet covered). Even if you can't understand all of the code in these latter examples, try running them all the same and see if you can work out why they work the way they do. Colin Potts |
can our turtles dance and write at the same time if we so choose
i'm using a math equation to have my turtles/planets orbit. There are two parts where it seems like the turtles just teleport. I think it is because the coordinate for the y position is a fraction and I have to use the int() function to make the input acceptable. Is it ok if my turtles/planets jump a small section of their orbital paths because of this?
| Include a mention of it in your description, and I'm sure you'll be fine. That said, you can avoid it, if you want to, by doing the math to determine each step, and tracking it separately from the turtle's position (the coordinate position that the turtles use only tracks ints), as a floating point number. But it's up to you if you want to do that or not. Student5493 |
Could the turtles be scaled? I tried turtle.scale(2) but it returned an error.
| Actually, yes, they can. Use turtle.width and turtle.height. No paranthesis, use them as variables. So if you use simply turtle.width, it will return the width of the turtle, if you do turtle.width = 5, it'll set the width of the turtle to 5, etc (these all assume you have a turtle called 'turtle'. If it's called Brian, use Brian.width, Brian.height, etc). Student5493 |
In my recitation I noticed that the TAs were using a chart with all of the turtle commands on it. Is that available to us to use?
| I just e-mailed the TAs, it should be coming soon. Thanks! Brittany Duncan |
| Are you referring to the function list in JES perhaps? If not it may help to know who's recitation you are in... Student3703 |
If you are referring to the function list in JES, you can find it by selecting "11 Turtles and Worlds" under the Help menu in JES or by clicking the following link Turtles and Worlds & Methods/Functions -Student4737 |
so lines and shapes drawn by turtles are not required, correct?
in response to Buck's answer.... thanks for that, but the list I'm referring to was much longer! it had a lot of funtions that haven't been shown in class or recitation.
| I think it was probably a list that Bryan had. Thanks! Brittany Duncan |
| I have posted a list of Turtle methods to the slides/code page. Colin Potts |
So what exactly is 'Turtle', the word in JES?
| It's the name of the class of turtles. So to call the Turtle constructor from inside a Frog's constructor (as in the NoisyFrog example), you call Turtle.__init__() (i.e. the __init__() method belonging to Turtle, as opposed to the one belonging to Frog). You don't need to understand this unless you want to experiment with constructors in your homework. Colin Potts |
Why couldn't I say:
class Turtle:
def __init__(self):
(turtle modifiers)
| You could if you were defining the Turtle class. I thought you were asking why the word Turtle was used in the constructor of Frog. Colin Potts |
I write:
class Turtle:
def __init__(self):
self.height = 100
but it doesnt have any effect on the turtles. What is wrong?
| Try self.setHeight(100) instead. You may need to repaint the world before you see any change. Colin Potts |
| Unfortunately, these commands seem a little weird. I don't think anything is wrong with what you've done, I think there may just be a weirREMOVEDss with how they're implemented. Try resizing the turtle at runtime (i.e., in your script, rather then your class definition). Student5493 |
Can i use array elements as names for variables? What i am trying to do is create a function that creates a certain number of turtles (user-defined), each with a unique name.
| Excellent idea. So for a herd of turtles, you could say something like herd[i].forward(100) to move the i'th turtle in the herd. (What is the collective noun for turtles, btw? A herd?) Colin Potts |
| A Collective noun for turtles? How about speed bumps ? X-D Just kidding! I believe the proper term is a bale of turtles Student3703 |
| From Wikipedia Bale, bevy, dule, nest, or turn. Let's go with turn, it fits the assignment. Student5493 |
Is there any way to make a blank turtle? i.e. create a "variable" turtle that is undefined but can be defined later?
In Java, it is done like this...
Turtle a;
| Not to my knowledge. In Java, you aren't actually making a blank turtle, either, you're just allocating the memory space to the variable (which serves as a pointer for objects, same as in JES). Why do you need it? Student5493 |
| OK, everyone else ignore this. This is a Java question. What the above Java declaration does is merely announce that a is going to be the name of a turtle. The turtle doesn't exist – i.e. can't be moved or displayed or queried – until it is instantiated (its constructor is called using the Java keyword new). The reason that you do this in two steps in Java is that Java is strongly typed: you have to declare every variable before using it, and using it includes creating it by name. In Python, in contrast, you don't declare any variables in advance. You just initialize them when you want them. Colin Potts |
What I wanted to do with that Java reference question is to make a turtle that isn't explicitly defined that I can later instantiate to tell the current turtle(planet) which turtle is its parent(star), the parent turtle being the one that I have a question about being a "blank" turtle.
Wow, that's pretty confusing.
So, is there no way to do this in Jython?
| Sure. You can create a "sleeper" turtle star by creating it with makeTurtle(), make it invisible by using turtle.hide(), and then when you want to associate it with its solar system of turtle planets, you can put it in the right place/orientation, size it appropriately, and make it visible. I'm not sure why you need to do this rather than just plonking it down in the center to start with and making everything else revolve around it, but yes. Colin Potts |
I am trying to make all turtles default with penup.
class penup:
def __init__(self):
self.penUp(self)
Why isnt this working? ?I tried variations on the last line such as 'self.penUp()', 'penUp(self)', 'penUp()'.
Can't tell you why it's not working, because it's been a while since I've dealt with OO in Python, in general, and never in JES...but why do it that way? Why not simply use
for t in getTurtleList(world):
t.penUp()
at the start of your code? Student5493 |
o yeah that is much better, thanks!
| I think you must have meant to make penup (preferably "Penup" in caps) a subclass of Turtle. Since your penup wasn't a type of turtle, your penup objects didn't know how to lift their pens, go forward – or anything else reptilian. But Chris's solution is much better. You should avoid subclassing unless you really have a special case of turtles. Colin Potts |
To seperate a string through a specified dedlimiter, is (for the string 'list' delimited by comma)
list.split(',')
correct?
I tried it and split() was not recognized as an attribute.
o im sorry i found it; i was trying to seperate an array with one element already; I was supposed to apply it to a string only.
| Yes, split will take a string, and return it as a list containing the elements separated by the specified delimiter. I can't imagine what you're using it for in JES, but I must say I'm impressed you're finding uses for this stuff in either an extracurricular pursuit, or somehow in going above and beyond for this project. Student5493 |
will the music and turtle animations go out of sync?
| They could. You will need to adjust the sleep durations and distances moved so that the turtles are approximately in sync with the music. It's also best not to be running too many other processes in the background (email, IM, etc) so that the turtles and music run smoothly. Colin Potts |
Even though I say something like:
ted = makeTurtle(world)
When i type 'ted' and hit enter to find more info about ted, it says that it is a 'no name turtle at (position)'. Since i clearly defined that particular turtle as ted, what significance does the 'name' have?
| The variable name, and the turtle name, are two separate things. The variable name serves as a way to reference the turtle, whereas the turtle's name that is referenced when you enter 'ted', is actually a class field. What that means isn't very important (if you take 1331, you'll get to find out aaaaall about this, and I promise you, it'll make sense), but, if you -really- care for some reason, after you create ted, use ted.setName("ted") to change what it prints out when you enter 'ted' to get more information about the ted turtle. Student5493 |
turtle.forward(number) doesnt work if the turtle is facing an angle like 37 degrees, instead of a straight 90 or something! How can i get around this?
| Try using forward(turtle, number) instead. Turtles can be a bit finicky. Student5493 |
| When you say "it doesn't work", can you be more specific? Do you mean that the line is slightly the wrong length? (This is because of rounding error). Or do you mean that no movement occurs? (You must be doing something wrong). BTW, I just tried orienting a turtle at 37 degrees and going forward 100 using dot notation, and it works for me. Colin Potts |
ah i think i know whats wrong...i was using turtle.forward(1) at something like 37 degrees. When i changed the forward value to 2 pixels, it worked. I guess 1 pixel doesnt work because to calculate an odd angle trajectory pixel-by-pixel is not possible.
| If by odd you mean not a multiple of 90 degrees, then yes, that'd be it. Turtles store location as ints, since you can't have a part of a pixel. This leads to rounding down...so with an angle of 37, moving forward one pixel is not going to actually lead to movement (basic trig, longest side, that of the hypotenuse, is 1, so the two legs of the right triangle that is formed are both less then one. So since you're moving to the side less then 1, and vertically less than 1...you don't move anywhere at all). Student5493 |
Does our animation have to have a conclusion or can it be everlasting? Also, where can I find the coding for playing a sound on a infinite loop?
| Please make it stop. (Use a for loop.) FYI, to make anything repeat in an infinite loop deliberately, just put it in a block under the following "while 1:" (in other words, while true is true, or forever). But please don't do that or your TA will resort to thread death..... Colin Potts |
how can you get a picture to go away after being dropped by a turtle?
can i access pic via 'world.pic' ? If so, can i use 'world.pic.hide()' or something like that?
| I don't think you can do what you want to do other than by having a clean-up turtle go round dropping blank pictures on top of the originally dropped pictures when you want them to go away. Fortunately, any turtle tracks will appear on top of these blank pictures. Colin Potts |
how do you return an array? I have been working with an array of turtles and I finally have my base functions down, but now that I have to put everything under 'choreography()', I am having trouble getting the other functions to access my turtle array.
You just...return the array. For instance, the following code -
def choreography():
arr = myFunction()
def myFunction():
array = [1,2,3,4]
return array would make it so arr is equal to [1,2,3,4]. Is that what you're asking? Student5493 |
It does not seem to work. When I call on the array (defined like above) in another definition i get the error:The error was:(I used both arr and array)
Name not found globally.
A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
Why wouldnt 'array' work, anyhow?
If you are trying to then use that array in still another function, you will need to pass it into it. I.e., let's say you have a doSomething() function, that you want to run from the main function. You would do -
def choreography:
arr = myFunction()
doSomething(arr)
def doSomething(arr):
commands to manipulate the turtles in arr
def myFunction():
array = [1,2,3,4]
return array You could also, if you don't understand that, use getTurtleList(world) to get an array containing all turtles in a given world. However, that means you will still have to pass the world around in between each function. Student5493 |
.
Should the file be named "hw6.py" like the previous assignments? The instructions don't say.
| Go ahead and keep to the naming structure. Since it doesn't say, I suspect we won't count off, but it never hurts to be safe. Student5493 |
how do you stop a sound? I am tired of waiting for the song to end to get a fresh start again. I tried blockingPlay another short clip concurrently with the sound that is desired to be halted, but it just played concurrently, instead of blocking play.
| I don't think there is a way to explicitly stop a sound (if there is, I've never found it), but, you can duplicate the effect, perhaps, by using the playInRange() function, which allows you to specify the samples to start and stop from. Student5493 |
I used the following code to use only the first part of my sound:
def soundClip(sound):
source=sound
dest=makeEmptySound(23)
srcIndex=1
for destIndex in range(1, getLength(dest)+1):
srcSample=getSampleValueAt(source, srcIndex)
setSampleValueAt(dest, destIndex, srcSample)
srcIndex=srcIndex+1
return dest
What it does to the particular sound I am using is slow down its playback. I don't really know how to fix it, so I adjusted for it. Will this cause any problems if played on other computers?
I cant get my turtles to move at the same time when asking 4 turtles to do the square function. How can I make them move simultaneously?
| Make them all move one after another and only call time.sleep after they have each moved. Colin Potts |
I get a "SoundException" error when I try to do makeSound. What gives?
| Read all of the error message. The file may not be a valid sound file. Colin Potts |
How can i addText onto the world, if there's a picture on top of the world?
| You could have a turtle drop a small canvas (using makeEmptyPicture) that you had drawn the text onto before dropping it. (This is similar to what a lot of people did for the movie homework to have their names stand out against a changing background.) Colin Potts |
is there a time.count() or something that keeps track of time? there is a finale that must be done on time, even if i have to truncate the preceding movement a little.
| You can find the current time using time.time(). If you call that before a loop and again repeatedly during a loop, subtracting the starting time from the current time, you get the elapsed time. When that value reaches your deadline, you can exit early from the loop using a break (that's in the book). Colin Potts |
Here's an example of what I mean:
import time
def loopFor(interval): # Interval is a number of seconds
start = time.time()
printNow(time.ctime())
while 1:
for i in range(1000): # A long, pointless calculation just to use up time
x = sqrt(math.pi * math.e) * sqrt(math.pi * math.e)
now = time.time()
elapsed = now - start
if elapsed > interval:
break
printNow(time.ctime())
I'm trying to get rid of the lines already showing up on the screen.
JES gives me an error message when I write:
for turtles in list:
clearPath(turtles)
and it doesn't seem to do anything if I write:
for turtles in list:
turtles.clearPath
the "hide" method is is similarly giving me an error as well and I'll need to use that later.
also redropping the picture doesn't cover any of the lines either.
suggestions?
| You need parentheses after the method call, as in turtles.clearPath(). Clearing won't have an effect until something draws on the world. So if you test this from the command area, you have to follow the call to clearPath with another turtle movement or world.repaint(). Colin Potts |
how can i write a function that creates an identical turtle multiple times so I dont have to make each one individually?
| Create each turtle in a loop. You can refer to the turtles subsequently by going through the world's getTurtleList(). Alternatively, you could put each turtle into a list of your own making as soon as you create it in the loop. The turtles won't look the same unless you also set their colors when you create them. Colin Potts |
what do we type in the command area when testing turtles?
| Moved here - where it belongs - from the soapbox - where it doesn't Colin Potts |
| What do you type in the command area to test anything? What is your function called? Colin Potts |
Do I have to import anything to use setShellColor? For some reason, its not in my turtle functions.
| It's not a function. It's a method, so you have to use dot notation. Colin Potts |
nevermind, its working
Does the choreography have to be planned? I've got my turtles doing a sort of random walk in time with the music. It's fun to watch but I wouldn't really call it choreography.
| Some part of it should be planned, yes. Not just have them stumbling around. Now, you -could- choreograph something (a dance, say), then introduce an element of randomness into it (by adding a random value to the movement or turning), and call it, I dunno, "The Drunken Waltz". But some element should show some planning, and allow you to describe what is happening. Student5493 |
How would I add a Turtle subclass to a world? makeTurtle() returns a generic Turtle object, and I can't find any way to add a turtle to the world other than that.
Never mind. Got it from the code page.
does our code have to look "pretty" becuase mine does what I want it to do, but I'm sure there is a much tidier way to organize it. In short, I was just wondering if we are being graded on how nice our code is organized.
| No, we don't grade on how nicely you organize your code. Thanks! Brittany Duncan |
| If we did, I'll tell you right now, I could never pass this class. Student5493 |
With the for loop I'm using, I can't get the turtles to all dance at the same time. They dance one after the other. Is that ok? Is there a way to make them all move at the same time?
Yes, there is a way to make them all dance at the same time. Rather then have
for i in range(x,y):
turtle1 do stuff
for i in range(z, a):
turtle2 do stuff You can instead put all your commands together in the same loop. For instance:
for i in range(x,y):
turtle1 do stuff
turtle2 do stuff This will make it so each turtle is updated on each iteration of the loop, thereby making them appear to move at the same time. Use if statements, and multiple loops as needed if you want a turtle to stop moving while another continues to. Student5493 |
I am having a hard time making the sound play while the turtles are moving. I have four sounds, and I do blockingPlay of each at the beginning of my function, but the turtles and the world don't appear until the sounds have finished playing. How do I make the sound play while the turtles are moving?
| Try just using play, instead of blockingPlay. blockingPlay tells JES to wait on doing anything else until playing completes. Student5493 |
I am also having a hard time with the sounds. I could not get the sound homework to work before. I get the Sound Exception error...too. i have played the music file many times..would that not make it valid. the file is a wave sound...any help?
| 'The Sound Exception error' isn't too helpful. It's a wav...is it truly a wav, i.e., not some other format (like an mp3), whose extension you've changed? It's 22khz? You're correctly pointing to it in JES, and using makeSound()? What exactly is the error message? |
Why can't I get the random choice to pass two arguments into "moveTo()"? The error is: expected 2 args; got 1.
Thanks for your help!
positions=((0,768),(0,768/2),(0,0),(1024,0),(1024/2,0),(1024,768))
asteroid.moveTo(random.choice(positions))
That isn't how it works. You have a tuple consisting of a lot of other tuples. You need to reference a location twice, as moveTo() does not know how to break up a tuple. What I would recommend is
positions=((0,768),(0,768/2),(0,0),(1024,0),(1024/2,0),(1024,768))
pos = random.choice(positions)
asteroid.moveTo(pos[0], pos[1])
That will get a tuple stored into pos, and will then set the first item as the x coord to move to, and the second as the y coord. Student5493 |
Brilliant!! Thank you so much!
So every so often my program makes JES flat-out crash. I'm not doing anything silly like dividing by zero or creating a gazillion objects or anything, and 80-90% of the time it works perfectly, but sometimes the whole thing will run for a while and then just die. No Java exceptions, no nothing, just a freeze-for-a-second-and-crash. Help?
| Only thing I can think of is it could -possibly- be taking up too much memory. Try taking note of when it crashes; did you have a lot of other things open at the same time? If it's not that, I really couldn't say. Student5493 |
Doesn't seem like it. I just tested it with just a few other things open; JES's memory usage went from ~60MB to ~100MB while the program was running; it ran fine for a little while, then crashed.
...and tested again a few times, same memory usage, no crash. Weird.
| This sounds like a problem with the configuration for the Java Virtual Machine that JES depends on. It sounds as though crossing your fingers is working occasionally, so you may not need to worry about it. If you want to diagnose what is causing JES to crash or you can't live with this while working on the homework, you could comment out those lines that create and play sounds and which create and drop pictures. Just concentrate initially on the movement. You will need to reintroduce the sounds later of course so that you can tweak the sleeps to make the turtles be synchronized with the music. The last thing to worry about is any dropped pictures or backgrounds. The activity is a bit like plugging in a bunch of appliances one by one to see which one finally causes a circuit to blow. Colin Potts |
The program's a behavioral-simulation thing, so the chances of having the same world state twice in a row are pretty low, but I can run it a few hundred times over if I really have to. Are the TAs likely to take off points if the program randomly makes JES die? I've added a comment asking them just to re-run it if something goes wrong, but still...
How do I make a world that will be global? When I insert the command cosmos = makeWorld(x,y)
before the function definitions and load the program, a blank world appears with dimensions x by y, and then I have to run the main function to get my galaxy going. Is there a way to make it global (so all functions recognize 'cosmos') but not have it appear until after the main function is run? I tried putting it in the main function, but the sub-functions did not recognize it. Any advice?
| You can create the world in your main function and pass it to the sub-functions as a parameter. |
Wow. Thanks. I can't believe I didn't think of that. -.-
Hahaha, I made my turtle spin as it orbited around the star. I don't presume this counts as a moon (the legs/head spinning around the shell)... :P
No, but seriously, I have to have another turtle enact the satellite object, right?
| Yep. Mathematically, a good way to do that is to, for each iteration of the for loop, think of the moon turtle starting from the same place as the planet turtle, turning a little bit, and moving forward a given amount. If that makes sense. Student5493 |
if I use a turtle to drop a picture, how do I get the turtle to delete it? I only want the picture to show for 2 seconds and then I want it to disappear
| You can't actually delete the dropped picture, but what you can do, is make an empty picture of the same dimensions (so a blank canvas), and drop that from the same place. Student5493 |
How do I make orbits occur simultaneously? I have them split into various functions, and JES runs the main function in the order of the subsequent functions. (e.g., planet 1, then planet 2; goal is to have both planets orbit concurrently)
| You could make each of the functions just do a bit of an orbit instead of a whole one and then have the main program call each of them in a loop. If the distances moved and the delays are both short enough on each iteration of the loop, you won't notice the constant switching when the animation runs. Colin Potts |
In our program, do we need a some explicit function or command that makes the turtles move to the sound? Also, do we need a function that makes the turtles move relative to each other, or can they just move on their own, but still look like they are dancing together (for option #1)?
| Nope. I'm not even sure if you -could- make the turtle's movement dependent on the sound, rather then just appear like it is, at least in JES. And, no, the turtles can move on their own, as long as they look like they're dancing together. Student5493 |
How do I run two functions independently of each other at the same time? Like I am trying to create an audience function that does it stuff regardless of what the dancers do.
| You can't do this easily (at least not without another course or two). You're going to have to dissect your functions so that each does a bit of dancing or a bit of audience stuff. To keep switching between them you will need a main function that calls each function repeatedly inside a loop. Colin Potts |
This is the idea... You need two lists of turtles (a troupe of dancers and an audience) and suitable definitions of when to stop and how long to wait on each iteration of the loop:
def mainLoop(dancers, audience):
while not timeToStop:
doABitOfDancing(dancers)
doABitOfWatching(audience)
time.sleep(aMoment)
I have read the above comments but am still having trouble getting my tuttles to move at the same time. I am doing the football play option and this is a piece of my code for two receivers. Any suggestions on how to get them to run at the same time?
def moveToDest(turtle, dist):
for pos in range (1,dist+1):
turtle.forward(1)
time.sleep(.01)
def moveDiagonal(turtle, dist):
for pos in range(1, dist+1):
turtle.forward(2)
time.sleep(.015)
# curl route
moveToDest(wr,100)
wr.turnToFace(qb)
moveToDest(wr,20)
# corner route
moveToDest(sr,160)
sr.turnToFace(800,0)
moveDiagonal(sr,150)
Any suggestions?
| I assume you're a different student, since I wouldn't call the footballers dancers! Your code appears to have moveDiagonal itself call moveToDest and moveDiagonal (recursively). Or is the final block of code - ignoring the indentation - inside another main function? Colin Potts |
| Try the strategy I posted above. What you're doing, in contrast, is ALL the moveToDest movement (twice) and THEN all the moveDiagonal movement. Colin Potts |
You were right, the final block of code is inside another main function. Sorry about the indentation. So just to be clear you are saying that instead of moving a turtle the entire distance I need it to move I should move it a little then move the next one? Will this work for all 11 turtles? It seems that I am getting a step like appearance.
| Well, that's progress, isn't it? Try moving by smaller amounts. And yes, it will work for as many turtles as you like. Colin Potts |
To make the programming easier I decided to make every turtle have an instance of a class for itself... But it seems no matter what I do, JES doesn't let me refer to them. In the class I have
def __init__(self, posX, posY, facing, spin)
t = makeTurtle(field)
t.penUp()
t.moveTo(posX, posY)
t.turn(facing)
rot = spin
origRot = spin
and that makes each of the turtles fine. So I make an array of them with
for i in range(10):
turtleArray[i] = mcREMOVED(100, 200 + (i*40), 90, 2)
and that works fine. But when I try to move them
for i in range(10):
turtleArray[i].self.t.forward(20)
or
for i in range(10):
turtleArray[i].t.forward(20)
that gives me the error
The error was:instance of 'mcREMOVED' has no attribute 't'
Attribute not found.
You are trying to access a part of the object that doesn't exist.
| If you're going to be accessing the array-objects' turtles with the notation later, you should be setting things up like this: |
self.t = makeTurtle(field)
self.t.penUp()
# etc.
| You can't access a class's local variables, only the ones that're defined as its properties. |
First off, dunno if it was you or someone else, but let's leave the 'add' block in place. Second...you're certainly jumping through hoops to achieve an effect. I can't even follow your code. Perhaps you might try something simpler? Say...
for i in range(0, howevermanyturtlesyouwant):
turtle = makeTurtle(world)
for turtle in getTurtleList(world):
turtle.penUp()
for i in range(0,len(getTurtleList(world)):
getTurtleList(world)[i].setXPos(100)
getTurtleList(world)[i].setYPos(i*40)
getTurtleList(world)[i].turn(90)
for turtle in getTurtleList(world):
turtle.forward(20) Barring mistakes on my part, that will do what it -appears- you want the above code to do, and is far easier to trace through. Basically, you're making a buncha turtles in the first loop, and then the rest duplicate the effect of what your code appears to want to do, by using the function getTurtleList(world), which returns an array containing every turtle in a given world. Student5493 |
i'm having a problem getting my turtles to hide. at the end of my function, I want them to just disappear, so I'm using each of the turtles names and adding .hide() but they won't disappear! ???
Could be a number of different things. Try using this instead -
for turtle in getTurtleList(world):
turtle.hide() Replacing world with the name of the world you created, if it's different. If that doesn't work, then the snippet of code is not being run, for whatever reason. If it does work, then whatever you were trying to do wasn't working, but you now have a solution, so yay. Student5493 |
is there any way I can make turtles who have coordinates off the screen not be "adjusted" to be on the screen?
| Nope! Depending on your code, though, it might be feasible to store the location of where the turtle would be off the screen in a list (or two variables), and then change that based on how you want the turtle to move. Or, you could make the world bigger to accommodate the turtle, but have an if clause to check to see if its x or y position is greater/smaller then a given threshold, and if it is, hide it (and then show it when it crosses back over the threshold). How you choose to implement that, if you do, is up to you. Student5493 |
i can't find the commands for making a turtle pick up and drop pictures....
I can't get the hide and show functions to work either. I wasn't sure if it was just my code being goofy so I made this simple function
def test():
o = makeWorld(800, 800)
j = makeTurtle(o)
j.moveTo(300, 300)
time.sleep(2)
j.hide()
print j.isVisible()
time.sleep(2)
j.show()
print j.isVisible()
and sure enough the turtle never disappears, but it prints out the values 0 and 1, showing that the program thinks j is gone
| Good detective work. However, you have to repaint the world (or move/turn a turtle, which repaints it automatically) before the hide/show take effect visibly. Colin Potts |
Where can i find sound clips of marching or the waltz or anything that will work i have been looking for an hour?
| After about 2 minutes of Googling, here's an MP3 clip of the Waltz of the Flowers (from the Nutcracker, so nice and seasonal). Take it, convert it to a wav by using this website - http://media-convert.com/ , being sure to, when it asks, change from 44100 hertz to 22050 (leave all other options their default). Student5493 |
I cannot get my turtles to move while the sound plays. Someone above had that problem using blockingPlay but I am simply using the play command. any suggestions?
| JES won't wait for the sound to stop before moving your turtles if you use play (as opposed to blockingPlay) so the problem must be with your program's structure. Comment out the play commands and see whether the turtles ever actually move. Colin Potts |
A bit of help for people who can't get their sounds to play:
You are most likely using a .mpeg compressed wav file. JES will only accept uncompressed PCM type wavs.
If you check the properties of your .wav and see that it is compressed into .mpeg, download the converter from the link below. You don't have to buy or register anything, just download and follow the instructions. They are pretty simple. This was the problem with mine and now it plays perfectly.
http://www.audiotool.net/EaseAudioConverter/index.htm
still confused...
setPicture(Picture pict) what does Picture refer to? i defined pict as =makePicture(......)
getPicture() .......does this mean i would just type bob.getpicture() after first setting picture?
drop(Picture dropPicture). ......what do these variables refer to? pict or Picture?
I just want to set a background to my scene.
| If you want to set a background, the easiest thing to do is make an extra turtle, put its pen up, hide it, move it to 0,0, and use turtle.drop(pic). Student5493 |
My music file is too big to load onto T-square (it's about a 3 min WAV file). What should I do?!?
| Do you use all of it? If not, you might be able to use some utility to get rid of parts of it that aren't used. Otherwise, try zipping it. If it still won't fit, email your TA twice, one telling them you're sending them the music file, the other with the actual music file attached (the first is in case the second goes boink due to size or something, so you're covered just in case). Student5493 |
thanks...you're my TA so i'm sending it to you. (I can't figure out how to cut it, even though I only use half)
ok, what do you type into the command area to test a turtle program?
| There's nothing special about a turtle program in this respect. Just use the main function name and supply it with arguments. Without seeing your program, it's impossible to say exactly, but the principle is the same as all the programs you've been writing for the last four months. Colin Potts |
um, the music file won't even send via email. nothing will work, so what should i do?
| Either find a way to shorten it (by chopping off the part you aren't using; use a function to copy over just the samples you're playing to an empty sound, and write that newly-no-longer-empty sound to a file; use that file in the rest of your code), or get it to your TA in whatever manner works. Burning it to a CD, say, or getting some free web space that allows you to upload and transfer large amounts (http://www.freehostia.com lets you have around 250 megs for free, I believe, plus 5 gigs bandwidth, though registration confirmation takes a little time). Student5493 |
How do you make two turtles do the same thing at the same time? How do you make two turtles do different things at the same time?
| Make sure there is no call to sleep between them doing their things. BTW, there are some examples of this on the code/slides page, such as the predatory/prey turtles. Colin Potts |
Is that what the classes are for?
| No. Ignore the fact that it is object-oriented. The two turtles do a little bit of movement each time through the loop and the sleeps are short, so it looks as though they are moving simultaneously. Colin Potts |