def __init__(self): ''' initialize the scrolling text and color ''' super(ForFunLayer, self).__init__() x, y = director.get_window_size() title = text.Label( "CincyPy Flasklet Demo", (x / 2, y / 2), font_name='Gill Sans', font_size=60, anchor_x='center', anchor_y='center') title.element.color = (255, 255, 0, 255) self.objects.append(title) self.add(title) addresses = get_ipaddresses() k=0 for interface, ip in addresses: if ip != '0.0.0.0': t = text.Label("http://%s:%d (%s)" % (ip, flask_api.DEFAULT_PORT, interface), (0, (k + 1)*30+100), font_size=20) t.element.color = (255, 0, 0, 255) self.add(t) k += 1 sys.stderr.flush() # For handling of on_add_text flask_api.eventdispatcher.push_handlers(self) localdispatcher.push_handlers(self)
def draw(self): #global player_score_screen, pc_score_screen, round_screen text.Label(f"Player's\nscore: {str(self.score[1])}", font_size = 30, x = 800, y = 520, width = 300, align="center", anchor_x = "center", anchor_y="center", multiline=True).draw() text.Label(f"Computer's\nscore: {str(self.score[0])}", font_size = 30, x = 280, y = 520, width = 300, align="center", anchor_x = "center", anchor_y="center", multiline=True).draw() self.round_screen = text.Label(f"round: {self.round_no}", font_size = 40, x = 550, y = 80, anchor_x="center").draw()
def restart(self): #round if self.round_no > 3: # if the round is higher than 3, stop the number at 3 (dont rise the number to 4) self.round_screen = text.Label(f"round: 3", font_size = 40, x = 550, y = 80, anchor_x="center") if self.score[0] > self.score[1]: self.gameover_screen = text.Label(f"Computer wins!\nPress space to restart", font_size = 24, x = 550, y = 30, anchor_x = "center") self.gameover_screen.draw() elif self.score[1] > self.score[0]: self.gameover_screen = text.Label(f"You win!\nPress space to restart", font_size = 24, x = 550, y = 30, anchor_x = "center") self.gameover_screen.draw()
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # batch for efficient drawing self.batch = graphics.Batch() background = graphics.OrderedGroup(0) foreground = graphics.OrderedGroup(1) self.gamelayer = graphics.OrderedGroup(2) toplayer = graphics.OrderedGroup(3) # window size self.size = self.get_size() # start with empty asteroid set self.asteroids = set() # empty explosions set self.explosions = set() # background and moving foreground sprites self.background = physicalobject.ScaledMovingSprite(img=resources.background_image, screensize=self.size, batch=self.batch, group=background) self.debris = load.debris(screensize=self.size, batch=self.batch, group=foreground) self.splashscreen = load.ClickableSprite(hook_function=self.start, img=resources.splashscreen, x = self.size[0] / 2.0, y=self.size[1] / 2.0, batch=self.batch, group=toplayer) # player ship self.player = load.ship(screensize=self.size, batch=self.batch, group=self.gamelayer) self.score = 0 self.lives = LIVES self.started = False self.fps_display = clock.ClockDisplay() # Lives and score labels self.lives_label = text.Label(font_size=20, text="Lives: %d" % self.lives, x=40, y=self.size[1]-40, batch=self.batch, group=toplayer) self.score_label = text.Label(font_size=20, anchor_x='right', text="Score: %d" % self.score, x=self.size[0]-40, y=self.size[1]-40, batch=self.batch, group=toplayer) # update frequency clock.schedule_interval(self.update, 1 / 120) # spawn a new asteroid each second clock.schedule_interval(self.spawn_asteroid, 1) # add event handlers to the ship and splashscreen self.push_handlers(self.player) self.push_handlers(self.splashscreen)
def prepare_content(self): getstyle = self.getstyle fg = getstyle("fg", (1, 1, 1, 1)) font = getstyle("font") font_size = getstyle("font_size") italic = getstyle("italic", False) bold = getstyle("bold", False) text_width = self.getgeom("text_width") multiline = bool(text_width) color = [int(c * 255) for c in fg] if self.html: self.label = text.HTMLLabel(text=self.text, width=text_width, multiline=multiline, anchor_x='center', anchor_y='center') self.label.set_style('color', color) else: self.label = text.Label(text=self.text, font_name=font, font_size=font_size, color=color, italic=italic, bold=bold, width=text_width, multiline=multiline, anchor_x='center', anchor_y='center')
def label(xy, wh, text, size=10, color=None, batch=None, group=0, resize=True, xyPercInside=None): if xyPercInside: xy = [ xy[0] + reduceTo(wh[0], xyPercInside[0]), xy[1] + reduceTo(wh[1], xyPercInside[1]) ] label = pyTxt.Label(text, 'Times New Roman', size, x=xy[0], y=xy[1], batch=batch, group=genGroup(group)) if resize: while wh[1] > label.content_height: label.font_size += 1 while wh[1] < label.content_height: label.font_size -= 1 return label
def __init__(self): # self.canvas = None # self.context = None self.canvas_width = 1400 # 700 self.canvas_height = 1000 # 500 self.ai1 = Paddle(self, 'left') self.ai1.height = self.canvas_height // 3 # for debugging, need to remove. #self.ai1.speed = 5 # 6 self.ai2 = Paddle(self, 'right') self.ball = Ball(self) self.winning_score = 11 self.qagent = Qagent(self, self.ai1, self.ai2, self.ball) # self.ai2.speed = 8 # make ai2's paddle speed slower than ai1. # self.running = False # to check whether the game is running. self.turn = self.ai2 # it's the ai2's turn first. #self.round = 0 self.qlearn_mode = False # self.timer = 0 # self.color = '#000000' # color the game background black. # self.menu() # self.listen() # No need for these methods: # def end_game_menu(text): pass # def menu(): pass # def draw(): pass # def listen(): pass super().__init__(self.canvas_width, self.canvas_height, caption='Q-learning Pong') # create the paddles and the ball. self.paddle_colors = (255,255,255) self.ai1_rect = shapes.Rectangle(self.ai1.x, self.ai1.y, self.ai1.width, self.ai1.height, color=self.paddle_colors) #self.ai1_rect.opacity = 255 self.ai2_rect = shapes.Rectangle(self.ai2.x, self.ai2.y, self.ai2.width, self.ai2.height, color=self.paddle_colors) self.ball_rect = shapes.Rectangle(self.ball.x, self.ball.y, self.ball.width, self.ball.height, color=self.paddle_colors) self.line = shapes.Line(self.canvas_width//2, 0, self.canvas_width//2, self.canvas_height) self.ai1_scoreboard = text.Label("AI1: " + str(self.ai1.score), font_name='Courier New', font_size=50, x=(self.canvas_width // 2) - 500, y=800) self.ai2_scoreboard = text.Label("AI2: " + str(self.ai2.score), font_name='Courier New', font_size=50, x=(self.canvas_width // 2) + 200, y=800)
def add_solved(self, num, coords): obj = self.__object_matrice[coords] label = text.Label("{0}".format(num), font_name='Times New Roman', font_size=36, x=obj.x+obj.width/4, y=obj.y+obj.height/5, ) self.actives.append(label)
def set_label(self, batch): self.label = text.Label(f'{self.bucket}', font_name='Times New Roman', font_size=20, x=self.x, y=self.y, anchor_x='center', anchor_y='center', batch=batch)
def make_sym_labels(anchor_y: str): icon_label = txt.Label( self.card.suit.icon(), color = colour, font_name = 'Source Code Pro', font_size = 9, anchor_y = anchor_y, batch = self.batch, group = self.group_front ) value_label = txt.Label( str(self.card.value), color = colour, font_name = 'Source Code Pro', font_size = 6 if self.card.value == CardValue.TEN else 8, anchor_y = anchor_y, batch = self.batch, group = self.group_front ) return (icon_label, value_label)
def label(texty, y): label = text.Label( texty, x=335, color=(255, 0, 0, 255), y=y, font_name='Arial', font_size=20, ) return label
def on_add_text(self, t): ''' Update the scrolling text ''' x, y = director.get_window_size() l = text.Label(t, (randint(0,x - 100), randint(0,y - 50)), font_name='Gill Sans', font_size=40, anchor_x='center', anchor_y='center') l.element.color = (randint(0, 255), randint(0, 255), randint(0, 255), 255) self.objects.append(l) self.add(l)
def number_label(array): global list_draw, step cost = 0 for index in range(len(array)): cost += 66 number_label = text.Label(str(array[index]), x=275, y=-(15 - len(array)) / 2 * 66 + 1000 - cost, font_name='Arial', font_size=20) list_draw.append(number_label)
def _add_etc_left(self): pos = self._bounds_inner.centre_left - CardRowDrawer.ETC_PADDING self._etc_left = txt.Label("<<", color=(230, 230, 230, 255), x=pos.x, y=pos.y, anchor_x='right', anchor_y='center', font_name='Source Code Pro', font_size=16, batch=self._batch, group=self._group)
def draw_game_over(): game_over_screen = text.Label( f'Score: {len(tail)}\n(Press space to restart)', font_size=24, x=window.width // 2, y=window.height // 2, width=window.width, align='center', anchor_x='center', anchor_y='center', multiline=True) game_over_screen.draw()
def _create_label(self): self._label = text.Label(self.msg, font_name='Times New Roman', font_size=14, x=self._board_width // 2, y=self._board_height + LABEL_HEIGHT - LABEL_HEIGHT // 2, width=self._board_width - LABEL_PADDING, align='center', anchor_x='center', anchor_y='center', multiline=True)
def __init__(self, x, y, w, h, title="", function=None): self.x, self.y = x, y self.w, self.h = w, h self.label = text.Label(title, font_name="Arial", font_size=36, x=self.x, anchor_x='center', y=self.y, anchor_y='center') self.function = function self.isHover, self.isDown = False, False
def draw_game_over(): """Display game over message""" game_over_screen = text.Label(f'{game_over_msg}\nPress space to restart', font_size=24, x=window.width // 2, y=window.height // 2, width=window.width, align='center', anchor_x='center', anchor_y='center', multiline=True) game_over_screen.draw()
def draw_text(x, y, t: str, *args, **kwargs): label = text.Label(t, font_name='Fantasque Sans Mono', font_size=12, x=x, y=win.height - y, width=800, multiline=True, color=kwargs.get('color', (255, 255, 255, 255)) # anchor_y='bottom' ) label.draw()
def __init__(self, parent, x, y, options): super(SimpleMenu, self).__init__() self.parent = parent self.x, self.y = x, y self.options = [] y = y self.width = 0 for option in options: l = text.Label(option, x=x, y=y, color=(0, 0, 0, 255), anchor_y='top', batch=self) self.options.append(l) self.width = max(self.width, l.content_width) y -= l.content_height self.height = abs(self.y - y) # add some padding self.height += 4 self.width += 4 # adjust menu position to make sure whole menu is visible if self.x < 0: self.x = 0 if self.x + self.width > parent.width: self.x = parent.width - self.width if self.y - self.height < 0: self.y = self.height if self.y > parent.height: self.y = parent.height # reposition the items y = self.y - 2 for option in self.options: option.x = self.x + 2 option.y = y y -= l.content_height self.created_time = time.time() parent.push_handlers(self)
def init(program): global window window = program.window gl.glClearColor(1.2/100, 6.7/100, 19.2/100, 1) global label label = text.Label('Search', font_name='Verdana', font_size=36, x=window.width//2, y=window.height*0.5, anchor_x='center', anchor_y='center') @window.event def on_draw(): draw() @window.event def on_resize(*args): resize(*args)
def render_button(self, button, pos, size, overed=False, selected=False): b_color = (50, 50, 50) if overed else (0, 0, 0) box = shapes.BorderedRectangle(pos[0], pos[1], size[0], size[1], color=b_color, border_color=(255, 255, 255)) dev = button.item label = text.Label(button.name, x=pos[0] + size[0] / 2, y=pos[1] + size[1] / 2, anchor_x='center', anchor_y='center') box.draw() #if dev is not None: # self.draw_device(dev, pos[0] + 10, pos[1] + 10) label.draw()
def __init__(self, **kwds): """Create a TextBox object. Additional arguments are handled by `InterfaceComponent`. :Parameters: `font_name` : str The name of the font family to use. `font_size` : float The size at which to draw the font. `color` : (int, int, int, [int]) The color in which to draw the text. `text` : unicode The text to display. """ self._text = kwds.pop("text", u"") super(TextBox, self).__init__(**kwds) self.label = text.Label("")
def connect(self, control): super(GameOverMode, self).connect(control) if not self.initialised: self.animate_fade_in() self.ach_display = achievementdisplay.AchievementDisplay( self.control.window.width, self.control.window.height) txt = "You Win!" if self.game.victorious else "Game Over!" self.game_over_label = text.Label(txt, font_name=FONT_NAME, font_size=0.1 * self.window.height, batch=self.batch, x=0.5 * self.window.width, y=0.85 * self.window.height, anchor_x='center', halign='center') achievement.handler.push_handlers(self) self.window.push_handlers(self.menu)
def __init__(self, parent, name, x, y, text, color=(255, 255, 255), visible=True, **kwargs): """ Label constructor. :type x: float :param x: x coord :type y: float :param y: y coord :type text: str :param text: text :type batch: pyglet.graphics.Batch :param batch: the window's batch :type color: list(int * 3) :param color: color """ super().__init__(parent, name, x, y, 0, 0, visible) self.text = text self._pyglet_label = _text.Label(text, font_name='Menlo', font_size=8, x=x, y=y, batch=parent._batch, group=self.og2, color=(*color, 255), **kwargs) self.set_visible(visible)
def _update_visible(self): pad = CardDrawer.PADDING colour_ = (CardDrawer.RED_COLOUR if self.card.colour == CardColour.RED else CardDrawer.BLACK_COLOUR) colour = (*colour_, self.opacity) def make_sym_labels(anchor_y: str): icon_label = txt.Label( self.card.suit.icon(), color = colour, font_name = 'Source Code Pro', font_size = 9, anchor_y = anchor_y, batch = self.batch, group = self.group_front ) value_label = txt.Label( str(self.card.value), color = colour, font_name = 'Source Code Pro', font_size = 6 if self.card.value == CardValue.TEN else 8, anchor_y = anchor_y, batch = self.batch, group = self.group_front ) return (icon_label, value_label) self.sym_labels_top = make_sym_labels('top') self.sym_labels_bottom = make_sym_labels('baseline') self._adjust_sym_labels() self.num_label = txt.Label( str(self.card.value), color = colour, x = self.centre.x, y = self.centre.y, anchor_x = 'center', anchor_y = 'center', font_name = 'Source Code Pro', bold = True, font_size = 13, batch = self.batch, group = self.group_front )
def draw_node(self, node, net_color, batch): x = node.pos[0] y = node.pos[1] output = [] shape = shapes.Circle(x=x, y=y, radius=NODE_SIZE / 4, color=net_color, batch=batch, group=Renderer.group_network_nodes) output.append(shape) color = (255, 255, 255, 255) if node.current_net is not None: color = net_colors[node.current_net % len(net_colors)] color = (color[0], color[1], color[2], 255) label = text.Label(str(node.state), color=color, font_name='Arial', font_size=12, x=x, y=y + 20, anchor_x='center', anchor_y='center', batch=batch, group=Renderer.group_network_text) output.append(label) for c in node.connections: line = shapes.Line(c.node1.pos[0], c.node1.pos[1], c.node2.pos[0], c.node2.pos[1], width=2, color=net_color, batch=batch, group=Renderer.group_network_connections) output.append(line) return output
def on_draw(): window.clear() shapes.Rectangle(x=0, y=0, width=board_size, height=board_size, color=(30, 100, 160)).draw() for i in range(N): p = (i + 0.5) * field_size shapes.Line(x=p, y=0, x2=p, y2=board_size).draw() shapes.Line(x=0, y=p, x2=board_size, y2=p).draw() for i, row in enumerate(board.fields): for j, color in enumerate(row): if color != Color.NONE: v = 200 if color == Color.WHITE else 30 x, y = (i + 0.5) * field_size, (j + 0.5) * field_size shapes.Circle(x=x, y=y, radius=field_size / 2 - 2, color=(v, v, v)).draw() if last_pos == (i, j): shapes.Rectangle(x=x - 2, y=y - 2, width=5, height=5, color=(255, 0, 0)).draw() if info: text.Label(info, x=window.width / 2, y=50, anchor_x='center', color=(255, 255, 0, 255), bold=True).draw()
def batch(self, batch=None, show_basis_vectors=True, show_endpoint_coordinates=True): ''' Returns a pyglet.graphics.Batch containing vertex lists for all vectors in self.vectors, the basis vectors, and gridlines. ''' if batch is None: batch = graphics.Batch() if self.gridlines > 1: i_hat = self.basis[0] j_hat = self.basis[1] for i in range(0, self.gridlines): y = -j_hat + i * (j_hat*2/self.gridlines) v0 = self._transform_to_window_coordinates([-i_hat, y]) v1 = self._transform_to_window_coordinates([i_hat, y]) batch.add(2, gl.GL_LINES, None, ('v2f', [v0[0], v0[1], v1[0], v1[1]]), ('c4B', [64, 64, 64, 1]*2) ) for j in range(0, self.gridlines): x = -i_hat + j * (i_hat*2/self.gridlines) v0 = self._transform_to_window_coordinates([x, -j_hat]) v1 = self._transform_to_window_coordinates([x, j_hat]) batch.add(2, gl.GL_LINES, None, ('v2f', [v0[0], v0[1], v1[0], v1[1]]), ('c4B', [64, 64, 64, 1]*2) ) if show_basis_vectors is True: # Add basis vector lines to batch # i^ v0 = self._transform_to_window_coordinates([0, 0]) v1 = self._transform_to_window_coordinates([self.basis[0], 0]) color = [1.0, 0.0, 0.0] batch.add(2, gl.GL_LINES, None, ('v2f', [v0[0], v0[1], v1[0], v1[1]]), ('c3f', color*2) ) # j^ v0 = self._transform_to_window_coordinates([0, 0]) v1 = self._transform_to_window_coordinates([0, self.basis[1]]) color = [0.0, 1.0, 0.0] batch.add(2, gl.GL_LINES, None, ('v2f', [v0[0], v0[1], v1[0], v1[1]]), ('c3f', color*2) ) for vector in self.vectors: # Add vector lines to batch v0 = self._transform_to_window_coordinates(vector.origin) v1 = self._transform_to_window_coordinates(vector.endpoint) batch.add(2, gl.GL_LINES, None, ('v2f', [v0[0], v0[1], v1[0], v1[1]]), ('c3f', vector.color*2) ) if show_endpoint_coordinates is True: # Display planespace coordinates x = round(vector.endpoint[0], 3) y = round(vector.endpoint[1], 3) text.Label(str(x) + ", " + str(y), font_name='Consolas', font_size=12, x=v1[0]+10, y=v1[1]+10).draw() return batch
image_player_rock = image.load(image_dir / "choice_player-01.png") image_player_paper = image.load(image_dir / "choice_player-02.png") image_player_scissors = image.load(image_dir / "choice_player-03.png") chosen_image = image_player_rock player_start = sprite.Sprite(chosen_image, players_choice_pos[0], players_choice_pos[1], batch = options_all) image_pc_rock = image.load(image_dir / "choice_pc-01.png") image_pc_paper = image.load(image_dir / "choice_pc-02.png") image_pc_scissors = image.load(image_dir / "choice_pc-03.png") image_questionmark = image.load(image_dir / "questionmark.png") pc_start = sprite.Sprite(image_pc_rock, comps_choice_pos[0], comps_choice_pos[1]) questionmark = sprite.Sprite(image_questionmark, questionmark_pos[0], questionmark_pos[1], batch = options_all) pc_options = [image_pc_rock, image_pc_paper, image_pc_scissors] #global labels gameover_screen = text.Label("x win!", font_size = 24) tie_label = text.Label(f"It's a tie!", font_size = 20, x = 550, y = 250, anchor_x="center") chose_one = text.Label(f"click \non one:", font_size = 20, x = 1050, y = 470, width = 100, align="center", anchor_x = "right", anchor_y="center", multiline=True) @window.event def on_draw(): global its_a_tie, options_all window.clear() options_all.draw() game.draw() game.restart() player_start.draw() pc_start.draw() chose_one.draw() if its_a_tie == 1: