我想实现这样一个功能,即程序在按下鼠标的位置向屏幕上弹出一个“X”。然而,一旦我运行了程序,就什么都没有出现。take_turn函数将一个值存储在二维数组中,并将鼠标点击的位置存储在一个名为“位置”的列表中。然后,在主游戏循环中,X应该放置在位置列表中迭代的所有位置。我应该如何修改我的代码?
import pygame
import sys
pygame.init()
HEIGHT = 800
WIDTH = 600
BOARD_COLOR = (50, 50, 50)
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
ROWS = 3
SIZE = 200
count = 0
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
positions = []
font = pygame.font.Font('Poppins-ExtraBold.ttf', 150)
text = font.render('X', True, (255, 255, 255))
def draw_board(screen):
pygame.draw.line(screen, (255, 255, 255), (0, SIZE), (WIDTH, SIZE), width=5)
for i in range(0, ROWS-1):
pygame.draw.line(screen, (255, 255, 255), ((i+1)*SIZE, SIZE), ((i+1)*SIZE, HEIGHT))
pygame.draw.line(screen, (255, 255, 255), (0, ((i+1) * SIZE) + SIZE), (WIDTH, ((i+1) * SIZE) + SIZE))
def take_turn(position):
global count
global positions
if position[1] > SIZE:
if count % 2 == 0:
board[int((position[1] - SIZE) / SIZE)][int((position[0]) / SIZE)] = 1
for i in range(len(board)):
print(board[i])
positions.append((position[0], position[1]))
count += 1
else:
board[int((position[1] - SIZE) / SIZE)][int((position[0]) / SIZE)] = 2
for i in range(len(board)):
print(board[i])
count += 1
running = True
while running:
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
take_turn(mouse)
WIN.fill(BOARD_COLOR)
draw_board(WIN)
for pos in positions:
text.blit(text, pos)
pygame.display.update()
我试图将填充函数放置在游戏循环的不同位置,但没有任何变化。