0

What I have is just the start screen, As long as I can get the Quit button working I can probably figure out the rest on my own. I just need the Quit button to close the window and stop the program. y'know, like how a Quit button works.

Here's my current code

import turtle
import random

#Functions

#Create Buttons
#n=name
#t=text
#ts=textsize
#x=xcoord (center)
#y=ycoord (center)
#w=width
#h=height
def Create_Button(n,t,ts,x,y,w,h):
    n = turtle.Turtle()
    n.color('white','black')
    n.speed(0)
    n.width(5)
    n.up()
    n.goto(x,y)
    n.bk(w//2)
    n.rt(90)
    n.down()
    n.fd(h//2)
    n.lt(90)
    n.fd(w)
    n.lt(90)
    n.fd(h)
    n.lt(90)
    n.fd(w)
    n.lt(90)
    n.fd(h//2)
    n.up()
    n.hideturtle()
    n.goto(x,y-h/3)
    n.write(t, align='center',font=('Courier',ts,'bold'))
    #n.shape('square')
    #n.goto(x,y)
    #n.shapesize(stretch_wid=20, stretch_len=6)
    #n.showturtle()
    return n

#Title
def Title():
    Title = turtle.Turtle()
    Title.speed(0)
    Title.penup()
    Title.color('white')
    Title.goto(0,400)
    Title.write('Rock, Paper, Scissors!', align='center', font=('Courier', 64, 'bold' ,'italic'))
    Title.hideturtle()

# Window
win = turtle.Screen()
win.title('Rock, Paper, Scissors!')
win.bgcolor('black')
win.setup(800,600)
win.tracer(0)
win.listen()

#Title
Title()

#Start Button
Start_Button = Create_Button('Start_Button','Start',64,-250,-250,400,120)

#Quit Button
Quit_Button = Create_Button('Quit_Button','Quit',64,250,-250,400,120)

#Game Loop
while True:
    win.update()

I've tried just messing with things myself, and using some solutions from ChatGPT but nothing has worked.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Fr0gger_
  • 11
  • 3
  • `while True: win.update()` is not a good event loop. This slams the CPU as hard as possible rather than trying to achieve a particular framerate. Prefer `ontimer`. Did you try anything in the existing questions: [How can I create a button in turtle?](https://stackoverflow.com/questions/59902849/how-can-i-create-a-button-in-turtle) or [How to create a button with python turtle](https://stackoverflow.com/questions/61765658/how-to-create-a-button-with-python-turtle)? – ggorlen Apr 24 '23 at 02:13
  • I have, neither have worked. – Fr0gger_ Apr 25 '23 at 03:36
  • 1
    Can you share your attempt, please? I don't see any Tkinter buttons, click listeners or coordinate tests in your code here. Those are the standard ways to add buttons, as the links explain. Drawing something that looks like a button and using the word "button" in your code is a good start, but none of that implements any sort of button logic or behavior. Thanks. – ggorlen Apr 25 '23 at 03:37

1 Answers1

0

This was never going to work for two reasons. First, as @ggorlen sagely points out (+1), there's no button action set nor any button click detection. Second, a hidden turtle can't be clicked and a turtle can't hide behind that which it has drawn.

As long as I can get the Quit button working I can probably figure out the rest on my own.

OK, let's create simple, workable 'Quit' button that look more like a checkbox than traditional button. This fixes the problem of a turtle hiding behind it's own drawing:

from turtle import Screen, Turtle

CURSOR_SIZE = 20

def create_button(action, text, text_size, x, y):
    button = Turtle()
    button.hideturtle()

    button.shape('square')
    button.shapesize(text_size/CURSOR_SIZE, text_size/CURSOR_SIZE, 5)
    button.color('black', 'white')

    button.penup()
    button.goto(x, y - text_size/2)
    button.write(text, align='center', font=('Courier', text_size, 'bold'))

    button.goto(x - (text_size * len(text))/2, y)
    button.onclick(action)
    button.showturtle()

    return button

def bye(x, y):
    screen.ontimer(screen.bye)

screen = Screen()
screen.setup(800, 600)

quit_button = create_button(bye, 'Quit', 64, 250, -200)

screen.mainloop()

enter image description here

Not perfect, but likely one of the the simplest, functional buttons you can create in pure Python turtle.

cdlane
  • 40,441
  • 5
  • 32
  • 81