问题
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 pop up with 2 different turtle screens A: the one I called screen, and B: the one that automatically pops up when you create a turtle without a screen.
import turtle
from turtle import *
from turtle import Screen
import tkinter
from tkinter import *
import time
from time import sleep
from random import randint
import random
health = 50
damage = 10
root = Tk()
Fight = randint(10,20)
step = 0
cv = ScrolledCanvas(root,600,600,600,600)
cv.pack(side = tkinter.LEFT)
turtle = Turtle()
screen = TurtleScreen(cv)
turtle.up()
def Up(event):
global step
if step == Fight:
Combat()
step+=1
turtle.seth(90)
turtle.forward(10)
def Down(event):
global step
if step == Fight:
Combat()
step+=1
turtle.seth(-90)
turtle.forward(10)
def Left(event):
global step
if step == Fight:
Combat()
step+=1
turtle.seth(180)
turtle.forward(10)
def Right(event):
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.onkey(forward, "Up")
screen.onkey(backward, "Down")
screen.onkey(left, "Left")
screen.onkey(right, "Right")
screen.listen()
Thank you,
回答1:
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.
来源:https://stackoverflow.com/questions/44634947/how-to-set-a-turtle-to-a-turtle-screen