### Python-Turtle
---
### Module - Turtle
****
import turtle
---
### 設定螢幕
***
```python=
screen = turtle.Screen()
```
----
### 畫面設定
***
```python=
screen = turtle.Screen()
screen.title("Title") #設定標題
screen.setup(width=200,height=300) #螢幕大小
screen.tracer(0) #關閉作畫軌跡
screen.bgcolor("#F0F8FF")# 背景顏色
```
---
### 指令繪畫
***
```python=
turtle.forward(100)
turtle.mainloop()
```
----
### 指定turtel位置
***
turtle.setposition(x,y)
----
### 控制角度
***
turtle.setheading(90)
----
### 控制turtle停筆或下筆
***
停筆狀態 turtle.penup()、下筆狀態turtle.down()
----
### 改變顏色
***
turtle.color('red')
---
### 利用Turtle製作貪吃蛇小遊戲
----
### 設定螢幕
***
```python=
#creating screen
screen = turtle.Screen()
screen.title("Snake Game")
screen.setup(width=700,height=700)
screen.tracer(0)
screen.bgcolor("#1d1d1d")
```
----
### 建立邊界
```python=
# creating border
turtle.speed(1)
turtle.pensize(4)
turtle.penup()
turtle.goto(-310,250)
turtle.pendown()
turtle.color("red")
turtle.forward(600)
turtle.right(90)
turtle.forward(500)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.forward(500)
turtle.right(90)
turtle.penup()
turtle.hideturtle()
```
----
### 設定分數
```python=
score = 0
delay = 0.1
```
----
### 蛇的設定
```python=
snake = turtle.Turtle()
snake.speed()
snake.shape("square")
snake.color("red")
snake.penup()
snake.goto(0,0)
snake.direction = 'stop'
```
----
### 食物設定
```python=
#food
fruit = turtle.Turtle()
fruit.speed(0)
fruit.shape("square")
fruit.color("white")
fruit.penup()
fruit.goto(30,30)
old_fruit = []
```
----
### 分數計算
```python=
#scoring
scoring = turtle.Turtle()
scoring.speed(0)
scoring.color("white")
scoring.penup()
scoring.hideturtle()
scoring.goto(0,300)
scoring.write("Score:",align="center",font=("Courier",24,"bold"))
```
----
### 遊戲控制
```python=
#define how to move
def snake_go_up():
if snake.direction != "down":
snake.direction = "up"
def snake_go_down():
if snake.direction != "up":
snake.direction = "down"
def snake_go_left():
if snake.direction != "right":
snake.direction = "left"
def snake_go_right():
if snake.direction != "left":
snake.direction = "right"
def snake_move():
if snake.direction == "up":
y = snake.ycor()
snake.sety(y + 20)
if snake.direction == "down":
y = snake.ycor()
snake.sety(y - 20)
if snake.direction == "left":
x = snake.xcor()
snake.setx(x - 20)
if snake.direction == "right":
x = snake.xcor()
snake.setx(x + 20)
```
----
### 鍵盤輸入
```python=
# keybord binging
screen.listen()
screen.onkeypress(snake_go_up,"Up")
screen.onkeypress(snake_go_down,"Down")
screen.onkeypress(snake_go_left,"Left")
screen.onkeypress(snake_go_right,"Right")
```
----
### 遊戲設定
```python=
#main loop
while True:
screen.update()
#snake & fruit colision
if snake.distance(fruit) < 20:
x = random.randint(-290,270)
y = random.randint(-240,240)
fruit.goto(x,y)
scoring.clear()
score += 1
scoring.write("Score: {}".format(score),
align="center",
font=("Courier",24,"bold"))
delay -= 0.001
#creating new foods
new_fruit = turtle.Turtle()
new_fruit.speed(0)
new_fruit.shape("square")
new_fruit.color("red")
new_fruit.penup()
old_fruit.append(new_fruit)
#add ball to snake
for index in range(len(old_fruit) - 1 ,0,-1):
a = old_fruit[index - 1].xcor()
b = old_fruit[index-1].ycor()
old_fruit[index].goto(a,b)
if len(old_fruit) > 0:
a = snake.xcor()
b = snake.ycor()
old_fruit[0].goto(a,b)
snake_move()
# snake & border colision
if snake.xcor() > 280 or snake.xcor()<-300 or snake.ycor() > 240 or snake.ycor() <-240:
time.sleep(1)
screen.clear()
screen.bgcolor("turquoise")
scoring.goto(0,0)
scoring.write("GameOver \n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))
#snake colisions
for food in old_fruit:
if food.distance(snake) <20:
time.sleep(1)
screen.clear()
screen.bgcolor("turquoise")
scoring.goto(0,0)
scoring.write("GameOver \n Your Score is {}".format(score),align="center",font=("Courier",30,"bold"))
time.sleep(delay)
turtle.Terminator()
```
{"metaMigratedAt":"2023-06-18T06:25:23.585Z","metaMigratedFrom":"YAML","title":"Python Lesson6","breaks":true,"description":"View the slide with \"Slide Mode\".","contributors":"[{\"id\":\"3fc05001-8177-4478-9228-1c5f83fffcd5\",\"add\":11153,\"del\":6623}]"}