如何使海龟从墙壁弹跳

时间:2019-02-07 00:15:47

标签: python turtle-graphics

我必须为计算机科学类编写代码,该类代码使乌龟在矩形周围弹跳,并且每次这样做都会改变颜色。我试图根据矩形的前进程度将矩形分为4个部分,但是对于初学者来说这似乎不起作用,并且我已经尝试了一段时间。我完全迷路了,因此任何建议/帮助将不胜感激。

import turtle, random

t = turtle.Turtle()
t.speed(0)
t.pensize(5)
t.color("Black")


def Rectangle():
  for i in range(2):
    t.forward(400)
    t.left(90)
    t.forward(200)
    t.left(90)

Rectangle()


t.penup()
t.goto(205,100)
t.color("Blue")
t.shape("circle")


direct = random.randint(1,360)

def tDirection(direct):
  t.right(direct)

tDirection(direct)



while True:

  color = ['red', 'blue', 'green', 'yellow', 'orange', "purple", "pink", "turquoise"]

  t.forward(2)
  ty = t.ycor()
  tx = t.xcor()

  #breaking out bottom
  if ty < 9:
    t.color((random.choice(color)))
    angleCurr = t.heading()
    if(270>angleCurr>180):
      t.left(90)
    if(270<angleCurr<360):
      t.right(90)
    t.forward(2)

  #breaking out top
  if ty > 188:
    t.color((random.choice(color)))
    angleCurr = t.heading()
    if(0<angleCurr<90):
      t.right(90)  
    elif(90<angleCurr<180):
      t.left(90)
    t.forward(2)

  #breaking out left
  if tx < 11:
    t.color((random.choice(color)))
    angleCurr = t.heading()
    if(180<angleCurr<270):
      t.right(90)  
    elif(270<angleCurr<180):
      t.left(90)
    t.forward(2)

  #breaking out right
  if tx > 390:
    t.color((random.choice(color)))
    angleCurr = t.heading()
    if(0<angleCurr<90):
      t.left(90)  
    elif(0<angleCurr<-90):
      t.right(90)
    t.forward(2)

2 个答案:

答案 0 :(得分:0)

在角度方向更改时会犯一些错误,当您必须设置向右倾斜角度+90 时,您会设置向左倾斜角度+90 ,或者必须设置左角+90 ,可在左,右和底壁碰撞中设置右角+90

有时候乌龟会随机更改颜色。choice(color)会将颜色变成原来的颜色。我为更改颜色提供了更好的代码,因此您始终可以更改乌龟的颜色。

import turtle, random

t = turtle.Turtle()
t.speed(0)
t.pensize(5)
t.color("Black")


def Rectangle():
  for i in range(2):
    t.forward(400)
    t.left(90)
    t.forward(200)
    t.left(90)

Rectangle()


t.penup()
t.goto(205,100)
t.color("Blue")
t.shape("circle")

A = random.randint(30,60)
B = random.randint(120,150)
C = random.randint(210,240)
D = random.randint(300,330)
Directions = [A, B, C, D]
direct = random.choice(Directions)

def tDirection(direct):
  t.right(direct)

tDirection(direct)


speed = 2
angle = 90
while True:

  color = ['red', 'blue', 'green', 'yellow', 'orange', "purple", "pink", "turquoise"]

  t.forward(speed)
  ty = t.ycor()
  tx = t.xcor()

  #print(ty, tx)

  #breaking out bottom
  if ty < 9:
    Col = random.choice(color)
    while Col == t.color()[0]:
      Col = random.choice(color)
    t.color(Col)
    angleCurr = t.heading()
    if(270>angleCurr>180):
      t.right(angle)
    else:
      t.left(angle)

    t.forward(2)

  #breaking out top
  if ty > 188:
    Col = random.choice(color)
    while Col == t.color()[0]:
      Col = random.choice(color)
    t.color(Col)
    angleCurr = t.heading()
    if(0<angleCurr<90):
      t.right(angle)
    else:
      t.left(angle)

    t.forward(2)

  #breaking out left
  if tx < 11:
    Col = random.choice(color)
    while Col == t.color()[0]:
      Col = random.choice(color)
    t.color(Col)
    angleCurr = t.heading()
    if(180<angleCurr<270):
      t.left(angle)
    else:
      t.right(angle)

    t.forward(2)

  #breaking out right
  if tx > 390:
    Col = random.choice(color)
    while Col == t.color()[0]:
      Col = random.choice(color)
    t.color(Col)
    angleCurr = t.heading()
    if(0<angleCurr<90):
      t.left(angle)
    else:
      t.right(angle)

    t.forward(speed)

答案 1 :(得分:0)

我相信我们可以修复您的程序并简化它。然后 parameterize ,这样您就可以更改矩形的大小并保持其正常工作,而无需花费很多代码。并将其居中显示在屏幕上:

from turtle import Turtle
from random import randint

COLORS = ['red', 'blue', 'green', 'yellow', 'orange', "purple", "pink", "turquoise"]

WIDTH, HEIGHT = 400, 200

CURSOR_SIZE = 20

def Rectangle():
    t.pendown()

    for i in range(2):
        t.forward(WIDTH)
        t.left(90)
        t.forward(HEIGHT)
        t.left(90)

    t.penup()

def tDirection(direct):
    t.setheading(direct)

t = Turtle("circle", visible=False)
t.speed('fastest')
t.pensize(5)
t.penup()
t.goto(-WIDTH/2, -HEIGHT/2)

Rectangle()

index = 0

t.color(COLORS[index % len(COLORS)])
t.home()
t.showturtle()

direct = randint(1, 360)

tDirection(direct)

while True:
    t.forward(2)
    ty = t.ycor()

    # breaking out top or bottom
    if not CURSOR_SIZE/2 - HEIGHT/2 <= ty <= HEIGHT/2 - CURSOR_SIZE/2:
        index += 1
        t.color(COLORS[index % len(COLORS)])

        angleCurr = t.heading()

        if 0 < angleCurr < 180:
            tDirection(0 - angleCurr)
        else:
            tDirection(360 - angleCurr)

        t.forward(2)

    tx = t.xcor()

    # breaking out left or right
    if not CURSOR_SIZE/2 - WIDTH/2 <= tx <= WIDTH/2 - CURSOR_SIZE/2:
        index += 1
        t.color(COLORS[index % len(COLORS)])

        angleCurr = t.heading()

        if 0 < angleCurr < 180:
            tDirection(180 - angleCurr)
        else:
            tDirection(540 - angleCurr)

        t.forward(2)

您会注意到,我将其从使用 left right 旋转了90度,改为使用绝对航向来控制球从墙壁弹起。并使颜色在颜色列表中以循环方式前进(而不是随机选择一种(可能是相同的一种))。