OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 460|回复: 0

好长时间没有来论坛了,发个五子棋双人对战程序源码,python做的.

[复制链接]

50

主题

1804

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
6632
金钱
6632
注册时间
2016-5-29
在线时间
906 小时
发表于 2024-1-24 13:43:05 | 显示全部楼层 |阅读模式
本帖最后由 操作系统 于 2024-1-24 14:00 编辑

# 好长时间没有来论坛了,发个五子棋双人对战程序源码,python做的.
# 直接上代码.


import time
import pygame as pg
# import pygame.font
# import _thread as th

# import tkinter as tk
# from tkinter import messagebox
class My_form:
    def __init__(self,x=0,y=0,w=0,h=0):
        self.rect = [x,y,w,h]
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.x1=x
        self.x2=x+w
        self.y1=y
        self.y2=y+h
        self.center_x = (self.x1+self.x2)//2
        self.center_y = (self.y1+self.y2)//2



    def center_point_rect(self,x,y,w,h):
        self.cnter_x = x
        self.enter_y = y
        self.w = w
        self.h = h
        self.x = x - w//2
        self.y = y - h//2
        self.x1 = self.x
        self.y1 = self.y
        self.x2 = self.x1+w
        self.y2 = self.y1+h
        self.rect=[self.x1,self.y1,self.w,self.h]

    def on_mouse(self,x,y):
        if self.x1 <= x <= self.x2 and self.y1 <= y <= self.y2:
            return True
        else:
            return False



class  Button:
    def __init__(self,
    screen,
    text,
    x, y, w, h,
    up_color,
    down_color,
    on_color,
    keydown=None,
    keyup=None):
        self.screen = screen
        self.text = text
        self.myForm = My_form(x,y,w,h)
        self.up_color = up_color
        self.down_color = down_color
        self.on_color = on_color
        self.keydown=keydown
        self.keyup = keyup
        self.__keyState = False
        self.__time = 0





    def display(self):
        [mouse_x,mouse_y] = pg.mouse.get_pos()

        pg.draw.rect(self.screen, self.up_color,self.myForm.rect)
        if self.myForm.on_mouse(mouse_x,mouse_y):
            pg.draw.rect(self.screen, self.on_color, self.myForm.rect)
            if  pg.mouse.get_pressed()[0] == True:
                    pg.draw.rect(self.screen, self.down_color, self.myForm.rect)
                    self.__time += 1
                    # print(self.__time)
                    if self.__time >= 2000/100:
                        self.__time -=1
                        self.__keyState = False

                    if self.keydown != None and self.__keyState == False:
                        self.__keyState = True
                        self.keydown()
            else:
                self.__key_up()
        else:
                self.__key_up()


        font = pg.font.SysFont("SimHei", 24)
        text = font.render(self.text, True, BLACK_COLOR)
        self.screen.blit(text, (self.myForm.x1+9,self.myForm.y1+9))

    def __key_up(self):
        if self.__keyState == True:
                self.__keyState = False
                self.__time = 0
                if self.keyup != None:
                    self.keyup()

pg.init()

pg.display.set_caption("五子棋双人对战")
BLACK_COLOR = (0X00,0X00,0X00)
ORANG_COLOR=(0XFF,0X99,0X11)
RED_COLOR =( 0XFF,0X00,0X00)
WHITE_COLOR=(0XFF,0XFF,0XFF)
CHESS_PIECE_COLOR_BLACK = 2
CHESS_PIECE_COLOR_WHITE = 1
CHESS_LINE_COLOR = (0X44,0X66,0X11)
cw = 40
n = 15
map =[0] *n
for i in range(n):
    map[i]=[0]*n

map_n = 0
cur_map_n = 0
map_buf=[0,0,0]*n*n
back_key_keyState = False
forward_key_keyState = False
last_point =[0,0]
click_en =True
map_color = CHESS_PIECE_COLOR_BLACK
map_color_change = False
offset_x = cw/2+20
offset_y = cw/2+10

ww = cw*(n-1) + offset_x+200
hh = cw*(n-1) + offset_y+50
chess_rect = My_form(offset_x,offset_y,cw*(n-1),cw*(n-1))

screen = pg.display.set_mode((ww,hh))

def reset_map():
    global click_en
    global map_color
    global map_color_change
    global map_n
    global map_buf
    global cur_map_n
    if cur_map_n == 0:
        return
    map_n = 0
    cur_map_n = 0
    for i in range(n*n):
        map_buf[i]=[0,0,0]
    map_color_change = False
    click_en = True
    map_color = 2
    for i in range(n):
         for j in range(n):
            map[i][j]=0

def clear_screen():
    screen.fill(ORANG_COLOR)  # 清除屏幕上的内容,重新画

def draw_line_row_col_15X15():# 画15条横线,15条竖线
    for i in range(n):# 画15条横线
        x1= chess_rect.x1
        y1= cw * i + chess_rect.y1
        y2 = y1
        x2 = chess_rect.x2
        pg.draw.line(screen,CHESS_LINE_COLOR,[x1,y1],[x2,y2],3)

    for i in range(n):# 画15条竖线
        y1= chess_rect.y1
        x1=cw * i + chess_rect.x1
        x2 = x1
        y2 = chess_rect.y2
        pg.draw.line(screen,CHESS_LINE_COLOR,[x1,y1],[x2,y2],3)


def on_mouse_click(x,y,button):# 鼠标左键单击事件
    global map_n
    global map_buf
    global cur_map_n
    if map[x][y] == 0 :
        if button == 1:  
            map[x][y]=map_color
            map_buf[map_n] = x,y,map_color
            map_n += 1
            cur_map_n = map_n
            global map_color_change  
            map_color_change = True


def display_mouse():# 画鼠标 光标  方框
    global map  # 声明 引用 全局变量 map
    x,y = pg.mouse.get_pos()
    px = round((x-offset_x)/cw)
    py  = round((y-offset_y)/cw)
    x = px*cw + offset_x
    y = py*cw+ offset_y
    if chess_rect.on_mouse(x,y):
        r = My_form()
        r.center_point_rect(x,y,cw+4,cw+4)  # 重新调用 中心点矩形

        # if map[px][py] == 0:
        if map_color == CHESS_PIECE_COLOR_BLACK:
            pg.draw.rect(screen,BLACK_COLOR, r.rect,2)
            # pg.draw.circle(screen,BLACK_COLOR,[x,y],cw//2,3)
            # pg.draw.circle(screen,BLACK_COLOR,[x,y],cw//4)
        else:
            pg.draw.rect(screen,WHITE_COLOR,r.rect,2)
            # pg.draw.circle(screen,WHITE_COLOR,[x,y],cw//2,3)
            # pg.draw.circle(screen,WHITE_COLOR,[x,y],cw//4)


def display_chess_piece(): # 显示所有的棋子
    global map
    for x in range(n):
        for y in range(n):
            if map[x][y]==CHESS_PIECE_COLOR_WHITE:
                pg.draw.circle(screen ,WHITE_COLOR,[offset_x+x*cw,offset_y+y*cw],cw//2-2)
            elif map[x][y]==CHESS_PIECE_COLOR_BLACK:
                pg.draw.circle(screen , BLACK_COLOR,[offset_x+x*cw,offset_y+y*cw],cw//2-2)



def draw_line(x1,y1,x2,y2):
    global click_en
    x1 = x1 * cw + offset_x
    x2 = x2 * cw + offset_x
    y1 = y1 * cw + offset_y
    y2 = y2 * cw + offset_y
    pg.draw.line(screen,RED_COLOR,[x1,y1],[x2,y2],3)
    click_en = False
    font = pg.font.Font(None,80)

    if map_color == 2:
        s = "Black winer"
    elif map_color == 1:
        s = "White winer"
    else:
        s = "Error = 1"
    text = font.render(s, True, RED_COLOR)
    screen.blit(text, ((x1+x2)//2, (y1+y2)//2))
    # messagebox.showinfo(title='五子棋', message='游戏结束')



def display_chess(): # 双人对战主体函数
    clear_screen() # 清除屏幕,重新画
    draw_line_row_col_15X15()  # 画棋盘
     # 画棋盘中心的那个点  
    pg.draw.circle(screen , BLACK_COLOR,[offset_x+(n//2)*cw,offset_y+(n//2)*cw],6)
    pg.draw.circle(screen , BLACK_COLOR,[offset_x+(3)*cw,offset_y+(3)*cw],6)
    pg.draw.circle(screen , BLACK_COLOR,[offset_x+(3)*cw,offset_y+(n-4)*cw],6)
    pg.draw.circle(screen , BLACK_COLOR,[offset_x+(n-4)*cw,offset_y+(3)*cw],6)
    pg.draw.circle(screen , BLACK_COLOR,[offset_x+(n-4)*cw,offset_y+(n-4)*cw],6)
    display_mouse()  # 画当前的光标  一个正方形的框
    display_chess_piece()  # 画所有的棋子




def check_chess_piece5_in_line(color):
    global last_point
    pointN =0
    for x in range(n):
        y = last_point[1]
        if map[x][y] == color:
            pointN += 1
            if pointN >= 5 :
                draw_line(x,y,x-pointN+1,y)
        else:
            pointN = 0

    for y in range(n):
        x = last_point[0]
        if map[x][y] == color:
            pointN += 1
            if pointN >= 5 :
                draw_line(x,y,x,y-pointN+1)
        else:
            pointN = 0  
    [x,y] = last_point
    pointN = 0  
    while x >= 0 and y < n:
        if map[x][y] == color:
            pointN += 1
        else:
            break
        x -= 1
        y += 1
    [x,y] = last_point
    x += 1
    y -= 1     
    while x < n and y > 0:
        if map[x][y] == color:
            pointN += 1
        else:
            break
        x += 1
        y -= 1
    if pointN >= 5:
        draw_line(x-1,y+1,x-pointN,y+pointN)   


    [x,y] = last_point
    pointN = 0  
    while x >= 0 and y >= 0:
        if map[x][y] == color:
            pointN += 1
        else:
            break
        x -= 1
        y -= 1
    [x,y] = last_point
    x += 1
    y += 1     
    while x < n and y < n:
        if map[x][y] == color:
            pointN += 1
        else:
            break
        x += 1
        y += 1
    if pointN >= 5:
        draw_line(x-1,y-1,x-pointN,y-pointN)

def get_event():
    global running  
    global last_point                  
    for event in pg.event.get():# 查找所有的事件
        # print(event.type)
        if event.type == pg.QUIT:# 如果事件是退出,按到 关闭按钮
            running = False  # 关闭 程序运行 标志位

        elif event.type == pg.MOUSEBUTTONDOWN :# 如果事件是鼠标按下事件
                x,y = pg.mouse.get_pos()   # 查询鼠标 X,Y坐标,
                x = round((x-offset_x)/cw)
                y = round((y-offset_y)/cw)
                if click_en:
                    if x >= 0 and y >= 0 :
                        if x < n and y < n :
                            on_mouse_click(x,y, event.button ) # 调用 鼠标单击处理函数
                            last_point = x,y

def map_change_color():
        global map_color_change
        global map_color
        if map_color_change:
            map_color_change = False
            if map_color == CHESS_PIECE_COLOR_WHITE:
                map_color = CHESS_PIECE_COLOR_BLACK  # 黑色
            else:
                map_color = CHESS_PIECE_COLOR_WHITE # 白色
        display_black_white()




def display_black_white():
    font = pg.font.Font(None,60)
    if map_color == CHESS_PIECE_COLOR_BLACK:
        s = "Black"
        text = font.render(s, True, BLACK_COLOR)
        screen.blit(text, [cw * n+offset_x, cw*(n//2)+offset_y])
    else:
        s = "White"
        text = font.render(s, True, WHITE_COLOR)
        screen.blit(text, [cw * n+offset_x, cw*0+offset_y])




def button_back():
    global map_n
    global map_buf
    global map
    global back_key_keyState
    global click_en
    global map_color
    click_en = True
    if map_n > 1 :
        map_n -= 1
        [x,y,z]=map_buf[map_n]
        map[x][y]=0
        map_color = z


def button_forward():
    global map_n
    global map_buf
    global map
    global map_color
    if map_n < cur_map_n :
        [x,y,c]= map_buf[map_n]
        map[x][y]= c  
        if c == CHESS_PIECE_COLOR_WHITE:
            map_color = CHESS_PIECE_COLOR_BLACK
        else:
            map_color = CHESS_PIECE_COLOR_WHITE

        check_chess_piece5_in_line(c)
        map_n += 1


def button_forward_keyup():
    pass

def button_back_keyup():
    pass


new_game_button=Button(screen,"再来一局",cw*n+offset_x,cw*(n-4) +offset_y,120,cw-4,"#aaaaaa","#555555","#ffffff",reset_map)

forward_button =Button(screen,"下一步",cw*n+offset_x,cw*(n-2) +offset_y,120,cw-4,"#aaaaaa","#555555","#ffffff",button_forward,button_forward_keyup)

back_button=Button(screen,"上一步",cw*n+offset_x,cw*(n-3) +offset_y,120,cw-4,"#aaaaaa","#555555","#ffffff",button_back,button_back_keyup)              


# pg.event.set_allowed(pg.MOUSEBUTTONDOWN)
# pg.event.pump()
running = True   # 默认可以运行
while running:
    # pg.event.wait()
    pass
    get_event()
    display_chess()  # 调用 显示函数 ,刷新界面  
    new_game_button.display()
    forward_button.display()
    back_button.display()
    check_chess_piece5_in_line(CHESS_PIECE_COLOR_WHITE)
    check_chess_piece5_in_line(CHESS_PIECE_COLOR_BLACK)
    if click_en:
        map_change_color()
    pg.display.update()   # 打开 显示到屏幕上去        
    time.sleep(0.05)   # 当前线程 延时 50 毫秒,如果没有这一行 CPU 占用率偏高
pg.quit()



















1706075971695.jpg
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-5-16 06:26

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表