五子棋(Python实现)

'''
五子棋游戏制作流程:
    1.准备工作:
        导入、初始化、创建屏幕、游戏说明、while循环、延迟时间、退出事件、更新
    2.加载所需图片
    3.下棋的制作流程:
        是否能下棋,下的什么棋、图片展示
    4.判断某一方胜利的规则的代码编写
    5.游戏结束弹幕展示
    6.重置系统
'''
import pygame,time,sys
from pygame.locals import *
from pygame import Rect
pygame.init()
pm=pygame.display.set_mode((800,800))
pygame.display.set_caption('五子棋')
bac=pygame.image.load('bac.jpg')
bacc=pygame.image.load('bacc.png')
black=pygame.image.load('black.png')
new_black=pygame.image.load('new_black.png')
new_white=pygame.image.load('new_white.png')
white=pygame.image.load('white.png')
def get():#这个函数是为了确定鼠标位置是否在合法区域--可以下棋的区域
    for i in range(19):
        for j in range(19):
            if Rect(20 + 40 * j,20 + 40 * i,40,40).collidepoint(event.pos):
                return i,j
def is_win(map,si,sj):
    num = 5#五子棋
    winflag = 1#没有找之前,本身算一个
    chess_find = map[si][sj]#刚下的棋的种类
    
    #左右找
    #往左找
    for i in range(1,5):#最少找一个  最多找四个
        if sj - i >= 0:#最小不能小于0
            if map[si][sj - i] == chess_find:#判断移动的位置是否与刚下的棋一致
                winflag += 1#满足条件  计数+1
            else:
                break#不满足条件则不再继续向这个方向找
    #往右找
    for i in range(1,5):
        if sj + i <= 18:
            if map[si][sj + i] == chess_find:
                winflag += 1
            else:
                break
    if winflag >= num:#判断是否找够了
        return True#满足条件返回True
    else:
        winflag = 1#不满足条件还原变量,找其他方向
        
    
    #上下找
    #往上找
    for i in range(1,5):
        if si-i >=0:
            if map[si-i][sj] == chess_find:
                winflag=winflag+1
            else:
                break
    #往下找
    for i in range(1,5):
        if si+i <=18:
            if map[si+i][sj]==chess_find:
                winflag=winflag+1
            else:
                break
    if winflag >= num:
        return True
    else:
        winflag=1
                
    #左上右下找
    #往左上找
    for i in range(1,5):
        if si-i >=0 and sj-i >=0:
            if map[si-i][sj-i] == chess_find:
                winflag=winflag+1
            else:
                break
    #往右下找
    for i in range(1,5):
        if si+i <= 18 and sj+i <= 18:
            if map[si+i][sj+i] == chess_find:
                winflag=winflag+1
            else:
                break
    if winflag >= num:
        return True
    else:
        winflag=1
    #左下右上找
    #往左下找
    for i in range (1,5):
        if sj-i >= 0 and si+i <= 18:
            if map[si+i][sj-i] == chess_find:
                winflag=winflag+1
            else:
                break
    #往右下找
    for i in range(1,5):
        if si-i >=0 and sj+i <=18:
            if map [si-i][sj+i] == chess_find:
                winflag=winflag+1
            else:
                break
    if winflag>= num:
        return True
    else:
        winflag=1
    return False
def showOver(winner):
    font = pygame.font.Font("simhei.ttf",88)
    fontImg = font.render("winnner is " + winner,True,(255,0,0))
    pm.blit(fontImg,(50,350))
    
    fonts = pygame.font.Font("simhei.ttf",50)
    fontsImg = fonts.render("PRESS RETURN TO RESTART",True,(255,0,0))
    pm.blit(fontsImg,(100,450))    
map = []
for i in range(19):
    map.append([])
    for j in range(19):
        map[i].append(0)
put_black = True#黑棋能不能下的开关,默认黑棋先下
newi,newj = -1,-1
winner = ""
while True:
    time.sleep(0.03)
    pm.blit(bacc,(0,0))
    for event in pygame.event.get():
        if event.type ==QUIT:
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                if get():#鼠标点到的是合法区域
                    si,sj = get()#获取点击的行和列
                    if not map[si][sj]:#判断行和列对应的位置是不是0,表示没下过棋
                        newi,newj = si,sj
                        if put_black:#黑棋开关开着
                            map[si][sj] = 1#下黑棋
                        else:#黑棋开关关着
                            map[si][sj] = 2#下白棋
                        put_black = not put_black#每次下完一个棋就要变化一种状态
                        flag = is_win(map,si,sj)#这个函数是判断有没有胜利者的函数
                        if flag:#满足条件表示有胜利者出现
                            if put_black:
                                winner = "white"
                            else:
                                winner = "black"
                            
                        
    for i in range(19):
        for j in range(19):
            if map[i][j] == 1:
                pm.blit(black,(20 + j * 40,20 + i * 40))
            elif map[i][j] == 2:
                pm.blit(white,(20 + j * 40,20 + i * 40))
    if newi >= 0:#只要newi大于等于0就表示一定在棋盘内
        if put_black:
            pm.blit(new_white,(20 + newj * 40,20 + newi * 40))
        else:
            pm.blit(new_black,(20 + newj * 40,20 + newi * 40))
            
    if winner:
        showOver(winner)
        pygame.display.update()
        while winner:
            for event in pygame.event.get():
                if event.type ==QUIT:
                    pygame.quit()
                    sys.exit()  
                if event.type == KEYDOWN:
                    if event.key == K_RETURN:
                        map = []
                        for i in range(19):
                            map.append([])
                            for j in range(19):
                                map[i].append(0)
                        put_black = True#黑棋能不能下的开关,默认黑棋先下
                        newi,newj = -1,-1
                        winner = ""                        
    pygame.display.update()

五子棋(Python实现)_第1张图片

 五子棋(Python实现)_第2张图片

 

 

 

 

 

你可能感兴趣的:(python,pygame,开发语言)