需安装pygame进行游戏

import pygame
##需安装pygame来进行游戏
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((800, 600))

screen.fill((255, 255, 255))

running = True

cx, cy = 300, 200

# 需要定义一个保存棋子的数据结构 cb[(r,c)]="o"或者"x"
cb = {(r,c):None for r in range(1,4) for c in range(1,4)}

def drawText(text, posx, posy, textHeight=48, fontColor=(0, 0, 0), backgroudColor=(255, 255, 255)):
    fontObj = pygame.font.Font('freesansbold.ttf', textHeight)  # 通过字体文件获得字体对象
    textSurfaceObj = fontObj.render(text, True, fontColor, backgroudColor)  # 配置要显示的文字
    textRectObj = textSurfaceObj.get_rect()  # 获得要显示的对象的rect
    textRectObj.center = (posx, posy)  # 设置显示对象的坐标
    screen.blit(textSurfaceObj, textRectObj)  # 绘制字

# 判断胜负
def win():
    winner= "u"
    for i in range(3):
        if cb[(i+1,1)]==cb[(i+1,2)]==cb[(i+1,3)]!=None:
            winner=cb[(i+1,1)]
            break
        if cb[(1,i+1)]==cb[(2,i+1)]==cb[(3,i+1)]!=None:
            winner=cb[(1,i+1)]
            break
    if cb[(1,1)]==cb[(2,2)]==cb[(3,3)]!=None:
        winner=cb[(1,1)]
    if cb[(1,3)]==cb[(2,2)]==cb[(3,1)]!=None:
        winner=cb[(1,3)]
    return winner

def winline():
    x0,y0=0,0
    x1,y1=0,0
    for i in range(3):
        if cb[(i+1,1)]==cb[(i+1,2)]==cb[(i+1,3)]!=None:
            x0,y0=cx-40,cy+(i)*64+37
            x1,y1=cx+242,cy+(i)*64+37
            break
        if cb[(1,i+1)]==cb[(2,i+1)]==cb[(3,i+1)]!=None:
            x0,y0=cx+(i)*64+37,cy-40
            x1,y1=cx+(i)*64+37,cy+242
            break
    if cb[(1,1)]==cb[(2,2)]==cb[(3,3)]!=None:
        x0,y0=cx-40,cy-40
        x1,y1=cx+242,cy+242
    if cb[(1,3)]==cb[(2,2)]==cb[(3,1)]!=None:
        x0,y0=cx-20,cy+222
        x1,y1=cx+222,cy-20
    pygame.draw.line(screen,(0,0,255),(x0,y0),(x1,y1),4)

# 需要一个函数,将棋子下到r行c列
def put_chess(t, r, c):
    if cb[(r,c)]!=None:  # 表示这里已经有棋子了
        return False
    else:
        cb[(r, c)] = t
        return True


# 需要一个函数,画所有的棋子
def draw_chess():
    for (r, c) in cb:
        chess(cb[(r, c)], r, c)


def pos(r, c):
    return cx + 37 + 64 * (c - 1), cy + 37 + 64 * (r - 1)


def pos1(x, y):
    return (
        ((cx + 10 < x < cx + 10 + 54) + 2*(cx + 20 + 54 < x < cx + 20 + 2 * 54) + 3*(
            cx + 30 + 2 * 54 < x < cx + 30 + 3 * 54)),
        ((cy + 10 < y < cy + 10 + 54) + 2*(cy + 20 + 54 < y < cy + 20 + 2 * 54) + 3*(
            cy + 30 + 2 * 54 < y < cy + 30 + 3 * 54)))


def chessboard(x, y):
    pygame.draw.rect(screen, (0, 0, 0), Rect(x, y, 202, 202))
    for i in range(9):
        pygame.draw.rect(screen, (255, 255, 255), Rect(x + 10 + (i % 3) * 64, y + 10 + (i // 3) * 64, 54, 54))


def chess(t, r, c):
    if t == "o":
        pygame.draw.circle(screen, (0, 0, 0), pos(r, c), 20, 3)
    elif t == "x":
        x, y = pos(r, c)
        pygame.draw.line(screen, (0, 0, 0), (x - 18, y - 18), (x + 18, y + 18), 3)
        pygame.draw.line(screen, (0, 0, 0), (x + 18, y - 18), (x - 18, y + 18), 3)


i = 0
while running:
    # 1,判断是否退出(游戏例行代码)
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_q:
                running = False
            if event.key == K_r:
                i=0
                cb = {(r, c): None for r in range(1, 4) for c in range(1, 4)}
                drawText("The game ended in a tie",399,100,48,(255,255,255))
                pygame.draw.rect(screen,(255,255,255),(1,1,800,600))
        if event.type == QUIT:
            running = False


    # 2,如果有鼠标点击,则下棋到cb字典里面(交互的部分,只处理数据,不涉及可视化)
    x1, y1 = pygame.mouse.get_pos()
    lmb, mmb, rmb = pygame.mouse.get_pressed()
    t1 = "x"
    r, c = 1, 1
    if lmb and i<9:
        # 下面的代码是将合法的下棋放到cb数据结构里面,用put_chess()函数
        if i % 2 == 0:
            r, c = pos1(x1, y1)
            t1 = "x"
        elif i % 2 == 1:
            r, c = pos1(x1, y1)
            t1 = "o"
        if r==0 or c==0:
            continue
        if put_chess(t1,c,r):
            i+=1
    # 3,画棋盘和棋子(将数据可视化的部分)
    chessboard(cx, cy)
    drawText("press 'Q' to quit",80,20,20)
    drawText("press 'R' to new game",110,50,20)
    if win()!="u":
        drawText(win()+" is winner",399,100)
        winline()
        i=9
    elif i==9 and win()=="u":
        drawText("The game ended in a tie",399,100)
    draw_chess()

    # 4,系统刷新(游戏例行代码)
    pygame.display.update()




4 条评论

  • 1

信息

难度
1
分类
(无)
标签
(无)
递交数
7368
已通过
1213
通过率
16%
被复制
7
上传者