蟒蛇的井字游戏播放器出现问题

时间:2019-07-05 21:59:43

标签: python swap turtle-graphics

我正在编写一个井字游戏程序,作为我正在上课的作业。目前,我正在尝试使用pycharm中的乌龟创建井字游戏。我的意图是让我在其中运行程序的游戏开始,该程序首先随机选择要启动的玩家,然后显示所选玩家的乌龟,一旦该玩家开始,他们就可以使用箭头键在屏幕上移动乌龟。板,然后按空格画X或O,然后它自动结束回合并开始下一个玩家回合。这应该继续进行,直到一个或另一个已经连续宣布3个获胜者为止。到目前为止,我已经在乌龟上创建了一个3x3的棋盘,制作了一只玩家乌龟,并使其能够移动到9个正方形中任意一个的中心,然后在空间上绘制了x或o。

我的问题开始于切换玩家,我的意图是让一个for循环运行5次(将进行10次转弯,只有9个空格),而在那个for循环中,我希望有两个while循环(每个玩家一个)。我已经定义了播放器乌龟,因此我只需要在while循环中调用player_1即可创建一个可以在程序中控制的乌龟。然后,我计划在一个true或false变量上设置while循环,每当按下空格键以将玩家标记放到板上时,该变量将被更改,以便while循环结束并移至下一个玩家。但是我很快发现这种方法的问题在于,当while命令循环时,我的玩家乌龟会被无限地重新创建,因此当玩家尝试移动棋子时,它很快就会重置为其原始状态。

我可以看到需要发生的事情是我需要找到一种方法来使while语句在等待玩家做出决定时保持有效,但是我不知道该执行哪种命令。有谁知道我如何告诉程序等待while语句,直到在乌龟中做出选择?

import turtle


Window = turtle.Screen()
Window.bgcolor()
Window.title("Tic Tac Toe")

# Drawing the Board
def border():
    border_outer = turtle.Turtle()
    border_outer.speed(0)
    border_outer.penup()
    border_outer.setposition(-375, -375)
    border_outer.pendown()
    border_outer.pensize(3)
    for num in range(4):
        border_outer.forward(750)
        border_outer.left(90)
        border_outer.hideturtle()

        # inside border
        border_inner1 = turtle.Turtle()
        # border_inner1.color("red")
        border_inner1.speed(0)
        border_inner1.penup()
        border_inner1.setposition(-125, 375)
        border_inner1.pendown()
        border_inner1.pensize(3)
        border_inner1.right(90)
        border_inner1.forward(750)
        border_inner1.left(90)
        border_inner1.forward(250)
        border_inner1.left(90)
        border_inner1.forward(750)
        border_inner1.hideturtle()

        border_inner2 = turtle.Turtle()
        # border_inner2.color("red")
        border_inner2.speed(0)
        border_inner2.penup()
        border_inner2.setposition(-375, 125)
        border_inner2.pendown()
        border_inner2.pensize(3)
        border_inner2.forward(750)
        border_inner2.right(90)
        border_inner2.forward(250)
        border_inner2.right(90)
        border_inner2.forward(750)
        border_inner2.hideturtle()

# Player 1 Turtle
def player_1():
    # Player 1
    global player
    player = turtle.Turtle()
    player.shape("circle")
    player.penup()
    player.color("blue")
    player.speed()

playerspeed = 250

# Moving the player #1
def move_left():
    x = player.xcor()
    x -= playerspeed
    if x < -375:
        x = -250
        player.setx(x)

def move_right():
    x = player.xcor()
    x += playerspeed
    if x > 375:
        x = 250
        player.setx(x)

def move_down():
    y = player.ycor()
    y -= playerspeed
    if y < -375:
        y = -250
        player.sety(y)

def move_up():
    y = player.ycor()
    y += playerspeed
    if y > 375:
        y = 250
        player.sety(y)

def set_position():
    x = player.xcor()
    y = player.ycor()
    player.goto(x, y - 50)
    player.pendown()
    player.pensize(10)
    player.circle(50, None, None)
    player.penup()
    player.goto(x, y)
    global flag
    flag = False


# Keyboard Bindings for player 1
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")
turtle.onkey(set_position, "space")

flag = 'false'

border()

while flag == True:
    player_1


turtle.mainloop()

1 个答案:

答案 0 :(得分:0)

我只创建一个玩家并将其分配给变量p1p2

稍后,我将p1p2分配给变量player并将其用作当前播放器。

我不使用while循环,因为我计算了set_positon()内的移动(玩家的变化)。我也在set_positon()内更改了播放器。

它不会在行/列中检查3,但它也应该在set_positon()

中执行此操作
import turtle

window = turtle.Screen()
window.bgcolor()
window.title("Tic Tac Toe")

# Drawing the Board
def draw_border():
    t = turtle.Turtle()
    t.speed(0)
    t.pensize(3)

    t.penup()
    t.home()   
    t.setposition(-375, -375)
    t.pendown()

    for num in range(4):
        t.forward(750)
        t.left(90)
        t.hideturtle()

    t.penup()
    t.home()   
    t.setposition(-125, 375)
    t.pendown()

    t.right(90)
    t.forward(750)
    t.left(90)
    t.forward(250)
    t.left(90)
    t.forward(750)
    t.hideturtle()

    t.penup()
    t.home()   
    t.setposition(-375, 125)
    t.pendown()

    t.forward(750)
    t.right(90)
    t.forward(250)
    t.right(90)
    t.forward(750)
    t.hideturtle()

def create_player(color):
    p = turtle.Turtle()

    p.shape("circle")
    p.penup()
    p.color(color)
    p.speed()
    p.hideturtle()

    return p

# Moving the player

def move_left():
    x = player.xcor()
    x -= playerspeed
    if x < -375:
        x = -250
    player.setx(x)

def move_right():
    x = player.xcor()
    x += playerspeed
    if x > 375:
        x = 250
    player.setx(x)

def move_down():
    y = player.ycor()
    y -= playerspeed
    if y < -375:
        y = -250
    player.sety(y)

def move_up():
    y = player.ycor()
    y += playerspeed
    if y > 375:
        y = 250
    player.sety(y)

def set_position():
    global player
    global count_changes

    x = player.xcor()
    y = player.ycor()

    if (x,y) in occupied_places:
        print("Can't put here")
        return

    occupied_places.append( (x,y) )

    #TODO: check board if place is empty (using list with occupied places)        
    player.goto(x, y - 50)
    player.pendown()
    player.pensize(10)
    player.circle(50, None, None)
    player.penup()
    player.goto(x, y)

    #TODO: check board if player win (using list with occupied places)

    # change player
    if player == p1:
        player.hideturtle()
        player = p2
        player.showturtle()
    else:
        player.hideturtle()
        player = p1
        player.showturtle()

    # count changes
    count_changes += 1
    if count_changes >= 9:
        print("END")
        #exit()

# Keyboard Bindings for active player 
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")
turtle.onkey(set_position, "space")

draw_border()

# create players
p1 = create_player('blue')
p2 = create_player('red')
playerspeed = 250

# select first player
player = p1
player.showturtle()

occupied_places = []

# count how many times player was changed
count_changes = 0

turtle.mainloop()