#🔒 Game of pexeso (match 2) doesn't work as supposed

12 messages · Page 1 of 1 (latest)

boreal oriole
#

I have a problem with my code. I'm trying to program a game of pexeso (matching 2 cards and gaining points for each pair) but for some reason the cards do whatever they want to. I'm relatively new to programming so I don't know how to find a problem in the code. The cards are supposed to turn to a picture and reset if they are not the same (cards still reset if of same kind). When I click 2 cards there is supposed to be a delay so shouldn't be able to click other cards during the delay (I still can click them).
I'm using tkinter and PIL

reef voidBOT
#

@boreal oriole

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

boreal oriole
#

import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import random
import time

image_paths = ["rezistor.png","kondenzator.png","uzemnenie.png",
"dioda.png","segmentovka.png","tranzistor.png",
"voltmeter.png"]

spstlogo_path = "spstlogo.jpg"

spstlogo_path = spstlogo_path

otocene_karty = []
stav_karty = []
images = []
labels = []
par_kariet = set()

def zobraz_obrazky():
global images, stav_karty, par_kariet, otocene_karty

    random.shuffle(image_paths)
    zdvojene_image_paths = image_paths * 2
    random.shuffle(zdvojene_image_paths)
    spst_image = Image.open(spstlogo_path).resize((200,200))
    spst_tk = ImageTk.PhotoImage(spst_image)

    ine_obrazky = []
    for path in zdvojene_image_paths:
        img = Image.open(path).resize((200,200))
        ine_obrazky.append(ImageTk.PhotoImage(img))  

    images.clear()


    for ine_img in ine_obrazky:
        images.append((spst_tk, ine_img))

    stav_karty.clear()
    stav_karty.extend([0] * len(zdvojene_image_paths))
    par_kariet.clear()
    otocene_karty.clear()
reef voidBOT
#

Hey @boreal oriole!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
boreal oriole
#

def update_obrazku(label_index):
global otocene_karty, par_kariet
if label_index in par_kariet or label_index in otocene_karty:
return
stav_karty[label_index] = 1
labels[label_index].config(image=images[label_index][1])
labels[label_index].image = images[label_index][1]
otocene_karty.append(label_index)

    if len(otocene_karty) == 2:
        root.after(1000, par)

def par():
global otocene_karty, par_kariet

if len(otocene_karty) != 2:
    return

idx1, idx2 = otocene_karty
if images[idx1][1] == images [idx2][1]:
    par_kariet.add(idx1)
    par_kariet.add(idx2)
    print("Našiel si pár!")
else:
    stav_karty[idx1] = 0
    stav_karty[idx2] = 0
    labels[idx1].config(image=images[idx1][0])
    labels[idx1].image = images[idx2][0]
    labels[idx2].config(image=images[idx1][0])
    labels[idx2].image = images[idx2][0]
    print("Skús znova!")

otocene_karty.clear()

def klik_obrazku(event, label_index):
if label_index not in par_kariet:
update_obrazku(label_index)
def hlavne_okno(root):
global labels
for label in labels:
label.destroy()
labels.clear()

    cols = 5
    for i, (spstlogo, ine_obrazky) in enumerate(images):
        label = tk.Label(root, image=images[i][0])
        label.bind("<Button-1>", lambda event, idx=i: klik_obrazku(event, idx))
        row, col = divmod(i, cols)
        label.grid(row=row, column=col, padx=10, pady=10) 
        labels.append(label)
    btn_restart = tk.Button(root, text="Hrať znova", command=lambda: [zobraz_obrazky(), hlavne_okno(root)])
    btn_restart.grid(row=len(images) // cols + 1, column=0, columnspan=cols, pady=20) 

if name == "main":
root = tk.Tk()
root.title("Obrázky vedľa seba")
root.geometry("1125x800")
zobraz_obrazky()
hlavne_okno(root)
root.mainloop()

errant dirge
#

This is potentially an old and annoying tkinter bug

#

load your images somewhere where they're accessible for the entire program

#

like storing the list of images as a global variable and not clearing it until after the mainloop is done

boreal oriole
#

Those images are saved in "image_paths" at the start of the program, that means they should be visible everywhere or am I wrong ?
And I dont think they are being cleared somewhere, Im just clearing the states of them so I can turn those cards

boreal oriole
#

In this clip, you can see how the code works:

https://outplayed.tv/media/n4rjLR

If I find a pair it still resets and I shouldnt be able to click multiple images but I still can

Outplayed - The ultimate capture app for gamers

Outplayed - The ultimate capture app for gamers. While playing, it automatically captures your best moments and biggest plays. When the match is over, relive your best (and not so best) moments by watching them in the match timeline.

â–¶ Play video
reef voidBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.