【问题标题】:How can I get my memory game to end when I have matched all pairs?当我匹配完所有配对后,如何让我的记忆游戏结束?
【发布时间】:2018-09-19 07:59:26
【问题描述】:

我已经完成了几乎所有的代码。我的问题是我在定义 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()

【问题讨论】:

  • 我怀疑这与您在 while 循环中设置 count=0 有关 => 计数永远不会达到 8?
  • 尝试将“elif count == 8:”更改为“if count ==8”
  • 那你为什么要重置计数器?

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


【解决方案1】:

要使 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()

作为一个小建议,在您的情况下应该不重要,但我发现更可靠的是使用“等于或大于”>= 而不是“等于”==

【讨论】:

    【解决方案2】:

    首先确保 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      相关资源
      最近更新 更多