Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 乌龟正在走出国境_Python_Turtle Graphics - Fatal编程技术网

Python 乌龟正在走出国境

Python 乌龟正在走出国境,python,turtle-graphics,Python,Turtle Graphics,我正在做一个蟒蛇龟游戏,你必须抓住一只老鼠。当我靠近鼠标时,它应该会消失,但它并没有完全消失。问题是当我去抓老鼠的时候,我的乌龟能够逃出我的游戏边界,而通常当我不追赶老鼠的时候,我的乌龟不会逃出边界。我不明白为什么。有人能帮我吗 我是python的初学者,所以我的代码有点凌乱,效率可能不太高。很抱歉 这是我的密码: import turtle import os import math import random from turtle import * #Screen wn = turtl

我正在做一个蟒蛇龟游戏,你必须抓住一只老鼠。当我靠近鼠标时,它应该会消失,但它并没有完全消失。问题是当我去抓老鼠的时候,我的乌龟能够逃出我的游戏边界,而通常当我不追赶老鼠的时候,我的乌龟不会逃出边界。我不明白为什么。有人能帮我吗

我是python的初学者,所以我的代码有点凌乱,效率可能不太高。很抱歉 这是我的密码:

import turtle
import os
import math
import random

from turtle import *

#Screen
wn = turtle.Screen()
wn.bgcolor("green")
wn.title("Catch and run")
#wn.bgpic("background.gif")
#scene
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-200)
border_pen.pensize(3)
border_pen.pendown()
for side in range(2):
    border_pen.fd(600)
    border_pen.lt(90)
    border_pen.fd(405)
    border_pen.lt(90)
#border coordinates:
#x: -300 - 300
#y: -190 - 200
border_pen.hideturtle()

#player
player = turtle.Turtle()
player.color("black")
player.shape("triangle")
player.penup()
player.speed(0)
player.setposition(0,90)
player.lt(90)
player_speed = 15

#movment
def right():
    if player.xcor() < 280:
        player.setheading(360)
        x = player.xcor()
        x += player_speed
        player.setx(x)
def left():
    if player.xcor() > -280:
        player.setheading(180)
        x = player.xcor()
        x -= player_speed
        player.setx(x)
def up():
    if player.ycor() < 185:
        player.setheading(90)
        y = player.ycor()
        y += player_speed
        player.sety(y)
def down():
    if player.ycor() > -170:
        player.setheading(270)
        y = player.ycor()
        y -= player_speed
        player.sety(y)

#mouse
mouse = turtle.Turtle()
mouse.color("white")
mouse.shape("circle")
mouse.penup()
mouse.speed(0)
mouse.setposition(100,150)
mouse.lt(90)
mouse.shapesize(0.5,0.5)
mouse_speed = 14
mouse_direction = "fd"

def move_mouse():
    global mouse_direction
    distance = math.sqrt(math.pow(player.xcor()-mouse.xcor(),2) + math.pow(player.ycor()-mouse.ycor(),2))
    if distance < 80:
        x = mouse.xcor()
        y = mouse.ycor()
        if x < -290:
            mouse_direction = "rt"
        elif x > 290:
            mouse_direction = "lt"
        elif y < -180:
            mouse_direction = "fd"
        elif y > 190:
            mouse_direction = "dw"
        if mouse_direction == "fd":
            y += mouse_speed
        elif mouse_direction == "dw":
            y -= mouse_speed
        elif mouse_direction == "lt":
            x -= mouse_speed
        else:
            x += mouse_speed
        mouse.setx(x)
        mouse.sety(y)

#key binding
turtle.listen()
turtle.onkey(right,"Right")
turtle.onkey(left, "Left")
turtle.onkey(up, "Up")
turtle.onkey(down, "Down")

def game_loop():
    move_mouse()
    turtle.ontimer(game_loop,1)


turtle.ontimer(game_loop,1)
turtle.mainloop()
导入海龟
导入操作系统
输入数学
随机输入
从海龟进口*
#屏风
wn=tutle.Screen()
wn.bgcolor(“绿色”)
wn.标题(“抓住并跑掉”)
#bgpic(“background.gif”)
#场面
border\u pen=海龟。海龟()
边界笔速度(0)
边框颜色(“白色”)
border_pen.penup()
边界笔设置位置(-300,-200)
边境养老金(3)
border_pen.pendown()
对于范围内的侧面(2):
border_pen.fd(600)
边界_pen.lt(90)
border_pen.fd(405)
边界_pen.lt(90)
#边界坐标:
#x:-300-300
#y:-190-200
边界_pen.hideturtle()
#玩家
玩家=乌龟。乌龟()
播放器颜色(“黑色”)
玩家形状(“三角形”)
player.penup()
玩家速度(0)
玩家设置位置(0,90)
player.lt(90)
玩家速度=15
#运动
def right():
如果player.xcor()小于280:
播放器设置标题(360)
x=player.xcor()
x+=玩家速度
player.setx(x)
def left():
如果player.xcor()>-280:
游戏者.设置标题(180)
x=player.xcor()
x-=玩家速度
player.setx(x)
def up():
如果player.ycor()小于185:
玩家设置标题(90)
y=player.ycor()
y+=玩家速度
球员赛蒂(y)
def down():
如果player.ycor()>-170:
玩家设置标题(270)
y=player.ycor()
y-=玩家速度
球员赛蒂(y)
#老鼠
鼠标=海龟。海龟()
鼠标颜色(“白色”)
鼠标形状(“圆形”)
鼠标
鼠标速度(0)
鼠标。设置位置(100150)
鼠标。lt(90)
鼠标形状大小(0.5,0.5)
鼠标速度=14
鼠标方向=“fd”
def move_mouse():
全局鼠标方向
距离=math.sqrt(math.pow(player.xcor()-mouse.xcor(),2)+math.pow(player.ycor()-mouse.ycor(),2))
如果距离小于80:
x=mouse.xcor()
y=鼠标。ycor()
如果x<-290:
鼠标方向=“rt”
elif x>290:
鼠标方向=“lt”
elif y<-180:
鼠标方向=“fd”
elif y>190:
鼠标方向=“dw”
如果鼠标方向=“fd”:
y+=鼠标速度
elif鼠标方向==“dw”:
y-=鼠标速度
elif鼠标方向==“lt”:
x-=鼠标速度
其他:
x+=鼠标速度
鼠标.setx(x)
鼠标。sety(y)
#密钥绑定
乌龟,听着
海龟。安基(右,“右”)
乌龟。onkey(左,“左”)
乌龟。安基(向上,“向上”)
乌龟。安基(向下,“向下”)
def game_loop():
移动鼠标()
乌龟。ontimer(游戏1)
乌龟。ontimer(游戏1)
tutle.mainloop()

我发现球员的移动有两个问题。首先,如果
onkey()
事件处理程序花费的时间太长,即按键重复太快,则事件可能会重叠,从而将玩家推到边界之外。第二,当玩家移动边界时,步长是固定的且较大,因此它可能会将玩家放在边界的错误一侧——步长需要是固定步长和到边界的剩余距离中较小的一个

下面是对您的代码的重新编写,它试图解决上述播放器问题,但忽略了鼠标参与者,因为其运动逻辑不完整:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 600, 400

PLAYER_SPEED = 15
PLAYER_SIZE = 20

# Movement
def right():
    screen.onkey(None, 'Right')  # disable handler inside handler

    x = player.xcor()

    if x < WIDTH/2 - PLAYER_SIZE:
        player.setheading(0)
        player.setx(x + min(PLAYER_SPEED, WIDTH/2 - PLAYER_SIZE - x))

    screen.onkey(right, 'Right')  # reenable on exit

def left():
    screen.onkey(None, 'Left')

    x = player.xcor()

    if x > PLAYER_SIZE - WIDTH/2:
        player.setheading(180)
        player.setx(x - min(PLAYER_SPEED, x + WIDTH/2 - PLAYER_SIZE))

    screen.onkey(left, 'Left')

def up():
    screen.onkey(None, 'Up')

    y = player.ycor()

    if y < HEIGHT/2 - PLAYER_SIZE:
        player.setheading(90)
        player.sety(y + min(PLAYER_SPEED, HEIGHT/2 - PLAYER_SIZE - y))

    screen.onkey(up, 'Up')

def down():
    screen.onkey(None, 'Down')

    y = player.ycor()

    if y > PLAYER_SIZE - HEIGHT/2:
        player.setheading(270)
        player.sety(y - min(PLAYER_SPEED, y + HEIGHT/2 - PLAYER_SIZE))

    screen.onkey(down, 'Down')

# Screen
screen = Screen()
screen.bgcolor('green')
screen.title("Catch and run")

# Border
border_pen = Turtle()
border_pen.hideturtle()
border_pen.speed('fastest')
border_pen.color('white')
border_pen.pensize(3)

border_pen.penup()
border_pen.setposition(-WIDTH/2, -HEIGHT/2)
border_pen.pendown()

for side in range(2):
    border_pen.fd(WIDTH)
    border_pen.lt(90)
    border_pen.fd(HEIGHT)
    border_pen.lt(90)

# player
player = Turtle()
player.shape('triangle')
player.speed('fastest')
player.penup()

# key binding
screen.onkey(right, 'Right')
screen.onkey(left, 'Left')
screen.onkey(up, 'Up')
screen.onkey(down, 'Down')
screen.listen()

screen.mainloop()
从海龟导入屏幕,海龟
宽度,高度=600400
玩家速度=15
播放器大小=20
#运动
def right():
screen.onkey(无,'右')#禁用处理程序内部的处理程序
x=player.xcor()
如果x<宽度/2-播放器大小:
播放器设置标题(0)
player.setx(x+min(player\u速度、宽度/2-player\u大小-x))
screen.onkey(右,'右')#退出时可重新进入
def left():
screen.onkey(无,“左”)
x=player.xcor()
如果x>播放器大小-宽度/2:
游戏者.设置标题(180)
player.setx(x-min(player\u速度,x+宽度/2-player\u大小))
screen.onkey(左,‘左’)
def up():
screen.onkey(无,'Up')
y=player.ycor()
如果y<高度/2-玩家尺寸:
玩家设置标题(90)
玩家设置(y+min(玩家速度、高度/2-玩家大小-y))
screen.onkey(向上,“向上”)
def down():
screen.onkey(无,'Down')
y=player.ycor()
如果y>玩家大小-高度/2:
玩家设置标题(270)
player.sety(y-min(player\u速度,y+高度/2-player\u大小))
screen.onkey(向下,“向下”)
#屏风
screen=screen()
screen.bgcolor('green'))
屏幕标题(“捕获并运行”)
#边界
border_pen=海龟()
边界_pen.hideturtle()
边界笔速度(“最快”)
边框颜色(“白色”)
边境养老金(3)
border_pen.penup()
边框笔设置位置(-宽度/2,-高度/2)
border_pen.pendown()
对于范围内的侧面(2):
border_pen.fd(宽度)
边界_pen.lt(90)
border_pen.fd(高度)
边界_pen.lt(90)
#玩家
玩家=乌龟()
player.shape('三角形')
玩家速度(“最快”)
player.penup()
#密钥绑定
screen.onkey(右,‘右’)
screen.onkey(左,‘左’)
screen.onkey(向上,“向上”)
screen.onkey(向下,“向下”)
screen.listen()
screen.mainloop()

for side in range(2)用于绘制边界。啊,我的错!我不认识乌龟,我只是读了一些关于它的东西,因为它看起来很有趣。