I have an assignment where I have to make a game in python with turtles.
I am able to generate a grid like I want to, and I am able to generate multiple different turtles. my
- I have no clue where to start in terms of figuring out how to have each of my three different turtles each move throughout the board randomly, while staying on the grid line.
- the "game" must stop after two of the turtles have hit the edge of the screen, I do not know how to do this, either.
here is what I have so far:
import turtle
import random
turtle.pensize(2)
turtle.penup()
turtle.goto(-160,-160)
turtle.pendown()
turtle.color("green")
turtle.speed(0)
for i in range(4):
turtle.forward(320)
turtle.left(90)
for y in range(-160,160,80):
for x in range(-160,160,80):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
for k in range(4):
turtle.forward(40)
turtle.left(90)
turtle.end_fill()
for y in range(-120,160,80):
for x in range(-120,160,80):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
for k in range(4):
turtle.forward(40)
turtle.left(90)
billy = turtle.Turtle()
billy.shape("turtle")
billy.color("red")
billy.penup()
billy.left(90)
rick = turtle.Turtle()
rick.shape("turtle")
rick.color("orange")
rick.penup()
mike = turtle.Turtle()
mike.shape("turtle")
mike.color("purple")
mike.penup()
turtle.done()
strong guidance would be VERY helpful.
You could take advantage of object orientation. Here is my version of your turtle script:
import turtle
import random
# I added randint.
from random import randint
# Let's define a class so we can create turtles
class MyTurtle():
# Here's the constructor. Where we give some initial values.
def __init__(self, name, color):
self.name = name
self.color = color
self.turtle = turtle.Turtle()
self.turtle.shape("turtle")
self.turtle.color(self.color)
self.turtle.speed(1)
self.turtle.pd()
# We need to know if this turtle is off the board.
def off_board(self):
x = self.turtle.xcor()
y = self.turtle.ycor()
return x < -160 or 160 < x or y < -160 or 160 < y
# calling this will move the turtle in a random direction.
def move(self):
turn = randint(0,2)
if turn == 1:
self.turtle.lt(45)
elif turn == 2:
self.turtle.rt(45)
self.turtle.forward(5)
# Put your code for drawing the grid
# Let's create some turtles and start moving them.
turtles = []
turtles.append( MyTurtle('billy', 'red'))
turtles.append( MyTurtle('chris', 'blue'))
turtles.append( MyTurtle('lilly', 'pink'))
turtles.append( MyTurtle('kevin', 'green'))
ok = True
while ok:
for t in turtles:
t.move()
if t.off_board():
print ("Turtle %s wins!!!" % (t.name))
ok = False
turtle.done()
Come on! Just one upvote for this awesome turtle script?
Related
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.
I am trying to move the square to right and draw 36 squares to make a circle out of it:
def draw_art(x,y):
print("Started the op")
window = turtle.Screen()
window.bgcolor("blue")
print("start the drwaing")
c =0
brad = turtle.Turtle()
brad.shape("turtle")
brad.color("green")
brad.speed(3)
print("enter loop")
for i in range(1,37):
draw_square(x,y)
brad.right(10)
window.exitonclick()
draw_art(200,90)
This might get you closer to what you're trying to do. It defines the missing draw_square() function and then draws 36 squares in a circle:
from turtle import Screen, Turtle
def draw_square(turtle):
for _ in range(4):
turtle.forward(50)
turtle.right(90)
def draw_art(turtle, x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for _ in range(36):
draw_square(turtle)
turtle.right(10)
print("Started the app")
window = Screen()
window.bgcolor('blue')
brad = Turtle()
brad.shape('turtle')
brad.color('green')
brad.speed('fastest') # because I have no patience
print("Start the drawing")
draw_art(brad, 200, 90)
brad.hideturtle()
window.exitonclick()
I am trying to code a program that allows the user to choose out of several options of a scene to draw. I come to an issue with drawing the sun. When I run the program the sun wont stop drawing and fill in the color. Everything else works.
import turtle
import random
t = turtle.Turtle()
turtle.setup(800,600)
def draw_sunshine():
t.penup()
t.goto(350,250)
t.pendown()
t.color('black', 'yellow')
t.speed(9)
t.begin_fill()
while True:
t.forward(100)
t.left(170)
t.right(100)
if abs(t.pos()) < 1:
break
t.end_fill()
t.done()
weather = False
while weather == False:
weather = input("Would you like to draw rain or sunshine? ")
if weather == "rain":
draw_rain()
else:
draw_sunshine()
The problem is likely this termination test:
if abs(t.pos()) < 1:
break
The pos() method returns a pair of numbers, e.g. (0.0, 0.0), as a turtle.Vec2D type, which abs() only operates on the first, x. In this situation, x will never be less than 1 as that's near the center of the screen and we're far off to the right.
Below is a different approach where we control the loop based on the number of iterations it will take to complete the drawing of the sun. To make this work, I've adjusted the sun drawing to finish in a reasonable (integral) number of iterations and still fill with color nicely:
from turtle import Turtle, Screen
def draw_sunshine(turtle):
turtle.penup()
turtle.goto(350, 250)
turtle.pendown()
turtle.color('black', 'yellow')
turtle.speed("fast")
turtle.begin_fill()
for _ in range(24):
turtle.forward(100)
turtle.left(75)
turtle.end_fill()
screen = Screen()
screen.setup(800, 600)
yertle = Turtle()
weather = ''
while not weather:
weather = input("Would you like to draw 'rain' or 'sunshine'? ")
if weather == "rain":
draw_rain(yertle)
else:
draw_sunshine(yertle)
yertle.hideturtle()
screen.exitonclick()
When going through the book "How to think like...", I got stuck with exercise 4.9.2.
The question is: "Write a program to draw this. Assume the innermost square is 20 units per side, and each successive square is 20 units bigger, per side, than the one inside it"
The following code represents how far I got till now:
import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
def draw_square(t,size):
for i in range(4):
tess.forward(size)
tess.left(90)
size = 20
for j in range(3):
tess.pensize(3)
draw_square(tess,size)
size = size + 20
tess.penup()
tess.goto(-20, -20)
tess.pendown()
wn.mainloop()
Can someone be so kind and show me the right direction please?
Thanks!
Swen
The problem is here:
tess.goto(-20, -20)
You have two issues. First, if each square is 20 units bigger and you offset each square by (-20, -20) all squares would share one corner. Instead you want to offset the corner of the square by (-10, -10) so that the inner square is offset by 10 units on all sides.
The second issue is that .goto(x, y) sets an absolute position, not an offset. To move to an offset you need to calculate a new absolute position based on the offset:
tess.goto(tess.xcor()-10, tess.ycor()-10)
Or
tess.goto(tess.pos() + (-10, -10))
Sometimes when you're stuck, a good approach with turtle graphics is to think outside the square. If we consider the desired result as badly drawn concentric circles then the problem reduces to:
from turtle import Turtle, Screen
HYPOTENUSE = (2 ** 0.5) / 2
screen = Screen()
screen.bgcolor("lightgreen")
tess = Turtle()
tess.pencolor("red")
tess.setheading(45)
tess.width(3)
for radius in range(20, 20 * 5 + 1, 20):
tess.penup()
tess.goto(radius/2, -radius/2)
tess.pendown()
tess.circle(radius * HYPOTENUSE, steps=4)
screen.exitonclick()
OUTPUT
import turtle
def drawSquare (t, size):
for i in range (4):
t.forward(size)
t.left(90)
def main():
wn = turtle.Screen()
wn.bgcolor('black')
pat = turtle.Turtle()
pat.pensize(2)
pat.speed(10)
pat.color('blue')
space = -10
for i in range(20, 105, 20):
drawSquare(pat,i)
pat.up()
pat.goto(space, space)
pat.down()
space = space - 10
wn.exitonclick()
main()
import turtle
def draw_sqr(name,size):
for i in range(4):
name.forward(size)
name.left(90)
name.penup()
name.backward(10)
name.right(90)
name.forward(10)
name.left(90)
name.pendown()
window = turtle.Screen()
window.bgcolor('lightgreen')
window.title("conc_sqr")
x = turtle.Turtle()
x.color('hotpink')
x.pensize(3)
for i in range(5):
draw_sqr(x,20 + 20*i)
window.mainloop()
def draw_rectangle(animal, width, height):
for _ in range(2):
animal.forward(width)
animal.left(90)
animal.forward(height)
animal.left(90)
def draw_square(animal, size):
draw_rectangle(animal, size, size)
def make_window(back_color,title):
window = turtle.Screen()
window.bgcolor(back_color)
window.title(title)
return window
def make_turtle(color, size):
animal = turtle.Turtle()
animal.color(color)
animal.pensize(size)
return animal
import turtle
window = make_window("lightgreen","Squares")
tess = make_turtle("hotpink",3)
size_sqr=20
adjustment = 10
for _ in range(5):
draw_square(tess,size_sqr)
size_sqr += 20
tess.penup()
tess.backward(20/2)
tess.right(90)
tess.forward(20/2)
tess.left(90)
tess.pendown()
import turtle
def drawsquare(t,sz): #(turtle, size)
for i in range(4):
t.fd(sz)
t.right(90)
def main():
wn = turtle.Screen()
your_turtle = turtle.Turtle()
your_turtle.pensize(3)
wn.bgcolor("light green")
your_turtle.pencolor("hot pink")
for i in range(1,6):
drawsquare(your_turtle,20*i)
your_turtle.up()
your_turtle.left(135)
your_turtle.fd(14.142) #the distance between each square
your_turtle.right(135)
your_turtle.down()
main()
wn.exitonclick()