【问题标题】:PyGame Not Drawing XPyGame 不绘制 X
【发布时间】:2021-05-18 15:15:42
【问题描述】:

您好,我正在 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, 10, 190, 190),
    pygame.Rect(400, 10, 190, 190),
    pygame.Rect(10, 210, 180, 180),
    pygame.Rect(200, 210, 190, 180),
    pygame.Rect(400, 210, 190, 180),
    pygame.Rect(10, 400, 180, 190),
    pygame.Rect(200, 400, 190, 190),
    pygame.Rect(400, 400, 190, 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,"blue",(width+x+i,y),(x+i,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,10,10,580))
    pygame.draw.rect(screen, line_color, (390, 10, 10, 580))
    pygame.draw.rect(screen, line_color, (10, 200, 580, 10))
    pygame.draw.rect(screen, line_color, (10, 390, 580, 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, background, rect)
                pygame.draw.ellipse(screen, "blue", 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()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

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

    添加一个变量,指示轮到“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, background, 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()
    

    【讨论】:

    • 谢谢你的工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多