在做python实践的时候突然对python写游戏代码产生兴趣这,于是查了查资料,敲了敲代码,这样能够熟悉pygame模块,发现还挺好玩的
讲解一下编写单机五子棋程序的几个重要部分: ①创建初始化棋盘方法initChessSquare(x,y):根据棋盘图片的交叉点个数,遍历其所有交叉点坐标。 ②创建监听各种事件的方法eventHander():如:鼠标点击退出位置,程序能实现退出响应(退出很重要,因为游戏界面的显示就是一个死循环whileTrue;一个不断刷新界面(背景图片)的过程,因此,不做退出操作,此窗口会进入死锁状态,用户将无法操作);鼠标只有点击棋盘的交叉点位置时,才通过pygame.mouse.get_pos()获取鼠标点击的坐标并给出相应的棋子类型value。通过列表封装后,再存入到全局列表initChessList中。 ③创建判断输赢的规则方法judgeResult(i,j,value):创建标志位flag为False,当满足赢的规则时,标志位flag再为True;判断横向、纵向、左斜向、右斜向有没有出现5连(在边缘依次向内逐一遍历,是否五个棋子的类型一样,一样则为赢,即五子连珠) ④创建主方法main():其中包含初始化游戏环境pygame.init();绘制游戏窗口pygame.display.set_mode((600,600),0,0)(其中第一个参数(600,600)为设定窗口的大小,第二个参数为标志位,如果不用什么特性就指定为0(指定为FULLSCREEN时,绘制的是一个全屏窗口,如果没有相应的退出机制,只有按 菜单键 才能跳回之前界面),第三个参数为色深);通过pygame.image.load(“图片路径”)的方法加载棋子以及判赢的图片;遍历全局列表initChessList中的子列表,再遍历子列表中封装的棋子类型value,通过之前的响应值来绘制相应棋子类型的图片;当其中一方为赢时,清空棋盘,重新初始化棋盘,绘制赢的图片,再更新显示的视图;最后调用监听事件方法(函数),监听用户的鼠标操作。 ⑤定义入口函数,调用主方法main()。
代码如下:
import sys
import time
import pygame
from pygame.locals import *
Chessboardlist = []
initRole = 1
resultFlag = 0
class Gobang():
def __init__(self,x,y,value):
'''
x: 代表x轴坐标
y: 代表y轴坐标
value: 当前坐标点的棋子:0:没有棋子 1:白子 2:黑子
'''
self.x = x
self.y = y
self.value = value
def initChessboard(x,y):
for i in range(15):
rowlist = []
for j in range(15):
pointX = x+ j*40
pointY = y+ i*40
sp = Gobang(pointX,pointY,0)
rowlist.append(sp)
Chessboardlist.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 Chessboardlist:
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 Chessboardlist[i][x].value == value and \
Chessboardlist[i][x + 1].value == value and \
Chessboardlist[i][x + 2].value == value and \
Chessboardlist[i][x + 3].value == value and \
Chessboardlist[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 Chessboardlist[x][j].value == value and \
Chessboardlist[x + 1][j].value == value and \
Chessboardlist[x + 2][j].value == value and \
Chessboardlist[x + 3][j].value == value and \
Chessboardlist[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 Chessboardlist[y][x].value == value and \
Chessboardlist[y - 1][x + 1].value == value and \
Chessboardlist[y - 2][x + 2].value == value and \
Chessboardlist[y - 3][x + 3].value == value and \
Chessboardlist[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 Chessboardlist[y][x].value == value and \
Chessboardlist[y + 1][x + 1].value == value and \
Chessboardlist[y + 2][x + 2].value == value and \
Chessboardlist[y + 3][x + 3].value == value and \
Chessboardlist[y + 4][x + 4].value == value:
flag = True
if flag:
resultFlag = value
print("白棋赢" if value ==1 else "黑棋赢")
def main():
global Chessboardlist,resultFlag
initChessboard(27,27)
pygame.init()
screen = pygame.display.set_mode((620,620),0,0)
pygame.display.set_caption("大碗吃不下")
background = pygame.image.load("bg_20190825_083840.png")
whiteStorn = pygame.image.load("storn_white_20190825_083840.png")
blackStorn = pygame.image.load("storn_black.png")
resultStorn = pygame.image.load("resultStorn.jpg")
rect = blackStorn.get_rect()
while True:
screen.blit(background,(0,0))
for temp in Chessboardlist:
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:
Chessboardlist = []
initChessboard(27,27)
screen.blit(resultStorn,(200,200))
pygame.display.update()
if resultFlag >0:
time.sleep(3)
resultFlag = 0
eventHander()
if __name__ == '__main__':
main()
pass
注意!!! 其中用到的图片:黑棋、白棋、棋盘,可以通过百度网盘获取,但一定记得要将图片与代码源文件保存在一个文件夹中才可以运行成功 链接:https://pan.baidu.com/s/1EXnVDuuSxYaI2HrmXhpHog 提取码:eujv
|