Pygame、Python五子棋游戏

五子棋游戏

Pygame

pygame是python的一个第三方库,用来实现游戏界面的开发比较方便,我只用了很简单的一部分来实现五子棋的游戏,想要学习更多的Pygame知识可以看下xishui大大的博客:http://eyehere.net/2011/python-pygame-novice-professional-index/

主要功能

主要是实现了两个人的对弈,利用Pygame绘制游戏界面,利用python写了一个判断胜负的函数,以及一个重新开局的方法。
Pygame、Python五子棋游戏_第1张图片

不足与改进

这个demo可以改进的第三比较多,主要内容之间的耦合复杂,可以用面向对象写成低耦合的应用,功能没能全部函数化,缺少与外界交互的接口,数据结构较为复杂,其实不需要这么麻烦。
改进主要可以往人机对弈、与socket结合实现远程对弈、开发App实现移动设备对弈,其实不同平台上大体的内容都是相似的,就是绘图方式的不同而已,希望以后自己能把这个改进完成。

全部代码

这里使用了Pygame库和Numpy库,Python3的环境,没有的话直接用pip install就可以了。

pip install numpy
pip install pygame

这个例子中使用的棋盘图片是百度上找的,棋子自己画的,所以使用不同的图片的话,代码中关于这一部分应该修改。

background_image_filename = 'source/timg.jpg'

white_image = 'source/white.png'
black_image = 'source/black.png'

import pygame
from pygame.locals import *
from sys import exit
import numpy as np

def checkIsWin(x,y,array):
    count1,count2,count3,count4 = 0,0,0,0
    i = x-1
    while(i>=0):
        if array[i][y] == 1:
            count1+=1
            i -= 1
        else:
            break
    i = x+1
    while i<13:
        if array[i][y] == 1:
            count1+=1
            i += 1
        else:
            break
    j =y-1
    while (j >= 0):
        if array[x][j] == 1:
            count2 += 1
            j -= 1
        else:
            break
    j = y + 1
    while j < 13:
        if array[x][j] == 1:
            count2 += 1
            j += 1
        else:
            break

    i,j = x-1,y-1
    while(i>=0 and j>=0):
        if array[i][j] == 1:
            count3 += 1
            i -= 1
            j -= 1
        else :
            break
    i, j = x + 1, y + 1
    while (i <= 12 and j <= 12):
        if array[i][j] == 1:
            count3 += 1
            i += 1
            j += 1
        else:
            break

    i, j = x + 1, y - 1
    while (i >= 0 and j >= 0):
        if array[i][j] == 1:
            count4 += 1
            i += 1
            j -= 1
        else:
            break
    i, j = x - 1, y + 1
    while (i <= 12 and j <= 12):
        if array[i][j] == 1:
            count4 += 1
            i -= 1
            j += 1
        else:
            break


    if count1>=4 or count2>=4 or count3 >= 4 or count4 >= 4:
        return True
    else:
        return False



pygame.init()
screen = pygame.display.set_mode((490, 557), 0, 32)
background = pygame.image.load(background_image_filename).convert()
white = pygame.image.load(white_image).convert_alpha()
black = pygame.image.load(black_image).convert_alpha()

screen.blit(background, (0,0))
font = pygame.font.SysFont("黑体", 40);



white_list = np.zeros((13,13))
black_list = np.zeros((13,13))

pygame.event.set_blocked([1,4,KEYUP,JOYAXISMOTION,JOYBALLMOTION,JOYBUTTONDOWN,JOYBUTTONUP,JOYHATMOTION])
pygame.event.set_allowed([MOUSEBUTTONDOWN,MOUSEBUTTONUP,12,KEYDOWN])

dot_list = [(52+i*32-white.get_width()/2,122+j*32-white.get_height()/2) for i in range(13 ) for j in range(13)]
start = True
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        if event.type == KEYDOWN:
            if event.key == K_r:
                screen.blit(background,(0,0))
                white_list = np.zeros((13,13))
                black_list = np.zeros((13, 13))
                pygame.event.set_blocked(
                    [1, 4, KEYUP, JOYAXISMOTION, JOYBALLMOTION, JOYBUTTONDOWN, JOYBUTTONUP,
                     JOYHATMOTION])
                pygame.event.set_allowed([MOUSEBUTTONDOWN, MOUSEBUTTONUP, 12, KEYDOWN])
                dot_list = [(52 + i * 32 - white.get_width() / 2, 122 + j * 32 - white.get_height() / 2) for i in
                            range(13) for j in range(13)]
                start = True
                break
        if event.type == MOUSEBUTTONDOWN:

            # for item in dot_list:
            #     screen.blit(white, item)

            x,y = pygame.mouse.get_pos()
            if 42 <= x <= 446 and 112 <= y <= 516 and ((x - 52) % 32 <= 10 or (x - 52) % 32 >= 22) and (
                        (y - 122) % 32 <= 10 or (y - 122) % 32 >= 22):

                m = int(round((x-52)/32))
                n = int(round((y-112)/32))
                try:
                    if start:
                        start = not start
                        screen.blit(black, dot_list[13*m+n])
                        #print(m,n,'----------')
                        black_list[n][m] = 1
                        if checkIsWin(n,m,black_list):

                            screen.blit(font.render('GAME OVER,Black is win!', True, (0, 0, 0)), (80, 300))
                            pygame.event.set_blocked(
                                [1, 4, KEYUP, JOYAXISMOTION, JOYBALLMOTION, JOYBUTTONDOWN, JOYBUTTONUP,
                                 JOYHATMOTION,MOUSEBUTTONDOWN,MOUSEBUTTONUP])
                            pygame.event.set_allowed(QUIT)

                    else:
                        start = not start
                        screen.blit(white, dot_list[13 * m + n])
                        #print(m, n, '----------')
                        white_list[n][m] = 1
                        if checkIsWin(n,m,white_list):
                            screen.blit(font.render('GAME OVER,White is win!', True, (0, 0, 0)), (80, 300))
                            pygame.event.set_blocked(
                                [1, 4, KEYUP, JOYAXISMOTION, JOYBALLMOTION, JOYBUTTONDOWN, JOYBUTTONUP,
                                 JOYHATMOTION, MOUSEBUTTONDOWN, MOUSEBUTTONUP])
                            pygame.event.set_allowed(QUIT)



                    dot_list[13*m+n] = ''

                except :
                    pass

                # print('This true')


            # print('---------------black------------------')
            # for line in black_list:
            #     print(line )
            # print('---------------white------------------')
            # for line in white_list:
            #     print(line)


        # m,n = (x-20)%32,(y-90)%32


        # x -= white.get_width()/2
        # y -= white.get_height()/2
        # screen.blit(white,(x,y))

        # print(dot_list[m*15+n+1])


    pygame.display.update()

你可能感兴趣的:(python,五子棋,python)