[程序设计] 基于python的pygame函数库实现井字棋游戏

游戏素材及游戏代码如下,请将图片素材路径修改为自己的绝对地址

import pygame



def value_winner(point):

    temp = [0,0,0,0,0,0,0,0,0]

    for i in range (len(point)):
        if point[i] == 0:
            break
        elif point[i] % 2 == 0:
            temp[point[i]-1] = i%2 + 1
        else:
            temp[point[i]-1] = i%2 + 1

    #print ("落子列表:",temp,'/n')
    winner = None

    # 判断所给棋子是否获胜
    win = {(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)}
    for r in win:
        if temp[r[0]] == temp[r[1]] == temp[r[2]] == 2:
            winner = "ai"
            break
        elif temp[r[0]] == temp[r[1]] == temp[r[2]] == 1:
            winner = "human"
            break
        elif 0 not in temp:
            winner = "draw,nobody"

    return winner



def game_display(point):


    for i in range(len(point)):

        if point[i] == 0:
            break

        h = (point[i] - 1) // 3
        v = (point[i] - 1) % 3

        if (i % 2) == 0:
            cross = pygame.image.load(r"C:\Users\yliue\Desktop\chess_cross\C.png")
            cross = pygame.transform.scale(cross, (200, 200))
            screen.blit(cross, ((200 * v), (200 * h)))
            # print("X")

        else:
            round = pygame.image.load(r"C:\Users\yliue\Desktop\chess_cross\R.png")
            round = pygame.transform.scale(round, (300, 200))
            screen.blit(round, ((200 * v - 50), (200 * h)))
            # print ("O")



    pygame.display.update()



if __name__ == '__main__':

    pygame.init() #pygame初始化
    clock = pygame.time.Clock() #定义时钟对象
    clock.tick(60) #设置屏幕刷新率
    position = [0, 0, 0, 0, 0, 0, 0, 0, 0]
    winner = None

    screen = pygame.display.set_mode((600, 600))
    background = pygame.image.load(r"C:\Users\yliue\Desktop\chess_cross\P.png")
    background = pygame.transform.scale(background, (1000, 1000))
    screen.blit(background, (-200, -200))
    pygame.display.update()

    # 游戏循环
    while True:

        # 事件监听
        for event in pygame.event.get():
            #game_display(position)
            winner = value_winner(position)
            #print (winner)
            # 判断用户是否点击了关闭按钮

            if event.type == pygame.MOUSEBUTTONDOWN and winner == None:

                h_num = (event.pos[0]) // 200 + 1
                v_num = (event.pos[1]) // 200 + 1

                num = h_num + 3 * (v_num - 1)
                for i in range(len(position)):

                    if (position[i] == 0) and (num not in position):
                        position[i] = num
                        #print("顺序列表:",position)
                        game_display(position)
                        break

                    else:
                        continue

            elif event.type == pygame.QUIT:
                print("退出游戏...")
                pygame.quit()
                # 直接退出系统
                exit()

            if winner != None:
                print(winner,"获得胜利!")



[程序设计] 基于python的pygame函数库实现井字棋游戏_第1张图片
[程序设计] 基于python的pygame函数库实现井字棋游戏_第2张图片
[程序设计] 基于python的pygame函数库实现井字棋游戏_第3张图片

你可能感兴趣的:(笔记,python,pygame,游戏)