微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PyGame 不绘制 X

如何解决PyGame 不绘制 X

您好,我正在 pygame 中制作井字游戏,我遇到的问题是,当我在板上绘制 x 时,它不会绘制。当我绘制一个圆圈时,如代码所示,它会起作用。请告诉我如何解决,你能告诉我如何在这个游戏中轮流,因为我无法弄清楚如何画一个 x 并让电脑画一个 o 等等。我想等待用户一个 x然后让电脑画一个o。 这是代码

import pygame,random

pygame.init()

width = 600

height = 600

res = (width,height)

screen = pygame.display.set_mode(res)

pygame.display.set_caption("Tic Tac Toe")

background = (255,150,150)

color_light = (170,170,170)

color_dark = (100,100,100)

hover_color = (255,204,203)

rect_list = [

    pygame.Rect(10,10,180,190),pygame.Rect(200,190,pygame.Rect(400,pygame.Rect(10,210,180),400,190)]

clicked_list = [0 for _ in rect_list]

count = 0


def draw_x(x,y,width,height):

    for i in range(5):
        pygame.draw.aaline(screen,"blue",(x+i,y),(width+x+i,height+y))  # start_pos(x+thickness,y)---end_pos(width+x+thickness,height+y)
        pygame.draw.aaline(screen,height+y))  # start_pos(x+width+thickness,y)---end_pos(x+thickness,y+height)


def draw_line():

    line_color = (212,212,255)
    pygame.draw.rect(screen,line_color,(190,580))
    pygame.draw.rect(screen,(390,(10,200,580,10))
    pygame.draw.rect(screen,390,10))


def highlight():

    for rect in rect_list:
        if rect.collidepoint(mouse):   
            pygame.draw.rect(screen,hover_color,rect)


def random_no():

    randomno = random.randint(0,len(rect_list)-1)
    if clicked_list[randomno] != 1:
        pass
    else:
        random_no()
    return randomno


def mouse_click():

    x_turn = True
    y_turn = False
    o_no = random_no()
    clicked_list[o_no] = 2
    for i,rect in enumerate(rect_list):
        if clicked_list[i] == 1:
            if x_turn and clicked:
                pygame.draw.rect(screen,background,rect)
                draw_x(rect.x,rect.y,rect.width,rect.height)
        if clicked_list[i] == 2:
            if y_turn:
                pygame.draw.rect(screen,rect)
                pygame.draw.ellipse(screen,rect,5)
            # rect_list.remove(rect_list[i])
            # clicked_list.remove(clicked_list[i])
while True:

    mouse = pygame.mouse.get_pos()
    x = screen.get_at(mouse)[:3]
    screen.fill(background)
    for ev in pygame.event.get():
        if ev.type == pygame.QUIT:
            pygame.quit()
        if ev.type == pygame.MOUSEBUTTONDOWN:
            clicked = True
            for i,rect in enumerate(rect_list):
                if rect.collidepoint(ev.pos) and x == hover_color:
                    clicked_list[i] = 1
    draw_line()
    highlight()
    mouse_click()
    pygame.display.update()

解决方法

鼠标点击必须在事件循环中处理。但是,您需要一个函数 draw_borad 来绘制“X”和“O”。

添加一个变量,指示它是轮到“X”还是“O”:

x_turn = True

如果按下鼠标并且某个字段为空 (0),则更改该字段的状态并更改 x_turn 变量:

if rect.collidepoint(ev.pos) and x == hover_color:
    if clicked_list[i] == 0:
        clicked_list[i] = 1 if x_turn else 2
        x_turn = not x_turn

draw_borad 中画出所有的“X”和“O”:

def draw_borad():
    for i,rect in enumerate(rect_list):
        if clicked_list[i] == 1:
            pygame.draw.rect(screen,background,rect)
            draw_x(rect.x,rect.y,rect.width,rect.height)
        if clicked_list[i] == 2:
            pygame.draw.rect(screen,rect)
            pygame.draw.ellipse(screen,"blue",rect,5)

x_turn = True
while True:
    mouse = pygame.mouse.get_pos()
    x = screen.get_at(mouse)[:3]
    screen.fill(background)
    for ev in pygame.event.get():
        if ev.type == pygame.QUIT:
            pygame.quit()
        if ev.type == pygame.MOUSEBUTTONDOWN:
            clicked = True
            for i,rect in enumerate(rect_list):
                if rect.collidepoint(ev.pos) and x == hover_color:
                    if clicked_list[i] == 0:
                        clicked_list[i] = 1 if x_turn else 2
                        x_turn = not x_turn

    draw_line()
    highlight()
    draw_borad()
    pygame.display.update()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。