Source code for view.display
'''
The primary display interfaces.
'''
# FILE VERSION: released 5/5/2017 @ 13:00
import random
from PyQt4 import QtCore, QtGui
from qt_configs import Ui_CitizenPacMainWindow
[docs]def randomColor():
'''
Returns a random color.
:Return:
:class:`PyQt4.QtGui.QColor`
A random color generated by choosing a random integer in ``[0, 255]`` for the
red, green, and blue channels.
'''
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return QtGui.QColor(r, g, b)
[docs]class CitizenPacMainWindow(QtGui.QMainWindow, Ui_CitizenPacMainWindow):
'''
Ui interface. This class is identical to that which would be generated by the
:ref:`ui_toolkit` using the ``-w`` flag, only we also include a resize event handler
to allow the game to scale up / down with resizing.
.. danger::
Do not **ever** forget to call ``self.setupUi(self)`` in the constructor! See
the generated Ui file in the ``qt_configs`` directory for why.
:Attributes:
``scene`` (:class:`model.Scene`)
The scene everything is being drawn in.
'''
def __init__(self, parent=None, f=QtCore.Qt.WindowFlags()):
QtGui.QMainWindow.__init__(self, parent, f)
self.setupUi(self)
self.scene = None
[docs] def attachScene(self, scene):
'''
We need the scene in order to determine what size we are trying to display in.
:Parameters:
``scene`` (:class:`model.Scene`)
The scene we are going to store a reference to internally.
'''
self.scene = scene
[docs] def resizeEvent(self, e):
'''
When a resize event occurs, resize the scene viewport to show the game at the
maximum resolution possible *without* distorting the aspect ratio. Makes use
of the :class:`PyQt4.QtGui.QGraphicsView`'s ``fitInView`` method.
:Parameters:
``e`` (:class:`PyQt4.QtGui.QResizeEvent`)
The resize event being dispatched by the Qt backend.
'''
newSize = self.scene.view.sceneRect()
self.scene.view.fitInView(newSize, QtCore.Qt.KeepAspectRatio)
[docs]class GameStats(object):
'''
Wrapper class for displaying the game statistics in the top.
:Parameters:
``gameRunning`` (:class:`PyQt4.QtGui.QCheckBox`)
The checkbox in the top-left of the UI representing whether or not the game
is currently running or not.
``speedBoost`` (:class:`PyQt4.QtGui.QProgressBar`)
The progress bar just to the right of the checkbox indicating by how much
the speed of the game has been boosted.
``numLives`` (:class:`PyQt4.QtGui.QLCDNumber`)
The LCD panel to display the number of lives left on.
``scoreBoard`` (:class:`PyQt4.QtGui.QLCDNumber`)
The LCD panel to display the game score on.
:Attributes:
``gameRunning`` (:class:`PyQt4.QtGui.QCheckBox`)
The checkbox in the top-left of the UI representing whether or not the game
is currently running or not.
``speedBoost`` (:class:`PyQt4.QtGui.QProgressBar`)
The progress bar just to the right of the checkbox indicating by how much
the speed of the game has been boosted.
``numLives`` (:class:`PyQt4.QtGui.QLCDNumber`)
The LCD panel to display the number of lives left on.
``scoreBoard`` (:class:`PyQt4.QtGui.QLCDNumber`)
The LCD panel to display the game score on.
'''
def __init__(self, gameRunning, speedBoost, numLives, scoreBoard):
self.gameRunning = gameRunning
self.gameRunning.setEnabled(False)
self.speedBoost = speedBoost
self.numLives = numLives
self.scoreBoard = scoreBoard
[docs] def setRunning(self, running):
'''
Checks the ``gameRunning`` checkbox.
:Parameters:
``running`` (bool)
The value to set checked to, ``True`` for checked, ``False`` otherwise.
'''
self.gameRunning.setChecked(running)
[docs] def displayGameScore(self, score):
'''
Sets the game score display.
:Parameters:
``score`` (float)
The value to display on the game ``scoreBoard``.
'''
self.scoreBoard.display(score)
[docs] def displayGameSpeed(self, speed):
'''
Sets the game speed boost progress bar.
:Parameters:
``speed`` (float)
The value to display on the ``speedBoost`` progress bar.
'''
self.speedBoost.setValue(speed)
[docs] def setLives(self, val):
'''
Sets the lives left display.
:Parameters:
``val`` (int)
The value to display on the game ``scoreBoard``.
'''
self.numLives.display(float(val))