From 7f82860b83c2fada338565a2210a606df600dcd2 Mon Sep 17 00:00:00 2001 From: hnu202309010219 <3590268412@qq.com> Date: Mon, 27 May 2024 23:48:39 +0800 Subject: [PATCH] ADD file via upload --- 五子棋.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 五子棋.py diff --git a/五子棋.py b/五子棋.py new file mode 100644 index 0000000..d54f398 --- /dev/null +++ b/五子棋.py @@ -0,0 +1,74 @@ +import pygame +pygame.init() #初始化 +screen = pygame.display.set_mode((600, 600)) #窗口长宽 +pygame.display.set_caption('五子棋') #窗口名 +flag = 1 # 1黑 2白 +space = 20 #四周边距 +cell_size = 40 #格子大小 +cell_num = 15 #线条数 +chess_arr = []# 落子位置 +game_state = 1 #游戏状态1.正常进行 2.白胜 3.黑胜 +def get_one_dire_num(lx, ly, dx, dy, m):# 判断连子情况 + tx = lx + ty = ly + s = 0 + while True: #判断各个方向 + tx += dx + ty += dy + if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: + return s + s+=1 #连子累加1 +def check_win(chess_arr, flag): + m = [[0] * cell_num for i in range(cell_num)] #定义15*15的全0的数组 + for x, y, c in chess_arr: + if c == flag: + m[y][x] = 1 # 上面有棋则标1 + lx = chess_arr[-1][0] # 最后一个子的x + ly = chess_arr[-1][1] # 最后一个子的y + dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)],[(-1, 1), (1, -1)]] # 4个方向数组 + for dire1, dire2 in dire_arr: + dx, dy = dire1 + num1 = get_one_dire_num(lx, ly, dx, dy, m) + dx, dy = dire2 + num2 = get_one_dire_num(lx, ly, dx, dy, m)#连子数为5的获胜 + if num1 + num2 + 1 >= 5: return True + return False +while True: #死循环保持窗口显示 + for event in pygame.event.get(): #遍历所有事件 + if event.type == pygame.QUIT: #如果单击关闭 + pygame.quit() #关闭pygame + exit() #结束程序 + if game_state == 1 and event.type == pygame.MOUSEBUTTONUP: #鼠标弹起 + x, y = pygame.mouse.get_pos() #获取鼠标位置 + xi = int(round((x - space) * 1.0 / cell_size)) #x坐标 + yi = int(round((y - space) * 1.0 / cell_size)) #y坐标 + if 0<=xi