소속은 문과지만 필교때문에 파이썬을 배우는 중인데요
과제가 파이썬으로 터틀런 게임을 만들어오는건데
터틀 이동하는거, 적 터틀이 따라오는거 까지는 다 되고 이해도 되는데
경계선을 그리고 그 안에서만 움직이게 하는 건 도저히 못하겠네요...
좌표 -300, -300 부터 300, 300까지 네모난 경계선을 그리고
경계선에 도달하면 멈추고 방향을 바꾸면 움직이게 설정하라는데
멈추고 방향 바꾸면 움직이는걸 도저히 못하겠습니다.. 도와주세요
교수님이 힌트랍시고 고작 주신 것은
1) 경계선을 그릴 때 turtle 숨기기
t.hideturtle() # 숨기기
t.showturtle() # 보이기
2) 플레이어 터틀의 현재의 위치를 아는 방법
xpos, ypos = t.pos() # 플레이어 터틀의 x축, y축 좌표를 리턴해줌
이고
밑에는 지금까지 배운내용을 써서 만든 소스코드들입니다.
import turtle as t
t.shape('turtle')
t.color('white')
t.bgcolor('purple')
t.penup()
t.speed(0)
t.hideturtle()
t.goto(-300, -300)
t.pendown()
t.fd(600)
t.left(90)
t.fd(600)
t.left(90)
t.fd(600)
t.left(90)
t.fd(600)
t.left(90)
t.penup()
t.goto(0, 0)
t.showturtle()
t.speed(0)
te=t.Turtle()
te.shape('turtle')
te.color('red')
te.speed(0)
te.up()
te.goto(0, 200)
def turn_right():
t.seth(0)
def turn_up():
t.seth(90)
def turn_left():
t.seth(180)
def turn_down():
t.seth(270)
t.onkeypress(turn_right, 'Right')
t.onkeypress(turn_up, 'Up')
t.onkeypress(turn_left, 'Left')
t.onkeypress(turn_down, 'Down')
t.listen()
def play():
xpos, ypos = t.pos()
ang=te.towards(t.pos())
te.seth(ang)
te.forward(9)
t.fd(10)
t.ontimer(play,100)
t.ontimer(play,100)