鼠标点击游戏界面即可
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 5 11:34:50 2021
@author: Administrator
"""
import pygame
import sys
from pygame.locals import *
from collections import Counter
screen=pygame.display.set_mode((400,450))
pygame.display.set_caption('五子棋')
pygame.init()
img_board=pygame.image.load('F:/images/五子棋/chess_board.png')
white=(255,255,255)
black=(0,0,0)
chess_board=[[]]
x,y=0,0
while True:
if x==400:
x=0
y+=40
if y<400:
chess_board.append([])
if y==400:
break
chess_board[-1].append([x,y])
x+=40
def draw_board(screen,img_board,chess_board):
for i in chess_board:
for j in i:
screen.blit(img_board,(j[0],j[1]))
pygame.display.update()
img_bchess=pygame.image.load('F:/images/五子棋/black_chess.jpg')
img_wchess=pygame.image.load('F:/images/五子棋/white_chess.jpg')
chess_exist=[[0 for i in range(10)]for j in range(10)]
black_chess,white_chess=[],[]
def set_chess(screen,img_bchess,img_wchess,chess_exist,chess_kind,black_chess,white_chess):
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type==MOUSEBUTTONDOWN:
pos=pygame.mouse.get_pos()
for i in range(len(chess_board)):
for j in range(len(chess_board[i])):
if chess_board[i][j][0]<pos[0]<chess_board[i][j][0]+40 and chess_board[i][j][1]<pos[1]<chess_board[i][j][1]+40:
if chess_exist[i][j]==0:
if chess_kind==1:
screen.blit(img_bchess,(chess_board[i][j][0],chess_board[i][j][1]))
black_chess.append([i,j])
elif chess_kind==0:
screen.blit(img_wchess,(chess_board[i][j][0],chess_board[i][j][1]))
white_chess.append([i,j])
chess_exist[i][j]=1
pygame.display.update()
return 1
def draw_text(screen,text,x,y,size):
pygame.font.init()
fontObj=pygame.font.SysFont('SimHei',size )
textSurfaceObj=fontObj.render(text, True, white,black)
textRectObj=textSurfaceObj.get_rect()
textRectObj.center=(x,y)
screen.blit(textSurfaceObj, textRectObj)
pygame.display.update()
def row_column_win(x,m,n,chess):
for i in x:
if x[i]>=5:
xy=[]
for j in chess:
if j[m]==i:
xy.append(j[n])
xy.sort()
count=0
for j in range(len(xy)-1):
if xy[j]+1==xy[j+1]:
count+=1
else:
count=0
if count>=4:
return 1
def xiejiao_win(chess):
x,y=[],[]
chess.sort()
for i in chess:
x.append(i[0])
y.append(i[1])
c,first,last=0,0,0
for i in range(len(x)-1):
if x[i+1]!=x[i]:
if x[i]+1==x[i+1]:
c+=1
last=i+1
else:
if c<4:
first=i+1
c=0
else:
last=i
print(last)
break
else:
last=i+1
if c>=4:
dis=[]
for i in range(first,last+1):
dis.append(x[i]-y[i])
count=Counter(dis)
for i in count:
if count[i]>=5:
return 1
dis=[]
x2=[i*(-1) for i in x]
for i in range(first,last+1):
dis.append(x2[i]-y[i])
count=Counter(dis)
for i in count:
if count[i]>=5:
return 1
def gameover(white_chess,black_chess,wcx,wcy,bcx,bcy):
wcx_count,wcy_count,bcx_count,bcy_count=Counter(wcx),Counter(wcy),Counter(bcx),Counter(bcy)
if row_column_win(wcx_count,0,1,white_chess)==1:
print('执白棋者赢') #白棋行布五子赢
draw_text(screen,'执白棋者赢', 200, 420, 15)
return 1
elif row_column_win(bcx_count,0,1,black_chess)==1:
print('执黑棋者赢') #黑棋行布五子赢
draw_text(screen,'执黑棋者赢', 200, 420, 15)
return 1
elif row_column_win(wcy_count,1,0,white_chess)==1:
print('执白棋者赢') #白棋列布五子赢
draw_text(screen,'执白棋者赢', 200, 420, 15)
return 1
elif row_column_win(bcy_count,1,0,black_chess)==1:
print('执黑棋者赢') #黑棋列布五子赢
draw_text(screen,'执黑棋者赢', 200, 420, 15)
return 1
elif xiejiao_win(white_chess)==1:
print('执白棋者赢') #白棋斜角布五子赢
draw_text(screen,'执白棋者赢', 200, 420, 15)
return 1
elif xiejiao_win(black_chess)==1:
print('执黑棋者赢') #黑棋斜角布五子赢
draw_text(screen,'执黑棋者赢', 200, 420, 15)
return 1
pass
def restart(wcx,wcy,bcx,bcy,chess_exist,white_chess,black_chess):
for event in pygame.event.get():
if event.type==MOUSEBUTTONDOWN:
pos=pygame.mouse.get_pos()
if 330<pos[0]<370 and 410<pos[1]<450:
black_chess,white_chess,wcx,wcy,bcx,bcy=[],[],[],[],[],[]
chess_exist=[[0 for i in range(10)]for j in range(10)]
draw_board(screen, img_board,chess_board)
print(1)
def main(screen,black_chess,white_chess,chess_exist):
draw_board(screen, img_board,chess_board)
chess_kind=1
print('黑棋先走')
draw_text(screen,'黑棋先走', 200, 420, 15)
draw_text(screen,'重开',350,430,12)
wcx,wcy,bcx,bcy=[],[],[],[] #white_chess_x
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type==MOUSEBUTTONDOWN:
pos=pygame.mouse.get_pos()
if 330<pos[0]<370 and 410<pos[1]<450:
black_chess,white_chess,wcx,wcy,bcx,bcy=[],[],[],[],[],[]
chess_exist=[[0 for i in range(10)]for j in range(10)]
draw_board(screen, img_board,chess_board)
print(1)
if set_chess(screen, img_bchess, img_wchess, chess_exist, chess_kind,black_chess,white_chess)==1:
if chess_kind==1:
bcx.append(black_chess[-1][0])
bcy.append(black_chess[-1][1])
print('轮到白棋走')
draw_text(screen,'轮到白棋走', 200, 420, 15)
chess_kind=0
else:
wcx.append(white_chess[-1][0])
wcy.append(white_chess[-1][1])
print('轮到黑棋走')
draw_text(screen,'轮到黑棋走', 200, 420, 15)
chess_kind=1
if gameover(white_chess,black_chess,wcx,wcy,bcx,bcy)==1:
leave=0
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elif event.type==MOUSEBUTTONDOWN:
pos=pygame.mouse.get_pos()
if 330<pos[0]<370 and 410<pos[1]<450:
black_chess,white_chess,wcx,wcy,bcx,bcy=[],[],[],[],[],[]
chess_exist=[[0 for i in range(10)]for j in range(10)]
draw_board(screen, img_board,chess_board)
print(1)
leave=1
break
if leave==1:
break
if __name__=='__main__':
main(screen,black_chess,white_chess,chess_exist)
棋盘图片:
黑棋图片:
白棋图片:
游戏界面:
运行效果: