# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import pygame
import time
#from gobang_ai import GobangAI
from enum import Enum
N = 15
class ChessboardState(Enum):
EMPTY = 0
BLACK = 1
WHITE = 2
Flag=[0]
class GoBang(object):
def __init__(self):
self.__chessMap = [[ChessboardState.EMPTY for j in range(N)] for i in range(N)]
self.__currentI = -1
self.__currentJ = -1
self.__currentState = ChessboardState.EMPTY
def get_chessMap(self):
return self.__chessMap
def get_chessboard_state(self, i, j):
return self.__chessMap[i][j]
def set_chessboard_state(self, i, j, state):
self.__chessMap[i][j] = state
self.__currentI = i
self.__currentJ = j
self.__currentState = state
def get_chess_result(self):
if self.have_five(self.__currentI, self.__currentJ, self.__currentState):
return self.__currentState
else:
return ChessboardState.EMPTY
def count_on_direction(self, i, j, xdirection, ydirection, color):
count = 0
for step in range(1, 5): #除当前位置外,朝对应方向再看4步
if xdirection != 0 and (j + xdirection * step < 0 or j + xdirection * step >= N):
break
if ydirection != 0 and (i + ydirection * step < 0 or i + ydirection * step >= N):
break
if self.__chessMap[i + ydirection * step][j + xdirection * step] == color:
count += 1
else:
break
return count
def have_five(self, i, j, color):
#四个方向计数 横 竖 左斜 右斜
directions = [[(-1, 0), (1, 0)], \
[(0, -1), (0, 1)], \
[(-1, 1), (1, -1)], \
[(-1, -1), (1, 1)]]
for axis in directions:
axis_count = 1
for (xdirection, ydirection) in axis:
axis_count += self.count_on_direction(i, j, xdirection, ydirection, color)
if axis_count >= 5:
return True
return False
IMAGE_PATH = 'UI/'
WIDTH = 540
HEIGHT = 540
WIDTH1 = 650
MARGIN = 22
GRID = (WIDTH - 2 * MARGIN) / (N - 1)
PIECE = 32
class GameRender(object):
def __init__(self, gobang):
# 绑定逻辑类
self.__gobang = gobang
# 黑棋开局
self.__currentPieceState = ChessboardState.BLACK
# 初始化 pygame
pygame.init()
#pygame.display.set_mode((WIDTH1, HEIGHT1))
# pygame.display.set_mode((width, height), flags, depth)
self.__screen = pygame.display.set_mode((WIDTH1, HEIGHT), 0, 32)
pygame.display.set_caption('五子棋')
# UI 资源
self.__ui_chessboard = pygame.image.load(IMAGE_PATH + 'chessboard.jpg').convert()
self.__ui_piece_black = pygame.image.load(IMAGE_PATH + 'piece_black.png').convert_alpha()
self.__ui_piece_white = pygame.image.load(IMAGE_PATH + 'piece_white.png').convert_alpha()
def coordinate_transform_map2pixel(self, i, j):
# 从 chessMap 里的逻辑坐标到 UI 上的绘制坐标的转换
return MARGIN + j * GRID - PIECE / 2, MARGIN + i * GRID - PIECE / 2
def coordinate_transform_pixel2map(self, x, y):
# 从 UI 上的绘制坐标到 chessMap 里的逻辑坐标的转换
i , j = int(round((y - MARGIN + PIECE / 2) / GRID)), int(round((x - MARGIN + PIECE / 2) / GRID))
# 有MAGIN, 排除边缘位置导致 i,j 越界
if i < 0 or i >= N or j < 0 or j >= N:
return None, None
else:
return i, j
def draw_chess(self):
# 棋盘
self.__screen.blit(self.__ui_chessboard, (0,0))
# 棋子
for i in range(0, N):
for j in range(0, N):
x,y = self.coordinate_transform_map2pixel(i,j)
state = self.__gobang.get_chessboard_state(i,j)
if state == ChessboardState.BLACK:
self.__screen.blit(self.__ui_piece_black, (x,y))
elif state == ChessboardState.WHITE:
self.__screen.blit(self.__ui_piece_white, (x,y))
else: # ChessboardState.EMPTY
pass
def draw_mouse(self):
# 鼠标的坐标
x, y = pygame.mouse.get_pos()
if x <= 540 and y <= 540:
# 棋子跟随鼠标移动
if self.__currentPieceState == ChessboardState.BLACK:
self.__screen.blit(self.__ui_piece_black, (x - PIECE / 2, y - PIECE / 2))
else:
self.__screen.blit(self.__ui_piece_white, (x - PIECE / 2, y - PIECE / 2))
def draw_result(self, result):
font = pygame.font.Font('Tensentype-DouDouF.ttf', 50)
tips = u"本局结束:"
if result == ChessboardState.BLACK :
tips = tips + u"黑棋胜利"
Flag[0]=1
elif result == ChessboardState.WHITE:
tips = tips + u"白棋胜利"
Flag[0]=1
else:
tips = tips + u"平局"
Flag[0]=1
text = font.render(tips, True, (255, 0, 0))
self.__screen.blit(text, (WIDTH / 2 - 200, HEIGHT / 2 - 50))
def one_step(self):
i, j = None, None
# 鼠标点击
mouse_button = pygame.mouse.get_pressed()
# 左键
if mouse_button[0]:
x, y = pygame.mouse.get_pos()
i, j = self.coordinate_transform_pixel2map(x, y)
if not i is None and not j is None:
# 格子上已经有棋子
if self.__gobang.get_chessboard_state(i, j) != ChessboardState.EMPTY:
return False
else:
self.__gobang.set_chessboard_state(i, j, self.__currentPieceState)
return True
return False
def change_state(self):
if self.__currentPieceState == ChessboardState.BLACK:
self.__currentPieceState = ChessboardState.WHITE
else:
self.__currentPieceState = ChessboardState.BLACK
def main():
gobang = GoBang()
render = GameRender(gobang)
#先给AI留个接口
#ai = GobangAI(gobang, ChessboardState.WHITE)
result = ChessboardState.EMPTY
enable_ai = False
Flag[0]==0
while Flag[0]==0:
# 捕捉pygame事件
for event in pygame.event.get():
# 退出程序
if event.type == 12:
pygame.quit()
elif event.type == 5:
# 成功着棋
if render.one_step():
result = gobang.get_chess_result()
else:
continue
if result != ChessboardState.EMPTY:
break
if enable_ai:
#ai.one_step()
result = gobang.get_chess_result()
else:
render.change_state()
# 绘制
render.draw_chess()
render.draw_mouse()
if result != ChessboardState.EMPTY:
render.draw_result(result)
# 刷新
pygame.display.update()
time.sleep(2)
if Flag[0]==1:
pygame.quit()
#pygame.screen.destroy()
'''main()'''
main()
评论0
最新资源