源码及资源请前往github获取
如果觉得还不错的话,请赏个 star 呗。
继续贴出流程图:
从流程图中可以看出,首先判断棋子有没有被选中(鼠标点击位置是否有棋子)
pygame 提供了现成的方法:
def is_chess_clicked(chess_list,event):
for each in chess_list:
if (each.rect.collidepoint(event.pos)):#----------------现成的方法
return each
return None
如果是 hidden 状态,则进行翻转,翻转之后该棋子状态变为 active,这样翻转就实现了
elif selected.state == ChessPieces.HIDDEN_STATE:
# 翻转
selected.state = ChessPieces.ACTIVE_STATE
然后我们来看棋子的移动功能的实现
移动棋子有两个步骤:
1、选中一枚本方棋子(选中之后 select_chess就被赋值了)
2、第二次点击位置没有棋子,并且满足选中棋子的移动规则
棋子的移动规则:
棋子的移动只能向左,向右,向上,向下移动。
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:# 按下鼠标左键
#print(event.pos)
selected = is_chess_clicked(chess_list,event)
#print(selected)
if selected is not None:#-------------------------------------- 点击到了棋子
# 本次点击点击到了棋子
if selected.state == ChessPieces.CHOOSED_STATE:
pass
elif selected.state == ChessPieces.ACTIVE_STATE:
if player_role == selected.role:#---------------------- 点击的是自己的棋子
# 当前用户点击自己的棋子
select_chess = selected # ------------------------- 关键
selected.state = ChessPieces.ACTIVE_STATE
else:
# 当前用户点击别人的棋子
pass
elif selected.state == ChessPieces.HIDDEN_STATE:
# 翻转
selected.state = ChessPieces.ACTIVE_STATE
else:
# 本次点击没有点击棋子,只是点击到了棋盘
print('本次点击没有点击棋子,只是点击到了棋盘')
print(select_chess)
if select_chess is not None:
# 判断被选中的棋子是否可以移动到当前位置
if select_chess.move(event.pos):
select_chess=None #------------------- 如果成功发生了移动,则该轮操作完成
棋子是否可以移动到鼠标点击的当前位置,怎么来判断呢?
思路如下:
如果鼠标点击位置在对应的区域内,就修改选中对象的位置,这样就实现了棋子的移动。
def move(self,pos):
return can_move_one_step(self,pos)
def can_move_one_step(self,pos):
# 首先判断移动方向,然后进行移动
# 判断移动方向
# 判断是否在棋盘之内
if pos[0] < 80 or pos[0] > 399 or pos[1] < 63 or pos[1] > 596:
print("点击超出了范围")
elif self.rect.left - 90 < pos[0] < self.rect.left - 50 and self.rect.top < pos[1] < self.rect.top + 50:
# 需要向左移动一位
self.rect.left -= 90
print('需要向左移动一位')
return True
elif self.rect.left < pos[0] < self.rect.left + 40 and self.rect.top - 70 < pos[1] < self.rect.top - 40:
# 需要向上移动一位
self.rect.top -= 71
print('需要向上移动一位')
return True
elif self.rect.left + 90 < pos[0] < self.rect.left + 130 and self.rect.top < pos[1] < self.rect.top + 40:
# 需要向右移动一位
self.rect.left += 90
print('需要向右移动一位')
return True
elif self.rect.left < pos[0] < self.rect.left + 40 and self.rect.top + 70 < pos[1] < self.rect.top + 110:
# 需要向下移动一位
self.rect.top += 71
print('需要向下移动一位')
return True
这款小游戏是一个双人对战的游戏,那总有一个标志来决定每一方玩家行为的开始和结束。再通俗点儿,就是什么标志着行为的开始,那些标志着行为的结束(交接)。思考之后,大概是以下三种情况:
- 翻牌完毕
- 棋子移动完毕
- 棋子吃子完毕
注意,都是要完毕,意味着三个动作必须有一个动作完成才能交接,我这儿使用 selelct_chess 变量来判定
select_chess 存储的是要移动或要去吃子的棋子,一旦一个动作完成,我们就将 sellect_chess 置为 None。
if selected is not None:
# 本次点击点击到了棋子
if selected.state == ChessPieces.CHOOSED_STATE:
pass
elif selected.state == ChessPieces.ACTIVE_STATE:
if player_role == selected.role:
# 当前用户点击自己的棋子
select_chess = selected
selected.state = ChessPieces.ACTIVE_STATE
# 判断被选中的棋子是否可以移动到当前位置
if select_chess.move(event.pos):
operation_completed()
select_chess=None