Okay, so you probably haven't seen my previous question, whats wrong with this code. For those who have, you know what I'm talking about. If not, heres what I have to do.
Randomly place a pyramid (yellow, large turtle) on the screen. Have a second turtle move
randomly until it is close to the pyramid. Stop the seeking turtle once it has found the pyramid.
We have to do this using object-oriented coding. We don't have to use classes, but I just chose to do so. This also needs to happen in turtle graphics. Heres my code: -
from turtle import *
-
from random import *
-
-
def find_the_pyramid():
-
class Pyramid(Turtle()):
-
color="yellow"
-
shape="triangle"
-
shapesize=4
-
def __init__(self,color,shape,shapesize):
-
self.color=color
-
self.shape=shape
-
self.shapesize=shapesize
-
self.goto(randint(-100,100),randint(-100,100))
-
self.goto(randint(-100,100),randint(-100,100))
-
-
-
class Lost_Turtle(Turtle()):
-
name="Kirk"
-
color="brown"
-
shape="turtle"
-
shapesize=2
-
def __init__(self,name,color,shape,shapesize):
-
self.name=name
-
self.color=color
-
self.shape=shape
-
self.shapesize=shapesize
-
-
Sphinx=Pyramid("yellow","triangle",5)
-
Billy=Lost_Turtle("Billy","blue","turtle",3)
-
-
-
while(Billy.distance(Sphinx)>20):
-
Billy.forward(randint(40,80))
-
Billy.right(randint(0, 360))
-
-
find_the_pyramid()
The only problem is that I keep getting sent errors, which I don't understand. If so, can you take a look at my code and try to tell me what I'm doing wrong. Much appreaciated. Also, if there's an easier way to do this without using classes, that would be great. Thanks.
2 1949 bvdet 2,851
Expert Mod 2GB
ccgrl451,
Please use code tags when posting code. See posting guidelines here. I appreciate your creative use of indentation, but code tags are much better suited to displaying code.
To inherit from another class, leave off the parentheses:
There is no need to create class variables in your case. You can assign defaults like this: - def __init__(self, color='yellow', shape='triangle', shapesize=4):
I don't see how you can be sure you will ever find the pyramid by moving only forward and right. You should check the turtle's position after every move. In your while loop, you are moving twice before checking the location. I would do something like this: - while True:
-
# 'a' and 'b' can be any numbers of your choice
-
Billy.forward(randint(a,b))
-
if Billy.distance(Sphinx)>20:
-
break
-
Billy.right(randint(a,b))
-
if Billy.distance(Sphinx)>20:
-
break
-
Billy.backward(randint(a,b))
-
if Billy.distance(Sphinx)>20:
-
break
-
Billy.left(randint(a,b))
-
if Billy.distance(Sphinx)>20:
-
break
-
# summarize the results here
-
return 'summarized results'
I would not encapsulate the classes and movement in the same function. I would suggest this format:
imports
class 1 definition
class 2 definition
define function "main()" where you create class instances and random movements and return results
Then: - if __name__ == "__main__":
-
main()
Sorry this took too long to check. I was busy. Thanks this helped me tons. Also, the link to the posting guidelines are great for future reference!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: Sarah Tanembaum |
last post by:
Beside its an opensource and supported by community, what's the fundamental
differences between PostgreSQL and those high-price commercial database (and
some are bloated such as Oracle) from...
|
by: Vic |
last post by:
Dear All,
I found this code snippet on this list (taken from a nice webpage of a
courteous fellow), which I used to filter a form on a combo box. I
wanted to repeat the same code to have an...
|
by: ben |
last post by:
this programme from sedgewick's 'algorithms in c parts 1-4' "is a
sample client program that ... uses a symbol table to find the distinct
values in a sequence of keys (randomly generated or read...
|
by: Keith K |
last post by:
Having developed with VB since 1992, I am now VERY
interested in C#. I've written several applications with
C# and I do enjoy the language.
What C# Needs:
There are a few things that I do...
|
by: Jason Huang |
last post by:
Hi,
Would someone explain the following coding more detail for me? What's the
( ) for?
CurrentText = (TextBox)e.Item.Cells.Controls;
Thanks.
Jason
|
by: Xah Lee |
last post by:
in March, i posted a essay “What is Expressiveness in a Computer
Language”, archived at:
http://xahlee.org/perl-python/what_is_expresiveness.html
I was informed then that there is a academic...
|
by: fiaolle |
last post by:
Hi
The first set of source code is the class for a combobox in a grid,
hopefully. In the second set of code we try to use the combobox class, but
the grid is empty.
I don't understand how this...
|
by: Enkidu |
last post by:
Beginner question, sorry!
I am using indexers to access an array of StringBuilders in an instance
of a class:
Getting:
value = board1;
Setting:
board1 = value1 ;
|
by: Tubular Technician |
last post by:
Hello, World!
Reading this group for some time I came to the conclusion that
people here are split into several fractions regarding size_t,
including, but not limited to,
* size_t is the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| | |