Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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 当我让两只海龟相撞时,巨蟒冻结并崩溃 导入海龟 导入操作系统 输入数学 ms=turtle.Screen() bgcolor女士(“灰色”) title女士(“ok”) 地面=海龟。海龟() 地面速度(0) 地面颜色(“黑色”) 地面 接地设置位置(-500,-500) 地面形状(“方形”) 地面.形状尺寸(20,200) 玩家=乌龟。乌龟() 玩家形状(“正方形”) 播放器颜色(“蓝色”) player.penup() 玩家速度(0) 玩家设置位置(-450,-280) 球员速度=15 prop=海龟。海龟() 道具速度(0) 道具形状(“方形”) 第五号提案 道具颜色(“红色”) 道具设置位置(-200,-50) def向左移动() x=player.xcor() x-=玩家速度 如果x 460: x=460 player.setx(x) def move_down(): y=player.ycor() y-=球员速度 如果y 290: y=290 球员赛蒂(y) 乌龟,听着 乌龟。上键(向左移动,“左”) 乌龟.安基(向右移动,“向右”) 乌龟。上键(向上移动,“向上”) 乌龟。上键(向下移动,“向下”) def聚合(t1、t2): 距离=math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2)) 如果距离小于10: 返回真值 其他: 返回错误 尽管如此: 如果集合(玩家、道具): 播放器设置位置(100100)_Python_Turtle Graphics - Fatal编程技术网

Python 当我让两只海龟相撞时,巨蟒冻结并崩溃 导入海龟 导入操作系统 输入数学 ms=turtle.Screen() bgcolor女士(“灰色”) title女士(“ok”) 地面=海龟。海龟() 地面速度(0) 地面颜色(“黑色”) 地面 接地设置位置(-500,-500) 地面形状(“方形”) 地面.形状尺寸(20,200) 玩家=乌龟。乌龟() 玩家形状(“正方形”) 播放器颜色(“蓝色”) player.penup() 玩家速度(0) 玩家设置位置(-450,-280) 球员速度=15 prop=海龟。海龟() 道具速度(0) 道具形状(“方形”) 第五号提案 道具颜色(“红色”) 道具设置位置(-200,-50) def向左移动() x=player.xcor() x-=玩家速度 如果x 460: x=460 player.setx(x) def move_down(): y=player.ycor() y-=球员速度 如果y 290: y=290 球员赛蒂(y) 乌龟,听着 乌龟。上键(向左移动,“左”) 乌龟.安基(向右移动,“向右”) 乌龟。上键(向上移动,“向上”) 乌龟。上键(向下移动,“向下”) def聚合(t1、t2): 距离=math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2)) 如果距离小于10: 返回真值 其他: 返回错误 尽管如此: 如果集合(玩家、道具): 播放器设置位置(100100)

Python 当我让两只海龟相撞时,巨蟒冻结并崩溃 导入海龟 导入操作系统 输入数学 ms=turtle.Screen() bgcolor女士(“灰色”) title女士(“ok”) 地面=海龟。海龟() 地面速度(0) 地面颜色(“黑色”) 地面 接地设置位置(-500,-500) 地面形状(“方形”) 地面.形状尺寸(20,200) 玩家=乌龟。乌龟() 玩家形状(“正方形”) 播放器颜色(“蓝色”) player.penup() 玩家速度(0) 玩家设置位置(-450,-280) 球员速度=15 prop=海龟。海龟() 道具速度(0) 道具形状(“方形”) 第五号提案 道具颜色(“红色”) 道具设置位置(-200,-50) def向左移动() x=player.xcor() x-=玩家速度 如果x 460: x=460 player.setx(x) def move_down(): y=player.ycor() y-=球员速度 如果y 290: y=290 球员赛蒂(y) 乌龟,听着 乌龟。上键(向左移动,“左”) 乌龟.安基(向右移动,“向右”) 乌龟。上键(向上移动,“向上”) 乌龟。上键(向下移动,“向下”) def聚合(t1、t2): 距离=math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2)) 如果距离小于10: 返回真值 其他: 返回错误 尽管如此: 如果集合(玩家、道具): 播放器设置位置(100100),python,turtle-graphics,Python,Turtle Graphics,如果我将距离更改为>10 它工作,但不是我想要的。 当prop和player之间的距离小于等于10像素时,我希望它更改播放器的位置。 我已经尝试了我所知道的大多数事情,但我仍然是Python或任何编程语言的新手 但是我不知道什么会导致它冻结和崩溃——非常感谢您的帮助。我看到您的代码的主要问题是,而True:不应该在基于事件的环境中使用。通过创建紧密循环,可以防止事件(按键、光标移动、窗口关闭)被处理 下面是使用计时器事件而不是对代码进行的返工,而True:。我还对它进行了参数化,因为每个海龟用户

如果我将距离更改为
>10
它工作,但不是我想要的。 当
prop
player
之间的距离小于等于10像素时,我希望它更改
播放器的位置。
我已经尝试了我所知道的大多数事情,但我仍然是Python或任何编程语言的新手


但是我不知道什么会导致它冻结和崩溃——非常感谢您的帮助。

我看到您的代码的主要问题是
,而True:
不应该在基于事件的环境中使用。通过创建紧密循环,可以防止事件(按键、光标移动、窗口关闭)被处理

下面是使用计时器事件而不是
对代码进行的返工,而True:
。我还对它进行了参数化,因为每个海龟用户都会根据屏幕的尺寸看到不同大小的默认窗口。因此,您需要使用
setup()
强制设置一个固定大小的窗口,或者让代码调整到窗口大小。现在它会根据窗口大小进行调整。我还让
prop
在碰撞时移动到一个随机位置,只是为了让游戏对我来说更有趣。我把你的距离码扔给了turtle自己的实现:

import turtle
import os
import math

ms = turtle.Screen()
ms.bgcolor("grey")
ms.title("ok")

ground = turtle.Turtle()
ground.speed(0)
ground.color("black")
ground.penup()
ground.setposition(-500, -500)
ground.shape("square")
ground.shapesize(20, 200)

player = turtle.Turtle()
player.shape("square")
player.color("blue")
player.penup()
player.speed(0)
player.setposition(-450, -280)

playerspeed = 15

prop = turtle.Turtle()
prop.speed(0)
prop.shape("square")
prop.penup()
prop.color("red")
prop.setposition(-200, -50)

def move_left():
    x = player.xcor()
    x-= playerspeed
    if x <-460:
        x = - 460
    player.setx(x)

def move_right():
    x = player.xcor()
    x+= playerspeed
    if x >460:
        x =  460
    player.setx(x)

def move_down():
    y = player.ycor()
    y-= playerspeed
    if y <-290:
        y = - 290
    player.sety(y)

def move_up():
    y = player.ycor()
    y+= playerspeed
    if y >290:
        y =  290
    player.sety(y)

turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")

def isCollision(t1, t2):
    distance = math.sqrt(math.pow(t1.xcor() - t2.xcor(), 2) + math.pow(t1.ycor() - t2.ycor(), 2))
    if distance < 10:
        return True
    else:
        return False

while True:
    if isCollision(player, prop):
        player.setposition(100, 100)
从turtle导入屏幕,turtle,mainloop
从随机导入randint
玩家速度=15
地面高度=100
接近度=10
光标大小=20
def向左移动()
x=player.xcor()-播放器速度
如果x<光标大小-宽度/2:
x=光标大小-宽度/2
player.setx(x)
def向右移动()
x=播放器.xcor()+播放器速度
如果x>宽度/2-光标大小:
x=宽度/2-光标大小
player.setx(x)
def move_down():
y=player.ycor()-播放器速度
如果y<光标大小/2+地面高度-高度/2:
y=光标大小/2+地面高度-高度/2
球员赛蒂(y)
def上移()
y=player.ycor()+player\u速度
如果y>高度/2-光标大小:
y=高度/2-光标大小
球员赛蒂(y)
def聚合(t1、t2):
返回t1.距离(t2)<接近度
def随机_位置():
x=randint(光标大小-宽度//2,宽度//2-光标大小)
y=randint(光标大小+地面高度-高度//2,高度//2-光标大小)
返回x,y
def check():
如果集合(玩家、道具):
属性设置位置(随机位置()
安蒂默女士(支票,100)
ms=屏幕()
bgcolor女士(‘灰色’)
title女士(“ok”)
宽度,高度=ms.window_宽度(),ms.window_高度()
地面=乌龟(‘方形’)
地面形状大小(地面高度/光标大小、宽度/光标大小)
地面速度(“最快”)
地面
地面设置(地面高度/2-高度/2)
玩家=乌龟('square')
玩家速度(“最快”)
player.color('blue'))
player.penup()
播放器设置位置(光标大小/2-宽度/2,地面高度+光标大小/2-高度/2)
道具=乌龟(方形)
道具速度(“最快”)
道具颜色(“红色”)
第五号提案
属性设置位置(随机位置()
昂基女士(向左移动,'向左')
昂基女士(向右移动,'向右')
昂基女士(上移,“上移”)
昂基女士(下移“下移”)
listen女士()
检查()
mainloop()

我看到您的代码的主要问题是
,而True:
不应在基于事件的环境中使用。通过创建紧密循环,可以防止事件(按键、光标移动、窗口关闭)被处理

下面是使用计时器事件而不是
对代码进行的返工,而True:
。我还对它进行了参数化,因为每个海龟用户都会根据屏幕的尺寸看到不同大小的默认窗口。因此,您需要使用
setup()
强制设置一个固定大小的窗口,或者让代码调整到窗口大小。现在它会根据窗口大小进行调整。我还让
prop
在碰撞时移动到一个随机位置,只是为了让游戏对我来说更有趣。我把你的距离码扔给了turtle自己的实现:

import turtle
import os
import math

ms = turtle.Screen()
ms.bgcolor("grey")
ms.title("ok")

ground = turtle.Turtle()
ground.speed(0)
ground.color("black")
ground.penup()
ground.setposition(-500, -500)
ground.shape("square")
ground.shapesize(20, 200)

player = turtle.Turtle()
player.shape("square")
player.color("blue")
player.penup()
player.speed(0)
player.setposition(-450, -280)

playerspeed = 15

prop = turtle.Turtle()
prop.speed(0)
prop.shape("square")
prop.penup()
prop.color("red")
prop.setposition(-200, -50)

def move_left():
    x = player.xcor()
    x-= playerspeed
    if x <-460:
        x = - 460
    player.setx(x)

def move_right():
    x = player.xcor()
    x+= playerspeed
    if x >460:
        x =  460
    player.setx(x)

def move_down():
    y = player.ycor()
    y-= playerspeed
    if y <-290:
        y = - 290
    player.sety(y)

def move_up():
    y = player.ycor()
    y+= playerspeed
    if y >290:
        y =  290
    player.sety(y)

turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")

def isCollision(t1, t2):
    distance = math.sqrt(math.pow(t1.xcor() - t2.xcor(), 2) + math.pow(t1.ycor() - t2.ycor(), 2))
    if distance < 10:
        return True
    else:
        return False

while True:
    if isCollision(player, prop):
        player.setposition(100, 100)
从turtle导入屏幕,turtle,mainloop
从随机导入randint
玩家速度=15
地面高度=100
接近度=10
光标大小=20
def向左移动()
x=player.xcor()-播放器速度
如果x<光标大小-宽度/2:
x=光标大小-宽度/2
player.setx(x)
def向右移动()
x=播放器.xcor()+播放器速度
如果x>宽度/2-光标大小:
x=宽度/2-光标大小
player.setx(x)
def move_down():
y=player.ycor()-播放器速度
如果y<光标大小/2+地面高度-高度/2:
y=光标大小/2+地面高度-高度/2
球员赛蒂(y)
def上移()
y=player.ycor()+player\u速度
如果y>高度/2-光标大小:
y=高度/2-光标大小
球员赛蒂(y)
def聚合(t1、t2):
返回t1.距离(t2)<接近度
def随机_位置():
x=randint(光标大小-宽度//2,