当前位置: 首页 > 教学资源 > 五子棋 (Pygame Zero )

五子棋 (Pygame Zero )

2022年02月12日 16:02:46 访问量:1285

SIZE = 40
WIDTH = SIZE*15
HEIGHT = SIZE*15
board = [[" "for i in range(15)]for j in range(15)]
chesses = []
turn = "b"
lastturn="w"

def draw():
    screen.fill((210,180,140))
    for i in range(15):
        screen.draw.line((20,SIZE*i+20),(580,SIZE*i+20),(0,0,0))
        screen.draw.line((SIZE*i+20,20),(SIZE*i+20,580),(0,0,0))
    for chess in chesses:
        chess.draw()
        if len(chesses)>0:
            screen.draw.rect(Rect(chesses[-1].topleft,(36,36)),(255,255,255))
    if check():
        if lastturn == "b" :
            screen.draw.text("Black Win!",center=(WIDTH//2,HEIGHT//2),fontsize=100,color="red")
        if lastturn == "w":
            screen.draw.text("White Win!",center=(WIDTH//2,HEIGHT//2),fontsize=100,color="red")   

def on_mouse_down(pos,button):
    global turn,lastturn
    if button == mouse.LEFT:
        col =pos[0]//SIZE
        row =pos[1]//SIZE
        if board[col][row] != " ":
            return
        if turn =="b":
            chess = Actor("goblack",(col*SIZE+20,row*SIZE+20))
        if turn =="w":
            chess = Actor("gowhite",(col*SIZE+20,row*SIZE+20))
        chesses.append(chess)
        board[col][row] = turn
        lastturn=turn
        if turn =="b":
            turn ="w"
        else:
            turn="b"
    elif button == mouse.RIGHT :
        if len(chesses)<2:
            return
        for i in range(2):
            chess = chesses.pop()
            col=int(chess.x-20)//SIZE
            row=int(chess.y-20)//SIZE
            board[col][row]=" "

def check():
    a=lastturn
    for i in range(11):
        for j in range(11):
            if board[i][j] == a and board[i+1][j+1] == a and board[i+2][j+2] == a and board[i+3][j+3] == a and board[i+4][j+4] == a :
                return True
    for i in range(11):
        for j in range(4,15):
            if board[i][j] == a and board[i+1][j-1] == a and board[i+2][j-2] == a and board[i+3][j-3] == a and board[i+4][j-4] == a :
                return True            
    for i in range(15):
        for j in range(11):
            if board[i][j] == a and board[i][j+1] == a and board[i][j+2] == a and board[i][j+3] == a and board[i][j+4] == a :
                return True
    for i in range(11):
        for j in range(15):
            if board[i][j] == a and board[i+1][j] == a and board[i+2][j] == a and board[i+3][j] == a and board[i+4][j] == a :
                return True
                
def update():
    if check():
        sounds.win.play()
  • 附件(1个)
  • gobang.rar (659kb) 下载
编辑:郑志平
评论区
发表评论

评论仅供会员表达个人看法,并不表明网校同意其观点或证实其描述
教育部 中国现代教育网 不良信息 垃圾信息 网警110
郑重声明:本站全部内容均由本单位发布,本单位拥有全部运营和管理权,任何非本单位用户禁止注册。本站为教育公益服务站点,禁止将本站内容用于一切商业用途;如有任何内容侵权问题请务必联系本站站长,我们基于国家相关法律规定严格履行【通知—删除】义务。本单位一级域名因备案流程等原因,当前临时借用网校二级域名访问,使用此二级域名与本单位官网权属关系及运营管理权无关。韶山学校 特此声明。