pygame 五子棋

# -*- coding:UTF-8 -*-
import pygame as pg
import sys,random
pg.init()
size=width,height=700,500
screen=pg.display.set_mode(size)
pg.display.set_caption('五子棋')
blackchess=pg.image.load('.//image//blackchess.png')
whitechess=pg.image.load('.//image//whitechess.png')
font=pg.font.SysFont('SimHei',40)
font2=pg.font.SysFont('SimHei',30)
list=[[0]*15 for i in range(15)]
background=(201,202,187)
button=(52,53,44)
left=9
top=9
widthsize=32.6
heightsize=32.6
select=selectx,selecty=-1,-1
chessx,chessy=-1,-1
tran_B=True
running=True 
historylist=[]
def board():
    screen.fill(background)
    pg.draw.rect(screen,button,[550,150,100,50],2)
    pg.draw.rect(screen,button,[550,250,140,50],2)
    text=font2.render('悔棋', True,(0,0,0))
    screen.blit(text,(570,160))
    text2=font2.render('重新开始', True,(0,0,0))
    screen.blit(text2,(560,260))
    board=pg.image.load('.//image//board.png').convert_alpha()
    screen.blit(board,(0,0))
def zuobiao(x,y):
    i=round((x-left-widthsize/2)/widthsize)
    j=round((y-left-heightsize/2)/heightsize)
    return i,j
def clear():
    global list
    for i in range(15):
        for j in range(15):
            list[i][j]=0  
def draw():
    for i in range(15):
        for j in range(15):
            if list[i][j]==1:
                screen.blit(blackchess,(i*32.6+left,j*32.6+top))
            elif list[i][j]==2:
                screen.blit(whitechess,(i*32.6+left,j*32.6+top))
def chesswhite():
    while True:
        chessx=random.randint(0,14)
        chessy=random.randint(0,14)
        if list[chessx][chessy]==0:
            list[chessx][chessy]=2
            break                             
def event():
    global selectx,selecty,chessx,chessy,tran_B,list,running,historylist
    for event in pg.event.get():
        if event.type==pg.QUIT:
            sys.exit() 
        elif event.type==pg.MOUSEMOTION:
            x,y=event.pos
            if left<x<500-left and top<y<500-top:
                selectx,selecty=zuobiao(x,y)              
            else:
                selectx,selecty=-1,-1
           
        elif event.type==pg.MOUSEBUTTONDOWN:
            a,b=event.pos
            if left<a<width-left and top<b<height-top:
                chess=chessx,chessy=zuobiao(a,b) 
            if 550<a<690 and 250<b<300:
                clear()
                running=True  
            if 550<a<650 and 150<b<200:
                regretx=historylist[-1][0]
                regrety=historylist[-1][1]
                list[regretx][regrety]=0
                tran_B=not tran_B                                          
            if selectx>=0 and selecty>=0:
                try:
                    if running:                                             
                        if list[selectx][selecty]==0 and event.button == 1:                    
                            if tran_B:         
                                list[selectx][selecty]=1
                                tran_B=not tran_B
                                historylist.append(chess)                                  
        #                     list[selectx][selecty]=2
        #                     chesswhite()  
                            else:
                                list[selectx][selecty]=2 
                                tran_B=not tran_B
                                historylist.append(chess)
                except:
                    pass            
            
                            
def drawselect():
    try:
        if running:
            if selectx>=0 and selecty>=0 and list[selectx][selecty]==0:
                if tran_B:
                    screen.blit(blackchess,(selectx*32.6+left,selecty*32.6+top)) 
                else:
    #         if selectx>=0 and selecty>=0 and list[selectx][selecty]==0:
                    screen.blit(whitechess,(selectx*32.6+left,selecty*32.6+top))
    except:
        pass               
def judgesuccess(x,y):
    #上下
    count=1
    for i in range(1,5):
        if y+i>14:
            break
        elif list[x][y]==list[x][y+i]==1:
            count+=1
        else:
            break
    for i in range(1,5):
        if y-i<0:
            break
        elif list[x][y]==list[x][y-i]==1:
            count+=1
        else:
            break 
#     print(count)   
    if count>=5:   
#         print("你赢了")  
        return True
    #左右
    count2=1
    for i in range(1,5):
        if x+i>14:
            break
        elif list[x][y]==list[x+i][y]==1:
            count2+=1
        else:
            break
    for i in range(1,5):
        if x-i<0:
            break
        elif list[x][y]==list[x-i][y]==1:
            count2+=1
        else:
            break 
#     print(count)   
    if count2>=5:   
#         print("你赢了")  
        return True
   #斜方向
    count3=1
    for i in range(1,5):
        if x+i>14 or y+i>14:
            break
        elif list[x][y]==list[x+i][y+i]==1:
            count3+=1
        else:
            break 

    for i in range(1,5):
        if x-i<0 or y-i<0:
            break
        elif list[x][y]==list[x-i][y-i]==1:
            count3+=1
        else:
            break
     
    if count3>=5:   
#         print("你赢了")  
        return True                     
    count4=1
    for i in range(1,5):
        if x-i<0 or y+i>14:
            break
        elif list[x][y]==list[x-i][y+i]==1:
            count4+=1
        else:
            break
    for i in range(1,5):
        if x+i>14 or y-i<0:
            break
        elif list[x][y]==list[x+i][y-i]==1:
            count4+=1
        else:
            break
    if count4>=5:
        return True
def judgesuccess2(x,y):
    #上下
    count=1
    for i in range(1,5):
        if y+i>14:
            break
        elif list[x][y]==list[x][y+i]==2:
            count+=1
        else:
            break
    for i in range(1,5):
        if y-i<0:
            break
        elif list[x][y]==list[x][y-i]==2:
            count+=1
        else:
            break 
#     print(count)   
    if count>=5:   
#         print("你赢了")  
        return True
    #左右
    count2=1
    for i in range(1,5):
        if x+i>14:
            break
        elif list[x][y]==list[x+i][y]==2:
            count2+=1
        else:
            break
    for i in range(1,5):
        if x-i<0:
            break
        elif list[x][y]==list[x-i][y]==2:
            count2+=1
        else:
            break 
#     print(count)   
    if count2>=5:   
#         print("你赢了")  
        return True
   #斜方向
    count3=1
    for i in range(1,5):
        if x+i>14 or y+i>14:
            break
        elif list[x][y]==list[x+i][y+i]==2:
            count3+=1
        else:
            break 

    for i in range(1,5):
        if x-i<0 or y-i<0:
            break
        elif list[x][y]==list[x-i][y-i]==2:
            count3+=1
        else:
            break
     
    if count3>=5:   
#         print("你赢了")  
        return True                     
    count4=1
    for i in range(1,5):
        if x-i<0 or y+i>14:
            break
        elif list[x][y]==list[x-i][y+i]==2:
            count4+=1
        else:
            break
    for i in range(1,5):
        if x+i>14 or y-i<0:
            break
        elif list[x][y]==list[x+i][y-i]==2:
            count4+=1
        else:
            break
    if count4>=5:
        return True 

while True:
    board()
#     for event in pg.event.get():
#         if event.type==pg.QUIT:
#             pg.quit() 
    event()  
    draw()
    drawselect()
   
    try: 
        if judgesuccess(chessx,chessy):
            FONT=font.render('游戏结束,黑棋赢!', True,(0,255,0))
            screen.blit(FONT,(80,height/2-20))
            running=False
        elif judgesuccess2(chessx,chessy):
            FONT=font.render('游戏结束,白棋赢!', True,(0,255,0))
            screen.blit(FONT,(80,height/2-20))
            running=False
    except:
        pass
    pg.display.flip()

board 尺寸 500,500

棋子尺寸  25,25

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值