怎么使用Python+Pygame实现走四棋儿游戏
一、游戏解说
“走四儿”大部分活跃在山东济南、聊城、菏泽等地,是一种棋类游戏,特别适合儿童试玩。
在一个4×4的棋盘上,双方各有4子,分别摆放在棋盘两个最上面的两端线的四个位置上。下图
就是“走四儿”开局的样子。
二、游戏规则
“走四儿”的游戏规则是:
立即学习“Python免费学习笔记(深入)”;
1.双方轮流走,每一步只能在上下左右中的一个无子的方向上走一个格,不能斜走。如果一方无法移动,则由另一方走。
2.当甲方的一个子移动到一条线上之后,这条线上只有甲方的两个子和乙方的一个子,且甲方的这两子相连,乙方的子与甲方那两子中的一个子相连,那么乙方的这个子就被吃掉。
下图是可以吃子的样式例举:
3.少于2个子的一方为输者。双方都无法胜对方就可以认为是和棋。
三、环境安装
1)素材(图片)
2)运行环境 小编使用的环境:Python3、Pycharm社区版、Pygame、 numpy模块部分自带就不一一 展示啦。
模块安装:pip install -i https://pypi.douban.com/simple/+模块名
四、代码展示
import pygame as pgfrom pygame.locals import *import sysimport timeimport numpy as nppg.init()size = width, height = 600, 400screen = pg.display.set_mode(size)f_clock = pg.time.Clock()fps = 30pg.display.set_caption("走四棋儿")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 0: selected_item.pos_x -= 1 else: if selected_item.pos_x 0: selected_item.pos_y -= 1 else: if selected_item.pos_y <h3>五、效果展示</h3><p><img src="https://img.php.cn/upload/article/000/887/227/168412579024135.png" alt="怎么使用Python+Pygame实现走四棋儿游戏"></p>