def loop(self): self.clear() self.ball.update() scores = "%02x %02x" % (self.player_paddle.points, self.computer_paddle.points) render_text(self.frame, theme["pong_score"], theme["background"], scores, 2, 2) c = randint(0, 100) > 30 if self.ball.y > self.computer_paddle.y1 and c: self.computer_paddle.move_down() if self.ball.y < self.computer_paddle.y0 and c: self.computer_paddle.move_up() self.ball.draw(self.frame) self.player_paddle.draw(self.frame) self.computer_paddle.draw(self.frame) if self.is_key_pressed("UP"): self.player_paddle.move_up() if self.is_key_pressed("DOWN"): self.player_paddle.move_down() if self.is_key_up("B"): self.parent.back()
def drawBoard(self, frame, nextBlock): for y in range(self.height): frame[y, self.mapOffset[0] - 1] = theme["tetris_borders"] frame[y, self.width + self.mapOffset[0]] = theme["tetris_borders"] render_text(frame, theme["text"], theme["tetris_background"], "%x" % self.score, self.width + self.mapOffset[0] + 2, 1) yOffset = 8 xOffset = self.mapOffset[0] + self.width + 5 for y in range(nextBlock.getHeight()): for x in range(nextBlock.getWidth()): shape = nextBlock.getShape() if shape[x][y] != 0: frame[y + yOffset, x + xOffset] = nextBlock.color
def loop(self): t = time.localtime() text = "%02d:%02d" % (t.tm_hour, t.tm_min) self.frame = np.zeros( (20, 35, 3), np.uint8) + theme["clock_background"] x = (35 / 2) - ((len(text) * 4) / 2) render_text(self.frame, theme["clock_color"], theme["clock_background"], text, x, 14) tmp = np.zeros((5, 4 * len(text), 3)) render_text(tmp, theme["clock_color"], theme["clock_background"], text, 0, 0) tmp = np.rot90(tmp, 2) self.frame[1:6, x:x + (4 * len(text))] = tmp for i in range(0, 35): self.frame[9, i] = theme["clock_spacer"] self.frame[10, i] = theme["clock_spacer"] if self.last_keys_down["B"] and not self.keys_down["B"]: self.parent.back()
def loop(self): if self.last_keys_down["B"] and not self.keys_down["B"]: self.parent.back() if len(self.text) > 0: self.frame = np.zeros((20, 35, 3), np.uint8) if self.new_text: if len(self.text) >= 9: self.text += " -- " self.actual_render = np.zeros((20, 4 * len(self.text), 3), np.uint8) render_text(self.actual_render, (255, 255, 255), (0, 0, 0), self.text, 1, 8) self.new_text = False self.show_mode = "text" self.go_to_foreground() self.schedule_background(timedelta=10) elif self.new_photo: self.new_photo = False self.show_mode = "photo" ext = self.photo.split(".")[-1] with open(os.devnull, 'w') as devnull: if subprocess.call( ["wget", "-O", "/tmp/image.%s" % ext, self.photo], stdout=devnull) == 0: im = Image.open("/tmp/image.%s" % ext) im.thumbnail((35, 20), Image.ANTIALIAS) data = list(im.getdata()) width, height = im.size tmp = np.zeros((height, width, 3)) i = 0 for y in range(0, height): for x in range(0, width): tmp[y, x] = data[i] i += 1 self.imshowframe = np.zeros((20, 35, 3), dtype=np.uint8) x = (35 / 2) - (width / 2) y = (20 / 2) - (height / 2) self.imshowframe[y:y + height, x:x + width] = np.uint8(tmp) subprocess.call(["rm", "/tmp/image.%s" % ext ]) # remove image to free storage else: self.show_mode = "" self.text = "Failed to show image" self.new_text = True if self.show_mode == "text": if self.actual_render.shape[1] <= 35: x = (35 / 2) - (self.actual_render.shape[1] / 2) self.frame[ 0:20, x:x + self.actual_render.shape[1]] = self.actual_render else: self.frame = np.zeros((20, 35, 3), np.uint8) self.frame[0:20, 0:35] = self.actual_render[0:20, 0:35] self.actual_render = np.roll(self.actual_render, -1, 1) time.sleep(0.1) if self.show_mode == "photo": self.frame = np.zeros((20, 35, 3), np.uint8) self.frame = np.array(self.imshowframe) time.sleep(0.1)
def loop(self): if self.ingame: if self.is_key_down("LEFT"): if not self.direction[0] == 1: self.direction = [-1, 0] if self.is_key_down("RIGHT"): if not self.direction[0] == -1: self.direction = [1, 0] if self.is_key_down("UP"): if not self.direction[1] == 1: self.direction = [0, -1] if self.is_key_down("DOWN"): if not self.direction[1] == -1: self.direction = [0, 1] if self.is_key_down("B"): self.ingame = False self.firstrun = False self.firstmen = True self.frame = np.zeros((20, 35, 3), np.uint8) + theme["snake_background"] particle_x, particle_y = self.particle self.frame[particle_y, particle_x] = theme["snake_food"] newpoint = list(self.snake[-1]) newpoint[0] = (newpoint[0] + int(self.direction[0] * self.speed)) % 35 newpoint[1] = (newpoint[1] + int(self.direction[1] * self.speed)) % 20 self.snake.append(newpoint) if newpoint in self.snake[:-1]: self.ingame = False self.firstrun = False if newpoint[0] == self.particle[0] and newpoint[1] == self.particle[1]: self.length += 1 self.particle = [random.randint(0, 34), random.randint(0, 19)] if len(self.snake) > self.length: self.snake.pop(0) time.sleep(0.03) n = 1 for point in self.snake: point_x, point_y = point self.frame[point_y, point_x] = np.uint8(np.array(theme["snake_snake"]) * (n / float(self.length))) n += 1 else: render_text(self.get_self_frame(), theme["header"], theme["snake_background"], "Snake", 0, 0) if self.menu_point == "top": if self.firstrun and self.firstmen: render_text(self.get_self_frame(), theme["snake_welcome"], theme["snake_background"], "Welcome", 2, 8) elif self.firstmen: self.menu_point = "lose_screen" self.firstmen = False render_text(self.get_self_frame(), theme["snake_lose"], theme["snake_background"], "You lose", 2, 7) else: render_text(self.get_self_frame(), theme["snake_lose"], theme["snake_background"], "You lose", 2, 7) if self.keys_down["A"] and not self.last_keys_down["A"]: self.particle = [random.randint(0, 34), random.randint(0, 19)] self.snake = [[random.randint(0, 34), random.randint(0, 19)]] self.direction = [1, 0] random.shuffle(self.direction) self.length = 3 self.ingame = True self.firstmen = True self.menu_point = "top" if self.last_keys_down["B"] and not self.keys_down["B"]: self.parent.back() if self.menu_point == "lose_screen": render_text(self.get_self_frame(), theme["snake_lose"], theme["snake_background"], "You lose", 2, 7) if self.score_board_selection: render_text(self.get_self_frame(), theme["yes"], theme["snake_background"], "Y", 6, 14) render_text(self.get_self_frame(), theme["out_grayed"], theme["snake_background"], "X", 26, 14) else: render_text(self.get_self_frame(), theme["out_grayed"], theme["snake_background"], "Y", 6, 14) render_text(self.get_self_frame(), theme["no"], theme["snake_background"], "X", 26, 14) if self.is_key_up("A"): if self.score_board_selection: self.frame = np.zeros(self.frame.shape) self.menu_point = "scoreboard_enter" else: self.menu_point = "top" self.firstmen = False self.clear() if self.is_key_down("RIGHT"): self.score_board_selection = False if self.is_key_down("LEFT"): self.score_board_selection = True if self.menu_point == "scoreboard_enter": self.clear() render_text(self.get_self_frame(), theme["header"], theme["snake_background"], "Score", 0, 0) scoreboard_text = ''.join(self.scoreboard_text) render_text(self.get_self_frame(), theme["no"], theme["snake_background"], scoreboard_text, 4, 10) x = self.scoreboard_text_sel * 4 + 5 self.frame[8, x] = theme["elements"] if self.is_key_down("RIGHT"): self.scoreboard_text_sel = (self.scoreboard_text_sel + 1) % 7 if self.is_key_down("LEFT"): self.scoreboard_text_sel = (self.scoreboard_text_sel - 1) % 7 if self.is_key_down("UP"): current = self.scoreboard_text[self.scoreboard_text_sel] ind = self.scoreboard_alphabet.index(current) new_ind = (ind + 1) % len(self.scoreboard_alphabet) new = self.scoreboard_alphabet[new_ind] self.scoreboard_text[self.scoreboard_text_sel] = new if self.is_key_down("DOWN"): current = self.scoreboard_text[self.scoreboard_text_sel] ind = self.scoreboard_alphabet.index(current) new_ind = (ind - 1) % len(self.scoreboard_alphabet) new = self.scoreboard_alphabet[new_ind] self.scoreboard_text[self.scoreboard_text_sel] = new if self.is_key_down("A"): path = defaults.get_data_directory("snake") + "scoreboard.json" if not os.path.exists(path): with open(path, "a+") as target: json.dump(self.default_scoreboard, target) else: db_scoreboard = self.default_scoreboard with open(path) as target: db_scoreboard = json.load(target) db_scoreboard.update({ ''.join(self.scoreboard_text): [self.length, int(time.time())] }) with open(path, 'w+') as target: json.dump(db_scoreboard, target) self.menu_point = "top" self.clear()