Before reading the blog, please click “like” to form a habit and pay attention to it, which is the biggest encouragement to me!!

Chinese chess phase will be played by everyone, whim, thinking about how to use Python to make the Quintessence of Chinese chess ??????

First, let’s look at python as usual:

The quintessence of Chinese chess

  • Step 1: Import the resource bundle
  • Step 2: Initialize
  • Step 3: Define chess names and coordinates
  • Step 4: Draw the board
  • Step 5: Define the subfunction
  • Step 6: Mouse click events

Step 1: Import the resource bundle

This time, the turtle was used again

import turtle
Copy the code

Step 2: Initialize

Initialization starts with the turtle’s pen, and then sets the window’s size, title, and background.

# Initialize pen = turtle.pen ()# Get turtle's pen turtle.setup(714,800)# Set window size turtle.title(" Chinese chess ")# Set window title Turtle.bgcolor ("#F4C79E")# Set window background pen.hideturtle() turtle.tracer(False)Copy the code

Step 3: Define chess names and coordinates

Chinese chess consists of chariot, horse, xiang (xiang), shi (shi), gun, pawn, general and shuai (shuai). Each piece has corresponding coordinates. This is for reference, if there is a problem you can set the corresponding coordinates.

Array = [# A square piece {" text ", "car", "role" : "A", "pix:" (369-330)}, {" text ", "horse", "role" : "A", "pix" : (247.0, 369.0)}, {" text ", "like", "role" : "A", "pix" : (166.0, 369.0)}, {" text ":" gentleman ", "role" : "A", "pix" : (86.0, 368.0)}, {" text ", "will", "role" : "A", "pix" : (5.0, 369.0)}, {" text ":" gentleman ", "role" : "A", "pix" : (79.0, 368.0)}, {" text ", "like", "role" : "A", "pix" : (159.0, 368.0)}, {" text ", "horse", "role" : "A", "pix" : (239.0, 367.0)}, {" text ", "car", "role" : "A", "pix" : (318.0, 369.0)}, {" text ":" single ", "role" : "A", "pix" : (329.0, 126.0)}, {" text ":" single ", "role" : "A", "pix" : (167.0, 126.0)}, {" text ":" single ", "role" : "A", "pix" : (6.0, 126.0)}, {" text ":" single ", "role" : "A", "pix" : (156.0, 126.0)}, {" text ":" single ", "role" : "A", "pix" : (319.0, 126.0)}, {" text ", "gun", "role" : "A", "pix" : (248.0, 209.0)}, {" text ", "gun", "role" : "A", "pix" : (239.0, 208.0)}, # B party pieces {" text ", "car", "role" : "B", "pix" : (330.0, 359.0)}, {" text ", "horse", "role" : "B", "pix" : (247.0, 359.0)}, {" text ":" phase ", "role" : "B", "pix" : (166.0, 359.0)}, {" text ", "shi", "role" : "B", "pix" : (86.0, 359.0)}, {" text ", "handsome", "role" : "B", "pix" : (5.0, 359.0)}, {" text ", "shi", "role" : "B", "pix" : (79.0, 359.0)}, {" text ":" phase ", "role" : "B", "pix" : (159.0, 359.0)}, {" text ", "horse", "role" : "B", "pix" : (239.0, 359.0)}, {" text ", "car", "role" : "B", "pix" : (318.0, 359.0)}, {" text ":" single ", "role" : "B", "pix" : (329.0, 126.0)}, {" text ":" single ", "role" : "B", "pix" : (167.0, 126.0)}, {" text ":" single ", "role" : "B", "pix" : (6.0, 126.0)}, {" text ":" single ", "role" : "B", "pix" : (156.0, 117.0)}, {" text ":" single ", "role" : "B", "pix" : (319.0, 117.0)}, {" text ", "gun", "role" : "B", "pix" : (248.0, 199.0)}, {" text ", "gun", "role" : "B", "pix" : (239.0, 199.0)},]Copy the code

Step 4: Draw the board

As shown below, the chessboard is made up of a grid with a “Chu River — Han Kingdom” in the middle.

Def draw(): Pen.penup () pen.setposition(-360, 402) pen.pendown() pen.color("#6E3F25") pen.width(30) for x in range(1,5): if x % 2 ! = 0: pen.forward(710) else: Pen.penup () pen.setPosition (-330, 370) pen.width(2) pen.pendown() for x in range(9): pen.forward(650) pen.backward(650) pen.right(90) pen.forward(81) pen.left(90) pen.forward(650) pen.left(90) for x in range(8): pen.forward(730) pen.backward(730) pen.left(90) pen.forward(81) pen.right(90) pen.penup() pen.setposition(-280, 6) pen.pendown() pen.pencolor("#F4C79E") pen.right(90) pen.width(79) pen.forward(550) pen.width(1) pen.penup() Color ("#6E3F25") pen. Write (" chouhe ", align="center", font=("Baoli TC", 50, "Bold ") pen.penup() pen.forward(450) pen.write(" hanzao ", align="center", font=("Baoli TC", 50, "Bold")) pen. Penup () for x in [[3.0, 290.0], [4.0, 278.0]] : pen.up() pen.setposition(x) pen.down() pen.setheading(45) pen.pendown() pen.width(2) pen.color("#5E3F25") for x in range(4): pen.forward(114) pen.backward(114) pen.left(90) for x in array: if x["role"] == "A": chess(x["text"], x["pix"], "#A46A0C", "#2F1500") else: Chess (x x [" text "], [" pix] ", "# E69772", "# AB2A0E)" turtle. The update () # refresh the imageCopy the code

Step 5: Define the subfunction

As shown in the picture below, every time we click a piece and drop it, the background will show the status of the piece we click and whether it falls.

Def chess(text, pix, bgcolor, textcolor): "" Pen.penup () pen.setPosition (pix) pen.pendown() pen.color("#6E3F25") pen.dot(70) pen.color(bgcolor) pen.dot(55) pen.color("white") pen.penup() pen.setheading(270) pen.forward(25) pen.color(textcolor) pen.write(text, align="center", font=("Baoli TC", 40, "bold"))Copy the code

Drop situation:

Step 6: Mouse click events

When we click on a piece, the corresponding time is triggered, as shown in the image above. For example, when we click on “pawn”, it will show us which side of the piece we clicked on and what the coordinates of the piece are.

def click(x, y): global priChess if priChess == {}: for z in array: if abs(z["pix"][0] - x) <= 35 and abs(z["pix"][1] - y) <= 35: Print (" find target: ", z) priChess = z pen.penup() pen.setposition(z["pix"]) pen.color("white") pen.penup() pen.setheading(270) pen.forward(25)  pen.write(z["text"], align="center", font=("Baoli TC", 40, "bold")) break else: Print (" pix") priChess["pix"] = (x, y) array.append(priChess) priChess = {} pen.reset() draw()Copy the code

Resource portal

  1. Pay attention to [be a gentle program ape] public account
  2. In [do a tender program ape] public account background reply [Python information] [2020 autumn recruit] can get the corresponding surprise oh!

“❤️ thank you.”

  1. Click “like” to support it, so that more people can see this content.
  2. Share your thoughts with me in the comments section, and record your thought process in the comments section.
  • Excel/Word/CSV with Python
  • Programmers generally like to browse 40 websites, tun so many years, I will not hide private, personally strongly recommend
  • Image encryption and restoration based on chaotic Logistic encryption algorithm
  • Write two dozen lines of code to draw dynamic fireworks in Python
  • AttributeError: ‘Module’ object has no attribute ‘XXXXX’
  • How to parse XML and PDF easily in Python
  • Affine transformations (translation, rotation, scaling, and flipping) in Python
  • ValueError: Not enough values to unpack (Expected 3, got 2)
  • Python implements the raspberry PI camera to continuously record video and send it to the host
  • Image affine transform in Python – Extracting handwritten digital image samples
  • How to play with sound files in Python, cutting sounds into fragments according to the gaps
  • Python implementation of image mask mask processing, super detailed explanation!!
  • QT implements message and file interaction between the client and server
  • ❤️QT implements file interaction between the client and server ❤️