Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Qt5 to realize simple Gobang Game in Python3

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "Python3 how to use Qt5 to achieve simple Gobang game", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python3 how to use Qt5 to achieve simple Gobang game"!

In order to write a Gobang game, the first thing we need to solve is how to play, how to judge the five pieces, rather than how to draw the picture, so we first determine the chessboard.

Gobang uses a 15-15 chessboard, so we can use a two-dimensional list to create a chessboard. We might as well think that 0 means no pieces have been placed, 1 means placing white pieces, and 2 means placing sunspots.

It is obvious that you can create a list, but note that you cannot use * to copy the list

Self.chess_board = [[0 for i in range (15)] for i in range (15)]

The steps of playing chess are very easy to do, you only need to find the corresponding index to assign values, and the next step should be to solve the problem of how to judge the five beads.

Whenever we are finished, we should judge whether we have completed the Wuzi Lianzhu. For a chess piece that has just been placed, there are four possible situations:

1. Horizontal

two。 Oblique lower right

3. Upright

4. Oblique upper right

To judge whether it has been successful or not, we first traverse four pieces forward with the newly placed pieces as the starting point, and calculate the number of the same pieces. Once we encounter different pieces, we stop, and then traverse the four pieces back from the starting point. Until all the traversal has been completed or the total number of pieces has reached 5, you can return. We only need to pay attention to how to get the front and rear pieces of the chess piece and the boundary of the chessboard, the chess piece can not exceed the chessboard, so the pieces that are traversed can not exceed the chessboard.

Take the level as an example, you can get the code.

Def judge_1 (self,x:int,y:int)-> bool: count = 1 if self.chess_ Board [x] [y]! = 0: for i in range (1meme 5): if Ymuri > = 0: if self.chess_ Board [x] [y] = = self.chess_ Board [x] [Ymuri]: print (x Ymuri) count + = 1 else: break else: break for i in range (1 None 5): if y + I None: qp = QPainter () qp.begin (self) qp.fillRect (self.rect () QColor ("light blue") qp.drawRect (self.rect ()) qp.setBackground (QColor ("yellow")) qp.setPen (QPen (QColor (0,0,0), 2, Qt.SolidLine)) for i in range (15): qp.drawLine (QPoint (30,30 + 30 * I), QPoint 30 + 30 * I)) for i in range (15): qp.drawLine (QPoint (30 + 30 * I, 30), QPoint (30 + 30 * i450)) qp.setBrush (QColor (0,0,0)) key_points = [(3,3), (11,3), (3,11), (11,11), (7) 7)] if len (self.black_chess)! = 0: for t in self.black_chess: # draw sunspot qp.drawEllipse (QPoint (30 + 30 * t [1], 30 + 30 * t [0]), 6, 6) for t in key_points: # 5 fixed-point qp.drawEllipse of the chessboard (QPoint (30 + 30 * t [0]) 30 + 30 * t [1]), 3,3) qp.setBrush (QColor (255255255)) if len (self.white_chess)! = 0: for t in self.white_chess: # drawing white qp.drawEllipse (QPoint (30 + 30 * t [1], 30 + 30 * t [0]), 6,6) qp.end ()

Another problem that needs to be solved in GUI is how to get the coordinates of the pieces to be played. We can solve the problem by rewriting the mouse event, rewriting the stand-alone event mousePressEvent, and modifying the x and y coordinates of the chess piece. in addition, the user cannot happen to point to our specified coordinate point every time, so we need to give a rough range judgment. Here, my way is to get the coordinates first, and then find the nearest point according to the coordinates.

Def mousePressEvent (self, e)-> None: if e.buttons () = = QtCore.Qt.LeftButton: if e.x () > 15 and e.x ()

< 465 and e.y() >

15 and e.y ()

< 465: x = e.x()/30 - e.x()//30 y = e.y()/30 - e.y()//30 self.y = (e.x()-30)//30 if x < 0.5 else (e.x()-30)//30 + 1 self.x = (e.y()-30)//30 if y < 0.5 else (e.y()-30)//30 + 1 if self.flag: print(self.x,self.y) if self.player % 2 == 1: if goBang.put_white_chess(self.x,self.y): self.player += 1 print('黑子行动') else: print('白子行动') if goBang.judge(self.x,self.y): msg_box = QMessageBox(QMessageBox.Information, '提示', '白子获胜!') msg_box.exec_() else: if goBang.put_black_chess(self.x,self.y): self.player += 1 print('白子行动') else: print('黑子行动') if goBang.judge(self.x,self.y): msg_box = QMessageBox(QMessageBox.Information, '提示', '黑子获胜!') msg_box.exec_() 每当游戏完成,我们应该可以清空棋盘,也就是将所有储存数据的变量都重新初始化再重绘棋盘 #清除棋盘,重开游戏 def clear(self) ->

None: self.x =-1 self.y =-1 self.player = 0 self.flag = False self.white_chess = [] self.black_chess = [] self.chess_board = [[0 for i in range (15)] for i in range (15)] self.update ()

This is about the end!

Here is the whole code:

From PyQt5 import * from PyQt5 import QtCorefrom PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import sys class GoBang (QWidget): # initialize the chessboard def _ _ init__ (self): super (). _ init__ () self.setWindowTitle ('Gobang Hi~ o (* ▽ players *)') self.x =-1 self.y =-1 # distinguish players Self.player = 0 # start tag self.flag = False # Storage of white spots self.white_chess = [] # Storage of sunspots self.black_chess = [] self.setFixedSize (800600) self.chess_board = [[0 for i in range (15)] for i in range (15)] btn1 = QPushButton ('start') Self) btn1.setGeometry (500jue 100je 50je 30) btn1.clicked.connect (self.setFlag) btn2 = QPushButton ('reopen', self) btn2.setGeometry (550pr 100je 50pint 30) btn2.clicked.connect (self.clear) self.show () # draw chessboard and pieces def paintEvent (self E)-> None: qp = QPainter () qp.begin (self) qp.fillRect (self.rect (), QColor ("light blue")) qp.drawRect (self.rect ()) qp.setBackground (QColor ("yellow")) qp.setPen (QPen (QColor (0,0,0), 2, Qt.SolidLine) for i in range (15): qp.drawLine (QPoint (30) 30 + 30 * I), QPoint (450,30 + 30 * I) for i in range (15): qp.drawLine (QPoint (30 + 30 * I, 30), QPoint (30 + 30 * I, 450)) qp.setBrush (QColor (0,0,0)) key_points = [(3,3), (11,3), (3,11), (11,11), (7) 7)] if len (self.black_chess)! = 0: for t in self.black_chess: # draw sunspot qp.drawEllipse (QPoint (30 + 30 * t [1], 30 + 30 * t [0]), 6, 6) for t in key_points: # 5 fixed-point qp.drawEllipse of the chessboard (QPoint (30 + 30 * t [0]) Start the game def setFlag (self)-> None: self.flag = True def mousePressEvent (self, e)-> None: if e.buttons () = = QtCore.Qt.LeftButton: if e.x () > 15 and e.x ()

< 465 and e.y() >

15 and e.y ()

< 465: x = e.x()/30 - e.x()//30 y = e.y()/30 - e.y()//30 self.y = (e.x()-30)//30 if x < 0.5 else (e.x()-30)//30 + 1 self.x = (e.y()-30)//30 if y < 0.5 else (e.y()-30)//30 + 1 if self.flag: print(self.x,self.y) if self.player % 2 == 1: if goBang.put_white_chess(self.x,self.y): self.player += 1 print('黑子行动') else: print('白子行动') if goBang.judge(self.x,self.y): msg_box = QMessageBox(QMessageBox.Information, '提示', '白子获胜!') msg_box.exec_() else: if goBang.put_black_chess(self.x,self.y): self.player += 1 print('白子行动') else: print('黑子行动') if goBang.judge(self.x,self.y): msg_box = QMessageBox(QMessageBox.Information, '提示', '黑子获胜!') msg_box.exec_() #下白子 def put_white_chess(self,x:int,y:int) ->

Bool: if self.chess_ Board [x] [y]! = 0: msg_box = QMessageBox (QMessageBox.Information, 'hint', 'there are already pieces in this position!') Msg_box.exec_ () return False else: self.chess_ Board [x] [y] = 1 self.white_chess.append ((XMagine y)) self.update () return True # sunspot def put_black_chess (self,x:int Y:int)-> bool: if self.chess_ Board [x] [y]! = 0: msg_box = QMessageBox (QMessageBox.Information, 'hint', 'there are already pieces in this position!') Msg_box.exec_ () return False else: self.chess_ Board [x] [y] = 2 self.black_chess.append ((XMagine y)) self.update () return True # clear the chessboard Restart the game def clear (self)-> None: self.x =-1 self.y =-1 self.player = 0 self.flag = False self.white_chess = [] self.black_chess = [] self.chess_board = [[0 for i in range (15)] for i in range (15)] self.update () # to determine whether or not Already have five sons def judge (self) Bool: if self.judge_1 (XMagne y) or self.judge_2 (XMague y) or self.judge_3 (XMagi y) or self.judge_4 (xMagol y): return True return False # judge the horizontal line def judge_1 (self,x:int Y:int)-> bool: count = 1 if self.chess_ Board [x] [y]! = 0: for i in range (1meme 5): if Ymuri > = 0: if self.chess_ Board [x] [y] = = self.chess_ Board [x] [yMui]: print (x Ymuri) count + = 1 else: break else: break for i in range (1 bool 5): if y + I bool: count = 1 if self.chess_ Board [x] [y]! = 0: For i in range (1mae 5): if Xmuri > = 0 and ymuri > = 0: if self.chess_ Board [x] [y] = = self.chess_ Board [x-I] [yMui]: print (xmuri Ymuri) count + = 1 else: break else: break for i in range (1mai 5): if x + I = 0: if self.chess_ Board [x] [y] = self.chess_board [ XMuri] [y]: print (Xmuri Y) count + = 1 else: break else: break for i in range (1 bool 5): if x + I bool: count = 1 if self.chess_ Board [x] [y]! = 0: For i in range (1BME 5): if x-I > = 0 and y + I

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report