Okay, so here is the exercise I'm trying to complete. Create a turtle object in turtle graphics in a coin field, with each coin an object itself. Make the turtle randomly move while simultaneously collecting/eating coins(in my while loop, I used the random generator). As the turtle collects a coin, the turtle gets bigger and the coins should dissapear from the screen(also in my while loop, I made the shapesize increase,the coin it contacted white). When the turtle leaves the coin field boundaries, the turtle should stop and a score should be printed (in my while loop, I have two conditionals for this and I increased the score in the while loop).
The problem is, I keep getting errors, that I don't understand, which means I don't know if my code works or not. So can you help me by spotting the error(s) and seeing if my code works? Thanks. - from turtle import *
-
from random import *
-
-
s = Screen()
-
s.setup(560,560)
-
s.title("A turtle collecting coins!!")
-
-
s.tracer(False)
-
writer = Turtle(visible = False)
-
writer.penup()
-
writer.goto(0, -275)
-
-
coins = []
-
for i in range(-4,5):
-
for j in range(-4, 5):
-
c = Turtle(shape="circle")
-
c.color("", "orange")
-
c.shapesize(0.5)
-
c.goto(40*i, 40*j)
-
coins.append(c)
-
s.tracer(True)
-
-
Molly=Turtle(shape="turtle")
-
Molly.color("","green")
-
Molly.shapesize(1)
-
Molly.goto(-175,-175)
-
-
score=0
-
while True:
-
Molly.fd(randint(10,30))
-
if Molly.distance(c)<15:
-
c.color("","white")
-
Molly.shapesize+=1
-
score+=1
-
Molly.rt(randint(0,360))
-
if Molly.distance(c)<15:
-
c.color("","white")
-
Molly.shapesize+=1
-
score+=1
-
Molly.bk(randint(10,30))
-
if Molly.distance(c)<15:
-
c.color("","white")
-
Molly.shapesize+=1
-
score+=1
-
Molly.bk(randint(10,30))
-
if Molly.distance(c)<15:
-
c.color("","white")
-
Molly.shapesize+=1
-
score+=1
-
Molly.lt(randint(0,360))
-
if Molly.distance(c)<15:
-
c.color("","white")
-
Molly.shapesize+=1
-
score+=1
-
-
if Molly.xcor()<-175 or >175:
-
write("Score:"+str(score),align="center",font=("Courier",14,bold))
-
break
-
-
if Molly.ycor()<-175 or >175:
-
write("Score:"+str(score),align="center",font=("Courier",14,bold))
-
break
2 1601 Glenton 391
Recognized Expert Contributor
Comments in your code would help you and us a lot!
However, in your loop you have "if Molly.distance( c)<15". This doesn't make sense to me. Surely c is just the last coin you've generated! So you're only checking against one of your coin!
I'd also be inclined to randomise the direction too and save some lines of code, rather than go round in circles. The problem with the current approach is that you don't check whether the turtle is out of bounds after each move. Only after each fourth move.
Another issue may be that you do the move, and then check whether it's out of bounds. That could certainly give an error, but I'm not too familiar with turtle.
Perhaps you could tell us what errors you get too?
A good way to debug is to step through the code. Or, failing that, copy it line by line into the python prompt and see what each line does (except the while loop line).
Debugging is about as important as writing the code and probably harder to do! So it's worth getting good at it on this kind of simple program...
Good luck, and let us know how it goes...
Hi,
there were errors in you code
# if Molly.xcor()<-175 or >175:
# write("Score:"+ str(score),alig n="center",font =("Courier",14, bold))
# break
#
# if Molly.ycor()<-175 or >175:
# write("Score:"+ str(score),alig n="center",font =("Courier",14, bold))
# break
see the correction
# if (Molly.xcor() < -175) or (Molly.ycor() >175):
# write("Score:"+ str(score),alig n="center",font =("Courier",14, bold))
# break
#
# if (Molly.ycor() < -175) or (Molly.ycor()>1 75):
# write("Score:"+ str(score),alig n="center",font =("Courier",14, bold))
# break
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mike |
last post by:
I am sure that I am making a simple boneheaded mistake and I would
appreciate your help in spotting in. I have just installed
apache_2.0.53-win32-x86-no_ssl.exe
php-5.0.3-Win32.zip
Smarty-2.6.7.tar.gz
on a system running WindowsXP SP2.
Apache and PHP tested out fine. After adding Smarty, I ran the
following http://localhost/testphp.php
|
by: John Bailo |
last post by:
The war of the OSes was won a long time ago.
Unix has always been, and will continue to be, the Server OS in the form
of Linux.
Microsoft struggled mightily to win that battle -- creating a poor man's
DBMS, a broken email server and various other /application/ servers to
try and crack the Internet and IS markets.
In the case where they didn't spend their own money to get companies to
|
by: TokiDoki |
last post by:
Hello there,
I have been programming python for a little while, now. But as I am
beginning to do more complex stuff, I am running into small organization
problems.
It is possible that what I want to obtain is not possible, but I would
like the advice of more experienced python programmers.
I am writing a relatively complex program in python that has now around
40 files.
|
by: Brian W |
last post by:
Yet another editor problem
To reproduce do the following
1) Open a Webform and switch to HTML edit mode
2) Enter the Following (include spaces)
This is some text before <asp:hyperlink id="hl1" runat="server "
navigateurl="http://www.microsoft.com">This is my link</asp:hyperlink> And
this is my text after the Hyperlink
|
by: Anoop |
last post by:
Hi,
I am new to this newsgroup and need help in the following questions.
1. I am workin' on a GUI application. Does C# provides Layout Managers the
way Java does to design GUI? I know that it can be done using the designer
but I intentionally don't want to use that. The one reason is that you
cannot change the code generated by the designer. The other could be that
you have more free hand and control to design your GUI.
2....
| |
by: Tom Cole |
last post by:
While I've done javascript work for as long as I can remember (since
Netscape first released it), I've only ever used it for trivial
things, change a color here, validate a text element there, blah blah
blah. With Ajax (actually, the uncovering of the XmlHTTPRequest
object) I absolutely see the benefit of moving more of the UI work to
the client, rather than doing page refreshes.
I know there are a bunch of libraries out there...
|
by: john_smith_nebraska |
last post by:
I am using VISTA . I have IIS enabled .
I installed VS 2005 successfully.
Under old ASPX you knwo you go to inetpub wwwroot and run a test
default.aspx file to see if it executes properly.
I pulled a starter program from Microsoft site and pasted it into VS
2005 at the default.aspx page of a new wesite called website1. I made
sure website1 was added into IIS and was running.
|
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= |
last post by:
Hello,
I know I sound like a one-note Johnny on this but I'm still looking for a
solution. I need to display characters coming in from a serial port or a
socket. I also need to be able to type characters into the display myself -
but that's not the main issue at this time. I've tried a scrolling multiline
text box but once the original viewable area fills up and it starts scrolling
the flashing of the entire area drives me nuts. The...
|
by: jabbari |
last post by:
Hello, Please Help us...!
I have a big problem ,so i searched on google and other search engine ,then I realized that so many other people have the same problem and they, all, have'nt been able to solve it.
My problem is:
I want to import a dll in asp.net, and I have a major problem
First i want to say that this dll works fine in a windows application written in C#, and It works fine when I use the ASP.NET development server (Local...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |