In this article, we share the example of python to achieve the specific code of the game of backgammon, for your reference, the details are as follows
Let's start with the code.
# Calling the pygame library import pygame import sys # Calling common keyword constants from import QUIT,KEYDOWN import numpy as np #Initialize pygame () # Get access to the display system and create a window screen #Window size 670x670 screen = .set_mode((670,670)) screen_color=[238,154,73]#Set the canvas color, [238,154,73] corresponds to tan. line_color = [0,0,0]# Set the line color, [0,0,0] corresponds to black. def check_win(over_pos):#Judgemental Five in a Row mp=([15,15],dtype=int) for val in over_pos: x=int((val[0][0]-27)/44) y=int((val[0][1]-27)/44) if val[1]==white_color: mp[x][y]=2# means white. else: mp[x][y]=1# indicates a black child for i in range(15): pos1=[] pos2=[] for j in range(15): if mp[i][j]==1: ([i,j]) else: pos1=[] if mp[i][j]==2: ([i,j]) else: pos2=[] if len(pos1)>=5:#Five in a row return [1,pos1] if len(pos2)>=5: return [2,pos2] for j in range(15): pos1=[] pos2=[] for i in range(15): if mp[i][j]==1: ([i,j]) else: pos1=[] if mp[i][j]==2: ([i,j]) else: pos2=[] if len(pos1)>=5: return [1,pos1] if len(pos2)>=5: return [2,pos2] for i in range(15): for j in range(15): pos1=[] pos2=[] for k in range(15): if i+k>=15 or j+k>=15: break if mp[i+k][j+k]==1: ([i+k,j+k]) else: pos1=[] if mp[i+k][j+k]==2: ([i+k,j+k]) else: pos2=[] if len(pos1)>=5: return [1,pos1] if len(pos2)>=5: return [2,pos2] for i in range(15): for j in range(15): pos1=[] pos2=[] for k in range(15): if i+k>=15 or j-k<0: break if mp[i+k][j-k]==1: ([i+k,j-k]) else: pos1=[] if mp[i+k][j-k]==2: ([i+k,j-k]) else: pos2=[] if len(pos1)>=5: return [1,pos1] if len(pos2)>=5: return [2,pos2] return [0,[]] def find_pos(x,y):#Find a location that shows where you can land for i in range(27,670,44): for j in range(27,670,44): L1=i-22 L2=i+22 R1=j-22 R2=j+22 if x>=L1 and x<=L2 and y>=R1 and y<=R2: return i,j return x,y def check_over_pos(x,y,over_pos):#Check if the current position has been set for val in over_pos: if val[0][0]==x and val[0][1]==y: return False return True#Saying that there's no drop flag=False tim=0 over_pos=[]# Indicates the position where the pieces have been placed white_color=[255,255,255]#White color black_color=[0,0,0]#Black color while True:# Constant training refreshes the canvas for event in ():# Get the event, if the mouse clicks on the close button in the upper right corner, close the if in (QUIT,KEYDOWN): () (screen_color)#ClearScreen for i in range(27,670,44): # Draw the vertical line first if i==27 or i==670-27:#The edge line is a little thicker (screen,line_color,[i,27],[i,670-27],4) else: (screen,line_color,[i,27],[i,670-27],2) # And then draw the line if i==27 or i==670-27:#The edge line is a little thicker (screen,line_color,[27,i],[670-27,i],4) else: (screen,line_color,[27,i],[670-27,i],2) # Draw a small circle in the center of the board to indicate the center position # (screen, line_color,[27+44*7,27+44*7], 8,0) for val in over_pos:# Show all the pieces that have fallen (screen, val[1],val[0], 20,0) #Determine if there is a pentomino res=check_win(over_pos) if res[0]!=0: for pos in res[1]: (screen,[238,48,167],[pos[0]*44+27-22,pos[1]*44+27-22,44,44],2,1) ()# Refresh display continue# Game over, stop the following # Get mouse coordinate information x,y = .get_pos() x,y=find_pos(x,y) if check_over_pos(x,y,over_pos):# Determine if you can drop a stone, then display (screen,[0 ,229 ,238 ],[x-22,y-22,44,44],2,1) keys_pressed = .get_pressed()# Get mouse button information #The left mouse button is used to delay the time, because the time interval between each loop is very broken, which can lead to the left mouse button being pressed only once, but it is fetched multiple times, thinking that I pressed it multiple times. if keys_pressed[0] and tim==0: flag=True if check_over_pos(x,y,over_pos):#Determine if you can drop, then drop again if len(over_pos)%2==0:#Black over_pos.append([[x,y],black_color]) else: over_pos.append([[x,y],white_color]) # Left mouse button delay function if flag: tim+=1 if tim%50==0:# 200ms delay flag=False tim=0 ()#refresh display
rendering (visual representation of how things will turn out)
Code Details
I. pygame initialization canvas interface
# Calling the pygame library import pygame import sys # Calling common keyword constants from import QUIT,KEYDOWN #Initialize pygame () # Get access to the display system and create a window screen #Window size 670x670 screen = .set_mode((670,670)) screen_color=[238,154,73]#Set the canvas color, [255,255,255] corresponds to white. while True:# Constantly training to refresh the canvas for event in ():# Get the event, if the mouse clicks on the close button in the upper right corner, close the if in (QUIT,KEYDOWN): () (screen_color)#ClearScreen ()#refresh display
II. Drawing the board
The parameter screen means draw on the current window, line_color means line color, [0,0] means start position, [670,670] means end position, 2 means line thickness, the larger the value the thicker the line.
The general board size of our five pieces is 15x15, and we initialize the window size to 670x670, because there should be a certain interval between each piece so that it looks better.
So we define the radius size of each piece to be 20, and then each piece is spaced by 2, which means that two adjacent horizontal or vertical lines are spaced by 22 * 2 = 44.
Then because 670 - 44 * (15-1) = 54
Then 54 / 2 = 27. i.e. the distance between the two most marginal lines and the edge of the board should be 27.
So we use the loop to start drawing 15 lines of the board, which should start at 27, with a loop interval of 44
Considering the details again, we should hit a marker point, a small black solid circle, in the center of the board.
The code for drawing a solid circle is as follows:
(screen, line_color,[27+44*7,27+44*7], 8,0)
screen and line_color explained above, [27+447,27+447] for the location of the center of the circle, here is the center of the board, 8 for the size of the radius of the circle, 0 means a solid circle, if you set it to 1 output hollow circle.
III. Getting Mouse Information to Play Chess Pieces
Now we have to play according to the mouse information, we click the left mouse button and drop the pieces, in order to make the effect look better, we should get the mouse position information in real time, and then show the current position where we should play.
The code thrown to get the left mouse button is as follows:
# Get mouse coordinate information x,y = .get_pos()
After we get the mouse coordinate information, we need to find out exactly where this coordinate falls into, so we need to judge it:
def find_pos(x,y):#Find a location that shows where you can land for i in range(27,670,44): for j in range(27,670,44): L1=i-22 L2=i+22 R1=j-22 R2=j+22 if x>=L1 and x<=L2 and y>=R1 and y<=R2: return i,j return x,y
After returning the location where we can drop a child, we need to show it, we use a square box out, the code to throw this square box is as follows:
x,y=find_pos(x,y) (screen,[0 ,229 ,238 ],[x-22,y-22,44,44],2,1)
and just draw a circle to draw a straight line function is basically the same, [0 ,229 ,238 ] is the color of the RGB, I found is a fluorescent green similar color, and then [x-22,y-22,44,44] that from the position (x-22,y-22) start to the right to extend the length of 44 downward respectively, to get a square, 2 that the thickness of the line degree of thinness, 1 that the hollow square , the same as drawing a circle there.
# Calling the pygame library import pygame import sys # Calling common keyword constants from import QUIT,KEYDOWN #Initialize pygame () # Get access to the display system and create a window screen #Window size 670x670 screen = .set_mode((670,670)) screen_color=[238,154,73]#Set the canvas color, [238,154,73] corresponds to tan. line_color = [0,0,0]# Set the line color, [0,0,0] corresponds to black. def find_pos(x,y):#Find a location that shows where you can land for i in range(27,670,44): for j in range(27,670,44): L1=i-22 L2=i+22 R1=j-22 R2=j+22 if x>=L1 and x<=L2 and y>=R1 and y<=R2: return i,j return x,y while True:# Constant training refreshes the canvas for event in ():# Get the event, if the mouse clicks on the close button in the upper right corner, close the if in (QUIT,KEYDOWN): () (screen_color)#ClearScreen for i in range(27,670,44): # Draw the vertical line first if i==27 or i==670-27:#The edge line is a little thicker (screen,line_color,[i,27],[i,670-27],4) else: (screen,line_color,[i,27],[i,670-27],2) # And then draw the line if i==27 or i==670-27:#The edge line is a little thicker (screen,line_color,[27,i],[670-27,i],4) else: (screen,line_color,[27,i],[670-27,i],2) # Draw a small circle in the center of the board to indicate the center position # (screen, line_color,[27+44*7,27+44*7], 8,0) # Get mouse coordinate information x,y = .get_pos() x,y=find_pos(x,y) (screen,[0 ,229 ,238 ],[x-22,y-22,44,44],2,1) ()#refresh display
Now it's time to drop, and here it's easy because we've got the specific position (x,y), get the left mouse button information, drop if the left button is pressed, and then use the function for drawing a circle to draw a circle at that position.
The throw code is as follows:
keys_pressed = .get_pressed() if keys_pressed[0]==True: print('Indicates that the left mouse button was pressed')
Since we can't drop any more children in this position after we drop a child, we need to store the position where we have already dropped a child, and define over_pos to indicate the position where we have already dropped a child.
The code is as follows:
# Calling the pygame library import pygame import sys # Calling common keyword constants from import QUIT,KEYDOWN #Initialize pygame () # Get access to the display system and create a window screen #Window size 670x670 screen = .set_mode((670,670)) screen_color=[238,154,73]#Set the canvas color, [238,154,73] corresponds to tan. line_color = [0,0,0]# Set the line color, [0,0,0] corresponds to black. def find_pos(x,y):#Find a location that shows where you can land for i in range(27,670,44): for j in range(27,670,44): L1=i-22 L2=i+22 R1=j-22 R2=j+22 if x>=L1 and x<=L2 and y>=R1 and y<=R2: return i,j return x,y def check_over_pos(x,y,over_pos):#Check if the current position has been set for val in over_pos: if val[0][0]==x and val[0][1]==y: return False return True#Saying that there's no drop flag=False tim=0 over_pos=[]# Indicates the position in which the pieces have been placed white_color=[255,255,255]#White color black_color=[0,0,0]#Black color while True:# Constant training refreshes the canvas for event in ():# Get the event, if the mouse clicks on the close button in the upper right corner, close the if in (QUIT,KEYDOWN): () (screen_color)#ClearScreen for i in range(27,670,44): # Draw the vertical line first if i==27 or i==670-27:#The edge line is a little thicker (screen,line_color,[i,27],[i,670-27],4) else: (screen,line_color,[i,27],[i,670-27],2) # And then draw the line if i==27 or i==670-27:#The edge line is a little thicker (screen,line_color,[27,i],[670-27,i],4) else: (screen,line_color,[27,i],[670-27,i],2) # Draw a small circle in the center of the board to indicate the center position # (screen, line_color,[27+44*7,27+44*7], 8,0) # Get mouse coordinate information x,y = .get_pos() x,y=find_pos(x,y) if check_over_pos(x,y,over_pos):# Determine if you can drop a stone, then display (screen,[0 ,229 ,238 ],[x-22,y-22,44,44],2,1) keys_pressed = .get_pressed()# Get mouse button information #The left mouse button is used to delay the time, because the time interval between each loop is very broken, which can lead to the left mouse button being pressed only once, but it is fetched multiple times, thinking that I pressed it multiple times. if keys_pressed[0] and tim==0: flag=True if check_over_pos(x,y,over_pos):#Determine if you can drop, then drop again if len(over_pos)%2==0:#Black over_pos.append([[x,y],black_color]) else: over_pos.append([[x,y],white_color]) #Left mouse button delay function if flag: tim+=1 if tim%200==0:# 200ms delay flag=False tim=0 for val in over_pos:# Show all the pieces that have fallen (screen, val[1],val[0], 20,0) ()#refresh display
Now all that's missing is a judgment function to determine the quintuplets.
def check_win(over_pos):#Judgemental Five in a Row mp=([15,15],dtype=int) for val in over_pos: x=int((val[0][0]-27)/44) y=int((val[0][1]-27)/44) if val[1]==white_color: mp[x][y]=2# means white. else: mp[x][y]=1# indicates a black child for i in range(15): pos1=[] pos2=[] for j in range(15): if mp[i][j]==1: ([i,j]) else: pos1=[] if mp[i][j]==2: ([i,j]) else: pos2=[] if len(pos1)>=5:#Five in a row return [1,pos1] if len(pos2)>=5: return [2,pos2] for j in range(15): pos1=[] pos2=[] for i in range(15): if mp[i][j]==1: ([i,j]) else: pos1=[] if mp[i][j]==2: ([i,j]) else: pos2=[] if len(pos1)>=5: return [1,pos1] if len(pos2)>=5: return [2,pos2] for i in range(15): for j in range(15): pos1=[] pos2=[] for k in range(15): if i+k>=15 or j+k>=15: break if mp[i+k][j+k]==1: ([i+k,j+k]) else: pos1=[] if mp[i+k][j+k]==2: ([i+k,j+k]) else: pos2=[] if len(pos1)>=5: return [1,pos1] if len(pos2)>=5: return [2,pos2] for i in range(15): for j in range(15): pos1=[] pos2=[] for k in range(15): if i+k>=15 or j-k<0: break if mp[i+k][j-k]==1: ([i+k,j-k]) else: pos1=[] if mp[i+k][j-k]==2: ([i+k,j-k]) else: pos2=[] if len(pos1)>=5: return [1,pos1] if len(pos2)>=5: return [2,pos2] return [0,[]]
If the run doesn't work download pip install pygame on pycharm, pip install numpy
My version is pycharm 3.7.
This is the whole content of this article.