class GameApp(QDeclarativeView): block_tpls = { Block: 'blocks/block.qml', Bomb: 'blocks/bomb.qml', Space: 'blocks/space.qml', } #current = 'blocks/active.qml' mega_start = Signal() def __init__(self, menu, settings, *args, **kwargs): QDeclarativeView.__init__(self, *args, **kwargs) self.exception = None self.sound = Sounder() self.menu = menu self.settings = settings self.rt_pool = []#fix fault self.setWindowTitle('findZbomb!') self.setSource('interface.qml') self.set_map(None) self.redraw() context = self.rootContext() context.setContextProperty('obj', self) self.mega_start.connect(self._mega_start) self.setResizeMode(QDeclarativeView.SizeRootObjectToView) code = self.settings.value('code') if code: self.rootObject().set_code(code) def set_map(self, map_path): self.map_path = map_path self.old_cp = None self.game = Game(self) self.map = Map(self.game, map_path) self.map.failed.connect(self.sound.boom) self.map.failed.connect(self.failed) self.map.finished.connect(self.sound.win) self.rootObject().set_map_name(self.map.title) if hasattr(self, 'robot'): self.rootObject().robot_to_active_pos(*self.robot.position) self.draw_map() if self.map.background: self.rootObject().set_custom_background(self.map.background) self.map.finished.connect(self.win) if getattr(self, 'rt', None): self.rt.quit() try: self.robot.stop = True except AttributeError: pass self.rt = RoboThread() self.rt_pool.append(self.rt) def draw_map(self): #grid = self.rootContext().contextProperty('mapGrid') root = self.rootObject() prepared_map = map(lambda items: map(lambda item: self.block_tpls[type(item)], items), self.map.map) root.draw_map(prepared_map) self.rootObject().robot_to_active_pos(*self.map.position) #print grid def redraw(self): if self.old_cp != self.map.cur_position: #self.draw_map() self.old_cp = self.map.cur_position if hasattr(self, 'robot'): self.rootObject().set_map_count(self.robot.moves) self.rootObject().robot_to_active_pos(*self.robot.position) if self.exception: if hasattr(self, 'robot'): self.robot.stop = True self.rootObject().show_exception(unicode(self.exception)) self.exception = None QTimer.singleShot(300, self.redraw) @Slot(result=int) def start(self): self.set_map(self.map_path) self.rootObject().remove_notify() self.code = 'class Robot(BaseRobot):\n%s' % self.rootObject().get_code() self.mega_start.emit() return 0 @Slot() def _mega_start(self): self.rt.start() def _start(self): Robot = BaseRobot#fix code highlighting self.exception = None try: exec(self.code) self.robot = Robot(self.map) self.map.put_robot(self.robot) self.rootObject().robot_to_active_pos(*self.robot.position) except Exception, e: self.exception = e
from PySide.QtCore import QCoreApplication from maps import Map from robot import BaseRobot from game import Game import unittest import sys class Robot(BaseRobot): def on_start(self): self.go(self.RIGHT) def on_move(self, status): if status: self.go(self.RIGHT) print self.watch(self.RIGHT) if __name__ == '__main__': app = QCoreApplication(sys.argv) g = Game() m = Map(g) r = Robot(m) m.put_robot(r)
class GameApp(QDeclarativeView): block_tpls = { Block: 'blocks/block.qml', Bomb: 'blocks/bomb.qml', Space: 'blocks/space.qml', } #current = 'blocks/active.qml' mega_start = Signal() def __init__(self, menu, settings, *args, **kwargs): QDeclarativeView.__init__(self, *args, **kwargs) self.exception = None self.sound = Sounder() self.menu = menu self.settings = settings self.rt_pool = [] #fix fault self.setWindowTitle('findZbomb!') self.setSource('interface.qml') self.set_map(None) self.redraw() context = self.rootContext() context.setContextProperty('obj', self) self.mega_start.connect(self._mega_start) self.setResizeMode(QDeclarativeView.SizeRootObjectToView) code = self.settings.value('code') if code: self.rootObject().set_code(code) def set_map(self, map_path): self.map_path = map_path self.old_cp = None self.game = Game(self) self.map = Map(self.game, map_path) self.map.failed.connect(self.sound.boom) self.map.failed.connect(self.failed) self.map.finished.connect(self.sound.win) self.rootObject().set_map_name(self.map.title) if hasattr(self, 'robot'): self.rootObject().robot_to_active_pos(*self.robot.position) self.draw_map() if self.map.background: self.rootObject().set_custom_background(self.map.background) self.map.finished.connect(self.win) if getattr(self, 'rt', None): self.rt.quit() try: self.robot.stop = True except AttributeError: pass self.rt = RoboThread() self.rt_pool.append(self.rt) def draw_map(self): #grid = self.rootContext().contextProperty('mapGrid') root = self.rootObject() prepared_map = map( lambda items: map(lambda item: self.block_tpls[type(item)], items), self.map.map) root.draw_map(prepared_map) self.rootObject().robot_to_active_pos(*self.map.position) #print grid def redraw(self): if self.old_cp != self.map.cur_position: #self.draw_map() self.old_cp = self.map.cur_position if hasattr(self, 'robot'): self.rootObject().set_map_count(self.robot.moves) self.rootObject().robot_to_active_pos(*self.robot.position) if self.exception: if hasattr(self, 'robot'): self.robot.stop = True self.rootObject().show_exception(unicode(self.exception)) self.exception = None QTimer.singleShot(300, self.redraw) @Slot(result=int) def start(self): self.set_map(self.map_path) self.rootObject().remove_notify() self.code = 'class Robot(BaseRobot):\n%s' % self.rootObject().get_code( ) self.mega_start.emit() return 0 @Slot() def _mega_start(self): self.rt.start() def _start(self): Robot = BaseRobot #fix code highlighting self.exception = None try: exec(self.code) self.robot = Robot(self.map) self.map.put_robot(self.robot) self.rootObject().robot_to_active_pos(*self.robot.position) except Exception, e: self.exception = e
from PySide.QtCore import QCoreApplication from maps import Map from robot import BaseRobot from game import Game import unittest import sys class Robot(BaseRobot): def on_start(self): self.go(self.RIGHT) def on_move(self, status): if status: self.go(self.RIGHT) print self.watch(self.RIGHT) if __name__ == "__main__": app = QCoreApplication(sys.argv) g = Game() m = Map(g) r = Robot(m) m.put_robot(r)