def __init__(self): QtGui.QMainWindow.__init__(self) self.var = Vars() self.setFixedSize(self.var.boardWidth + 250, self.var.boardHeight) self.setWindowTitle('Tower Defense!') self.menuwindow = menuWindow(self) self.menuwindow.show() self.road = [] self.board = Board(self) self.setCentralWidget(self.board) self.menuboard = menuBoard(self) self.timer = QtCore.QBasicTimer() self.timer.start(self.var.gameSpeed, self) self.update()
def __init__(self, parent=None): self.parent = parent if self.parent is None: # for tests from Towers import Tower from var import Vars self.var = Vars() else: self.var = parent.var self.gameOver = False self.winner = False self.enemiesGone = False self.road = [] self.occupied = [] self.wave = 1 self.waveSent = False self.waveCalled = False self.enemies = [] self.towers = [] self.tower = 0 self.enemiesToGo = {} self.bombRadius = 100 self.bombDamage = 1000
class TowerDefense(QtGui.QMainWindow): """ Main class """ def __init__(self): QtGui.QMainWindow.__init__(self) self.var = Vars() self.setFixedSize(self.var.boardWidth + 250, self.var.boardHeight) self.setWindowTitle('Tower Defense!') self.menuwindow = menuWindow(self) self.menuwindow.show() self.road = [] self.board = Board(self) self.setCentralWidget(self.board) self.menuboard = menuBoard(self) self.timer = QtCore.QBasicTimer() self.timer.start(self.var.gameSpeed, self) self.update() def campaign(self): self.pause(False) self.var.level = 1 self.var.campaign = True self.var.reset() self.board.setCurrentIndex(0) self.board.gameBoard.start([]) self.menuwindow.hide() self.var.mode = self.var.GameMode.game def startGame(self): """ Starting game after choosing a map """ self.pause(False) self.var.campaign = False self.var.reset() self.board.setCurrentIndex(0) self.menuwindow.selectMap() self.board.gameBoard.start(self.road) self.menuwindow.hide() self.var.mode = self.var.GameMode.game def default(self): """ Starting game with default map """ self.pause(False) self.var.campaign = False self.var.reset() self.board.setCurrentIndex(0) self.menuwindow.defaultMap() self.board.gameBoard.start(self.road) self.menuwindow.hide() self.var.mode = self.var.GameMode.game def toMapEditor(self): """ Opens map editor """ self.pause(False) self.board.setCurrentIndex(1) self.board.editBoard.start() self.menuwindow.hide() self.var.mode = self.var.GameMode.edit '''Event handlers''' def keyPressEvent(self, event): """ handler for ke """ if (event.key() == QtCore.Qt.Key_S and self.var.mode == self.var.GameMode.edit): self.board.editBoard.save() if (event.key() == QtCore.Qt.Key_M and self.var.mode == self.var.GameMode.edit): self.board.editBoard.changeMode() if (event.key() == QtCore.Qt.Key_P and self.var.mode == self.var.GameMode.game): self.pause(True) if (event.key() == QtCore.Qt.Key_Shift and self.var.mode == self.var.GameMode.game): self.var.shiftPressed = True def keyReleaseEvent(self, event): if (event.key() == QtCore.Qt.Key_Shift and self.var.mode == self.var.GameMode.game): self.var.shiftPressed = False def mousePressEvent(self, event): """ handler for mousePress event """ if self.var.mode == self.var.GameMode.game: self.board.gameBoard.placeTowerOrBomb() if self.var.mode == self.var.GameMode.edit: self.board.editBoard.setRoad() def eventFilter(self, source, event): """ handler for mouseMove event """ if event.type() == QtCore.QEvent.MouseMove: if (event.buttons() == QtCore.Qt.NoButton and (str(source).find("gameField") > 0 or str(source).find("editField") > 0)): mouse = event.pos() if self.var.mode == self.var.GameMode.game: self.board.gameBoard.updateMouse(mouse.x(), mouse.y()) self.board.gameBoard.mouseIn = True self.board.gameBoard.repaint() elif self.var.mode == self.var.GameMode.edit: self.board.editBoard.updateMouse(mouse.x(), mouse.y()) self.board.editBoard.mouseIn = True else: if self.var.mode == self.var.GameMode.game: self.board.gameBoard.mouseIn = False elif self.var.mode == self.var.GameMode.edit: self.board.editBoard.mouseIn = False self.update() return QtGui.QMainWindow.eventFilter(self, source, event) def timerEvent(self, event): """ game thread moves mobs """ if event.timerId() == self.timer.timerId(): self.board.gameBoard.state.enemyManager() self.board.gameBoard.newLevel() self.board.gameBoard.state.countDamage() self.board.gameBoard.state.isGameOver() self.board.gameBoard.state.isWinner() self.menuboard.repaint() self.board.gameBoard.repaint() else: QtGui.QFrame.timerEvent(self, event) def quit(self): self.timer.stop() self.deleteLater() def pause(self, f=None): if f is None: f = not self.var.pause if f: self.timer.stop() self.menuboard.pauseBtn.setText("Play") self.var.pause = True else: self.timer.start(self.var.gameSpeed, self) self.update() self.menuboard.pauseBtn.setText("Pause") self.var.pause = False