2
\$\begingroup\$
from random import choice
from sys import (exit, argv)
from time import sleep
from PyQt5.QtCore import Qt
from PyQt5.QtGui import (QIcon, QPixmap, QFont)
from PyQt5.QtWidgets import (QToolTip, QPushButton, QApplication,
 QWidget, QLabel, QComboBox, QDesktopWidget)
ICON = 'icon.png'
DICE = ('dice_1.png', 'dice_2.png', 'dice_3.png', 'dice_4.png', 'dice_5',
 'dice_6.png')
ROLLING_ANIMATION = 'dice_rolling_animation.png'
SCALED_PARMS = 162, 302, Qt.KeepAspectRatio, Qt.FastTransformation
ONE_DIE_PARMS = 1427, 30, 162, 201
TWO_DICE_PARMS = 1265, 30, 324, 201
SLEEP_TIME = 0.5
class DiceRollSimulator(QWidget):
 def __init__(self):
 super().__init__()
 self.initUI()
 def initUI(self):
 self.die1 = QLabel(self)
 self.die1.setPixmap(QPixmap(choice(DICE)).scaled(*SCALED_PARMS))
 self.die1.move(0.5, 0.5)
 self.die2 = QLabel(self)
 self.die2.setPixmap(QPixmap(choice(DICE)).scaled(*SCALED_PARMS))
 self.die2.move(162, 0.5)
 self.die2.setVisible(False)
 self.btn = QPushButton('Roll', self)
 self.btn.setFont(QFont('SansSerif', 20))
 self.btn.setToolTip('Click to Roll Die')
 self.btn.clicked.connect(self.rolldie)
 self.btn.resize(166, 43)
 self.btn.move(-2, 161)
 self.dice_amount = QComboBox(self)
 self.dice_amount.addItems(['1', '2'])
 self.dice_amount.activated[str].connect(self.dice_amount_changed)
 self.dice_amount.move(135, -2)
 QToolTip.setFont(QFont('SansSerif', 10))
 QApplication.desktop()
 screen = QDesktopWidget().screenGeometry()
 self.setFixedSize(162, 201)
 x = screen.width() - self.width() - 15
 self.move(x, 1)
 self.setWindowTitle('Dice Roll Simulator')
 self.setWindowIcon(QIcon(ICON))
 self.show()
 def rolldie(self):
 self.die1.setPixmap(QPixmap(ROLLING_ANIMATION).scaled(*SCALED_PARMS))
 self.die2.setPixmap(QPixmap(ROLLING_ANIMATION).scaled(*SCALED_PARMS))
 QApplication.processEvents()
 sleep(SLEEP_TIME)
 self.die1.setPixmap(QPixmap(choice(DICE)).scaled(*SCALED_PARMS))
 self.die2.setPixmap(QPixmap(choice(DICE)).scaled(*SCALED_PARMS))
 QApplication.processEvents()
 def dice_amount_changed(self):
 geo = str(self.geometry())
 geo2 = geo[19:len(geo) - 1]
 loc = [int(x) for x in geo2.split(',')]
 if self.dice_amount.currentText() == '1':
 self.setFixedSize(162, 201)
 x = loc[0] + 154
 y = loc[1] - 30
 self.move(x, y)
 self.die2.setVisible(False)
 self.btn.resize(166, 43)
 self.dice_amount.move(132, -2)
 else:
 self.setFixedSize(324, 201)
 x = loc[0] - 170
 y = loc[1] - 30
 self.move(x, y)
 self.die2.setVisible(True)
 self.btn.resize(328, 43)
 self.dice_amount.move(294, -2)
if __name__ == '__main__':
 app = QApplication(argv)
 ex = DiceRollSimulator()
 ex.show()
 exit(app.exec_())

This is the updated version of the dice roll simulator code I posted some time ago. I've made several changes and additions including ones suggested by users here.

asked Aug 28, 2017 at 14:22
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Nice code, so only some comments.

ONE_DIE_PARMS = 1427, 30, 162, 201
TWO_DICE_PARMS = 1265, 30, 324, 201

Never used, typo in the first of them.

In contrast, there are other magic numbers in your code which deserve names for them.


DICE = ('dice_1.png', 'dice_2.png', 'dice_3.png', 'dice_4.png', 'dice_5',
 'dice_6.png')

may become

DICE_TEMPLATE = 'dice_{}.png'
DICE = [DICE_TEMPLATE.format(i) for i in range(1, 7)]

(BTW. it's common to use plural for names of collections - DICES)


geo2 = geo[19:len(geo) - 1]

may become

geo2 = geo[19:-1]

as negative numbers in slices mean "from the end".

answered Aug 28, 2017 at 16:13
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.