怎么使用Python+Pygame实现走四棋儿游戏
来源:亿速云
2024-04-24 18:51:31
0浏览
收藏
IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《怎么使用Python+Pygame实现走四棋儿游戏》,聊聊,我们一起来看看吧!
一、游戏解说
“走四儿”大部分活跃在山东济南、聊城、菏泽等地,是一种棋类游戏,特别适合儿童试玩。
在一个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()五、效果展示

以上就是《怎么使用Python+Pygame实现走四棋儿游戏》的详细内容,更多关于Python,Pygame的资料请关注golang学习网公众号!
版本声明
本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除
mac管理员密码关闭?
- 上一篇
- mac管理员密码关闭?
- 下一篇
- PHP 函数的安全性与代码的可读性和可维护性之间的权衡
查看更多
最新文章
-
- 文章 · python教程 | 19分钟前 |
- Python如何搭建个人知识库系统并实现自动更新【教学】
- 304浏览 收藏
-
- 文章 · python教程 | 33分钟前 |
- 如何使用Python计算数据相似度?余弦定理实现
- 161浏览 收藏
-
- 文章 · python教程 | 38分钟前 |
- FastAPI与HTMX集成:从JSON响应中精确渲染特定数据
- 284浏览 收藏
-
- 文章 · python教程 | 43分钟前 |
- python中使用logging的好处
- 494浏览 收藏
-
- 文章 · python教程 | 45分钟前 |
- 如何在Python中使用正则表达式分割字符串?
- 182浏览 收藏
-
- 文章 · python教程 | 1小时前 |
- 如何用Python实现可解释AI_模型解释工具使用方法【技巧】
- 128浏览 收藏
-
- 文章 · python教程 | 1小时前 |
- Python自动化统计团队工作量并生成可视化仪表盘的脚本方案【指导】
- 131浏览 收藏
-
- 文章 · python教程 | 1小时前 |
- Python脚本如何实现大规模日志批量格式化与重写【技巧】
- 316浏览 收藏
-
- 文章 · python教程 | 9小时前 |
- LangchainFAISS内存泄漏解决技巧
- 407浏览 收藏
-
- 文章 · python教程 | 9小时前 |
- Python实现URL编码解码方法
- 163浏览 收藏
-
- 文章 · python教程 | 9小时前 |
- Pandas多列创建方法与常见警告解决
- 316浏览 收藏
-
- 文章 · python教程 | 9小时前 |
- Python猜字游戏修复与常见错误解析
- 135浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- ChatExcel酷表
- ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
- 3365次使用
-
- Any绘本
- 探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
- 3574次使用
-
- 可赞AI
- 可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
- 3607次使用
-
- 星月写作
- 星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
- 4735次使用
-
- MagicLight
- MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
- 3979次使用
查看更多
相关文章
-
- Flask框架安装技巧:让你的开发更高效
- 2024-01-03 501浏览
-
- Django框架中的并发处理技巧
- 2024-01-22 501浏览
-
- 提升Python包下载速度的方法——正确配置pip的国内源
- 2024-01-17 501浏览
-
- Python与C++:哪个编程语言更适合初学者?
- 2024-03-25 501浏览
-
- 品牌建设技巧
- 2024-04-06 501浏览

