如何使用pygame和成员喜欢pygame为脚本编写单元测试.鼠标按钮向下



我正在用python制作一个简单的Connect 4游戏,并且正在使用pygame进行GUI,但是我似乎找不到为相同的单元测试编写的方法

这是我的python脚本脚本的链接

链接到我的项目

对于这样的方法,

def play(self, board, players, is_valid_move, make_move, is_winning_move):
        """Method to play the game in GUI using pygame
        Note:
            When playing with AI a mouse click is required to trigger AI move
        Args:
            board (numpy.ndarray): Game board
            players (list): List of players
            is_valid_move (function): Move validator
            make_move (function): Makes move
            is_winning_move (function): Ckeck for winning move"""
        turn = random.randint(0, len(players) - 1)
        while True:
            self.draw(board)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return None
                if event.type == pygame.MOUSEMOTION:
                    self.draw_black_rec()
                    self.draw_player_coin(players[turn].p_id, event)
                pygame.display.update()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    self.draw_black_rec()
                    if players[turn].name == "AI":
                        col = players[turn].get_move()
                    if is_valid_move(col):
                        row = make_move(col, players[turn].p_id)
                    turn = (turn + 1) % len(players)

我希望编写一个测试上述功能的单元测试,如果我不能仅测试整个方法的一部分,它也可以工作。

您可以使用任何必需的属性创建任何类型的pygame.event.Event()对象。使用 pygame.event.post 将事件放入队列中。

例如:

import pygame
pygame.init()    
pygame.event.get()
post_event = pygame.event.Event(pygame.MOUSEBUTTONDOWN, button = 2, pos = (5, 5))
pygame.event.post(post_event)
event = pygame.event.poll()
result = event.type == pygame.MOUSEBUTTONDOWN and event.button == 2 and event.pos == (5, 5)
print(result)
pygame.quit()

最新更新