import turtle#海龜庫
import time
import random
#定義上下左右按鍵內容,此處b為老鼠
def up():
b.setheading(90)#按x-y座標算,轉90°
b.forward(30)#30個畫素
def down():
b.setheading(270)
b.forward(30)
def left():
b.setheading(180)
b.forward(30)
def right():
b.setheading(0)
b.forward(30)
#定義螢幕
playground = turtle.screen()#先定義海龜playground為螢幕
playground.size=turtle.screensize(canvwidth=1920,canvheight=1080,bg=「yellow」)
#英文可不寫,注意格式,長,寬,背景色
playground.register_shape(「a.gif」)#將a.gif(貓)與py檔案放在同乙個資料夾
playground.register_shape(「b.gif」)
playground.onkey(up,「up」)#定義up(上)的動作由「up鍵」完成
playground.onkey(down,「down」)
playground.onkey(left,「left」)
playground.onkey(right,「right」)
playground.listen()#監聽海龜
h=turtle.turtle()#定義海龜h
h.color(「pink」)#畫筆顏色為粉色
h.penup()#提起筆移動,不繪製圖形,用於另起乙個地方繪製
h.home()#設定當前畫筆位置為原點,朝向東
h.write(「helloworld」,align =『center』,font=(「comic sans ms」,50,「bold」))
#居中,粗體
h.goto(0,-50)
h.write(「start」,align =『center』,
font=(「comic sans ms」,30,「bold」))
time.sleep(2)
h.clear()
a=turtle.turtle()
a.shape(「a.gif」)
a.penup()
a.goto(random.randint(-200,200),random.randint(-200,200))
a.pendown()
a.pensize(2)
a.color(「pink」)
b=turtle.turtle()
b.shape(「b.gif」)
b.penup()
b.goto(random.randint(-200,200),random.randint(-200,200))
b.speed(0)
start= time.time()
while true:
a.setheading(a.towards(b))
a.forward(5)
if a.distance(b) < 10:
playground.clear()
b.goto(-30,-100)
a.goto(0,0)
end=time.time()
b.write(「game over」,align=『center』,font=(「comic sans ms」,50))
a.write(「你活了秒」.format(end-start),align=『center』,font=(「comic sans ms」,50))
time.sleep(2)
break
分享一些指令/解析
習題 168 貓捉老鼠
有乙隻很霸道的貓,捉來好多好多老鼠,然後,這只貓就開始開大餐了。但為了顯得它與眾不同,它用了乙個很特別的就餐順序 它先定好步長k,然後它吃掉第1個位置上的,然後再每隔k只再吃乙隻老鼠 一輪結束後,它再次從第1個位置開始繼續吃,直到最後只剩下乙隻老鼠為止。而這最後乙隻老鼠這只貓會放走,因為它不想破壞生...
貓捉老鼠問題系列(一)
形象化的數學問題,總是引人入勝,這個世界創意還是很重要的。從網上收集了一些貓和老鼠的數學話題,僅供欣賞之。在數軸上,0的位置停著乙個不動的老鼠,1的位置在初始時刻有乙隻貓。貓是可以走動的,每一步在數軸上分別以二分之一的概率或朝著正方向或朝著負方向走1的距離。當貓到達0的位置時,貓就抓到老鼠了,遊戲結...
貓捉老鼠問題系列(四)
從有限到無限,似乎是乙個無法逾越的鴻溝,需要思維的跨越。數學中引入極限概念,實現有限和無限的連線,而極限概念更聯絡著許多知名的悖論,如芝諾悖論,旅館悖論等等。可以說有限和無限的秘密,將是乙個永恆的話題。本章與極限雖無直接關係,但仍可據此浮想聯翩。乙隻貓發現離它10步遠的前方有乙隻老鼠在奔跑,便哈腰緊...