I am having problems drawing circles and fitting the equation together I want to draw 3 circles in random locations that I have set with random radiuses like I set up but I can't fit the circumference equation in the draw class for it to work. like this
import turtle
from turtle import RawTurtle
class circle:
def __init__(self, R, D, turtle):
self._a = R
self._b = D
self._turtle = turtle
turtle.up()
def set_initial_position(self, x: float, y: float):
self._turtle.setpos(x, y)
def draw(self):
t = self._turtle
for _ in range(int(draw)):
turtle.circle(25)
turtle.forward(10)
t.up()
def hide(self):
self._turtle.clear()
r1 = circle(5, 5, RawTurtle(turtle.getscreen()))
r2 = circle(5, 10, RawTurtle(turtle.getscreen()))
r3 = circle(10, 20, RawTurtle(turtle.getscreen()))
r2.set_initial_position(-50, 100)
r3.set_initial_position(60, 200)
r1.draw()
r2.draw()
r3.draw()
r2.hide()
turtle.mainloop()
I can't tell what your program is trying to do from your explanation and code but below I've reworked it into something that runs from which you should be able to build what you really want:
from turtle import Screen, Turtle
class circle:
def __init__(self, radius, degrees, turtle):
self._r = radius
self._d = degrees
self._turtle = turtle
self._turtle.penup()
def set_initial_position(self, x, y):
self._turtle.setposition(x, y)
def draw(self):
self._turtle.pendown()
self._turtle.circle(self._r, extent=self._d)
self._turtle.penup()
def hide(self):
self._turtle.clear()
screen = Screen()
r1 = circle(150, 45, Turtle())
r2 = circle(15, 180, Turtle())
r3 = circle(100, 360, Turtle())
r2.set_initial_position(-50, 100)
r3.set_initial_position(60, 200)
r1.draw()
r2.draw()
r1.hide()
r3.draw()
screen.mainloop()
You seem to be using the tkinter embedded turtle API for a standalone turtle program so I've switched the method calls. And as the Python error notes, you use a draw variable which you fail to define. (And you shouldn't define one as you already have a draw function -- choose a different name for the variable.)
Related
I am a novice to Python trying to make the game Pong. I have created a Paddle class with the Turtle Graphics module, but I can't get the paddle to move. I just want to start with one direction and then down shouldn't be too hard from there. Can anyone see what I am doing wrong with my method?
from turtle import Turtle
COORDINATES = [(350, 20), (350, 0), (350, -20)]
X_COORDINATES = [350, 350, 350]
Y_COORDINATES = [20, 0, -20]
class Paddle(Turtle):
def __init__(self):
super().__init__()
self.paddle = []
self.create_paddles()
self.coordinate_number = 0
def create_paddles(self):
for coordinates in COORDINATES:
self.paddle_block = Turtle(shape='square')
self.paddle_block.goto(coordinates)
self.paddle_block.color('white')
self.paddle.append(self.paddle_block)
def w(self):
global Y_COORDINATES
Y_COORDINATES = [coordinate + 100 for coordinate in Y_COORDINATES]
for self.paddle_block in self.paddle:
self.paddle_block.goto(X_COORDINATES[self.coordinate_number], Y_COORDINATES[self.coordinate_number])
self.coordinate_number += 1
self.coordinate_number = 0
I tried to iterate through the y-coordinates and add to each of them with my function. From there, I tried to iterate through each paddle block and move it's current location to a new one, taking in the newly updated y coordinate. I expect movement, but I am not seeing any movement whatsoever.
This isn't the usual approach to this problem, but I can see why it might be advantageous. Your primary issue seems to be not being able to determine what should be global, what should be local, and what should be a property. Let's make this work to demonstrate the use of all three:
from turtle import Screen, Turtle
COORDINATES = [(350, 20), (350, 0), (350, -20)]
class Paddle(Turtle):
def __init__(self):
super().__init__()
self.paddle = []
self.coordinates = list(COORDINATES) # make copy
self.create_paddles()
def create_paddles(self):
for coordinate in self.coordinates:
paddle_block = Turtle(shape='square', visible=False)
paddle_block.penup()
paddle_block.color('white')
paddle_block.goto(coordinate)
paddle_block.showturtle()
self.paddle.append(paddle_block)
def move_up(self):
self.coordinates = [(x, y + 10) for x, y in self.coordinates]
for coordinate_number, paddle_block in enumerate(self.paddle):
paddle_block.goto(self.coordinates[coordinate_number])
def move_down(self):
self.coordinates = [(x, y - 10) for x, y in self.coordinates]
for coordinate_number, paddle_block in enumerate(self.paddle):
paddle_block.goto(self.coordinates[coordinate_number])
screen = Screen()
screen.bgcolor('black')
paddle_1 = Paddle()
screen.onkey(paddle_1.move_up, 'w')
screen.onkey(paddle_1.move_down, 's')
screen.listen()
screen.mainloop()
I'm currently creating a drawing project, where I use random to create random star points in the sky. I currently have the ability for two points to not be the same, but I would like to find a way to make it so they wouldn't land in a x radius circle. Is there any way in python to complete this
import turtle as t, random as r
screen=t.Screen()
screen.bgcolor("#3A3B3C")
t.speed(1)
def randomStars(y, num):
t.penup()
t.pensize(2)
t.color("white")
locations = []
for x in range(num):
repeat = True
t.penup()
t.seth(90)
y_pos = starLocationY(y)
x = starLocationX()
while repeat == True:
if [x,y_pos] in locations:
y_pos = starLocationY(y)
x = starLocationX()
else:
locations.append([x,y_pos])
repeat = False
t.goto(x,y_pos)
t.pendown()
t.seth(60)
t.fd(2)
t.fd(-2)
t.seth(-60)
t.fd(2)
t.fd(-2)
t.seth(240)
t.fd(2)
t.fd(-2)
t.seth(120)
t.fd(2)
t.fd(-2)
randomStars(85,30)
p.s: I'm using trinket for the project, as required by the class, so the modules are limited
link to trinket:https://trinket.io/python/9776ba1b8a
We can use any on a generator invoking turtle's distance() method on the elements of your locations list. Easier than it sounds:
from turtle import Screen, Turtle
from random import randint
EXCLUSION_RADIUS = 35 # in pixels
def starLocationY(y):
return randint(y, 190)
def starLocationX():
return randint(-190, 190)
def randomStars(y, number):
turtle.penup()
turtle.pensize(2)
turtle.color('white')
locations = []
for _ in range(number):
y_pos = starLocationY(y)
x = starLocationX()
while True:
turtle.goto(x, y_pos)
if any(turtle.distance(location) < EXCLUSION_RADIUS for location in locations):
y_pos = starLocationY(y)
x = starLocationX()
else:
locations.append((x, y_pos))
break
turtle.pendown()
for heading in range(0, 360, 120):
turtle.setheading(heading)
turtle.forward(2)
turtle.backward(4)
turtle.forward(2)
turtle.penup()
screen = Screen()
screen.bgcolor('#3A3B3C')
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest') # because I have no patience
randomStars(85, 20)
screen.exitonclick()
How to make draw like this with turtle?
right now my code looks like that:
class OperationsOnSets():
def __init__(self):
self.font_style = ("Times New Roman", 40, "bold")
def move_turtle_pos(self, x, y, turtle):
turtle.up()
turtle.setpos(x, y)
turtle.down()
def set_d(self):
turtle = Turtle()
turtle.pensize(2)
turtle.speed(2)
self.move_turtle_pos(-100, -50, turtle)
turtle.circle(200)
self.move_turtle_pos(100, -50, turtle)
turtle.circle(200)
sleep(5)
turtle.mainloop()
example = OperationsOnSets()
example.set_d()
and here is result
I though about pasting image, or make algorithm that would draw a lines, but I don`t know how to realize it. So I hope that someobody of you will help me with it...
My philosophy of turtles, is avoid the math by getting the turtle to do the hard work for you. Not quite perfect, but pretty close:
from turtle import Screen, Turtle
RADIUS = 200
NUMBER_LINES = 14
class OperationsOnSets():
def __init__(self):
self.turtle = Turtle()
self.turtle.pensize(2)
self.turtle.fillcolor(screen.bgcolor())
def move_turtle_pos(self, x, y):
self.turtle.penup()
self.turtle.setpos(x, y)
self.turtle.pendown()
def set_d(self):
self.move_turtle_pos(-RADIUS/2, -RADIUS)
points = []
for _ in range(NUMBER_LINES):
self.turtle.circle(RADIUS, 180 / NUMBER_LINES)
points.append(self.turtle.position())
for _ in range(NUMBER_LINES):
self.turtle.circle(RADIUS, 180 / NUMBER_LINES)
position = self.turtle.position()
self.turtle.goto(points.pop())
self.turtle.goto(position)
self.move_turtle_pos(RADIUS/2, -RADIUS)
self.turtle.begin_fill()
self.turtle.circle(RADIUS)
self.turtle.end_fill()
self.move_turtle_pos(-RADIUS/2, -RADIUS)
self.turtle.circle(RADIUS, 180)
self.turtle.hideturtle()
screen = Screen()
example = OperationsOnSets()
example.set_d()
screen.mainloop()
We make the turtle stop along its way around the circle to record points we'll need to finish the drawing.
How can I code turtle to draw a square like in paint, using two clicks -- one to set the starting point and a second click for the defined edge length?
I wanna: click once to set the center of a square; move the mouse to define the edge-length of the square; click a second time to draw the square with the defined edge-length and center point. But I couldn't manage it; do you have any ideas? I also tried to create a color palette so I can change the drawing color of my turtle if I click on one of the colors but it doesn't work.
Here's my code:
import turtle
beni=turtle.Screen()
beni.setup(900,700,)
t=turtle.Turtle()
#color palette
t1=turtle.Turtle()
t1.shape("circle")
t1.color("blue")
t1.penup()
t1.setposition(250, 300)
t2=turtle.Turtle()
t2.shape("circle")
t2.color("red")
t2.penup()
t2.setposition(220,300)
t3=turtle.Turtle()
t3.shape("circle")
t3.color("green")
t3.penup()
t3.setposition(280, 300)
def funktion(x,y):
t.color("green")
t.pencolor("green")
t3.onclick(funktion)
def funktion(x, y):
t.color("blue")
t.pencolor("blue")
t2.onclick(funktion)
def funktion(x, y):
t.color("red")
t.pencolor("red")
t1.onclick(funktion)
#freehandmode
def freehandmode(x, y):
t.ondrag(None)
t.goto(x, y)
t.ondrag(freehandmode)
t.ondrag(freehandmode)
#linemode
class Drawer:
def __init__(self):
self.drawing = False
def click(self, x, y):
if self.drawing:
turtle.down()
turtle.goto(x, y)
self.drawing = False
else:
turtle.up()
turtle.goto(x, y)
self.drawing = True
d = Drawer()
beni.onclick(d.click)
#squaremode
turtle.up()
turtle.mainloop()
There are several ways to go about this, I'm going to do it by swapping event handlers so the first and second clicks have different meanings. I'm going to use circles (pick the center, then the edge) to keep my example simple and leave some geometry for you to ponder:
from turtle import Screen, Turtle
def draw_circle(x, y):
screen.onclick(None)
center = turtle.position()
turtle.setposition(x, y)
turtle.setheading(turtle.towards(center) - 90)
turtle.pendown()
turtle.circle(turtle.distance(center))
turtle.penup()
turtle.clearstamps()
screen.onclick(pick_center)
def pick_center(x, y):
screen.onclick(None)
turtle.setposition(x, y)
turtle.stamp()
screen.onclick(draw_circle)
turtle = Turtle()
turtle.hideturtle()
turtle.shape('circle')
turtle.shapesize(0.5)
turtle.penup()
screen = Screen()
screen.onclick(pick_center)
screen.mainloop()
How can I fix this code to make the turtle a random color? I want to be able to click to make the turtle turn and change color.
import turtle
import random
turtle.colormode(255)
R = 0
G = 0
B = 0
def color(x, y):
turtle.color((R, G, B))
def turn(x, y):
turtle.left(10)
for i in range(10000):
turtle.onscreenclick(turn)
turtle.forward(1)
turtle.onrelease(color)
R = random.randrange(0, 257, 10)
B = random.randrange(0, 257, 10)
G = random.randrange(0, 257, 10)
def color(x, y):
turtle.color((R, G, B))
I want to be able to click then the turtle turns then change color.
I believe this does what you describe:
import turtle
import random
def change_color():
R = random.random()
B = random.random()
G = random.random()
turtle.color(R, G, B)
def turn_and_change_color(x, y):
turtle.left(10)
change_color()
turtle.onscreenclick(turn_and_change_color)
def move_forward():
turtle.forward(1)
turtle.ontimer(move_forward, 25)
move_forward()
turtle.mainloop()
Rather than the range(10000) loop, it uses a timer to keep the turtle moving which also allows the event loop to run properly. It should keep running until you close the turtle window:
import turtle, random
def color(x, y):
R = random.randrange(0,257,10)
G = random.randrange(0,257,10)
B = random.randrange(0,257,10)
turtle.color(R,G,B)
turtle.left(10)
turtle.colormode(255)
for i in range(10000):
turtle.onscreenclick(color)
turtle.forward(1)