Python:五子棋小游戏
程序员文章站
2024-03-18 20:03:52
...
临近期末考试,班主任把所有的不参与考试的副科课程全都停了,天天语数外历史物理,实在是无聊,同学们便在课间互相约战五子棋,棋盘便是平时写作业用的玛丽大号写字本,棋子就是xo,不到两个星期,我就成功地用完了一本32页的写字本(心疼我的写字本三秒钟),这也太浪费纸了吧!教室里都装有触屏的电子白板,又为什么不让他们在电脑上下呢?而且还是全班直播,多好!
出于这个念头,我便用Python的pygame库开发了一个单机双人的五子棋小游戏,以供大家娱乐。
Talk is cheap,show me the code.(话不多说,上代码):
import pygame
import time
import sys
import easygui
from pygame.locals import *
initChessList = []
initRole = 1
resultFlag = 0
class StornPoint():
def __init__(self, x, y, value):
'''
:param x: 代表x轴坐标
:param y: 代表y轴坐标
:param value: 当前坐标点的棋子:0:没有棋子 1:白子 2:黑子
'''
self.x = x
self.y = y
self.value = value
def initChessSquare(x, y):
for i in range(15):
rowlist = []
for j in range(15):
pointX = x + j*40
pointY = y + i*40
sp = StornPoint(pointX, pointY, 0)
rowlist.append(sp)
initChessList.append(rowlist)
def eventHander():
for event in pygame.event.get():
global initRole
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
i = 0
j = 0
for temp in initChessList:
for point in temp:
if x >= point.x-10 and x <= point.x+10 and y >= point.y-10 and y <= point.y+10:
if point.value == 0 and initRole == 1:
point.value = 1
judgeResult(i, j, 1)
initRole = 2
elif point.value == 0 and initRole == 2:
point.value = 2
judgeResult(i, j, 2)
initRole = 1
break
j += 1
i += 1
j = 0
def judgeResult(i, j, value):
global resultFlag
flag = False
for x in range(j - 4, j + 5):
if x >= 0 and x + 4 < 15:
if initChessList[i][x].value == value and \
initChessList[i][x + 1].value == value and \
initChessList[i][x + 2].value == value and \
initChessList[i][x + 3].value == value and \
initChessList[i][x + 4].value == value:
flag = True
break
pass
for x in range(i - 4, i + 5):
if x >= 0 and x + 4 < 15:
if initChessList[x][j].value == value and \
initChessList[x + 1][j].value == value and \
initChessList[x + 2][j].value == value and \
initChessList[x + 3][j].value == value and \
initChessList[x + 4][j].value == value:
flag = True
break
pass
for x, y in zip(range(j + 4, j - 5, -1), range(i - 4, i + 5)):
if x >= 0 and x + 4 < 15 and y + 4 >= 0 and y < 15:
if initChessList[y][x].value == value and \
initChessList[y - 1][x + 1].value == value and \
initChessList[y - 2][x + 2].value == value and \
initChessList[y - 3][x + 3].value == value and \
initChessList[y - 4][x + 4].value == value:
flag = True
for x, y in zip(range(j - 4, j + 5), range(i - 4, i + 5)):
if x >= 0 and x + 4 < 15 and y >= 0 and y + 4 < 15:
if initChessList[y][x].value == value and \
initChessList[y + 1][x + 1].value == value and \
initChessList[y + 2][x + 2].value == value and \
initChessList[y + 3][x + 3].value == value and \
initChessList[y + 4][x + 4].value == value:
flag = True
if flag:
resultFlag = value
print("白棋胜" if value == 1 else "黑棋胜")
easygui.msgbox("白棋胜" if value == 1 else "黑棋胜")
def main():
global initChessList, resultFlag
initChessSquare(27, 27)
pygame.init()
screen = pygame.display.set_mode((620, 620), 0, 0)
pygame.display.set_caption("五子棋")
background = pygame.image.load("images/bg.png")
whiteStorn = pygame.image.load("images/storn_white.png")
blackStorn = pygame.image.load("images/storn_black.png")
rect = blackStorn.get_rect()
while True:
screen.blit(background, (0, 0))
for temp in initChessList:
for point in temp:
if point.value == 1:
screen.blit(whiteStorn, (point.x-18, point.y-18))
elif point.value == 2:
screen.blit(blackStorn, (point.x-18, point.y-18))
if resultFlag > 0:
initChessList = []
initChessSquare(27, 27)
pygame.display.update()
if resultFlag > 0:
time.sleep(3)
resultFlag = 0
eventHander()
if __name__ == '__main__':
main()
pass
下面是程序中用到的图片素材 :