【发布时间】:2021-12-16 23:40:55
【问题描述】:
我的井字游戏项目再次出现问题,当我单击一个正方形时,圆圈没有出现在应有的位置,我尝试了多个不同的坐标但仍然无法弄清楚,所以如果你们中的任何人知道该怎么做,请告诉我知道,谢谢。
import pygame, sys
import numpy as np
pygame.init()
screen_color = (28, 170, 156)
line_color = (23, 140, 135)
line_width = 9
screen = pygame.display.set_mode((550, 450))
pygame.display.set_caption("Tic Tac Toe")
screen.fill(screen_color)
board = np.zeros((3, 3))
def draw_lines():
#1st horizontal
pygame.draw.line(screen, line_color, (0, 135), (550, 135), line_width)
#2nd horizontal
pygame.draw.line(screen, line_color, (0, 300), (550, 300), line_width)
#1st vertical
pygame.draw.line(screen, line_color, (175, 0), (175, 450), line_width)
#2nd vertical
pygame.draw.line(screen, line_color, (370, 0), (370, 450), line_width)
def draw_figures():
for row in range(3):
for col in range(3):
if board[row][col] == 1:
pygame.draw.circle(screen, 'cyan', (int(col * 550/3), int(row * 450/3)), 60, 10)
def mark_square(row, col, player):
board[row][col] = player
def available_square(row, col):
if board[row][col] == 0:
return True
else:
return False
def is_board_full():
for row in range(3):
for col in range(3):
if board[row][col] == 0:
return False
print(is_board_full())
print(board)
draw_lines()
player = 1
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouseX = event.pos[0] #X coordinate
mouseY = event.pos[1] #Y coordinate
clicked_row = int(mouseY * 3 // 450)
clicked_col = int(mouseX * 3 // 550)
#print(clicked_col)
#print(clicked_row)
if available_square(clicked_row, clicked_col):
if player == 1:
mark_square(clicked_row, clicked_col, 1)
player = 2
elif player == 2:
mark_square(clicked_row, clicked_col, 2)
player = 1
draw_figures()
print(board)
pygame.display.update()
(如果还有其他错误也请告诉我) 如果获取坐标是反复试验或有特定的方法,请告诉我,谢谢!
【问题讨论】: