def init(self): self.gameMode = 'Image' self.solve = False self.solveSteps = [] self.width = 500 self.height = 600 Star.init() self.star = pygame.sprite.Group() self.hint = False self.gameWon = False self.rows = 3 Block.init(self.rows) self.blocks = pygame.sprite.Group() self.puzzleWidth = 500 self.blockWidth = self.puzzleWidth / self.rows self.boardL = self.rows**2 - 1 for i in range(self.rows): for j in range(self.rows): #Not taking the last block, as it is supposed to be blank if i + j == (self.rows - 1) * 2: continue #Calculating location of blocks and adding them to sprite group x = j * (self.blockWidth) + (self.blockWidth / 2) y = i * (self.blockWidth) + (self.blockWidth / 2) self.blocks.add(Block(x, y, self.rows)) #Loading up the blank and adding it to sprite group Blank.init() blankInitial = self.puzzleWidth * (5 / 6) self.blocks.add(Blank(blankInitial, blankInitial, self.rows)) #If the board that was created is not valid, make a new one until it is if not self.isValidBoard(self, Block.board): self.init(self) return
def init(self): self.gameMode = 'Maze' self.gameWon = False self.gameLost = False Block.init() self.blocks = pygame.sprite.Group() self.rows = 4 self.puzzleWidth = 500 self.blockWidth = self.puzzleWidth / self.rows self.boardL = 15 self.coords = [] MazeBlock.init() for i in range(self.rows): for j in range(self.rows): index = i * self.rows + j #Not taking the last block, as it is supposed to be blank if index == self.rows**2-1: continue #Calculating location of blocks and adding them to sprite group x = j*(self.blockWidth)+(self.blockWidth/2) y = i*(self.blockWidth)+(self.blockWidth/2) self.coords.append((x, y)) #Creating the maze pieces cross = (True, True, True, True) upt = (True, True, False, True) downt = (False, True, True, True) tlc = (True, False, False, True) trc = (True, True, False, False) blc = (False, False, True, True) brc = (False, True, True, False) blank = (False, False, False, False) vert = (True, False, True, False) horz = (False, True, False, True) #Creating the maze maze1 = [horz, horz, cross, downt, upt, tlc, trc, blc, brc, blank, horz, blank, horz, blank, blank] for i in range(self.rows**2-1): self.blocks.add(MazeBlock(self.coords[i][0], self.coords[i][1], maze1[i])) #Loading up the blank and adding it to sprite group Blank.init() blankInitial = self.puzzleWidth * (7/8) self.blocks.add(Blank(blankInitial, blankInitial)) #Initiating the player Player.init() self.player = Player(62.5, 62.5) self.playerSprite = pygame.sprite.Group() self.playerSprite.add(self.player) self.playerIndex = 0 self.remainingMoves = 30 self.remainingTime = 30 self.timerCalled = 0
def read(file): with open(file, 'r') as f: if os.stat(file).st_size == 0: raise FileNotFoundError lines = f.readlines() B = [[None for x in range(16)] for y in range(9)] for line in lines: line = line.strip('\n') l = line.split(';') x = int(int(l[1]) / 100) y = int(int(l[2]) / 100) if l[0] == 'wall': e = Wall(x, y) e.up = to_rgb(l[3]) e.left = to_rgb(l[4]) e.down = to_rgb(l[5]) e.right = to_rgb(l[6]) e.color = to_rgb(l[7]) B[x][y] = e if l[0] == 'piece': e = Piece(x, y) e.up = to_rgb(l[3]) e.left = to_rgb(l[4]) e.down = to_rgb(l[5]) e.right = to_rgb(l[6]) if int(l[7]) == 1: e.upside = True B[x][y] = e if l[0] == 'blank': e = Blank(x, y) B[x][y] = e return B
def solve(self): self.reset() board = dynamics.algorithm_solve(self.__board) for cord in INNER: self.__board[cord[0]][cord[1]] = board[cord[0]][cord[1]] for cord in SHELF: self.__board[cord[0]][cord[1]] = Blank(cord[0], cord[1])
def itemContextMenuQuired(self, point): item = self.alarmList.itemAt(point) if item is not None : #print point, 'clicked', QtCore.QString().fromUtf8(item.text()) Editor = Blank(self.obj, item.text(), self) Editor.move(point) Editor.exec_()
def gen_board(self): # fill board with elements board = [[None for x in range(16)] for y in range(9)] for cord in WALLS: board[cord[0]][cord[1]] = Wall(cord[0], cord[1]) for cord in INNER: board[cord[0]][cord[1]] = Piece(cord[0], cord[1]) for cord in UPSIDE: board[cord[0]][cord[1]].upside = True for x in board: for y in x: if type(y) == Piece and y.upside: y.up = NOGO if type(y) == Piece and not y.upside: y.down = NOGO for cord in SHELF: board[cord[0]][cord[1]] = Blank(cord[0], cord[1]) # color elements for cord in SP_WALLS: e = board[cord[0]][cord[1]] col = random.choice(COL) e.left = col e.right = col board[cord[0]][cord[1] - 1].right = col board[cord[0]][cord[1] + 1].left = col for cord in INNER: e = board[cord[0]][cord[1]] col = [random.choice(COL) for i in range(4)] if e.up == BLANK: e.up = col[0] board[cord[0] - 1][cord[1]].down = col[0] if e.left == BLANK: e.left = col[1] board[cord[0]][cord[1] - 1].right = col[1] if e.down == BLANK: e.down = col[3] board[cord[0] + 1][cord[1]].up = col[3] if e.right == BLANK: e.right = col[2] board[cord[0]][cord[1] + 1].left = col[2] self.set_wall_color(board) self.__board = board
def test_is_valid(self): valid = self.game.is_valid() self.assertTrue(valid, "Board is valid, but is_valid returns False") board = self.game.get_board() board[2][3] = Blank(2, 3) valid = self.game.is_valid() self.assertFalse( valid, "Board is not valid (one element missing), but is_valid returns True" )
def keyPressEvent(self, event): self.canKeyboardInput = True key = event.key() if self.status == EGameStatus.EPlaying: if self.player is None: print('Null player') return # delta 값 초기화 deltaX = 0 deltaY = 0 if key == Qt.Key_Left: # print('Left Arrow Pressed') deltaX = -1 deltaY = 0 elif key == Qt.Key_Right: # print('Right Arrow Pressed') deltaX = 1 deltaY = 0 elif key == Qt.Key_Up: # print('Up Arrow Pressed') deltaX = 0 deltaY = -1 elif key == Qt.Key_Down: # print('Down Arrow Pressed') deltaX = 0 deltaY = 1 elif key == Qt.Key_R: self.onClickReset() return elif key == Qt.Key_1: if self.backButton.isEnabled(): self.onClickBack() return elif key == Qt.Key_2: if self.nextButton.isEnabled(): self.onClickNext() return else: return # currentStage에 있는 object들 deltaObject = self.currentStage[App.player.y + deltaY][App.player.x + deltaX] playerPositionOnStage = self.currentStage[App.player.y][ App.player.x] deltaClassName = type(deltaObject).__name__ # 끔찍한 코드... 더 낫게 할 수 없을까? if deltaClassName == 'Blank': print('You can go (Blank)') self.gamePlayerMove(deltaX, deltaY) elif deltaClassName == 'Button': if deltaObject.status == EButtonStatus.blank: print('You can go (Button-Blank)') self.gamePlayerMove(deltaX, deltaY) elif deltaObject.status == EButtonStatus.withBox: doubleDeltaObject = self.currentStage[App.player.y + deltaY * 2][App.player.x + deltaX * 2] doubleDeltaClassName = type(doubleDeltaObject).__name__ if doubleDeltaClassName == 'Blank': print('You can go (Button(with Box)-Blank)') deltaObject.changeStatus(EButtonStatus.blank) self.currentStage[doubleDeltaObject.y][ doubleDeltaObject.x] = Box(doubleDeltaObject.x, doubleDeltaObject.y, self.boxPixmap) self.goal += 1 self.gamePlayerMove(deltaX, deltaY) elif doubleDeltaClassName == 'Button': if doubleDeltaObject.status == EButtonStatus.blank: doubleDeltaObject.changeStatus( EButtonStatus.withBox) deltaObject.changeStatus(EButtonStatus.blank) print( 'You can go (Button(withBox) - Button(Blank))') self.gamePlayerMove(deltaX, deltaY) elif doubleDeltaObject.status == EButtonStatus.withBox: print( 'You can\'t go (Button(with Box)-(Button(with Box))' ) elif doubleDeltaClassName == 'Box': print('You can\'t go (Button(with Box)-Box)') elif doubleDeltaClassName == 'Wall': print('You can\'t go (Button(With Box) - Wall)') elif deltaClassName == 'Wall': print('You can\'t go (Wall)') pass elif deltaClassName == 'Box': doubleDeltaObject = self.currentStage[App.player.y + deltaY * 2][App.player.x + deltaX * 2] doubleDeltaClassName = type(doubleDeltaObject).__name__ if doubleDeltaClassName == 'Box': print('You can\'t go (Box - Box)') pass elif doubleDeltaClassName == 'Wall': print('You can\'t go (Box - Wall)') pass elif doubleDeltaClassName == 'Blank': print('You can go (Box - Blank)') self.swapInCurrentStage(doubleDeltaObject, deltaObject) self.gamePlayerMove(deltaX, deltaY) elif doubleDeltaClassName == 'Button': if doubleDeltaObject.status == EButtonStatus.blank: deltaObject.erase() print('You can go (Box - Button(Blank))') blank = Blank(deltaObject.x, deltaObject.y, self.blankPixmap0) self.currentStage[App.player.y + deltaY][App.player.x + deltaX] = blank blank.erase() del deltaObject self.goal -= 1 self.gamePlayerMove(deltaX, deltaY) doubleDeltaObject.changeStatus(EButtonStatus.withBox) elif doubleDeltaObject.status == EButtonStatus.withBox: print('You can\'t go (Box - Button(With Box))') if self.goal == 0: print('Clear!') self.nextButton.setEnabled(True)
def loadStage(self, number): self.eraseStage() self.currentStage = [[] for i in range(0, len(self.mapData[number]))] self.blankList = [] self.step = 0 goalNum = 0 self.nextButton.setEnabled(False) if number == 0: self.backButton.setEnabled(False) else: self.backButton.setEnabled(True) gc.collect() self.stageText.setText('Stage: ' + str(number + 1)) self.stepText.setText('Step: ' + str(self.step)) self.currentLevel = number # 게임 화면을 중앙으로 x = (self.width - Object.size * len(self.mapData[number][0])) * 0.5 y = (self.height - Object.size * len(self.mapData[number])) * 0.5 Object.setPositionOffset(x, y + 40) for y in range(0, len(self.mapData[number])): for x in range(0, len(self.mapData[number][y])): # 일단 모든 맵을 공백으로 채운다. blankList에 blank를 추가한다. print(type(self.mapData[number][y][x]), type(EObjects.EPlayer), y, x) if self.mapData[number][y][x] != EObjects.EBlankWithNoGlass: if randomPercent(0.1): blank = Blank(x, y, self.blankPixmap1) elif randomPercent(0.05): blank = Blank(x, y, self.blankPixmap2) elif randomPercent(0.02): blank = Blank(x, y, self.blankPixmap3) else: blank = Blank(x, y, self.blankPixmap0) self.blankList.append(blank) blank.lower() # pass self.background.stackUnder(blank) if self.mapData[number][y][x] == EObjects.EBox: box = Box(x, y, self.boxPixmap) self.currentStage[y].append(box) box.raise_() elif self.mapData[number][y][x] == EObjects.EPlayer: playerStartX = x playerStartY = y print('Player found!') blank = Blank(x, y, self.blankPixmap0) self.currentStage[y].append(blank) blank.erase() elif self.mapData[number][y][x] == EObjects.EWall: self.currentStage[y].append(Wall(x, y, self.wallPixmap)) elif self.mapData[number][y][x] == EObjects.EButton: goalNum += 1 self.currentStage[y].append(Button(x, y, self.buttonPixmap)) elif self.mapData[number][y][ x] == EObjects.EBlank or self.mapData[number][y][ x] == EObjects.EBlankWithNoGlass: blank = Blank(x, y, self.blankPixmap0) self.currentStage[y].append(blank) blank.erase() self.goal = goalNum self.goalText.setText('Goal: ' + str(self.goal)) # ordering을 위해 player는 마지막에 생성한다! App.player = Player(playerStartX, playerStartY, self.playerPixmap)
def addAlarmItem(self): _addItem = Blank(self.obj, None, self) _addItem.exec_()