patternpythonMinor
First Memory Game with PyQt and OOP
Viewed 0 times
withpyqtgamefirstmemoryandoop
Problem
So this is my very first time in this site. I have written this very basic Python code with PyQt5 to create a Memory Game. As I am not very familiar with OOP (but I'm trying to be!), I hope somebody could point me to where I could improve my game.
As it stands, the whole thing is working but it lacks of a pretty template (this is something I am going to improve in a second phase). What I am more interested in right now is to understand if my code could be better organized somehow (e.g. by splitting it into different classes/subclasses, instead of the many methods present).
The game works like this:
Basically I am kind of doing everything using images in a Qgridlayout, which are put in buttons with event listeners. When an "onclick" signal is emitted, the
One last thing: the cards (images) are not shuffled right now, I still need to implement this feature (otherwise the game would be completely boring...).
```
import os, sys, glob, math, random
from PyQt5.QtWidgets import (QMainWindow, QWidget,
QGridLayout, QPushButton, QApplication,
QAction, QFileDialog, QMessageBox)
from PyQt5.QtGui import QPixmap, QIcon, QCloseEvent
from PyQt5.QtMultimedia import QSound
from PyQt5 import QtCore
app_dir = os.path.dirname(__file__)
back_card_img = 'back'
sound_folder = 'sound'
sound_success = 'success.wav'
sound_fail = 'fail.wav'
sound_end = 'end.wav'
image_folder = 'img'
class MemoryGame(QMainWindow):
def __init__(self):
super().__init__()
self.back_card = os.path.join(app_dir, back_card_img) # remove the extension from the image name if present
self.status = {}
self.cell_width = 100
As it stands, the whole thing is working but it lacks of a pretty template (this is something I am going to improve in a second phase). What I am more interested in right now is to understand if my code could be better organized somehow (e.g. by splitting it into different classes/subclasses, instead of the many methods present).
The game works like this:
- the user chooses a folder of (JPG) images;
- a grid with all the images is formed, but each image is covered by the "back.jpg" image (it should stay in the same folder as the code)
- the game starts
Basically I am kind of doing everything using images in a Qgridlayout, which are put in buttons with event listeners. When an "onclick" signal is emitted, the
self.status attribute is analyzed and the game is restarted or goes on according to that.One last thing: the cards (images) are not shuffled right now, I still need to implement this feature (otherwise the game would be completely boring...).
```
import os, sys, glob, math, random
from PyQt5.QtWidgets import (QMainWindow, QWidget,
QGridLayout, QPushButton, QApplication,
QAction, QFileDialog, QMessageBox)
from PyQt5.QtGui import QPixmap, QIcon, QCloseEvent
from PyQt5.QtMultimedia import QSound
from PyQt5 import QtCore
app_dir = os.path.dirname(__file__)
back_card_img = 'back'
sound_folder = 'sound'
sound_success = 'success.wav'
sound_fail = 'fail.wav'
sound_end = 'end.wav'
image_folder = 'img'
class MemoryGame(QMainWindow):
def __init__(self):
super().__init__()
self.back_card = os.path.join(app_dir, back_card_img) # remove the extension from the image name if present
self.status = {}
self.cell_width = 100
Solution
Some no very important improvements:
may become
as you don't use
may be shortened:
Note omitting
Those are all constants, so by PEP 8 Style Guide their names should be written in all capital letters:
for k, v in self.back_dict.items():
self.back_dict[k] = self.back_cardmay become
for k in self.back_dict:
self.back_dict[k] = self.back_cardas you don't use
v (note fully omitting any method for self.back_dict, too).for p, cell in zip(positions, grid_cell):
if cell == '':
continue
self.card_dict[p] = cellmay be shortened:
for p, cell in zip(positions, grid_cell):
if not cell:
self.card_dict[p] = cellNote omitting
== 0 as value 0 itself is evaluated as True.app_dir = os.path.dirname(__file__)
back_card_img = 'back'
sound_folder = 'sound'
sound_success = 'success.wav'
sound_fail = 'fail.wav'
sound_end = 'end.wav'
image_folder = 'img'Those are all constants, so by PEP 8 Style Guide their names should be written in all capital letters:
APP_DIR = os.path.dirname(__file__)
BACK_CARD_IMG = 'back'
SOUND_FOLDER = 'sound'
SOUND_SUCCESS = 'success.wav'
SOUND_FAIL = 'fail.wav'
SOUND_END = 'end.wav'
IMAGE_FOLDER = 'img'Code Snippets
for k, v in self.back_dict.items():
self.back_dict[k] = self.back_cardfor k in self.back_dict:
self.back_dict[k] = self.back_cardfor p, cell in zip(positions, grid_cell):
if cell == '':
continue
self.card_dict[p] = cellfor p, cell in zip(positions, grid_cell):
if not cell:
self.card_dict[p] = cellapp_dir = os.path.dirname(__file__)
back_card_img = 'back'
sound_folder = 'sound'
sound_success = 'success.wav'
sound_fail = 'fail.wav'
sound_end = 'end.wav'
image_folder = 'img'Context
StackExchange Code Review Q#143672, answer score: 2
Revisions (0)
No revisions yet.