[英]Python ticTacToe game with turtle
我正在為班級作業制作TicTacToe游戲,在某些地方我被困住了並且可以使用一些幫助...該游戲應該能夠在網格中繪制Xs和Os並執行完整的游戲,這是我的代碼
import turtle
import math
import random
cellSize = 100
turtleWidth =10
gridColor = 'Black'
xColor = 'Blue'
oColor = 'Red'
winColor = 'green'
drawColor = 'Gray'
winninTriples = ((0,1,2)
def onclick(x, y):
print x, y
turtle.Screen().onscreenclick(onclick)
#the grid is here
def drawGrid(pen, length, xcoor, ycoor):
startX = [xcoor,xcoor,xcoor+length, xcoor+(2*length)]
endX = [xcoor+(3*length), xcoor+(3*length), xcoor+(length),xcoor+(2*length)]
startY = [ycoor+(2*length),ycoor+length, ycoor, ycoor]
endY = [ycoor+(2*length), ycoor+length, ycoor+(3*length),ycoor+(3*length)]
for grid in range(4):
p.up()
p.goto(startX[grid],startY[grid])
p.down()
p.goto(endX[grid],endY[grid])
pen = turtle.Turtle()
pen.width(5)
drawGrid(pen,100,-40,-50)
board = [0,1,2,
3,4,5,
6,7,8]
def theWinnner(grid0,grid1,grid2,grid3,grid4,grid5,grid6,grid7,grid8):
X = 'X wins'
O = 'O wins'
No = 'Draw'
#this was a code that was given, I am supposed to add on to it and I am not sure how it can be modified and what should I put as the parameters when executing it?
def drawX(t,x,y,size):
drawLine(t, x + size/4, y + size/4, x - size/4, y - size/4)
drawLine(t, x - size/4, y + size/4, x + size/4, y - size/4)
def drawO(t,x,y,size):
t.cirle(size)
def Move():
y = input("Please choose a row:")
x = input("Now please choose a column:")
if x in ['0','1','2'] and y in ['0','1','2']:
return("Your destination is " + y + " " + "and " + x)
else:
return("One or more of your dimensions is out of bounds")
到目前為止,這是我的工作,我對下一步的工作感到迷茫。
我獲取了代碼,使其正常工作,並弄清楚下一步該怎么做。 我要做的第一件事是使用Turtle()
函數設置一個板。 我將使用Turtle.ht()
隱藏烏龜,然后進行線條Turtle.ht()
,並找出每個正方形所在的區域。使用if循環,制作“按鈕”或空格(如果按下,則為X
或O
。 然后使用Turtle.forward()
, Turtle.right()
, Turtle.back()
和Turtle.left()
使X
和O
成為X
同樣,修復您的縮進,這將引發一些錯誤,並from turtle import *
添加。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.