当前位置:网站首页>[pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)
[pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)
2022-07-25 02:32:00 【Gu Muzi acridine】
Introduction
Everyone should have heard of a game before : be called Play four chess

It's been a long time since this game came out , When I was a child, the rural conditions in my hometown were limited , There is no formal toy store nearby
To toys , So when children get together to play , Most of its toys are made of local materials .
Draw several lines directly on the cement floor at home with the burnt charcoal ash , Just put a few stones . At that time, it was very popular
It is said to have reached a new height . Including I also liked this game at that time . Because time goes by , When I was a child, I played many games
It has been invisible in this era !

Today, Xiaobian will take you to recall your childhood —— Knock together 《 Play four chess 》 Little games , You played those games when you were a child
Well ?( Grab a stone 、 Jump rope 、 Throw away your handkerchief ....... Over your face .jpg)
Complete material for all articles + The source code is in

Text
One 、 Game commentary
“ Go four ” Most of them are active in Jinan, Shandong 、 Liaocheng 、 Heze and other places , It's a board game , Especially suitable for children to try .
In a 4×4 On the chessboard , Each side has its own. 4 Son , Put them on the four positions of the two uppermost ends of the chessboard . The figure below
Namely “ Go four ” How it started .

Two 、 The rules of the game
“ Go four ” The rules of the game are :
1. Both sides take turns , Each step can only take one grid in a childless direction up, down, left and right , Don't go sideways . If one side
Cannot move , Then let the other party go .
2. When a child of Party A moves to an online , There are only two subsidiaries of Party A and one subsidiary of Party B on this line , And Party A
The two sons of are connected , Party B's son is connected with one of Party A's two sons , Then Party B's son will be eaten .
The following figure is an example of the style of eating seeds :

3. Less than 2 One side of the sub is the loser . If neither side can win the other, it can be regarded as a draw .
3、 ... and 、 Environmental installation
1) material ( picture )

2) Running environment
The environment used by Xiaobian :Python3、Pycharm Community Edition 、Pygame、 numpy The module is not included one by one Show it .
Module installation :pip install -i https://pypi.douban.com/simple/+ Module name Four 、 Code display
import pygame as pg
from pygame.locals import *
import sys
import time
import numpy as np
pg.init()
size = width, height = 600, 400
screen = pg.display.set_mode(size)
f_clock = pg.time.Clock()
fps = 30
pg.display.set_caption(" Play four chess ")
background = pg.image.load("background.png").convert_alpha()
glb_pos = [[(90, 40), (190, 40), (290, 40), (390, 40)],
[(90, 140), (190, 140), (290, 140), (390, 140)],
[(90, 240), (190, 240), (290, 240), (390, 240)],
[(90, 340), (190, 340), (290, 340), (390, 340)]]
class ChessPieces():
def __init__(self, img_name):
self.name = img_name
self.id = None
if self.name == 'heart':
self.id = 2
elif self.name == 'spade':
self.id = 3
self.img = pg.image.load(img_name + ".png").convert_alpha()
self.rect = self.img.get_rect()
self.pos_x, self.pos_y = 0, 0
self.alive_state = True
def get_rect(self):
return (self.rect[0], self.rect[1])
def get_pos(self):
return (self.pos_x, self.pos_y)
def update(self):
if self.alive_state == True:
self.rect[0] = glb_pos[self.pos_y][self.pos_x][0]
self.rect[1] = glb_pos[self.pos_y][self.pos_x][1]
screen.blit(self.img, self.rect)
class Pointer():
def __init__(self):
self.img = pg.image.load("pointer.png").convert_alpha()
self.rect = self.img.get_rect()
self.show = False
self.selecting_item = False
def point_to(self, Heart_Blade_class):
if Heart_Blade_class.alive_state:
self.pointing_to_item = Heart_Blade_class
self.item_pos = Heart_Blade_class.get_rect()
self.rect[0], self.rect[1] = self.item_pos[0], self.item_pos[1] - 24
def update(self):
screen.blit(self.img, self.rect)
class GlobalSituation():
def __init__(self):
self.glb_situation = np.array([[2, 2, 2, 2],
[0, 0, 0, 0],
[0, 0, 0, 0],
[3, 3, 3, 3]], dtype=np.uint8)
self.spade_turn = None
def refresh_situation(self):
self.glb_situation = np.zeros([4, 4], np.uint8)
for i in range(4):
if heart[i].alive_state:
self.glb_situation[heart[i].pos_y, heart[i].pos_x] = heart[i].id
for i in range(4):
if spade[i].alive_state:
self.glb_situation[spade[i].pos_y, spade[i].pos_x] = spade[i].id
for i in range(4):
print(self.glb_situation[i][:])
print('=' * 12)
if self.spade_turn != None:
self.spade_turn = not self.spade_turn
def check_situation(self, moved_item):
curr_pos_x, curr_pos_y = moved_item.get_pos()
curr_pos_col = self.glb_situation[:, curr_pos_x]
curr_pos_raw = self.glb_situation[curr_pos_y, :]
enemy_die = False
if moved_item.id == 2:
if np.sum(curr_pos_col) == 7:
if (curr_pos_col == np.array([0, 2, 2, 3])).all():
enemy_die = True
self.glb_situation[3, curr_pos_x] = 0
for spade_i in spade:
if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 3:
spade_i.alive_state = False
elif (curr_pos_col == np.array([2, 2, 3, 0])).all():
enemy_die = True
self.glb_situation[2, curr_pos_x] = 0
for spade_i in spade:
if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 2:
spade_i.alive_state = False
elif (curr_pos_col == np.array([0, 3, 2, 2])).all():
enemy_die = True
self.glb_situation[1, curr_pos_x] = 0
for spade_i in spade:
if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 1:
spade_i.alive_state = False
elif (curr_pos_col == np.array([3, 2, 2, 0])).all():
enemy_die = True
self.glb_situation[0, curr_pos_x] = 0
for spade_i in spade:
if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 0:
spade_i.alive_state = False
if np.sum(curr_pos_raw) == 7:
if (curr_pos_raw == np.array([0, 2, 2, 3])).all():
enemy_die = True
self.glb_situation[curr_pos_y, 3] = 0
for spade_i in spade:
if spade_i.alive_state and spade_i.pos_x == 3 and spade_i.pos_y == curr_pos_y:
spade_i.alive_state = False
elif (curr_pos_raw == np.array([2, 2, 3, 0])).all():
enemy_die = True
self.glb_situation[curr_pos_y, 2] = 0
for spade_i in spade:
if spade_i.alive_state and spade_i.pos_x == 2 and spade_i.pos_y == curr_pos_y:
spade_i.alive_state = False
elif (curr_pos_raw == np.array([0, 3, 2, 2])).all():
enemy_die = True
self.glb_situation[curr_pos_y, 1] = 0
for spade_i in spade:
if spade_i.alive_state and spade_i.pos_x == 1 and spade_i.pos_y == curr_pos_y:
spade_i.alive_state = False
elif (curr_pos_raw == np.array([3, 2, 2, 0])).all():
enemy_die = True
self.glb_situation[curr_pos_y, 0] = 0
for spade_i in spade:
if spade_i.alive_state and spade_i.pos_x == 0 and spade_i.pos_y == curr_pos_y:
spade_i.alive_state = False
elif moved_item.id == 3:
if np.sum(curr_pos_col) == 8:
if (curr_pos_col == np.array([0, 3, 3, 2])).all():
enemy_die = True
self.glb_situation[3, curr_pos_x] = 0
for heart_i in heart:
if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 3:
heart_i.alive_state = False
elif (curr_pos_col == np.array([3, 3, 2, 0])).all():
enemy_die = True
self.glb_situation[2, curr_pos_x] = 0
for heart_i in heart:
if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 2:
heart_i.alive_state = False
elif (curr_pos_col == np.array([0, 2, 3, 3])).all():
enemy_die = True
self.glb_situation[1, curr_pos_x] = 0
for heart_i in heart:
if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 1:
heart_i.alive_state = False
elif (curr_pos_col == np.array([2, 3, 3, 0])).all():
enemy_die = True
self.glb_situation[0, curr_pos_x] = 0
for heart_i in heart:
if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 0:
heart_i.alive_state = False
if np.sum(curr_pos_raw) == 8:
if (curr_pos_raw == np.array([0, 3, 3, 2])).all():
enemy_die = True
self.glb_situation[curr_pos_y, 3] = 0
for heart_i in heart:
if heart_i.alive_state and heart_i.pos_x == 3 and heart_i.pos_y == curr_pos_y:
heart_i.alive_state = False
elif (curr_pos_raw == np.array([3, 3, 2, 0])).all():
enemy_die = True
self.glb_situation[curr_pos_y, 2] = 0
for heart_i in heart:
if heart_i.alive_state and heart_i.pos_x == 2 and heart_i.pos_y == curr_pos_y:
heart_i.alive_state = False
elif (curr_pos_raw == np.array([0, 2, 3, 3])).all():
enemy_die = True
self.glb_situation[curr_pos_y, 1] = 0
for heart_i in heart:
if heart_i.alive_state and heart_i.pos_x == 1 and heart_i.pos_y == curr_pos_y:
heart_i.alive_state = False
elif (curr_pos_raw == np.array([2, 3, 3, 0])).all():
enemy_die = True
self.glb_situation[curr_pos_y, 0] = 0
for heart_i in heart:
if heart_i.alive_state and heart_i.pos_x == 0 and heart_i.pos_y == curr_pos_y:
heart_i.alive_state = False
if enemy_die == True:
self.glb_situation = np.zeros([4, 4], np.uint8)
for i in range(4):
if heart[i].alive_state:
self.glb_situation[heart[i].pos_y, heart[i].pos_x] = heart[i].id
for i in range(4):
if spade[i].alive_state:
self.glb_situation[spade[i].pos_y, spade[i].pos_x] = spade[i].id
for i in range(4):
print(self.glb_situation[i][:])
print('=' * 12)
def check_game_over(self):
heart_alive_num, spade_alive_num = 0, 0
for heart_i in heart:
if heart_i.alive_state:
heart_alive_num += 1
for spade_i in spade:
if spade_i.alive_state:
spade_alive_num += 1
if heart_alive_num <= 1:
print('Spades win!')
GlobalSituation.__init__(self)
Pointer.__init__(self)
chess_pieces_init()
if spade_alive_num <= 1:
print('Hearts win!')
GlobalSituation.__init__(self)
Pointer.__init__(self)
chess_pieces_init()
heart, spade = [None] * 4, [None] * 4
for i in range(4):
heart[i] = ChessPieces('heart')
spade[i] = ChessPieces('spade')
def chess_pieces_init():
for i in range(4):
heart[i].pos_y, heart[i].pos_x = 0, i
spade[i].pos_y, spade[i].pos_x = 3, i
heart[i].alive_state = True
spade[i].alive_state = True
chess_pieces_init()
pointer = Pointer()
situation = GlobalSituation()
def check_click_item(c_x, c_y):
selected_item = None
if situation.spade_turn==None:
for heart_i in heart:
if heart_i.alive_state and heart_i.rect.collidepoint(c_x, c_y):
situation.spade_turn = False
selected_item = heart_i
for spade_i in spade:
if spade_i.alive_state and spade_i.rect.collidepoint(c_x, c_y):
situation.spade_turn = True
selected_item = spade_i
else:
if situation.spade_turn:
for spade_i in spade:
if spade_i.alive_state and spade_i.rect.collidepoint(c_x, c_y):
selected_item = spade_i
else:
for heart_i in heart:
if heart_i.alive_state and heart_i.rect.collidepoint(c_x, c_y):
selected_item = heart_i
return selected_item
def move_to_dst_pos(selected_item, c_x, c_y):
update_situation = False
enemy_exist = False
if selected_item.name == 'heart':
for spade_i in spade:
if spade_i.rect.collidepoint(c_x, c_y) and spade_i.alive_state:
enemy_exist = True
elif selected_item.name == 'spade':
for heart_i in heart:
if heart_i.rect.collidepoint(c_x, c_y) and heart_i.alive_state:
enemy_exist = True
if enemy_exist == False:
delta_y, delta_x = c_y - selected_item.rect[1], c_x - selected_item.rect[0]
if 80 <= abs(delta_x) <= 120 and abs(delta_y) <= 20:
if delta_x < 0:
if selected_item.pos_x > 0:
selected_item.pos_x -= 1
else:
if selected_item.pos_x < 3:
selected_item.pos_x += 1
update_situation = True
if 80 <= abs(delta_y) <= 120 and abs(delta_x) <= 20:
if delta_y < 0:
if selected_item.pos_y > 0:
selected_item.pos_y -= 1
else:
if selected_item.pos_y < 3:
selected_item.pos_y += 1
update_situation = True
return update_situation
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
elif event.type == pg.MOUSEBUTTONDOWN:
cursor_x, cursor_y = pg.mouse.get_pos()
clicked_item = check_click_item(cursor_x, cursor_y)
if clicked_item != None:
pointer.selecting_item = True
pointer.point_to(clicked_item)
else:
if pointer.selecting_item:
update_situation_flag = move_to_dst_pos(pointer.pointing_to_item, cursor_x, cursor_y)
if update_situation_flag:
situation.refresh_situation()
situation.check_situation(pointer.pointing_to_item)
situation.check_game_over()
pointer.selecting_item = False
screen.blit(background, (0, 0))
for heart_i in heart:
heart_i.update()
for spade_i in spade:
spade_i.update()
if pointer.selecting_item:
pointer.update()
f_clock.tick(fps)
pg.display.update()
5、 ... and 、 Effect display

Is the effect very similar to the above example ~ The chess piece effect is too rigid ! It will look better ! Xiaobian just put a pair
Fang The chess pieces are replaced by hearts and spades ! You can change it yourself ~
I won't show the effect of playing chess one by one , Follow the rules of the game and you can explore by yourself !
summary
Thinking of these childhood board games is really exciting and tired , Excited is childhood fun so happy , Tired is the hair after growing up
Now childhood “ Brutal and cold blooded ” The same play will never return .
All right. ! Don't say that “ sad ” The topic of , Game source code for you , Have a good time
Complete free source code collection office : Find me ! Public at the end of the article hao You can get it by yourself , Didi, I can also !
I recommend previous articles ——
project 0.1 Eschatological survival game
project 1.0 Bubble machine games
project 8.1 《 Basketball “ prince ”》 Little games
project 7.3 Want to kill the virus and defend the city 》 game
project 5.8 Flop games
A summary of the article ——
project 1.0 Python—2021 | Summary of existing articles | Continuous updating , Just read this article directly
( More + The source code is summarized in the article !! Welcome to ~)

边栏推荐
- Why can't reading more books improve your writing?
- Cloudrev deploy your own public cloud disk (pagoda installation method)
- Talk about what's going on with C # backstage GC?
- YuQue - a useful tool for document writing and knowledge precipitation
- Gbase 8s how to query relational databases in sentences to select sample syntax and results of data from complex types
- ByteDance confirmation will be self-developed chip: for internal use only; Musk: I have uploaded my brain to the cloud; Go language product head leaves | geek headline
- Focus on improving women's and children's sense of gain, happiness and security! In the next ten years, Guangzhou Women's and children's undertakings will make such efforts
- Let's customize the loader
- Scalar, vector, matrix calculus
- How to upload files to the server
猜你喜欢

Why can't reading more books improve your writing?
![ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience](/img/26/18fd3106f887f503479f081e29dac3.jpg)
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience

After six years of precipitation, new consumption has found a digital transformation paradigm

"I gave up programming and wrote a 1.3 million word hard science fiction."

Google launched another "man grabbing war" for core making, and Intel's 17 year veteran joined!

Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!

Simulate the implementation of strstr

Yunyuanyuan (VIII) | Devops in depth Devops

Today in history: the kotlin language was published for the first time; The father of the IMAP agreement was born; CT imaging achieves a new breakthrough

Example demonstration of "uncover the secrets of asp.net core 6 framework" [02]: application development based on routing, MVC and grpc
随机推荐
Explorer TSSD 2019 software installation package download and installation tutorial
ByteDance confirmation will be self-developed chip: for internal use only; Musk: I have uploaded my brain to the cloud; Go language product head leaves | geek headline
Cookies and sessions
[TinyML]EfficientFormer:Vision Transformers at MobileNet Speed
Custom types in C language
After six years of precipitation, new consumption has found a digital transformation paradigm
On Calc optimization of calcite
Summary and sorting of XSS (cross site script attack) related content
Gerrit statistics script
Detailed explanation of manually writing servlet in idea
Genesis, the world's first "consumption investment" public chain, was invited to participate in consensus 2022
Gbase 8s how to query relational databases in sentences to select sample syntax and results of data from complex types
A bit of knowledge - websites about scripts
Details of C language compilation preprocessing and comparison of macros and functions
CSRF attack principle scenario
July 8, 2022
B2B e-commerce trading platform of heavy metal industry: breaking the state of data isolation and improving the benefits of heavy metal industry
Large number processing -- use case
"I gave up programming and wrote a 1.3 million word hard science fiction."
Full analysis of new functions of report design tool FastReport online designer v2022.1