如何将乌龟设置为乌龟屏幕 [英] how to set a turtle to a turtle screen

查看:27
本文介绍了如何将乌龟设置为乌龟屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()

谢谢,

推荐答案

Python 海龟被设计为嵌入在您自己创建的 Tk 窗口中或嵌入到它正在创建的 Tk 窗口中.这两个选项的调用方式不同,但通过混合命令,您最终会同时拥有这两个选项.采用您开始的自定义 Tk 窗口方法:

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()

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

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.

这篇关于如何将乌龟设置为乌龟屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆