python - 如何修复井字游戏以使屏幕正常运行?
问题描述
我的井字游戏 pygame 无法正确执行。我错过了什么?
import sys, pygame
from pygame.locals import *
import random
import time
import re
def setup_board(width=860,height=640):
"""A function for setting up a tic-tac-toe board, note that you have to call this multiple times if you draw over it"""
size = width, height
screen = pygame.display.set_mode(size) #sets up the screen to the size we want
screen.fill('white')
pygame.draw.line(screen, 'black', (width/3, 0), (width/3,height), 3)
pygame.draw.line(screen, 'black', (2*width/3, 0), (2*width/3,height), 3)
pygame.draw.line(screen, 'black', (0, height/3), (860,height/3), 3)
pygame.draw.line(screen, 'black', (0, 2*height/3), (860,2*height/3), 3)
return screen, width, height
def get_board_position(x, y):
dw, dl = 860//3, 640//3
return x//dw, y//dl, dw, dl
def get_draw_position(x, y):
nx, ny, dw, dl = get_board_position(x, y)
return nx*dw + dw/2, ny*dl + dl/2
def toss_coin():
coin_states = ['heads', 'tails']
return random.choice(coin_states)
def get_computer_move(board):
for row in range(3):
for col in range(3):
if board[row][col] == '':
return row, col
def get_player_move():
"""Returns the player move as a row and column"""
valid_input = False
while not valid_input:
placement = input("Where would you like to play (enter as row,column e.g. 1,3)")
match = re.search('[1,2,3],[1,2,3]', placement)
valid_input = match is not None
if valid_input:
row, col = placement.split(',')
row, col = int(row)-1, int(col)-1
return row, col
def check_win(board):
"""Checks to see if there is a winner"""
for row in range(3):
if board[row][0] == board[row][1] == board[row][2] != '':
return True
for column in range(3):
if board[0][column] == board[1][column] == board[2][column] != '':
return True
if board[0][0] == board[1][1] == board[2][2] != '':
return True
elif board[2][0] == board[1][1] == board[0][2] != '':
return True
def print_board(board):
"""This function will print a tic-tac-toe board to the console"""
for index, row in enumerate(board):
print('|'.join(row))
if index < 2:
print('-----')
pygame.init()
font = pygame.font.SysFont('arial', 110)
small_font = pygame.font.SysFont('arial', 30)
screen, width, height = setup_board()
clock=pygame.time.Clock()#gets the game clock, we are using this to ensure that we only run at 60fps
rx = font.render("X", False, "black")
quit_text = small_font.render("Quit!", False, "red")
active_game = True
board = [['' ,'', ''], ['', '', ''],['', '', '']]
while active_game:
quit_rect = pygame.draw.rect(screen, 'gray', (50, 50, 60, 40), 1)
screen.blit(quit_text, quit_rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONUP:
ex, ey = event.pos
if quit_rect[0] <= ex <= quit_rect[0] + quit_rect[2] and quit_rect[1] <= ey <= quit_rect[1] + quit_rect[3]:
sys.exit()
else:
screen.blit(rx, get_draw_position(ex, ey))
if check_win(board):
active_game = False
# replace with drawing winner to the screen
print("You win!")
time.sleep(6)
pygame.display.flip()
clock.tick_busy_loop( 60 )
解决方案
我猜您正在将其从控制台应用程序转换为 GUI 应用程序。您允许点击,但您永远不会将它们输入到您的棋盘阵列中,因此您无法检查是否获胜,并且您永远不会让计算机移动。
推荐阅读
- google-cloud-platform - 通过 CLI 编写不带标头的 Google 存储对象
- c# - C# static List<> 有项目但不能访问它
- javascript - 由于复选框切换javascript而除以总和
- ios - 无法在swift ios中比较数组值
- acumatica - Customer Persist Override 在删除时会出现 NextMove 问题
- android - 如何解决此代码中的此 Null 异常错误?
- r - 如何在 R(dplyr 或 data.table)中移动行中的值?
- multithreading - 将多线程程序迁移到 Docker 容器
- blazor - Blazor 客户端 (WebAssembly) 在 startup.cs 上读取 json 文件
- reactjs - 使用 this.props.Navigation.navigate 发送数据时出错本机反应