import pygame,sys,random
from pygame.locals import *
windowWidth = 800
windowHeight = 800
rectSize = 39
fontColor = pygame.Color("red")
inputBoxColor = pygame.Color("white")
backgroundColor = pygame.Color("black")
defaultColor = pygame.Color("gray")
promptColor = pygame.Color("pink")
warningColor = pygame.Color("red")
safeColor = pygame.Color("green")
captionString = "扫雷游戏"
welcomeString = "请输入扫雷的宽,高,和雷的数量,用','将不同数据隔开"
failureString = "你踩到了雷,GAME OVER,按ENTER返回开始界面"
successString = "你已找出所有雷,游戏通关,按ENTER返回开始界面"
inputSurfaceWidth = 600
MaxNum = 20
pygame.init()
width = 0
height = 0
mineNum = 0
screen = pygame.display.set_mode((windowWidth, windowHeight))
initSurface = pygame.Surface((windowWidth, windowHeight), flags=pygame.HWSURFACE)
pygame.display.set_caption(captionString)
mode = 0
font = pygame.font.SysFont("华文楷体", size=30, bold=True)
inputFont = pygame.font.SysFont("华文楷体", size=30, bold=False)
welcome = font.render(welcomeString, True, fontColor, backgroundColor)
failure = font.render(failureString, True, fontColor, backgroundColor)
success = font.render(successString, True, fontColor, backgroundColor)
welcome_rect = welcome.get_rect()
failure_rect = failure.get_rect()
success_rect = success.get_rect()
welcome_rect.center = (windowWidth // 2, windowHeight // 2)
failure_rect.center = (windowWidth // 2, windowHeight // 2)
success_rect.center = (windowWidth // 2, windowHeight // 2)
inputSurface = pygame.Surface((inputSurfaceWidth, welcome_rect.height), flags=pygame.HWSURFACE)
inputSurface.fill(color = inputBoxColor)
inputSurfacePos = (windowWidth // 2 - inputSurfaceWidth // 2, windowHeight // 2 - welcome_rect.height // 2 - welcome_rect.height)
inputText = ""
hintText = ""
defaultRect = pygame.Surface((rectSize, rectSize), flags=pygame.HWSURFACE)
defaultRect.fill(color = defaultColor)
warningRect = pygame.Surface((rectSize, rectSize), flags=pygame.HWSURFACE)
warningRect.fill(color = warningColor)
resultRect = [0] * 10
resultRect[0] = pygame.Surface((rectSize, rectSize), flags=pygame.HWSURFACE)
resultRect[0].fill(color = safeColor)
for i in range(9):resultRect[i + 1] = pygame.Surface((rectSize, rectSize), flags=pygame.HWSURFACE)resultRect[i + 1].fill(color = promptColor)numPrompt = inputFont.render(str(i + 1), True, fontColor, promptColor)numPromptRect = numPrompt.get_rect()numPromptRect.center = ((rectSize + 1) // 2, (rectSize + 1) // 2)resultRect[i + 1].blit(numPrompt, numPromptRect)
mineMap = {}
stateList = []
mineNumList = []
startPos = [0, 0]
changeMap = {}
init = False
def initMineMap():global mineMap, mineNummaxNum = height * widthfor i in range(mineNum):index = random.randrange(0, maxNum)while index in mineMap:index = random.randrange(0, maxNum)mineMap[index] = 1
def initMineNumList():global mineNumList,height,widthfor index in range(height * width):if index == 0:mineNumList[0] = mineMap.get(0, 0) + mineMap.get(1, 0) + mineMap.get(width, 0) + mineMap.get(width + 1, 0)elif index == width - 1:mineNumList[width - 1] = mineMap.get(width - 2, 0) + mineMap.get(width - 1, 0) + mineMap.get(width * 2 - 2, 0) + mineMap.get(width * 2 - 1, 0)elif index == width * (height - 1):mineNumList[width * (height - 1)] = mineMap.get(width * (height - 1), 0) + mineMap.get(width * (height - 1) + 1, 0) + mineMap.get(width * (height - 2), 0) + mineMap.get(width * (height - 2) + 1, 0)elif index == width * height - 1:mineNumList[width * height - 1] = mineMap.get(width * height - 1, 0) + mineMap.get(width * height - 2, 0) + mineMap.get(width * (height - 1) - 1, 0) + mineMap.get(width * (height - 1) - 2, 0)elif index >0 and index < width - 1:mineNumList[index] = mineMap.get(index, 0) + mineMap.get(index + 1, 0) + mineMap.get(index - 1, 0) + \mineMap.get(index + width, 0) + mineMap.get(index + 1 + width, 0) + mineMap.get(index - 1 + width, 0)elif index > width * (height - 1) and index < width * height - 1:mineNumList[index] = mineMap.get(index, 0) + mineMap.get(index + 1, 0) + mineMap.get(index - 1, 0) + \mineMap.get(index - width, 0) + mineMap.get(index + 1 - width, 0) + mineMap.get(index - 1 - width, 0)elif index % width == 0 and index > 0 and index < width * (height - 1):mineNumList[index] = mineMap.get(index, 0) + mineMap.get(index + 1, 0) + mineMap.get(index + width, 0) + \mineMap.get(index + 1 + width, 0) + mineMap.get(index - width, 0) + mineMap.get(index + 1 - width, 0)elif index % width == width - 1 and index > 0 and index < width * (height - 1):mineNumList[index] = mineMap.get(index, 0) + mineMap.get(index - 1, 0) + mineMap.get(index + width, 0) + \mineMap.get(index - 1 + width, 0) + mineMap.get(index - width, 0) + mineMap.get(index - 1 - width, 0)else :mineNumList[index] = mineMap.get(index, 0) + mineMap.get(index + 1, 0) + mineMap.get(index - 1, 0) + \mineMap.get(index + width, 0) + mineMap.get(index + 1 + width, 0) + mineMap.get(index - 1 + width, 0) + \mineMap.get(index - width, 0) + mineMap.get(index + 1 - width, 0) + mineMap.get(index - 1 - width, 0)
def initGame():global mineMap, stateList, mineNumList, startPos, height, width, mineNum, initmineMap.clear()changeMap.clear()startPos[0] = (windowWidth - width * 40) // 2startPos[1] = (windowHeight - height * 40) // 2stateList = [0] * height * widthmineNumList = [0] * height * widthinitMineMap()initMineNumList()init = Trueprint(mineMap)print(mineNumList)def pushIndex(indexNum):changeMap[indexNum] = 2stateList[indexNum] = 2if indexNum >= 0 and indexNum < width * height and mineNumList[indexNum] == 0:if indexNum == 0:if stateList[indexNum + width] != 2:pushIndex(indexNum + width)if stateList[indexNum + 1] != 2:pushIndex(indexNum + 1)if stateList[indexNum + width + 1] != 2:pushIndex(indexNum + width + 1)elif indexNum == width - 1:if stateList[indexNum - 1] != 2:pushIndex(indexNum - 1)if stateList[indexNum + width - 1] != 2:pushIndex(indexNum + width - 1)if stateList[indexNum + width] != 2:pushIndex(indexNum + width)elif indexNum == width * (height - 1):if stateList[indexNum - width + 1] != 2:pushIndex(indexNum - width + 1)if stateList[indexNum + 1] != 2:pushIndex(indexNum + 1)if stateList[indexNum - width] != 2:pushIndex(indexNum - width)elif indexNum == width * height - 1:if stateList[indexNum - width - 1] != 2:pushIndex(indexNum - width - 1)if stateList[indexNum - 1] != 2:pushIndex(indexNum - 1)if stateList[indexNum - width] != 2:pushIndex(indexNum - width)elif indexNum >0 and indexNum < width - 1:if stateList[indexNum - 1] != 2:pushIndex(indexNum - 1)if stateList[indexNum + width - 1] != 2:pushIndex(indexNum + width - 1)if stateList[indexNum + width] != 2:pushIndex(indexNum + width)if stateList[indexNum + 1] != 2:pushIndex(indexNum + 1)if stateList[indexNum + width + 1] != 2:pushIndex(indexNum + width + 1)elif indexNum > width * (height - 1) and indexNum < width * height - 1:if stateList[indexNum - width - 1] != 2:pushIndex(indexNum - width - 1)if stateList[indexNum - 1] != 2:pushIndex(indexNum - 1)if stateList[indexNum - width] != 2:pushIndex(indexNum - width)if stateList[indexNum - width + 1] != 2:pushIndex(indexNum - width + 1)if stateList[indexNum + 1] != 2:pushIndex(indexNum + 1)elif indexNum % width == 0 and indexNum > 0 and indexNum < width * (height - 1):if stateList[indexNum - width] != 2:pushIndex(indexNum - width)if stateList[indexNum + width] != 2:pushIndex(indexNum + width)if stateList[indexNum - width + 1] != 2:pushIndex(indexNum - width + 1)if stateList[indexNum + 1] != 2:pushIndex(indexNum + 1)if stateList[indexNum + width + 1] != 2:pushIndex(indexNum + width + 1)elif indexNum % width == width - 1 and indexNum > 0 and indexNum < width * (height - 1):if stateList[indexNum - width - 1] != 2:pushIndex(indexNum - width - 1)if stateList[indexNum - 1] != 2:pushIndex(indexNum - 1)if stateList[indexNum + width - 1] != 2:pushIndex(indexNum + width - 1)if stateList[indexNum - width] != 2:pushIndex(indexNum - width)if stateList[indexNum + width] != 2:pushIndex(indexNum + width)else :if stateList[indexNum - width - 1] != 2:pushIndex(indexNum - width - 1)if stateList[indexNum - 1] != 2:pushIndex(indexNum - 1)if stateList[indexNum + width - 1] != 2:pushIndex(indexNum + width - 1)if stateList[indexNum - width] != 2:pushIndex(indexNum - width)if stateList[indexNum + width] != 2:pushIndex(indexNum + width)if stateList[indexNum - width + 1] != 2:pushIndex(indexNum - width + 1)if stateList[indexNum + 1] != 2:pushIndex(indexNum + 1)if stateList[indexNum + width + 1] != 2:pushIndex(indexNum + width + 1)def checkSuccess():print(len(mineMap), width * height)if stateList.count(2) + len(mineMap) == width * height:return Trueelse:return False
if __name__ == '__main__':mode = 0while True:for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit()elif event.type == pygame.KEYDOWN:if mode == 0 and ((event.unicode <= '9' and event.unicode >= '0') or event.unicode == ','):inputText = inputText + event.unicodeelif mode == 0 and event.unicode == '\b':inputText = inputText[0:-1]elif event.key == pygame.K_RETURN:if mode == 0:if inputText == "":hintText = "输入数据为空"else :inputData = inputText.split(',')if len(inputData) != 3:hintText = "输入数据数量不正确"else :try:height = int(inputData[0])width = int(inputData[1])mineNum = int(inputData[2])if width > MaxNum or height > MaxNum or mineNum >= height * width - 1 or width < 3 or height < 3:hintText = "输入数据范围不正确,宽高,最小数量为3,最大数量均为" + str(MaxNum)else:hintText = ""mode = 1initGame()except:hintText = "输入数据格式不正确"else:mode = 0elif event.type == pygame.MOUSEBUTTONDOWN:if mode == 1:index = ((event.pos[0] - startPos[0]) // 40, (event.pos[1] - startPos[1]) // 40)indexNum = index[0] + index[1] * widthif event.button == 1:if stateList[indexNum] == 0:if mineMap.get(indexNum, 0) == 1:mode = 2else:pushIndex(indexNum)if checkSuccess():mode = 3else:print("该点击无效")elif event.button == 3:if stateList[indexNum] == 0:changeMap[indexNum] = 1stateList[indexNum] = 1elif stateList[indexNum] == 1:changeMap[indexNum] = 0stateList[indexNum] = 0else:print("该点击无效") if mode == 0:initSurface.fill(color=backgroundColor)screen.blit(initSurface, (0, 0))hintTextSurf = font.render(hintText, True, fontColor, backgroundColor)inputTextSurf = inputFont.render(inputText, True, backgroundColor, inputBoxColor)hintTextRect = hintTextSurf.get_rect()hintTextRect.center = (windowWidth // 2 , windowHeight // 2 - welcome_rect.height // 2 - welcome_rect.height - hintTextRect.height // 2)screen.blit(hintTextSurf, hintTextRect)screen.blit(welcome, welcome_rect)screen.blit(inputSurface, inputSurfacePos)screen.blit(inputTextSurf, inputSurfacePos)elif mode == 1:if init:initSurface.fill(color=backgroundColor)screen.blit(initSurface, (0, 0))for i in range(width):for j in range(height):screen.blit(defaultRect, (startPos[0] + i * 40, startPos[1] + j * 40))init = Falseelse :for i in changeMap.keys():x = i % widthy = i // widthif changeMap[i] == 1:screen.blit(warningRect, (startPos[0] + x * 40, startPos[1] + y * 40))elif changeMap[i] == 0:screen.blit(defaultRect, (startPos[0] + x * 40, startPos[1] + y * 40))elif changeMap[i] == 2:screen.blit(resultRect[mineNumList[i]], (startPos[0] + x * 40, startPos[1] + y * 40))changeMap.clear()elif mode == 2:initSurface.fill(color=backgroundColor)screen.blit(initSurface, (0, 0))screen.blit(failure, failure_rect)elif mode == 3:initSurface.fill(color=backgroundColor)screen.blit(initSurface, (0, 0))screen.blit(success, success_rect)pygame.display.update()
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!