當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python turtle.setworldcoordinates()用法及代碼示例

turtle 模塊以麵向對象和麵向過程的方式提供 turtle 圖形基元。由於它使用tkinter作為基礎圖形,因此需要安裝有Tk支持的Python版本。

turtle .setworldcoordinates()

該函數用於設置用戶定義的坐標係。這將執行重置。如果模式‘world’已經處於活動狀態,則根據新坐標重新繪製所有圖形。

注意:在用戶定義的坐標係中,角度可能會出現變形。

用法:turtle.setworldcoordinates(llx, lly, urx, ury)

參數:



  • llx:畫布左下角的數字,x坐標
  • lly:畫布左下角的數字y坐標
  • urx:畫布右上角的數字,x坐標
  • ury:畫布右上角的數字y坐標

下麵是上述方法的實現和一些示例:

範例1:

Python3

# importing package 
import turtle 
  
# make screen objcet and 
# set screen mode to world 
sc = turtle.Screen() 
sc.mode('world') 
  
# set world coordinates 
turtle.setworldcoordinates(-20, -20, 20, 20) 
  
# loop for some motion 
for i in range(20):
    turtle.forward(1+1*i) 
    turtle.right(90)

輸出:

甲部

範例2:

Python3

# importing package 
import turtle 
  
# make screen objcet and 
# set screen mode to world 
sc = turtle.Screen() 
sc.mode('world') 
  
# set world coordinates 
turtle.setworldcoordinates(-40, -40, 40, 40) 
  
# loop for some motion 
for i in range(20):
    turtle.forward(1+1*i) 
    turtle.right(90)

輸出:

在以上兩個示例中,代碼是相同的,隻有世界坐標的不同才使輸出不同,如下所示:

範例3:

Python3

# importing package 
import turtle 
  
# make screen object and 
# set mode to world 
sc = turtle.Screen() 
sc.mode('world') 
  
# set world coordinates 
turtle.setworldcoordinates(-50, -50, 50, 50) 
  
# do some motion 
for i in range(16):
    turtle.forward(1+1*i) 
    turtle.right(90) 
  
# set world coordinates 
turtle.setworldcoordinates(-40, -40, 40, 40) 
  
# do some motion 
for i in range(16):
    turtle.forward(1+1*(i+16)) 
    turtle.right(90) 
  
# set world coordinates 
turtle.setworldcoordinates(-30, -30, 30, 30) 
  
# do some motion 
for i in range(16):
    turtle.forward(1+1*(i+32)) 
    turtle.right(90)

輸出:

在這裏,我們可以看到所有先前的圖形都設置為新的世界坐標(圖形放大)。




相關用法


注:本文由純淨天空篩選整理自deepanshu_rustagi大神的英文原創作品 turtle.setworldcoordinates() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。