问题描述
我正在尝试在 PyGame 中制作井字游戏,但是当玩家连续得到三个“x”时,我无法改变分数。每当我运行代码时,游戏开始,我可以放置“x”和“o”,我可以清除棋盘并关闭窗口,但是即使有赢家,分数也不会改变。我希望每个“连续三个”都算作一个点。我想要积分,当一个球员得到五分时,他们就赢得了整场比赛。我尝试将分数打印到终端,但是在一场胜利后这些数字就搞砸了,但是如果您在几轮之间清除棋盘,那应该不是问题。我也没有收到任何错误,所以我不知道从哪里开始。我在谷歌上搜索了如何解决这个问题,但没有任何意义或适合这种情况。
我没有任何检查 'o's 是否获胜,但我只是复制/粘贴带有 'x's 的部分并更改一些内容,使其也适用于 'o's。
我不知道出了什么问题,所以这是我的所有代码:
import pygame
import os
import sys
from pygame.locals import *
pygame.init()
WIDTH,HEIGHT = 500,500
P_WIDTH,P_HEIGHT = 79,80
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("tic-tac-toe")
main_font = pygame.font.SysFont("Comic sans",40)
X_WINS = 0
O_WINS = 0
TIES = 0
BG = pygame.transform.scale(pygame.image.load(os.path.join("Board3.jpg")).convert_alpha(),(WIDTH,HEIGHT))
title_label1 = main_font.render("tic-tac-toe",True,(0,0))
Player_1 = pygame.transform.scale(pygame.image.load(os.path.join("X.png")).convert_alpha(),(P_WIDTH,P_HEIGHT))
Player_2 = pygame.transform.scale(pygame.image.load(os.path.join("O.png")).convert_alpha(),P_HEIGHT))
X_WINS_LABEL = main_font.render(f"X = {X_WINS}",0))
O_WINS_LABEL = main_font.render(f"O = {O_WINS}",0))
TIES_LABEL = main_font.render(f"Ties = {TIES}",0))
X_WON_LABEL = main_font.render("WINNER: X",0))
O_WON_LABEL = main_font.render("WINNER: O",0))
board_state = [[None,None,None],[None,]
def redraw_window():
global Player_1
global board_state
global TIES
global X_WINS
global X_WINS_LABEL
global O_WINS_LABEL
global TIES_LABEL
WIN.blit(BG,0))
WIN.blit(title_label1,(175,20))
WIN.blit(X_WINS_LABEL,(100,450))
WIN.blit(O_WINS_LABEL,(350,450))
WIN.blit(TIES_LABEL,(205,450))
y_cursor = 0
for row in (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30):
x_cursor = 0
for col in (0,30):
if board_state[row][col] == 'x':
WIN.blit(Player_1,(x_cursor,y_cursor))
elif board_state[row][col] == 'o':
WIN.blit(Player_2,y_cursor))
x_cursor += WIDTH // 31 # move the cursor across
y_cursor += HEIGHT // 31 # move the cursor down
pygame.display.update()
def main():
while True:
global X_WINS
global O_WINS
global TIES
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
elif event.type == pygame.locals.KEYDOWN:
# Place an "X"
if event.key == K_1:
board_state[7][7] = 'x'
if event.key == K_2:
board_state[7][13] = 'x'
if event.key == K_3:
board_state[7][19] = 'x'
if event.key == K_4:
board_state[13][7] = 'x'
if event.key == K_5:
board_state[13][13] = 'x'
if event.key == K_6:
board_state[13][19] = 'x'
if event.key == K_7:
board_state[19][7] = 'x'
if event.key == K_8:
board_state[19][13] = 'x'
if event.key == K_9:
board_state[19][19] = 'x'
# Place an "O"
if event.key == K_q:
board_state[7][7] = 'o'
if event.key == K_w:
board_state[7][13] = 'o'
if event.key == K_e:
board_state[7][19] = 'o'
if event.key == K_a:
board_state[13][7] = 'o'
if event.key == K_s:
board_state[13][13] = 'o'
if event.key == K_d:
board_state[13][19] = 'o'
if event.key == K_z:
board_state[19][7] = 'o'
if event.key == K_x:
board_state[19][13] = 'o'
if event.key == K_c:
board_state[19][19] = 'o'
# Check for wins
if board_state[7][7] == 'x' and board_state[7][13] == 'x' and board_state[7][19] == 'x':
X_WINS += 1
if board_state[13][7] == 'x' and board_state[13][13] == 'x' and board_state[13][19] == 'x':
X_WINS += 1
if board_state[19][7] == 'x' and board_state[19][13] == 'x' and board_state[19][19] == 'x':
X_WINS += 1
if board_state[7][7] == 'x' and board_state[13][7] == 'x' and board_state[19][7] == 'x':
X_WINS += 1
if board_state[7][13] == 'x' and board_state[13][13] == 'x' and board_state[19][13] == 'x':
X_WINS += 1
if board_state[7][19] == 'x' and board_state[13][19] == 'x' and board_state[19][19] == 'x':
X_WINS += 1
if board_state[7][7] == 'x' and board_state[13][13] == 'x' and board_state[19][19] == 'x':
X_WINS += 1
if board_state[19][7] == 'x' and board_state[13][13] == 'x' and board_state[7][19] == 'x':
X_WINS += 1
# Winning the game
if X_WINS >= 5:
WIN.blit(X_WON_LABEL,(190,175))
if O_WINS >= 5:
WIN.blit(O_WON_LABEL,175))
# Clear the board
if event.key == K_BACKSPACE:
board_state[7][7],board_state[7][13],board_state[7][19],board_state[13][7],board_state[13][13],board_state[13][19],board_state[19][7],board_state[19][13],board_state[19][19] = 'none','none','none'
# Clear the score
if event.key == K_DELETE:
X_WINS,O_WINS,TIES = 0,0
redraw_window()
main()
解决方法
当 X_WINS 的值发生变化时,您需要更新 X_WINS_LABEL
,例如
X_WINS += 1
X_WINS_LABEL = main_font.render(f"X = {X_WINS}",True,(0,0))
实际上,X_WINS_LABEL
是用一个值初始化但从未更改过,更新 X_WINS
不会导致 X_WINS_LABEL
自行更新。
您还可以通过在重绘时生成消息来消除 X_WINS_LABEL
变量:
WIN.blit(main_font.render(f"X = {X_WINS}",0)),(100,450))
这样会更简洁一些,因为您不必独立管理获胜次数和标签,但您会更频繁地调用 main_font.render
。根据呈现消息的成本,这可能是也可能不是值得的权衡。