def play(self, wi): import time while self.playing: direction = self.getdirection(wi) if direction == "R": self.right() time.sleep(0.23) elif direction == "L": self.left() time.sleep(0.3) elif direction == "U": self.up() time.sleep(0.3) elif direction == "D": self.down() time.sleep(0.3) if self.checkboard(): self.playing = False print "You win" text, textwidth = self.textHandler.make_text("YOU WIN!", 16,1, color="00ff00") engine =matrixHandler.MatrixEngine(text) for i in range(textwidth+16): engine.shift_left() matrix = engine.get_matrix(cycle=True, cycle_size_col = textwidth+16) sp.set_panel_memory_from_matrix(matrix) return for _ in range(5): sp.set_panel_color("003000") time.sleep(1) sp.panel_clear() time.sleep(1)
def game_over(self): for i in range(5): svetelny_panel.set_panel_color(colors.RED1) time.sleep(0.1) svetelny_panel.panel_clear() time.sleep(0.1) self.player = [0, PANEL_HEIGHT // 2] self.initialize_boards()
def game_over(self): #RED BLINK for i in range(5): svetelny_panel.set_panel_color(colors.RED1) time.sleep(0.1) svetelny_panel.panel_clear() time.sleep(0.1) self.__init__()
def checkforlost(self): self.oldboard = copy.deepcopy(self.board) self.shiftleft() self.leftcount() if self.board == self.oldboard: #cannot move left self.shiftright() self.rightcount() if self.board == self.oldboard: #cannot move right self.shiftup() self.upcount() if self.board == self.oldboard: #cannot move up self.shiftdown() self.downcount() if self.board == self.oldboard: #cannot move down #you lose! self.playing = False print "Game over" text, textwidth = self.textHandler.make_text( "GAME OVER!", 16, 1, color="ff0000") engine = matrixHandler.MatrixEngine(text) for i in range(textwidth + 16): engine.shift_left() matrix = engine.get_matrix( cycle=True, cycle_size_col=textwidth + 16) sp.set_panel_memory_from_matrix(matrix) return if self.paneldisplay: for _ in range(5): sp.set_panel_color("300000") time.sleep(1) sp.panel_clear() time.sleep(1) else: self.board = self.oldboard else: self.board = self.oldboard else: self.board = self.oldboard else: self.board = self.oldboard
def checkforlost(self): self.oldboard = copy.deepcopy(self.board) self.shiftleft() self.leftcount() if self.board == self.oldboard: #cannot move left self.shiftright() self.rightcount() if self.board == self.oldboard: #cannot move right self.shiftup() self.upcount() if self.board == self.oldboard: #cannot move up self.shiftdown() self.downcount() if self.board == self.oldboard: #cannot move down #you lose! self.playing = False print "Game over" text, textwidth = self.textHandler.make_text("GAME OVER!", 16,1, color="ff0000") engine =matrixHandler.MatrixEngine(text) for i in range(textwidth+16): engine.shift_left() matrix = engine.get_matrix(cycle=True, cycle_size_col = textwidth+16) sp.set_panel_memory_from_matrix(matrix) return if self.paneldisplay: for _ in range(5): sp.set_panel_color("300000") time.sleep(1) sp.panel_clear() time.sleep(1) else: self.board = self.oldboard else: self.board = self.oldboard else: self.board = self.oldboard else: self.board = self.oldboard
def wii_move(self, wi=None): """the snake controlling through wiimote wi is wiimote object returned from winit() """ if wi is None: """ wiimote initializing """ sp.wi = winit() old_position = [-1, -1] play = True while play: move_flag = False if len(self.pixels) > 0: self.position = [self.pixels[0][0], self.pixels[0][1]][:] if self.position != old_position: old_position = self.position[:] time.sleep(0.1) buttons = wi.state["buttons"] if buttons & 4: # trigger fire(self.position) old_position = [-1000, -1000] if len(self.pixels) > 0: fp = self.pixels[0] set_pixel_color(matrix(fp[0], fp[1]), fp[2]) if buttons & 256: # left self.position[1] -= 1 move_flag = True if buttons & 512: # right self.position[1] += 1 move_flag = True if buttons & 2048: # up self.position[0] += 1 move_flag = True if buttons & 1024: # down self.position[0] -= 1 move_flag = True if buttons & 8: # A button # go to left bottom """ the snake head goes to the [0, 0] position the rest of the snake leaves at the current position """ # the old snake head hiding set_pixel_color(matrix(self.pixels[0][0], self.pixels[0][1]), \ self.blank_color) self.position[0] = 0 self.position[1] = 0 self.pixels[0][0] = self.pixels[0][1] = 0 # show the new head (see moving) for pixel in self.pixels[:2]: sp.set_pixel_color(sp.matrix(pixel[0], pixel[1]), pixel[2]) if buttons & 128: # Home button # end of controlling loop play = False if buttons & cwiid.BTN_PLUS: """add pixel to snake""" self.add_pixel() old_position = [-1000, -1000] fp = self.pixels[0] sp.set_pixel_color(sp.matrix(fp[0], fp[1]), fp[2]) # wait for button release while wi.state["buttons"] & cwiid.BTN_PLUS: pass if buttons & cwiid.BTN_MINUS: """delete pixel at the end of the snake""" self.del_pixel() old_position = [-1000, -1000] # wait for button release while wi.state["buttons"] & cwiid.BTN_MINUS: pass if move_flag: self.move() """testing if the snake doesn't have the head on his own body""" if len(self.pixels) > 1 and [self.pixels[0][0], self.pixels[0][1]] \ in [coord[:2] for coord in self.pixels[1:]]: # colision - end of game for blink in range(3): sp.set_panel_color("ff0000") time.sleep(0.1) sp.set_panel_color("") time.sleep(0.1) self.pixels = [] self.position = [0, 0] self.add_pixel() fire(self.position) fp = self.pixels[0] set_pixel_color(matrix(fp[0], fp[1]), fp[2]) # food controlling self.food_service()