← Back to Community
Profile icon
plscodeinjava

# This is a tutorial made for my 121 cycle special.....css-1q3m8ps{margin-left:var(--space-4);margin-right:var(--space-4);display:none;}

Hello everyone! Today I am teaching you how to make a square clicker in turtle... I don't know WHY you would EVER want to use this, but anyway....
First, you are going to want to do the imports. I am using the shapely module to test for boundaries:

from shapely.geometry import Point from shapely.geometry.polygon import Polygon import turtle

Now, we are going to want to set up our variables:

screen = turtle.Screen() screen.bgcolor("black") screen.setup(800,600) draw = turtle.Turtle() draw.ht() draw.pencolor("blue") draw.pensize(5) draw.speed(0) draw.penup() draw.goto(-380,270) draw.pendown()

The next step is to declare the array for the squares:
squares = []
Next, we will have to make a huge for loop to draw all the squares:

for i in range(6): for x in range(9): for y in range(5):

Now, it's time for the confusing part. The shapely polygon module takes a set of points as the corners of the polygon. We will want to record these points:

if (y == 1): point1 = Point(draw.xcor(), draw.ycor()) if (y == 2): point2 = Point(draw.xcor(), draw.ycor()) if (y == 3): point3 = Point(draw.xcor(), draw.ycor()) if (y == 4): point4 = Point(draw.xcor(), draw.ycor())

Now, we will actually draw the square:

draw.forward(80) draw.right(90)

After we draw a square, we will want to set up for the next one:

draw.left(90) draw.forward(5)

Ok, so now, we will want to create a square out of our points and append it to the squares array:

square = Polygon([point1, point2, point3, point4]) squares.append(square)

And after we draw a row of squares, we will want to move down a row:

draw.penup() draw.goto(-380, draw.ycor() - 90) draw.pendown()

Ok, here is the entire point of this tutorial. I have made a squareCheck function which can be binded to onclick:

def squareCheck(x, y): click = Point(x, y) for square in squares: if (square.contains(click)): index = str(squares.index(square)+1) print("Square " + index + " was clicked.")

Now, the only other thing to do is bind this to onclick and run the program:

screen.onclick(squareCheck) screen.listen()

I really hope you enjoyed this tutorial and it helps you with your projects! Oh, and here is the full code:

from shapely.geometry import Point from shapely.geometry.polygon import Polygon import turtle screen = turtle.Screen() screen.bgcolor("black") screen.setup(800,600) draw = turtle.Turtle() draw.ht() draw.pencolor("blue") draw.pensize(5) draw.speed(0) draw.penup() draw.goto(-380,270) draw.pendown() squares = [] for i in range(6): for x in range(9): for y in range(5): if (y == 1): point1 = Point(draw.xcor(), draw.ycor()) if (y == 2): point2 = Point(draw.xcor(), draw.ycor()) if (y == 3): point3 = Point(draw.xcor(), draw.ycor()) if (y == 4): point4 = Point(draw.xcor(), draw.ycor()) draw.forward(80) draw.right(90) draw.left(90) draw.forward(5) square = Polygon([point1, point2, point3, point4]) squares.append(square) draw.penup() draw.goto(-380, draw.ycor() - 90) draw.pendown() def squareCheck(x, y): click = Point(x, y) for square in squares: if (square.contains(click)): index = str(squares.index(square)+1) print("Square " + index + " was clicked.") screen.onclick(squareCheck) screen.listen()

### Note: in the project I have attached there is some commented out lines. You do not need to put these in your code.

Voters
Profile icon
TylerForman
Profile icon
MemeManHimself
Profile icon
plscodeinjava
hotnewtop
Profile icon
CodingCactus

I swear you have posted this before…

Profile icon
plscodeinjava

@CodingCactus
Did you like it?

Profile icon
CodingCactus

¯\_(ツ)_/¯
clicking a square doesn't really interest me that much

Profile icon
plscodeinjava

@CodingCactus
DANG IT.... I thought EVERYONE would LOVE this tutorial

Profile icon
CodeLongAndPros

@amaddentcsec Project walkthroughs are frowned upon by me, don’t know about the rest though. The better kind of tutorials teach skills without much copypasta. But hey, whatever rows your canoe.

Profile icon
plscodeinjava

@CodeLongAndPros
This does teach a skill! It teaches you how to make a square clicker!

Profile icon
CodeLongAndPros

@amaddentcsec but that’s a specialized skill. Compare this to @DymanicSquid’s tutorial on memozation, or mine on bash.
You can use that to apply to many thing. Ain’t possible with this though.

Profile icon
plscodeinjava

@CodeLongAndPros
This could LITERALLY be applied to make BUTTONS in turtle. Think about that! BUTTONS in python turtle!!

Profile icon
CodeLongAndPros

@amaddentcsec Still specialized. Bash is on most computers, and memorization can be used anywhere.

Profile icon
plscodeinjava

@CodeLongAndPros

Profile icon
CodeLongAndPros

@amaddentcsec Well slow print is overrated, overused, and should never be used.

Profile icon
CodingCactus

@CodeLongAndPros

bruuuuuuh you don't row a canoe, you paddle

Profile icon
CodeLongAndPros

@CodingCactus You paddle a kayak, you row a canoe, since it’s a two-person boat.

Profile icon
CodingCactus

@CodeLongAndPros hrmmmm but you use a paddle so surely it is paddling?

Profile icon
CodeLongAndPros

@CodingCactus but, when you swim, are you “arming”, since your use your arms?

Profile icon
CodingCactus

@CodeLongAndPros true i guess

Profile icon
CodingCactus
Profile icon
CodeLongAndPros

Profile icon
CodingCactus

@CodeLongAndPros lol
Have a good day : )

Profile icon
CodeLongAndPros

@CodingCactus Aye, can do. Now to fix my usb.

Profile icon
MemeManHimself

Hahah cool

Profile icon
plscodeinjava

@MemeManHimself
Yo thanks!!!
Also, how did you find this tutorial? It was posted like a week ago xD

Profile icon
plscodeinjava

@MemeManHimself
Yo how did i find this tutorial, it was posted ten months ago > <