当我配对所有配对时,如何结束记忆游戏?

时间:2018-09-19 07:59:27

标签: python python-3.x while-loop counter

我几乎完成了所有代码。我的问题是我在定义clicks()下实现的计数器不起作用。我有16个项目,因此当我进行颜色比较的相等性运行8次时,我希望该功能停止。我剪切了很多不相关的代码,主要重点是clicks()的第一个if语句下。

import graphics as G
import random

WINDOW_WIDTH = 200
WINDOW_HEIGHT = 200
win = G.GraphWin('Memory Game', WINDOW_WIDTH, WINDOW_HEIGHT)

def run_game():
    random_assignment()
    clicks()
        #if count == 8:
        #    game_running = False
        #if clicks() == True:
        #    count += 1
        #if count == 8:
        #    game_running = False
    #if game_running = False:


def clicks():
    game_running = True
    while game_running:
        first_click = win.getMouse()
        x_cell1 = int(first_click.getX()//50)
        y_cell1 = int(first_click.getY()//50)
        (first_r, first_c) = click_loc(first_click)
        first_r.undraw()
        second_click = win.getMouse()
        x_cell2 = int(second_click.getX()//50)
        y_cell2 = int(second_click.getY()//50)
        (second_r, second_c) = click_loc(second_click)
        second_r.undraw()
        rgb1 = circles[y_cell1][x_cell1]
        rgb2 = circles[y_cell2][x_cell2]
        count = 0
        if rgb1[0] == rgb2[0] and rgb1[1] == rgb2[1] and rgb1[2] == rgb2[2]:
            count += 1
        elif count == 8:
            game_running = False
        else:
            first_r.draw(win)
            second_r.draw(win)
    win.close()
def click_loc(click):
    x_cell = int(click.getX()//50)
    y_cell = int(click.getY()//50)
    (r, c) = board[x_cell][y_cell]
    return (r, c)


run_game()

2 个答案:

答案 0 :(得分:0)

要使while循环正常工作,您想将计数器的初始化(count = 0)放在while循环之外。

此外,假设您想在计数达到8时退出循环,则需要允许它检查计数(if count >= 8)。使用elif意味着如果if条件为True,则不会选中此选项。

def clicks():
    game_running = True
    count = 0 # Initialize count before while loop
    while game_running:
        first_click = win.getMouse()
        x_cell1 = int(first_click.getX()//50)
        y_cell1 = int(first_click.getY()//50)
        (first_r, first_c) = click_loc(first_click)
        first_r.undraw()
        second_click = win.getMouse()
        x_cell2 = int(second_click.getX()//50)
        y_cell2 = int(second_click.getY()//50)
        (second_r, second_c) = click_loc(second_click)
        second_r.undraw()
        rgb1 = circles[y_cell1][x_cell1]
        rgb2 = circles[y_cell2][x_cell2]
        if rgb1[0] == rgb2[0] and rgb1[1] == rgb2[1] and rgb1[2] == rgb2[2]:
            count += 1
        if count >= 8: # use if instead of elif here 
            game_running = False
        else:
            first_r.draw(win)
            second_r.draw(win)
    win.close()

作为次要建议,在您的情况下不重要,但我发现更可靠的建议是使用“等于或大于” >=,而不是“等于” ==。 / p>

答案 1 :(得分:0)

首先确保count = 0不在while循环内,因为您在迭代时不断对其进行重置。 其次,将elif count == 8更改为if count == 8,因为使用elif
当语句中的计数实际达到8时,它可能会跳过elif count == 8

if rgb1[0] == rgb2[0] and rgb1[1] == rgb2[1] and rgb1[2] == rgb2[2]: count += 1

因此将其替换为if可以确保它将检查计数器状态。

最终代码如下:

def clicks():
count = 0
game_running = True
while game_running:
    first_click = win.getMouse()
    x_cell1 = int(first_click.getX()//50)
    y_cell1 = int(first_click.getY()//50)
    (first_r, first_c) = click_loc(first_click)
    first_r.undraw()
    second_click = win.getMouse()
    x_cell2 = int(second_click.getX()//50)
    y_cell2 = int(second_click.getY()//50)
    (second_r, second_c) = click_loc(second_click)
    second_r.undraw()
    rgb1 = circles[y_cell1][x_cell1]
    rgb2 = circles[y_cell2][x_cell2]
    if rgb1[0] == rgb2[0] and rgb1[1] == rgb2[1] and rgb1[2] == rgb2[2]:
        count += 1
    if count == 8: 
        game_running = False
    else:
        first_r.draw(win)
        second_r.draw(win)
win.close()