python - 单击屏幕时程序关闭

标签 python pygame

我是一名初学者,尝试通过以下视频教程编写 Tic Tac Toe 程序(在 VSC 中使用 Python):https://www.youtube.com/watch?v=yniQ15A7MLk 。但是,当我尝试让圆圈在单击时显示时,当我单击屏幕时程序会关​​闭,并且终端会打印此错误消息:

TypeError: function missing required argument 'radius' (pos 4)

我不认为这是点击功能,因为我在 VSC 终端中以文本形式打印结果。但它没有显示在程序窗口中。我不认为这是绘图函数中的问题,因为我尝试使用图像代替,并且发生了同样的事情(当我单击屏幕时程序关闭)。

import pygame
import numpy

pygame.init()


white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)

height = 700
width = 700
line_thickness = 20

display = pygame.display.set_mode((height, width))
display.fill(black)

board = numpy.zeros((3, 3))

def lines():
    pygame.draw.line(display, white, (50, 250), (650, 250), line_thickness)
    pygame.draw.line(display, white, (50, 450), (650, 450), line_thickness)
    pygame.draw.line(display, white, (250, 50), (250, 650), line_thickness)
    pygame.draw.line(display, white, (450, 50), (450, 650), line_thickness)

lines()

square_size = 200
o_radius = 60
o_width = 15

def draw_figures():
    for row in range(3):
        for col in range(3):
            if board[row, col] == 1:
                pygame.draw.circle(display, red, ((int(row * square_size + square_size // 2), int(col * square_size + square_size // 2)), o_radius, o_width))

def marked_square(row, col, player):
    board[row, col] = player

def available_square(row, col):
    return board[row, col] == 0

player = 1


pygame.display.update()


running = True
while running:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x = event.pos[0]
            mouse_y = event.pos[1]

            clicked_row = int(mouse_y // 200)
            clicked_col = int(mouse_x // 200)

            if available_square(clicked_row, clicked_col):
                if player == 1:
                    marked_square(clicked_row, clicked_row, 1)
                    player = 2

                elif player == 2:
                    marked_square(clicked_row, clicked_row, 2)
                    player = 1

                draw_figures()

                print(board)

最佳答案

在:

pygame.draw.circle(display, red, ((int(row * square_size + square_size // 2), int(col * square_size + square_size // 2)), o_radius, o_width))

你的括号不正确,所以你只有3个参数

pygame.draw.circle(
    display, 
    red, 
    ((int(row * square_size + square_size // 2), int(col * square_size + square_size // 2)), o_radius, o_width)
    )

我猜你的意思是

pygame.draw.circle(display, red, (int(row * square_size + square_size // 2), int(col * square_size + square_size // 2)), o_radius, o_width)

有一些辅助变量可能会更干净

x = int(row * square_size + square_size // 2)
y = int(col * square_size + square_size // 2)
pygame.draw.circle(display, red, (x, y), o_radius, o_width)

关于python - 单击屏幕时程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71047068/

相关文章:

python - 在python3中对同时包含字符串和整数的文件进行排序

python - Keras:精度非常低,损失非常高,并且每个输入的预测都是相同的

python - pygame Sprite 组绘制不绘制所有 Sprite

python - 如何在 pygame 中使用 .spritecollide() 来检测碰撞?

python - 如何在pygame中反转图像的颜色?

python - 使用 gdalwarp 重新采样导致 IndentationError : unexpected indent

python - Google Api Auth Http 模块错误

python - pygame.错误: display Surface quit after filling the surface black

python - 如何询问玩家是否愿意在死亡时重试,如果他们说愿意,则重新启动游戏

python - 如何使用 pygame.USEREVENT 让敌人定期开火