In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to achieve Gobang in python". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve Gobang in python" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
I use the pygame module to make the window
The code is as follows:
# 1. Introduce pygame and pygame.localsimport pygamefrom pygame.localsimport * import timeimport sys initChessList = [] initRole = 2 # for Baizi 2: represent the current sunspot resultFlag = 0userFlag = True class StornPoint (): def _ init__ (self, x, y) Value = 0):'': param x: represents x-axis coordinates: param y: represents y-axis coordinates: param value: chess pieces at the current coordinate point: 0: no chess pieces 1: White 2: sunspots' self.x = x self.y = y self.value = value pass def initChessSquare (x Y):''initialize the coordinates of the chessboard: param x:: param y:: return:''# use a two-dimensional list to save the coordinate system of the chessboard And the numerical value of each falling point for i in range (15): # intersection coordinates of each row rowList = [] for j in range (15): # intersection coordinates of each column pointX = x + juni40 pointY = y + iTun40 # value = 0 sp = StornPoint (pointX, pointY 0) rowList.append (sp) pass initChessList.append (rowList) pass # handle event def eventHandler (): global userFlag 'listens for various events: return:' 'for event in pygame.event.get (): global initRole # listens for dot product exit button event if event.type = = QUIT: Pygame.quit () sys.exit () pass # listens for mouse dot accumulation event if event.type = = MOUSEBUTTONDOWN: X Y = pygame.mouse.get_pos () # print ((x, y)) I = j = 0 for temp in initChessList: for point in temp: if x > = point.x-15 and x = point.y-15 and y = 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): # 垂直方向有没有出现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 # 判断东北方向的对角线是否出现了5连 for x, y in zip(range(j + 4, j - 5, -1), range(i - 4, i + 5)): if x >= 0 and xero4
< 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 break pass pass pass # 判断西北方向的对角是否出现了五连 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 break pass pass pass if flag: resultFlag = value pass pass # 加载素材def main(): initRole = 2 # 代表白子下 2:代表当前是黑子下 global resultFlag, initChessList 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') blackStorn = pygame.image.load('images/storn_black.png') whiteStorn = pygame.image.load('images/storn_white.png') winStornW = pygame.image.load('images/white.png') winStornB = pygame.image.load('images/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)) pass elif point.value == 2: screen.blit(blackStorn, (point.x - 18, point.y - 18)) pass pass pass # 如果已经判决出输赢 if resultFlag >0: initChessList = [] # clear the chessboard initChessSquare (27,27) # reinitialize the chessboard if resultFlag = = 1: screen.blit (winStornW, (50100)) else: screen.blit (winStornB (50100) pass pygame.display.update () if resultFlag > 0: time.sleep (3) resultFlag = 0 pass eventHandler () pass pass if _ _ name__ = = "_ _ main__": main () pass
Illustration: in the images folder in the same directory
Bg.png
Storn_white.png
Storn_black.png
White.png
Black.png
After reading this, the article "how to play Gobang in python" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.