Python+Pygame实现之走四棋儿游戏的实现
作者:木木子学python 发布时间:2023-08-29 21:34:55
导语
大家以前应该都听说过一个游戏:叫做走四棋儿
这款游戏出来到现在时间挺长了,小时候的家乡农村条件有限,附近也没有正式的玩具店能买到玩具,因此小朋友们聚在一起玩耍时,其玩具大多都是就地取材的。
直接在家里的水泥地上用烧完的炭火灰画出几条线,摆上几颗石头子即可。当时的火爆程度可谓是达到了一个新的高度。包括我当时也比较喜欢这款游戏。因为时间推移,小时候很多游戏都已经在现在这个时代看不到啦!
今天小编就带大家追忆童年——一起敲一敲《走四棋儿》小游戏,你小时候还玩儿过那些游戏呢?(抓石头、跳绳、丢手绢儿 .......捂脸.jpg)
一、游戏解说
“走四儿”大部分活跃在山东济南、聊城、菏泽等地,是一种棋类游戏,特别适合儿童试玩。
在一个4×4的棋盘上,双方各有4子,分别摆放在棋盘两个最上面的两端线的四个位置上。下图
就是“走四儿”开局的样子。
二、游戏规则
“走四儿”的游戏规则是:
1.双方轮流走,每一步只能在上下左右中的一个无子的方向上走一个格,不能斜走。如果一方无法移动,则由另一方走。
2.当甲方的一个子移动到一条线上之后,这条线上只有甲方的两个子和乙方的一个子,且甲方的这两子相连,乙方的子与甲方那两子中的一个子相连,那么乙方的这个子就被吃掉。
下图是可以吃子的样式例举:
3.少于2个子的一方为输者。双方都无法胜对方就可以认为是和棋。
三、环境安装
1)素材(图片)
2)运行环境 小编使用的环境:Python3、Pycharm社区版、Pygame、 numpy模块部分自带就不一一 展示啦。
模块安装:pip install -i https://pypi.douban.com/simple/+模块名
四、代码展示
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("走四棋儿")
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()
五、效果展示
是不是效果跟上面的示例图很像啦~棋子效果太死板啦!感觉会更加好看点儿撒!小编只是把双方的棋子换成了红心💓跟黑桃♠啦!大家可以自己更换的哈~
走棋的效果我就不一一展示了哈,跟着游戏规则大家可以自己来探索哦!
来源:https://juejin.cn/post/7121295376333668359


猜你喜欢
- 总有一些程序在windows平台表现不稳定,动不动一段时间就无响应,但又不得不用,每次都是发现问题了手动重启,现在写个脚本定时检测进程是否正
- 最近一直跟着廖大在学Python,关于分布式进程的小例子挺有趣的,这里做个记录。分布式进程Python的multiprocessing模块不
- 假如某个电脑生产商,它的数据库中保存着整机和配件的产品信息。用来保存整机产品信息的表叫做pc;用来保存配件供货信息的表叫做parts。在pc
- 使用cpan安装Net::SSH::Perl:cpan>install Net::SSH::Perl期间遇到了一些问题,记录在此,以备
- match()函数的使用。以及从文本中提取数据的方法。在学习re模块的相关函数前应了解正则表达式的特殊字符准备一个要爬取的文本文档:直接从某
- 这篇文章主要介绍了python类继承和多态原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- Git的使用基本教程git安装官网 msysgit.github.io(百度搜索git下载地址也行)下载git安装(路径选择你的路径或者默认
- <html><head><meta http-equiv="Content-Type" c
- 1.决定大小写是否敏感的参数在 MySQL 中,数据库与 data 目录中的目录相对应。数据库中的每个表都对应于数据库目录中的至少一个文件(
- on里面的xlrd模块详解(一) - 疯了的小蜗 - 博客园【内容】:>那我就一下面积个问题对xlrd模块进行学习一下:什么是xlrd
- 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想。OOP把对象作为程序的基本单元,一个
- 在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的 * ,从而保证爬虫快
- 1. 导入各种模块基本形式为:import 模块名from 某个文件 import 某个模块2. 导入数据(以两类分类问题为例,即numCl
- 就如平时我们很在分页中看到的,分页的时候返回的不仅包括查询的结果集(List),而且还包括总的页数(pageNum)、当前第几页(pageN
- CKEditor官方演示是有上传图片和浏览服务器文件功能的,但是我们自己下载回来的却没有这两个功能…… 其实还需要下载另外一个组件:CKFi
- 大部分情况下,这种动态生成的sql查询语句写法如下: 代码如下:select A表.字段1,A表.字段2,B表.字段返回,C表.字段返回 f
- Mako是一个模板库。一种嵌入式的语言,能够实现简化组件布局以及继承,主要的用途也是和作用域有关,但是效果是最直接切灵活的,这些都是mako
- 前言本文主要介绍通过Python提取csv文件中数据,并对数据进行处理。编译器:Anaconda3 语言:Python3一、使用pandas
- 实例 1基本的XPath语法类似于在一个文件系统中定位文件,如果路径以斜线 / 开始, 那么该路径就表示到一个元素的绝对路径。/AAA选择根
- 我在网上查找了下接口测试相关的资料,大都重点是以数据驱动的形式,将用例维护在文本或表格中,而没有说明怎么样去生成想要的用例,问题:测试接口时