我正在尝试制作游戏,但是当我创建我的屏幕和我的乌龟时,我的乌龟出现在与我制作的屏幕不同的屏幕上。 如果我要运行代码,它将弹出2个不同的海龟屏幕A:我称之为屏幕的屏幕,B:当你创建没有屏幕的海龟时自动弹出的屏幕。
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()
谢谢,
答案 0 :(得分:0)
Python龟被设计成嵌入你自己制作的Tk窗口或者它制作的Tk窗口。这两个选项的调用方式不同,但通过混合命令,您最终会同时使用这两个选项。采用您开始的自定义Tk窗口方法:
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模块创建窗口来简化一些事情,尽管我们可以通过方法调用根据需要对其进行整形:
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。