图形和海龟中的Python游戏无法正常工作

时间:2017-01-08 01:44:43

标签: python graphics turtle-graphics

图形和海龟中的Python游戏。

每当乌龟越过隐藏的目标时,它会因某种原因而减速。

感谢您的帮助

import turtle
from graphics import *
import time
import math
point = 0

win = turtle.Screen()
win.bgcolor("lightgreen")
win2 = GraphWin("Points",200, 200)
win2.setCoords(0.0, 0.0, 2.0, 2.0)
text = Text(Point(1,1.5), "Points")
text2 = Text(Point(1,1), point)
text.draw(win2)
text2.draw(win2)


border = turtle.Turtle()
border.penup()
border.setposition(-250,-250)
border.pendown()
border.pensize(4)
for borderside in range (4):
    border.forward(500)
    border.left(90)
border.hideturtle()

goal = turtle.Turtle()
goal.color("red")
goal.shape("circle")
goal.penup()
goal.speed(0)
goal.setposition(-200,200)


player = turtle.Turtle()
player.color("red")
player.shape("turtle")
player.pensize(4)
player.resizemode("auto")
player.penup()
player.speed(1)

speed = 1

def turnleft():
    player.left(20)

def turnright():
    player.right(20)

def increasespeed():
     global speed
     speed = speed + 0.5

turtle.listen()
turtle.onkey(turnleft,"Left")
turtle.onkey(turnright,"Right")
turtle.onkey(increasespeed,"Up")

以下是边界强制执行程序以及使目标隐藏导致问题的原因。

while True:
    player.forward(speed)
    if player.xcor() > 250 or player.xcor() < -250:
        player.right(180)
    if player.ycor() > 250 or player.ycor() < -250:
        player.right(180)
    distance = math.sqrt(math.pow(player.xcor()-goal.xcor(),2) + math.pow(player.ycor()-goal.ycor(),2))
    if distance < 20:
        goal.clear()
        goal.hideturtle()
        win2.delete("all")
        text = Text(Point(1, 1.5), "Points")
        text2 = Text(Point(1, 1), point + 10)
        text.draw(win2)
        text2.draw(win2)

0 个答案:

没有答案