关于python 3.x:如何将乌龟设置为乌龟屏幕

how to set a turtle to a turtle screen

我有一个游戏我正在尝试,但当我创建我的屏幕和我的乌龟,我的乌龟出现在一个不同的屏幕比我做的屏幕。如果我运行代码,它会弹出两个不同的海龟屏幕A:我称之为屏幕的屏幕,B:当你创建一个没有屏幕的海龟时,它会自动弹出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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()

谢谢您,


Python龟被设计成要么嵌入你自己制作的tk窗口,要么嵌入你自己制作的tk窗口。这两个选项的调用方式不同,但通过混合命令,最终会得到这两个选项。采用您开始的自定义tk窗口方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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()

或者,我们可以通过让Turtle模块创建窗口来简化一些事情,尽管我们可以通过其方法调用根据需要对其进行形状调整:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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()

在使用import时,您应该更加谨慎,因为导入相同的模块时,两种不同的方法最终会混淆您和/或python。