2013-02-19 83 views
0

我的代碼繪製了網格並設置了X和O.我的問題是,它在屏幕的右上角完成。我想找到一種方法來做到這一切,但中心。問題是我希望這樣做盡可能審美。一切正常,除了位置之外,我完全沒有問題。我無法找到將屏幕設置在屏幕中央的命令。居中繪製一個井字棋遊戲

from turtle import* 

def tttDrawLineFromMidpoint(pen, orientation, lineLength): 
    pen.setheading(orientation) 
    pen.down() 
    pen.fd(lineLength/2) 
    pen.bk(lineLength) 
    pen.fd(lineLength/2) 

def tttDrawXFromMidpoint(pen, cellSize): 
     lineLength = 1.41 * cellSize 
     tttDrawLineFromMidpoint(pen, 45, lineLength) 
     tttDrawLineFromMidpoint(pen, 135, lineLength) 

def tttDrawX(pen, cellsize, row, col): 

     x=col*cellsize 
     y=row*cellsize 

     x+=cellsize/2 
     y+=cellsize/2 

     pen.up() 
     pen.goto(x,y) 

     xSize = cellsize/2 
     tttDrawXFromMidpoint(pen, xSize) 

def tttDrawCircleFromPoint(pen, orientation,lineLength): 
     pen.setheading(orientation) 
     pen.down() 
     pen.up() 
     pen.fd(lineLength/1.5) 
     pen.lt(45) 
     pen.bk(25) 
     pen.down() 
     pen.circle(lineLength/2) 

def tttDrawOFromPoint(pen, cellSize): 
     lineLength = cellSize 
     tttDrawCircleFromPoint(pen,90, lineLength) 

def tttDrawO(pen, cellsize, row, col): 

     x=col*cellsize 
     y=row*cellsize 

     x+=cellsize/2 
     y+=cellsize/2 

     pen.up() 
     pen.goto(x,y) 

     oSize = cellsize/2 
     tttDrawOFromPoint(pen, oSize) 

def tttDrawLineFromEnd(pen, orientation, lineLength): 
     pen.setheading(orientation) 
     pen.down() 
     pen.fd(lineLength) 
     pen.bk(lineLength) 

def tttDrawGrid(pen,cellSize): 
    pen.ht() 

    for i in range (2): 
       pen.up() 
       pen.goto(0, cellSize * (i+1)) 
       tttDrawLineFromEnd(pen,0,3*cellSize) 
    for i in range(2): 
       pen.up() 
       pen.goto(cellSize*(i+1),0) 
       tttDrawLineFromEnd(pen,90,3*cellSize) 

def ttt(cellSize): 
     marker = Turtle() 
     marker.width(3) 
     marker.color('purple') 
     tttDrawGrid(marker,cellSize) 

def tttDrawXOTest(cellSize): 
     marker = Turtle() 
     marker.width(3) 
     marker.color('purple') 
     tttDrawGrid(marker,cellSize) 
     xTurtle = Turtle() 
     xTurtle.width(3) 
     xTurtle.color('orange') 
     for row in range(3): 
      for col in range(3): 
        tttDrawX(xTurtle, cellSize, row, col) 

     xTurtle = Turtle() 
     xTurtle.width(3) 
     xTurtle.color('pink') 
     for row in range(3): 
      for col in range(3): 
        tttDrawO(xTurtle, cellSize, row, col) 


t=Turtle() 
t.ht() 
ttt(100) 
print("Let's Play Tic Tac Toe >:D") 
print("X goes first!") 
x = input("Choose a row between 0-2: ") 
x = int(x) 
x = x 
y = input("Choose a coluumn between 0-2: ") 
y = int(y) 
tttDrawX(t,100,x,y) 

print("O goes next!") 
x = input("Choose a row between 0-2: ") 
x = int(x) 
x = x 
y = input("Choose a coluumn between 0-2: ") 
y = int(y) 
tttDrawO(t,100,x,y) 

exitonclick() 
+1

這是爲了一個班級,練習,還是它真的試圖做一個好的遊戲?因爲海龜只用於學習目的 – ghostbust555 2013-02-19 02:05:35

+0

班級分配。作業是創建圈子並進行X和O測試。我已經這樣做了,只是它沒有居中-__- 這個遊戲是由幾個作業編譯的。每個作業都在做另一部分遊戲。 – 2013-02-19 07:04:40

回答

0

由於這個問題是關於集中董事會,我已經刪除了一切,但在我的示例代碼下面的板的繪圖。關鍵是要做到圍繞零點你的數學,所以對在井字棋板線的位置,只有真正通過自己的標誌各不相同:

from turtle import Turtle, Screen 

def tttDrawLineFromEnd(pen, orientation, lineLength): 
    pen.setheading(orientation) 
    pen.down() 
    pen.forward(lineLength) 

def tttDrawGrid(pen, cellSize): 
    for sign in (-1, 1): 
     pen.up() 
     pen.goto(-3 * cellSize/2, sign * cellSize/2) 
     tttDrawLineFromEnd(pen, 0, 3 * cellSize) 

    for sign in (-1, 1): 
     pen.up() 
     pen.goto(sign * cellSize/2, -3 * cellSize/2) 
     tttDrawLineFromEnd(pen, 90, 3 * cellSize) 

def ttt(cellSize): 
    marker = Turtle(visible=False) 
    marker.width(3) 
    marker.color('purple') 
    tttDrawGrid(marker, cellSize) 

ttt(100) 

screen = Screen() 

screen.exitonclick() 

一招我用的時候用烏龜編程是正確的,我創建烏龜後,我打電話turtle.dot()在屏幕的中心做一個可見的斑點,所以我知道如果我正在繪製我所期望的。然後我在最終的代碼中刪除這個調試幫助。