how to set a turtle to a turtle screen

前端 未结 1 925
别跟我提以往
别跟我提以往 2021-01-26 07:55

I have a game I am trying to make, but when I create my screen and my turtle, my turtle shows up on a different screen than the screen I made. If I were to run the code it would

相关标签:
1条回答
  • 2021-01-26 08:23

    Python turtle was designed to either be embedded in a Tk window of your own making or in a Tk window of it's making. The two choices are invoked differently, but by mixing the commands you end up with both. Taking the custom Tk window approach that you started:

    from random import randint
    from tkinter import *
    from turtle import ScrolledCanvas, RawTurtle, TurtleScreen
    
    health = 50
    damage = 10
    fight = randint(10, 20)
    step = 0
    
    def up():
        global step
    
        if step == fight:
            combat()
        step += 1
        turtle.seth(90)
        turtle.forward(10)
    
    def down():
        global step
    
        if step == fight:
            combat()
        step += 1
        turtle.seth(-90)
        turtle.forward(10)
    
    def left():
        global step
    
        if step == fight:
            combat()
        step += 1
        turtle.seth(180)
        turtle.forward(10)
    
    def right():
        global step
    
        if step == fight:
            combat()
        step += 1
        turtle.seth(0)
        turtle.forward(10)
    
    def combat():
        enemy = RawTurtle(canvas)
        enemy.up()
        eHealth = randint(20, 100)
        eDamage = randint(10, 20)
    
    root = Tk()
    canvas = ScrolledCanvas(root)
    canvas.pack(side=LEFT)
    screen = TurtleScreen(canvas)
    turtle = RawTurtle(canvas)
    turtle.up()
    
    screen.onkey(up, "Up")
    screen.onkey(down, "Down")
    screen.onkey(left, "Left")
    screen.onkey(right, "Right")
    screen.listen()
    
    screen.mainloop()
    

    Or, we can simplify things a bit by letting the turtle module create the window, though we can shape it as needed through its method calls:

    from random import randint
    from turtle import Turtle, Screen
    
    health = 50
    damage = 10
    fight = randint(10, 20)
    step = 0
    
    def up():
        global step
    
        if step == fight:
            combat()
        step += 1
        turtle.seth(90)
        turtle.forward(10)
    
    def down():
        global step
    
        if step == fight:
            combat()
        step += 1
        turtle.seth(-90)
        turtle.forward(10)
    
    def left():
        global step
    
        if step == fight:
            combat()
        step += 1
        turtle.seth(180)
        turtle.forward(10)
    
    def right():
        global step
    
        if step == fight:
            combat()
        step += 1
        turtle.seth(0)
        turtle.forward(10)
    
    def combat():
        enemy = Turtle()
        enemy.up()
        eHealth = randint(20, 100)
        eDamage = randint(10, 20)
    
    screen = Screen()
    screen.setup(500, 350)  # visible portion of screen area
    screen.screensize(600, 600)  # scrollable extent of screen area
    turtle = Turtle()
    turtle.up()
    
    screen.onkey(up, "Up")
    screen.onkey(down, "Down")
    screen.onkey(left, "Left")
    screen.onkey(right, "Right")
    screen.listen()
    
    screen.mainloop()
    

    You should be more circumspect in your use of import as importing the same modules two different ways will eventually confuse you and/or Python.

    0 讨论(0)
提交回复
热议问题