• 【Pygame实战】怀旧经典—这款给娃的棋类游戏,你还记得叫什么吧?(一定要收藏)


    导语

    大家以前应该都听说过一个游戏:叫做走四棋儿

    这款游戏出来到现在时间挺长了,小时候的家乡农村条件有限,附近也没有正式的玩具店能买

    到玩具,因此小朋友们聚在一起玩耍时,其玩具大多都是就地取材的。

    直接在家里的水泥地上用烧完的炭火灰画出几条线,摆上几颗石头子即可。当时的火爆程度可

    谓是达到了一个新的高度。包括我当时也比较喜欢这款游戏。因为时间推移,小时候很多游戏

    都已经在现在这个时代看不到啦!

    今天小编就带大家追忆童年——一起敲一敲《走四棋儿》小游戏,你小时候还玩儿过那些游戏

    呢?(抓石头、跳绳、丢手绢儿 .......捂脸.jpg)

    所有文章完整的素材+源码都在👇👇

    粉丝白嫖源码福利,请移步至CSDN社区或文末公众hao即可免费。

    正文

    一、游戏解说

    “走四儿”大部分活跃在山东济南、聊城、菏泽等地,是一种棋类游戏,特别适合儿童试玩。

    在一个4×4的棋盘上,双方各有4子,分别摆放在棋盘两个最上面的两端线的四个位置上。下图

    就是“走四儿”开局的样子。

    二、游戏规则

    “走四儿”的游戏规则是:

    1.双方轮流走,每一步只能在上下左右中的一个无子的方向上走一个格,不能斜走。如果一方

    无法移动,则由另一方走。

    2.当甲方的一个子移动到一条线上之后,这条线上只有甲方的两个子和乙方的一个子,且甲方

    的这两子相连,乙方的子与甲方那两子中的一个子相连,那么乙方的这个子就被吃掉。

    下图是可以吃子的样式例举:

    3.少于2个子的一方为输者。双方都无法胜对方就可以认为是和棋。

    三、环境安装

     1)素材(图片) 

     2)运行环境

    小编使用的环境:Python3、Pycharm社区版、Pygame、 numpy模块部分自带就不一一 展示啦。 

     模块安装:pip install -i https://pypi.douban.com/simple/+模块名

    四、代码展示

    1. import pygame as pg
    2. from pygame.locals import *
    3. import sys
    4. import time
    5. import numpy as np
    6. pg.init()
    7. size = width, height = 600, 400
    8. screen = pg.display.set_mode(size)
    9. f_clock = pg.time.Clock()
    10. fps = 30
    11. pg.display.set_caption("走四棋儿")
    12. background = pg.image.load("background.png").convert_alpha()
    13. glb_pos = [[(90, 40), (190, 40), (290, 40), (390, 40)],
    14. [(90, 140), (190, 140), (290, 140), (390, 140)],
    15. [(90, 240), (190, 240), (290, 240), (390, 240)],
    16. [(90, 340), (190, 340), (290, 340), (390, 340)]]
    17. class ChessPieces():
    18. def __init__(self, img_name):
    19. self.name = img_name
    20. self.id = None
    21. if self.name == 'heart':
    22. self.id = 2
    23. elif self.name == 'spade':
    24. self.id = 3
    25. self.img = pg.image.load(img_name + ".png").convert_alpha()
    26. self.rect = self.img.get_rect()
    27. self.pos_x, self.pos_y = 0, 0
    28. self.alive_state = True
    29. def get_rect(self):
    30. return (self.rect[0], self.rect[1])
    31. def get_pos(self):
    32. return (self.pos_x, self.pos_y)
    33. def update(self):
    34. if self.alive_state == True:
    35. self.rect[0] = glb_pos[self.pos_y][self.pos_x][0]
    36. self.rect[1] = glb_pos[self.pos_y][self.pos_x][1]
    37. screen.blit(self.img, self.rect)
    38. class Pointer():
    39. def __init__(self):
    40. self.img = pg.image.load("pointer.png").convert_alpha()
    41. self.rect = self.img.get_rect()
    42. self.show = False
    43. self.selecting_item = False
    44. def point_to(self, Heart_Blade_class):
    45. if Heart_Blade_class.alive_state:
    46. self.pointing_to_item = Heart_Blade_class
    47. self.item_pos = Heart_Blade_class.get_rect()
    48. self.rect[0], self.rect[1] = self.item_pos[0], self.item_pos[1] - 24
    49. def update(self):
    50. screen.blit(self.img, self.rect)
    51. class GlobalSituation():
    52. def __init__(self):
    53. self.glb_situation = np.array([[2, 2, 2, 2],
    54. [0, 0, 0, 0],
    55. [0, 0, 0, 0],
    56. [3, 3, 3, 3]], dtype=np.uint8)
    57. self.spade_turn = None
    58. def refresh_situation(self):
    59. self.glb_situation = np.zeros([4, 4], np.uint8)
    60. for i in range(4):
    61. if heart[i].alive_state:
    62. self.glb_situation[heart[i].pos_y, heart[i].pos_x] = heart[i].id
    63. for i in range(4):
    64. if spade[i].alive_state:
    65. self.glb_situation[spade[i].pos_y, spade[i].pos_x] = spade[i].id
    66. for i in range(4):
    67. print(self.glb_situation[i][:])
    68. print('=' * 12)
    69. if self.spade_turn != None:
    70. self.spade_turn = not self.spade_turn
    71. def check_situation(self, moved_item):
    72. curr_pos_x, curr_pos_y = moved_item.get_pos()
    73. curr_pos_col = self.glb_situation[:, curr_pos_x]
    74. curr_pos_raw = self.glb_situation[curr_pos_y, :]
    75. enemy_die = False
    76. if moved_item.id == 2:
    77. if np.sum(curr_pos_col) == 7:
    78. if (curr_pos_col == np.array([0, 2, 2, 3])).all():
    79. enemy_die = True
    80. self.glb_situation[3, curr_pos_x] = 0
    81. for spade_i in spade:
    82. if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 3:
    83. spade_i.alive_state = False
    84. elif (curr_pos_col == np.array([2, 2, 3, 0])).all():
    85. enemy_die = True
    86. self.glb_situation[2, curr_pos_x] = 0
    87. for spade_i in spade:
    88. if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 2:
    89. spade_i.alive_state = False
    90. elif (curr_pos_col == np.array([0, 3, 2, 2])).all():
    91. enemy_die = True
    92. self.glb_situation[1, curr_pos_x] = 0
    93. for spade_i in spade:
    94. if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 1:
    95. spade_i.alive_state = False
    96. elif (curr_pos_col == np.array([3, 2, 2, 0])).all():
    97. enemy_die = True
    98. self.glb_situation[0, curr_pos_x] = 0
    99. for spade_i in spade:
    100. if spade_i.alive_state and spade_i.pos_x == curr_pos_x and spade_i.pos_y == 0:
    101. spade_i.alive_state = False
    102. if np.sum(curr_pos_raw) == 7:
    103. if (curr_pos_raw == np.array([0, 2, 2, 3])).all():
    104. enemy_die = True
    105. self.glb_situation[curr_pos_y, 3] = 0
    106. for spade_i in spade:
    107. if spade_i.alive_state and spade_i.pos_x == 3 and spade_i.pos_y == curr_pos_y:
    108. spade_i.alive_state = False
    109. elif (curr_pos_raw == np.array([2, 2, 3, 0])).all():
    110. enemy_die = True
    111. self.glb_situation[curr_pos_y, 2] = 0
    112. for spade_i in spade:
    113. if spade_i.alive_state and spade_i.pos_x == 2 and spade_i.pos_y == curr_pos_y:
    114. spade_i.alive_state = False
    115. elif (curr_pos_raw == np.array([0, 3, 2, 2])).all():
    116. enemy_die = True
    117. self.glb_situation[curr_pos_y, 1] = 0
    118. for spade_i in spade:
    119. if spade_i.alive_state and spade_i.pos_x == 1 and spade_i.pos_y == curr_pos_y:
    120. spade_i.alive_state = False
    121. elif (curr_pos_raw == np.array([3, 2, 2, 0])).all():
    122. enemy_die = True
    123. self.glb_situation[curr_pos_y, 0] = 0
    124. for spade_i in spade:
    125. if spade_i.alive_state and spade_i.pos_x == 0 and spade_i.pos_y == curr_pos_y:
    126. spade_i.alive_state = False
    127. elif moved_item.id == 3:
    128. if np.sum(curr_pos_col) == 8:
    129. if (curr_pos_col == np.array([0, 3, 3, 2])).all():
    130. enemy_die = True
    131. self.glb_situation[3, curr_pos_x] = 0
    132. for heart_i in heart:
    133. if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 3:
    134. heart_i.alive_state = False
    135. elif (curr_pos_col == np.array([3, 3, 2, 0])).all():
    136. enemy_die = True
    137. self.glb_situation[2, curr_pos_x] = 0
    138. for heart_i in heart:
    139. if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 2:
    140. heart_i.alive_state = False
    141. elif (curr_pos_col == np.array([0, 2, 3, 3])).all():
    142. enemy_die = True
    143. self.glb_situation[1, curr_pos_x] = 0
    144. for heart_i in heart:
    145. if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 1:
    146. heart_i.alive_state = False
    147. elif (curr_pos_col == np.array([2, 3, 3, 0])).all():
    148. enemy_die = True
    149. self.glb_situation[0, curr_pos_x] = 0
    150. for heart_i in heart:
    151. if heart_i.alive_state and heart_i.pos_x == curr_pos_x and heart_i.pos_y == 0:
    152. heart_i.alive_state = False
    153. if np.sum(curr_pos_raw) == 8:
    154. if (curr_pos_raw == np.array([0, 3, 3, 2])).all():
    155. enemy_die = True
    156. self.glb_situation[curr_pos_y, 3] = 0
    157. for heart_i in heart:
    158. if heart_i.alive_state and heart_i.pos_x == 3 and heart_i.pos_y == curr_pos_y:
    159. heart_i.alive_state = False
    160. elif (curr_pos_raw == np.array([3, 3, 2, 0])).all():
    161. enemy_die = True
    162. self.glb_situation[curr_pos_y, 2] = 0
    163. for heart_i in heart:
    164. if heart_i.alive_state and heart_i.pos_x == 2 and heart_i.pos_y == curr_pos_y:
    165. heart_i.alive_state = False
    166. elif (curr_pos_raw == np.array([0, 2, 3, 3])).all():
    167. enemy_die = True
    168. self.glb_situation[curr_pos_y, 1] = 0
    169. for heart_i in heart:
    170. if heart_i.alive_state and heart_i.pos_x == 1 and heart_i.pos_y == curr_pos_y:
    171. heart_i.alive_state = False
    172. elif (curr_pos_raw == np.array([2, 3, 3, 0])).all():
    173. enemy_die = True
    174. self.glb_situation[curr_pos_y, 0] = 0
    175. for heart_i in heart:
    176. if heart_i.alive_state and heart_i.pos_x == 0 and heart_i.pos_y == curr_pos_y:
    177. heart_i.alive_state = False
    178. if enemy_die == True:
    179. self.glb_situation = np.zeros([4, 4], np.uint8)
    180. for i in range(4):
    181. if heart[i].alive_state:
    182. self.glb_situation[heart[i].pos_y, heart[i].pos_x] = heart[i].id
    183. for i in range(4):
    184. if spade[i].alive_state:
    185. self.glb_situation[spade[i].pos_y, spade[i].pos_x] = spade[i].id
    186. for i in range(4):
    187. print(self.glb_situation[i][:])
    188. print('=' * 12)
    189. def check_game_over(self):
    190. heart_alive_num, spade_alive_num = 0, 0
    191. for heart_i in heart:
    192. if heart_i.alive_state:
    193. heart_alive_num += 1
    194. for spade_i in spade:
    195. if spade_i.alive_state:
    196. spade_alive_num += 1
    197. if heart_alive_num <= 1:
    198. print('Spades win!')
    199. GlobalSituation.__init__(self)
    200. Pointer.__init__(self)
    201. chess_pieces_init()
    202. if spade_alive_num <= 1:
    203. print('Hearts win!')
    204. GlobalSituation.__init__(self)
    205. Pointer.__init__(self)
    206. chess_pieces_init()
    207. heart, spade = [None] * 4, [None] * 4
    208. for i in range(4):
    209. heart[i] = ChessPieces('heart')
    210. spade[i] = ChessPieces('spade')
    211. def chess_pieces_init():
    212. for i in range(4):
    213. heart[i].pos_y, heart[i].pos_x = 0, i
    214. spade[i].pos_y, spade[i].pos_x = 3, i
    215. heart[i].alive_state = True
    216. spade[i].alive_state = True
    217. chess_pieces_init()
    218. pointer = Pointer()
    219. situation = GlobalSituation()
    220. def check_click_item(c_x, c_y):
    221. selected_item = None
    222. if situation.spade_turn==None:
    223. for heart_i in heart:
    224. if heart_i.alive_state and heart_i.rect.collidepoint(c_x, c_y):
    225. situation.spade_turn = False
    226. selected_item = heart_i
    227. for spade_i in spade:
    228. if spade_i.alive_state and spade_i.rect.collidepoint(c_x, c_y):
    229. situation.spade_turn = True
    230. selected_item = spade_i
    231. else:
    232. if situation.spade_turn:
    233. for spade_i in spade:
    234. if spade_i.alive_state and spade_i.rect.collidepoint(c_x, c_y):
    235. selected_item = spade_i
    236. else:
    237. for heart_i in heart:
    238. if heart_i.alive_state and heart_i.rect.collidepoint(c_x, c_y):
    239. selected_item = heart_i
    240. return selected_item
    241. def move_to_dst_pos(selected_item, c_x, c_y):
    242. update_situation = False
    243. enemy_exist = False
    244. if selected_item.name == 'heart':
    245. for spade_i in spade:
    246. if spade_i.rect.collidepoint(c_x, c_y) and spade_i.alive_state:
    247. enemy_exist = True
    248. elif selected_item.name == 'spade':
    249. for heart_i in heart:
    250. if heart_i.rect.collidepoint(c_x, c_y) and heart_i.alive_state:
    251. enemy_exist = True
    252. if enemy_exist == False:
    253. delta_y, delta_x = c_y - selected_item.rect[1], c_x - selected_item.rect[0]
    254. if 80 <= abs(delta_x) <= 120 and abs(delta_y) <= 20:
    255. if delta_x < 0:
    256. if selected_item.pos_x > 0:
    257. selected_item.pos_x -= 1
    258. else:
    259. if selected_item.pos_x < 3:
    260. selected_item.pos_x += 1
    261. update_situation = True
    262. if 80 <= abs(delta_y) <= 120 and abs(delta_x) <= 20:
    263. if delta_y < 0:
    264. if selected_item.pos_y > 0:
    265. selected_item.pos_y -= 1
    266. else:
    267. if selected_item.pos_y < 3:
    268. selected_item.pos_y += 1
    269. update_situation = True
    270. return update_situation
    271. while True:
    272. for event in pg.event.get():
    273. if event.type == pg.QUIT:
    274. sys.exit()
    275. elif event.type == pg.MOUSEBUTTONDOWN:
    276. cursor_x, cursor_y = pg.mouse.get_pos()
    277. clicked_item = check_click_item(cursor_x, cursor_y)
    278. if clicked_item != None:
    279. pointer.selecting_item = True
    280. pointer.point_to(clicked_item)
    281. else:
    282. if pointer.selecting_item:
    283. update_situation_flag = move_to_dst_pos(pointer.pointing_to_item, cursor_x, cursor_y)
    284. if update_situation_flag:
    285. situation.refresh_situation()
    286. situation.check_situation(pointer.pointing_to_item)
    287. situation.check_game_over()
    288. pointer.selecting_item = False
    289. screen.blit(background, (0, 0))
    290. for heart_i in heart:
    291. heart_i.update()
    292. for spade_i in spade:
    293. spade_i.update()
    294. if pointer.selecting_item:
    295. pointer.update()
    296. f_clock.tick(fps)
    297. pg.display.update()

    五、效果展示

    ​是不是效果跟上面的示例图很像啦~棋子效果太死板啦!感觉会更加好看点儿撒!小编只是把双

    方的棋子换成了红心💓跟黑桃♠啦!大家可以自己更换的哈~

    走棋的效果我就不一一展示了哈,跟着游戏规则大家可以自己来探索哦!

    总结

    想起这些童年的棋盘游戏真是又兴奋又疲惫,兴奋地是童年趣事那么欢乐,疲惫的是长大后发

    现童年“狼心狗肺”一样的玩耍再也一去不复返。

    好啦!不说那些“伤感”的话题了,游戏源码给你们,开心一下叭👇

    🎯完整的免费源码领取处:找我吖!文末公众hao可自行领取,滴滴我也可!

    🔨推荐往期文章——

    项目0.1  末世生存游戏

    【Pygame实战】末世来临,真正从零开始的残酷生存游戏,你能活多久?

    项目1.0  泡泡机游戏

     【Pygame实战】超有趣的泡泡游戏来袭——愿你童心不泯,永远快乐简单哦~

    项目8.1   《篮球“王子”》小游戏

     【Pygame实战】趣味篮球——迎“篮”而上 ,乐在“球”中,喜欢打篮球的小可爱前来报道~

    项目7.3  想灭病毒保卫城市》游戏

    【Pygame实战】疫情期间给不能出门的你推荐一款爽游 《消灭病毒保卫城市》【强推】愿早日结束

    项目5.8  翻牌小游戏

    Pygame实战:记忆差怎么办?别急,增强记忆力的小游戏送给你~【越玩越上瘾】

    🎄文章汇总——

    项目1.0 Python—2021 |已有文章汇总 | 持续更新,直接看这篇就够了

    (更多内容+源码都在文章汇总哦!!欢迎阅读~)

  • 相关阅读:
    Vue自定义指令
    PMBOK知识点整理【第七章项目成本管理】(49个子过程、输入、工具和技术、输出)
    进程管理(四)
    TIA博途V17中ProDiag功能的使用方法示例(三)文本列表
    ubuntu 20.04 Kimera semantic 运行记录
    错误: 找不到或无法加载主类 IpAddressTest
    sql之每日五题day02--多表联查/聚合函数/多值判断/函数
    第11/100天 阅读笔记
    Configuring HSRP(Hot Standby Routing Protocol)
    线程常用方法与守护线程
  • 原文地址:https://blog.csdn.net/weixin_55822277/article/details/125795519