显示走棋方

1.1 流程

想要实现交替显示走棋方功能,其流程如下:

  1. 显示当前走棋方
  2. 检测当前走棋是哪一方
  3. 落子后交换走棋方

1.2 显示默认走棋方

class Game(object):
    """
    游戏类
    """

    def __init__(self, screen):
        self.screen = screen
        self.player = "r"  # 默认走棋的为红方r
        self.player_tips_r_image = pygame.image.load("images/red.png")
        self.player_tips_r_image_topleft = (550, 500)
        self.player_tips_b_image = pygame.image.load("images/black.png")
        self.player_tips_b_image_topleft = (550, 100)

    def show(self):
        if self.player == "r":
            self.screen.blit(self.player_tips_r_image, self.player_tips_r_image_topleft)
        else:
            self.screen.blit(self.player_tips_b_image, self.player_tips_b_image_topleft)

image-20210821194919169

# 创建游戏对象(像当前走棋方、游戏是否结束等都封装到这个对象中)
game = Game(screen)

image-20210821194948123

# 显示游戏相关信息
game.show()

image-20210821194905197

1.3 运行效果

img

当重新运行程序之后,每次我们都设置红方为先要走棋的一方,所以看到上述的”红方走“的提示

Copyright © itprojects.cn 2021 all right reserved,著作权归itprojects.cn所有,由王铭东编写该文件修订时间: 2021-08-21 20:36:04