controllables.TextScreen
- class controllables.TextScreen(port: Optional[int] = None)
Reference to a text screen part.
Text screens can be useful both as the primary output for a script and as a static debugging output to examine the state of your program.
The following script uses the text screen for as its primary output and it assumes that it is assigned to port
0
and is at least 8 x 5 in size.from controllables import TextScreen from inputs import Input, ButtonState from time import time from random import random screen = TextScreen(0) screen.horizontal_alignment = TextScreen.HorizontalAlignment.LEFT screen.vertical_alignment = TextScreen.VerticalAlignment.TOP p1_up = Input.stream("w") p1_down = Input.stream("s") p2_up = Input.stream("i") p2_down = Input.stream("k") paddle_speed = 100 ball_speed = 1 arena_height = 5 arena_width = 20 score = [0,0] class GameState(): def __init__(self): self.paddle1 = 0 self.paddle2 = 0 self.ball = [arena_width // 2,arena_height // 2] self.balldir = [1 if random() < .5 else -1,1 if random() < .5 else -1] self.ballspeed = 2 def reset(self): self.ballspeed = 2 self.ball = [arena_width // 2,arena_height // 2] self.balldir = [1 if random() < .5 else -1,1 if random() < .5 else -1] def display(gamestate : GameState) -> str: lines = [[' ' for _ in range(0,arena_width)] for _ in range(0,arena_height)] lines[0][arena_width // 2 - 1] = str(score[0]) lines[0][arena_width // 2 + 1] = str(score[1]) lines[gamestate.paddle1][0] = '|' lines[gamestate.paddle2][arena_width - 1] = '|' lines[gamestate.ball[1]][gamestate.ball[0]] = '*' return '\n'.join([''.join(line) for line in lines]) def apply_input(gamestate : GameState): for event in p1_up: if event == ButtonState.UP: gamestate.paddle1 -= 1 for event in p1_down: if event == ButtonState.UP: gamestate.paddle1 += 1 for event in p2_up: if event == ButtonState.UP: gamestate.paddle2 -= 1 for event in p2_down: if event == ButtonState.UP: gamestate.paddle2 += 1 def clamp_paddles(gamestate : GameState): if gamestate.paddle1 < 0: gamestate.paddle1 = 0 if gamestate.paddle2 < 0: gamestate.paddle2 = 0 if gamestate.paddle1 >= arena_height: gamestate.paddle1 = arena_height - 1 if gamestate.paddle2 >= arena_height: gamestate.paddle2 = arena_height - 1 def clamp_ball(gamestate : GameState): if gamestate.ball[0] <= 0: if gamestate.paddle1 == gamestate.ball[1]: gamestate.ball[0] = 1 gamestate.balldir[0] = 1 gamestate.ballspeed *= 1.2 elif gamestate.ball[0] < 0: score[1] += 1 gamestate.reset() if gamestate.ball[1] < 0: gamestate.ball[1] = 0 gamestate.balldir[1] = 1 if gamestate.ball[0] >= arena_width - 1: if gamestate.paddle2 == gamestate.ball[1]: gamestate.ball[0] = arena_width - 2 gamestate.balldir[0] = -1 gamestate.ballspeed *= 1.2 elif gamestate.ball[0] > arena_width - 1: score[0] += 1 gamestate.reset() if gamestate.ball[1] >= arena_height: gamestate.ball[1] = arena_height - 1 gamestate.balldir[1] = -1 def move_ball(gamestate : GameState): gamestate.ball[0] += gamestate.balldir[0] gamestate.ball[1] += gamestate.balldir[1] gamestate = GameState() last_time_ball_moved = time() while True: apply_input(gamestate) clamp_paddles(gamestate) if time() - last_time_ball_moved > 1 / gamestate.ballspeed: move_ball(gamestate) last_time_ball_moved = time() clamp_ball(gamestate) screen.text = display(gamestate)
- property text: str
The text value that the screen displays.
- property size: float
The font size of the text.
- property color: color.Color
The color of the text.
- name() str
Returns the user editable name of the controllable as found in the properties tab of the game.
from ports import PortReference print(PortReference(0).name())
- property horizontal_alignment: HorizontalAlignment
The horizontal alignment setting. Centered by default.
- property vertical_alignment: VerticalAlignment
The vertical alignment setting. Centered by default.