Leonardo Cremona
Leonardo Cremona

Reputation: 29

How to bind enter key to a function using turtle

I'm trying to make a tic tac toe game where you use arrow keys to move a turtle around the board, and when you press the enter key it draws a circle (nought in the code) or a cross.

I've tried using similar code to what I used for the movement keys but replace it for the enter key and C key (for cross) but they don't work and automatically draw the cross when it starts up. I've also tried using monkey but that doesn't work.

import turtle

sc = turtle.Screen()
bo = turtle.Turtle()

#screen

sc.bgcolor("black")
sc.title("tic tac toe")

#board

bo.color("white")
bo.penup()
bo.speed(0)
bo.setposition(-100, -300)
bo.pendown()
bo.setposition(-100, 300)
bo.penup()
bo.setposition(100, 300)
bo.pendown()
bo.setposition(100, -300)
bo.penup()
bo.setposition(-300, 100)
bo.pendown()
bo.setposition(300, 100)
bo.penup()
bo.setposition(300, -100)
bo.pendown()
bo.setposition(-300,-100)
bo.penup()
bo.setposition(0,0)

#naught and cross function

def c()  :
    bo.pendown()
    x = bo.xcor()-80
    y = bo.ycor()+80
    bo.penup()
    bo.setposition(x,y)
    bo.pendown()
    x = bo.xcor()+160
    y = bo.ycor()-160
    bo.setposition(x,y)
    bo.penup()
    x = bo.xcor()-160
    bo.setposition(x,y)
    bo.pendown()
    x = bo.xcor()+160
    y = bo.ycor()+160
    bo.setposition(x,y)
    bo.penup()
    bo.setposition(0,0)

def n() :
    y = bo.ycor()-80
    x = bo.xcor()
    bo.setposition(x, y)
    bo.pendown()
    bo.circle(80)
    bo.penup()
    bo.setposition(0,0)

#movment
def move_left(event):
    x = bo.xcor()
    x -= 200
    if x < -300:
        x = -200
    bo.setx(x)

def move_right(event):
    x = bo.xcor()
    x += 200
    if x > 300:
        x = 200
    bo.setx(x)

def move_down(event):
    y = bo.ycor()
    y -= 200
    if y < -300:
        y = -200
    bo.sety(y)

def move_up(event):
    y = bo.ycor()
    y += 200
    if y > 300:
        y = 200
    bo.sety(y)

#Keybinding

turtle.listen()

turtle.getcanvas().bind("<Left>", move_left)
turtle.getcanvas().bind("<Right>", move_right)
turtle.getcanvas().bind("<Up>", move_up)
turtle.getcanvas().bind("<Down>", move_down)

I'd eventually like to have it where once you draw a cross, and press enter, it draws a circle where there isn't a cross.

Upvotes: 1

Views: 569

Answers (1)

cdlane
cdlane

Reputation: 41905

There's no need to drop down to the Tk Canvas to configure your key events:

turtle.getcanvas().bind("<Left>", move_left)

You can do the same from turtle itself:

sc.onkey(move_left, "Left")

I've reworked your code below, adding a key event for "Return" that draws the naughts and crosses. Since you don't have an underlying data structure for your game, I set it up to simply alternate between the two:

from turtle import Screen, Turtle

# naught and cross functions

def cross():
    x, y = board.position()
    board.penup()
    board.setposition(x - 80, y - 80)
    board.pendown()
    board.setposition(x + 80, y + 80)
    board.penup()
    board.setx(x - 80)
    board.pendown()
    board.setposition(x + 80, y - 80)
    board.penup()
    board.home()

def naught():
    board.sety(board.ycor() - 80)
    board.pendown()
    board.circle(80)
    board.penup()
    board.home()

first_player = True

def choose():
    global first_player

    if first_player:
        naught()
    else:
        cross()

    first_player = not first_player

# movement
def move_left():
    x = board.xcor() - 200

    if x < -300:
        x = -200

    board.setx(x)

def move_right():
    x = board.xcor() + 200

    if x > 300:
        x = 200

    board.setx(x)

def move_down():
    y = board.ycor() - 200

    if y < -300:
        y = -200

    board.sety(y)

def move_up():
    y = board.ycor() + 200

    if y > 300:
        y = 200

    board.sety(y)

# screen

screen = Screen()
screen.bgcolor("black")
screen.title("tic tac toe")

# board

board = Turtle()
board.color("white")
board.speed('fastest')

# vertical lines
board.penup()
board.setposition(-100, -300)
board.pendown()
board.sety(300)
board.penup()
board.setx(100)
board.pendown()
board.sety(-300)
board.penup()

# horizontal lines
board.setposition(-300, 100)
board.pendown()
board.setx(300)
board.penup()
board.sety(-100)
board.pendown()
board.setx(-300)
board.penup()
board.home()

# Keybinding

screen.onkey(move_left, "Left")
screen.onkey(move_right, "Right")
screen.onkey(move_up, "Up")
screen.onkey(move_down, "Down")

screen.onkey(choose, "Return")

screen.listen()
screen.mainloop()

This allows two people to play the game and should be something you can build upon.

Upvotes: 1

Related Questions