So I want to create a button that you can click on in python.
I am making a very simple game with the python turtle module.
here's my code x is the x position of the click and y is the y position of the click.
screen = turtle.Screen() #for some context
def click(x, y):
if (x <= 40 and x <= -40) and (y <= 20 and y <= -20):
#code here
screen.onscreenclick(click)
I want it to do the run some code when i click in a specific area but
this code doesn't work for me.
any help would be appreciated.
Thanks!
Approach the problem a different way. Don't draw a button with the turtle and try to determine if the user clicked within the drawing, make a button out of a turtle and do something when the user clicks on it:
from turtle import Screen, Turtle
def click(x, y):
button.hideturtle()
button.write("Thank you!", align='center', font=('Arial', 18, 'bold'))
screen = Screen()
button = Turtle()
button.shape('square')
button.shapesize(2, 4)
button.fillcolor('gray')
button.onclick(click)
screen.mainloop()
If you insist on doing it your way, then the equivalent code might be:
from turtle import Screen, Turtle
def click(x, y):
if -40 <= x <= 40 and -20 <= y <= 20:
turtle.write("Thank you!", align='center', font=('Arial', 18, 'bold'))
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.penup()
turtle.fillcolor('gray')
turtle.goto(-40, -20)
turtle.begin_fill()
for _ in range(2):
turtle.forward(80)
turtle.left(90)
turtle.forward(40)
turtle.left(90)
turtle.end_fill()
turtle.goto(0, 30)
screen.onclick(click)
screen.mainloop()
Related
I've created a game of Tic-Tac-Toe that can be played using the turtle module. It draws the board, then using class methods draws the circles or X's in the spot designated by a simple numbering system and coordinates.
I'm trying to finish this project up by testing for if a win condition has been satisfied by a player, and if yes, it draws a line through them and finishes the game.
Trouble is, I don't know where to start. I had the idea of adding the values of the position to a list for each player (for example, if the circle player puts a circle in position 1, a 1 gets added to the circles list) then creating if statements to check for certain conditions. However, I quickly got lost and now I'm not sure what to do. Here's what my code looks like
#Class that allows for drawing the circles and X's
class CircleOrSquare():
def __init__(self):
pass
def circle(self, x, y): #Draws from the bottom middle of the circle
import turtle
turtle.color('red')
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.circle(10)
def cross(self, x, y): #Draws from the bottom middle of the cross
import turtle
turtle.color('blue')
#position x,y at center base of cross
turtle.penup()
turtle.goto(x, y)
x = x-10 #Change x to the bottom left leg
#Draw the X
turtle.goto(x, y)
turtle.pendown()
turtle.goto(x+20, y+20)
turtle.penup()
turtle.goto(x, y+20)
turtle.pendown()
turtle.goto(x+20, y)
def main():
import turtle
canvas = turtle.Screen()
canvas.setup(200, 200)
canvas.bgcolor('light grey')
turtle.hideturtle()
#Vertical lines
turtle.penup()
turtle.goto(-25, 50)
turtle.pendown()
turtle.goto(-25, -50)
turtle.penup()
turtle.goto(25, 50)
turtle.pendown()
turtle.goto(25, -50)
turtle.penup()
#Horizontal lines
turtle.goto(-50, 20)
turtle.pendown()
turtle.goto(50, 20)
turtle.penup()
turtle.goto(-50, -20)
turtle.pendown()
turtle.goto(50, -20)
turtle.penup()
#Tic-Tac-Toe
turtle.goto(-28, 75)
turtle.write("Tic-Tac-Toe")
positions = {1: [-40, 30], 2: [0, 30], 3: [40, 30], 4: [-40, -10], 5: [0, -10],
6: [40, -10], 7: [-40, -45], 8: [0, -45], 9: [40, -45]}
screen = turtle.Screen()
for i in range(9):
move = screen.textinput("Next Move", "Starting from the top left"
", enter 1-9 for your move: ")
if i % 2 == 0:
answer = CircleOrSquare()
answer.circle(positions[int(move)][0], positions[int(move)][-1])
else:
answer = CircleOrSquare()
answer.cross(positions[int(move)][0], positions[int(move)][-1])
turtle.exitonclick()
main()
So I'm making a game where the two turtles race each other, but I want them to wait a bit before they start (to display a 3,2,1) screen. But I can't figure it out! I used time.sleep, and turtle.delay, but both did not work. What can I do? Here is the code :)
import turtle
import random
import time
turt = turtle.Turtle()
turt2 = turtle.Turtle()
turtle.Screen() .bgcolor("green")
turt.speed(10)
turt.penup()
turt.goto(200,100)
turt.pendown()
turt.right(90)
turt.width(10)
turt.forward(450)
turt.right(180)
turt.forward(800)
#position 1
turt.penup()
turt.goto(-400,200)
turt.pendown()
turt.right(90)
turt.forward(100)
#position 2
turt2.width(10)
turt2.penup()
turt2.goto(-400,-200)
turt2.pendown()
turt2.forward(100)
Contrary to the advice so far, I'd avoid sleep() in my turtle program and use ontimer() instead to countdown the start of the race:
from turtle import Screen, Turtle
from random import choice
FONT = ('Arial', 36, 'bold')
def race():
while turtle_1.xcor() < 200 > turtle_2.xcor():
choice([turtle_1, turtle_2]).forward(10)
def countdown(seconds=3):
pen.clear()
if seconds < 1:
screen.ontimer(race)
else:
pen.write(seconds, align='center', font=FONT)
screen.ontimer(lambda: countdown(seconds - 1), 1000)
screen = Screen()
screen.bgcolor('green')
marker = Turtle()
marker.hideturtle()
marker.speed('fastest')
marker.color('white')
marker.width(5)
marker.penup()
marker.goto(200, 300)
marker.pendown()
marker.right(90)
marker.forward(600)
pen = Turtle()
pen.hideturtle()
turtle_1 = Turtle()
turtle_1.shape('turtle')
turtle_1.color('red')
turtle_1.penup()
turtle_1.goto(-400, 200)
turtle_2 = turtle_1.clone()
turtle_1.color('blue')
turtle_2.goto(-400, -200)
countdown()
screen.exitonclick()
I'm working on a python project for my intro class where I want to make a blizzard with the turtle module. So far, I've been able to make a "snowflake" appear on each keypress but I'm not sure how to make it into a conditional loop where when I click, it becomes true and keeps looping without me having to click again.
Here's the code I have right now:
def snowing(x, y):
w.speed(0)
flake_size = randint(1, 5)
rx = randint(-250, 250)
ry = randint(-300, 300)
w.color(colours[5])
w.setposition(rx, ry)
w.pendown()
w.begin_fill()
w.circle(flake_size)
w.end_fill()
w.penup()
listen()
onscreenclick(snowing, add=None)
when I click, it becomes true and keeps looping without me having to
click again.
We can make a separate event handler that is a toggle, using a global to switch between on and off on subsequent clicks. We'll combine that with a timer event to keeps the flakes coming:
from turtle import Screen, Turtle
from random import randint, choice
COLOURS = ['light gray', 'white', 'pink', 'light blue']
is_snowing = False
def toggle_snowing(x, y):
global is_snowing
if is_snowing := not is_snowing:
screen.ontimer(drop_flake)
def drop_flake():
flake_radius = randint(1, 5)
x = randint(-250, 250)
y = randint(-300, 300)
turtle.setposition(x, y)
turtle.color(choice(COLOURS))
turtle.begin_fill()
turtle.circle(flake_radius)
turtle.end_fill()
if is_snowing:
screen.ontimer(drop_flake)
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.penup()
screen = Screen()
screen.setup(500, 600)
screen.bgcolor('dark blue')
screen.onclick(toggle_snowing)
screen.listen()
screen.mainloop()
When you click on the screen, the flakes will start appearing. When you click again, they will stop.
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'm currently making a 5x5 grid for a snakes and ladder but can't figure out how to display the numbers on the grid in this formation;
21,22,23,24,25
20,19,18,17,16
11,12,13,14,15
10, 9, 8, 7, 6
1 , 2, 3, 4, 5
The code I've wrote makes the 5x5 grid, I'm juts note sure how to add the labels of if I should take a
different approach with the code.
thanks for any help.
import turtle
t = turtle.Turtle()
speed = turtle.speed()
t.speed(10)
x=250
y=250
t.penup()
t.goto(-x,y)
t.pendown()
turtle.hideturtle()
turtle.ht()
for a in range(5):
t.penup()
t.goto(-x,-y)
t.pendown()
y=y-100
for b in range(5):
for n in range(5):
t.speed(10)
t.fd(100)
if n!=4:
t.right(90) here
Here's a solution that uses much of your existing logic, tweaking it a bit, and adds the numbers:
from turtle import Screen, Turtle
FONT_SIZE = 18
FONT = ('Arial', FONT_SIZE, 'bold')
BOXES = 5
BOX_SIZE = 100
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
x = -BOX_SIZE * BOXES / 2
y = -BOX_SIZE * BOXES / 2
for i in range(BOXES):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for j in range(BOXES):
for _ in range(4):
turtle.forward(BOX_SIZE)
turtle.left(90)
dx, dy = turtle.position()
turtle.penup()
turtle.goto(dx + BOX_SIZE/2, dy + BOX_SIZE/2 - FONT_SIZE / 2)
turtle.write(j + BOXES * i + 1, align='center', font=FONT)
turtle.setposition(dx, dy)
turtle.pendown()
turtle.forward(BOX_SIZE)
y += BOX_SIZE
screen.exitonclick()
Centering-wise, the vertical font correction (FONT_SIZE / 2) is an approximation that works fine on my system but if you want an accurate solution for all systems, see this answer