Compare commits
2 Commits
ed4b5abd24
...
1b73dc208b
Author | SHA1 | Date |
---|---|---|
|
1b73dc208b | |
|
a03bfaf3dc |
|
@ -0,0 +1,92 @@
|
|||
import sys,os
|
||||
import curses
|
||||
|
||||
def draw_menu(stdscr):
|
||||
k = 0
|
||||
cursor_x = 0
|
||||
cursor_y = 0
|
||||
|
||||
# Clear and refresh the screen for a blank canvas
|
||||
stdscr.clear()
|
||||
stdscr.refresh()
|
||||
|
||||
# Start colors in curses
|
||||
curses.start_color()
|
||||
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
|
||||
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
|
||||
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
|
||||
|
||||
# Loop where k is the last character pressed
|
||||
while (k != ord('q')):
|
||||
|
||||
# Initialization
|
||||
stdscr.clear()
|
||||
height, width = stdscr.getmaxyx()
|
||||
|
||||
if k == curses.KEY_DOWN:
|
||||
cursor_y = cursor_y + 1
|
||||
elif k == curses.KEY_UP:
|
||||
cursor_y = cursor_y - 1
|
||||
elif k == curses.KEY_RIGHT:
|
||||
cursor_x = cursor_x + 1
|
||||
elif k == curses.KEY_LEFT:
|
||||
cursor_x = cursor_x - 1
|
||||
|
||||
cursor_x = max(0, cursor_x)
|
||||
cursor_x = min(width-1, cursor_x)
|
||||
|
||||
cursor_y = max(0, cursor_y)
|
||||
cursor_y = min(height-1, cursor_y)
|
||||
|
||||
# Declaration of strings
|
||||
title = "Curses example"[:width-1]
|
||||
subtitle = "Written by Clay McLeod"[:width-1]
|
||||
keystr = "Last key pressed: {}".format(k)[:width-1]
|
||||
statusbarstr = "Press 'q' to exit | STATUS BAR | Pos: {}, {}".format(cursor_x, cursor_y)
|
||||
if k == 0:
|
||||
keystr = "No key press detected..."[:width-1]
|
||||
|
||||
# Centering calculations
|
||||
start_x_title = int((width // 2) - (len(title) // 2) - len(title) % 2)
|
||||
start_x_subtitle = int((width // 2) - (len(subtitle) // 2) - len(subtitle) % 2)
|
||||
start_x_keystr = int((width // 2) - (len(keystr) // 2) - len(keystr) % 2)
|
||||
start_y = int((height // 2) - 2)
|
||||
|
||||
# Rendering some text
|
||||
whstr = "Width: {}, Height: {}".format(width, height)
|
||||
stdscr.addstr(0, 0, whstr, curses.color_pair(1))
|
||||
|
||||
# Render status bar
|
||||
stdscr.attron(curses.color_pair(3))
|
||||
stdscr.addstr(height-1, 0, statusbarstr)
|
||||
stdscr.addstr(height-1, len(statusbarstr), " " * (width - len(statusbarstr) - 1))
|
||||
stdscr.attroff(curses.color_pair(3))
|
||||
|
||||
# Turning on attributes for title
|
||||
stdscr.attron(curses.color_pair(2))
|
||||
stdscr.attron(curses.A_BOLD)
|
||||
|
||||
# Rendering title
|
||||
stdscr.addstr(start_y, start_x_title, title)
|
||||
|
||||
# Turning off attributes for title
|
||||
stdscr.attroff(curses.color_pair(2))
|
||||
stdscr.attroff(curses.A_BOLD)
|
||||
|
||||
# Print rest of text
|
||||
stdscr.addstr(start_y + 1, start_x_subtitle, subtitle)
|
||||
stdscr.addstr(start_y + 3, (width // 2) - 2, '-' * 4)
|
||||
stdscr.addstr(start_y + 5, start_x_keystr, keystr)
|
||||
stdscr.move(cursor_y, cursor_x)
|
||||
|
||||
# Refresh the screen
|
||||
stdscr.refresh()
|
||||
|
||||
# Wait for next input
|
||||
k = stdscr.getch()
|
||||
|
||||
def main():
|
||||
curses.wrapper(draw_menu)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,18 @@
|
|||
import curses, curses.textpad
|
||||
def setup_input():
|
||||
inp = curses.newwin(8,55, 0,0)
|
||||
inp.addstr(1,1, "Please enter your username:")
|
||||
sub = inp.subwin(2,1)
|
||||
sub.border()
|
||||
sub2 = sub.subwin(3,2)
|
||||
global tb
|
||||
tb = curses.textpad.Textbox(sub2)
|
||||
inp.refresh()
|
||||
tb.edit(enter_is_terminate)
|
||||
|
||||
def enter_is_terminate(x):
|
||||
if x == 10:
|
||||
tb.do_command(7)
|
||||
tb.do_command(x)
|
||||
|
||||
setup_input()
|
|
@ -1,40 +1,89 @@
|
|||
from blessed import Terminal
|
||||
import curses
|
||||
from curses.textpad import Textbox, rectangle
|
||||
|
||||
class Graphics:
|
||||
def __init__(self):
|
||||
self.bottom_left_corner = u"└"
|
||||
self.bottom_right_corner = u"┘"
|
||||
self.top_left_corner = u"┌"
|
||||
self.top_right_corner = u"┐"
|
||||
self.border_horizontal = u"─"
|
||||
self.border_vertical = u"│"
|
||||
|
||||
class Window:
|
||||
def __init__(self, term, height, width, xpos, ypos, title=None):
|
||||
self.term = term
|
||||
|
||||
self.title = title
|
||||
def draw_main_menu(stdscr):
|
||||
# Command is the last char the user entered.
|
||||
command = 0
|
||||
|
||||
self.height = height
|
||||
self.width = width
|
||||
self.xpos = xpos
|
||||
self.ypos = ypos
|
||||
|
||||
def draw_borders(self):
|
||||
# Print the top
|
||||
print(
|
||||
self.term.move_xy(self.xpos, self.ypos) +
|
||||
u"┌" +
|
||||
u"─" * (self.width - 2) +
|
||||
u"┐"
|
||||
)
|
||||
# Clear, and refresh the screen
|
||||
stdscr.clear()
|
||||
stdscr.refresh()
|
||||
|
||||
# Print the middle
|
||||
# We exclude the top and bottom rows since we'll draw them with other chars
|
||||
for dx in range (1, self.height - 1):
|
||||
print(self.term.move_xy(self.xpos + dx, self.ypos) + u'│')
|
||||
print(self.term.move_xy(self.xpos + dx, self.ypos + self.width - 1) + u'│')
|
||||
# Start some curses colors
|
||||
curses.start_color()
|
||||
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
|
||||
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
|
||||
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
|
||||
|
||||
terminal = Terminal()
|
||||
my_window = Window(terminal, 4, 20, 10, 10)
|
||||
my_window.draw_borders()
|
||||
while command != ord('q'):
|
||||
# Clear the screen
|
||||
stdscr.clear()
|
||||
# Get the height, and width
|
||||
height, width = stdscr.getmaxyx()
|
||||
|
||||
|
||||
# Temporary strings, replace these with libdocs!
|
||||
title = 'Untitled Adventure Game PRE ALPHA'[:width - 1]
|
||||
subtitle = 'Joe Sedutto'[:width - 1]
|
||||
statusbarstr = "Press 'q' to exit "
|
||||
|
||||
# Centering calculations
|
||||
start_x_title = int((width // 2) - (len(title) // 2) - len(title) % 2)
|
||||
start_x_subtitle = int((width // 2) - (len(subtitle) // 2) - len(subtitle) % 2)
|
||||
start_y = int((height // 2) - 2)
|
||||
|
||||
# Rendering some text
|
||||
whstr = 'Width: {}, Height: {}'.format(width, height)
|
||||
stdscr.addstr(0, 0, whstr, curses.color_pair(1))
|
||||
|
||||
# Render status bar
|
||||
stdscr.attron(curses.color_pair(3))
|
||||
stdscr.addstr(height - 1, 0, statusbarstr)
|
||||
stdscr.addstr(height - 1, len(statusbarstr), ' ' * (width - len(statusbarstr) - 1))
|
||||
stdscr.attroff(curses.color_pair(3))
|
||||
|
||||
# Turning on attributes for title
|
||||
stdscr.attron(curses.color_pair(2))
|
||||
stdscr.attron(curses.A_BOLD)
|
||||
|
||||
# Rendering title
|
||||
stdscr.addstr(start_y, start_x_title, title)
|
||||
|
||||
# Turning off attributes for title
|
||||
stdscr.attroff(curses.color_pair(2))
|
||||
stdscr.attroff(curses.A_BOLD)
|
||||
|
||||
# Print rest of text
|
||||
stdscr.addstr(start_y + 1, start_x_subtitle, subtitle)
|
||||
stdscr.addstr(start_y + 3, (width // 2) - 2, '-' * 4)
|
||||
|
||||
# Refresh the screen
|
||||
stdscr.refresh()
|
||||
|
||||
# Wait for next input
|
||||
command = stdscr.getch()
|
||||
|
||||
|
||||
def main_textbox(stdscr):
|
||||
stdscr.addstr(0, 0, "Enter IM message: (hit Ctrl-G to send)")
|
||||
|
||||
editwin = curses.newwin(5,30, 2,1)
|
||||
rectangle(stdscr, 1,0, 1+5+1, 1+30+1)
|
||||
stdscr.refresh()
|
||||
|
||||
box = Textbox(editwin)
|
||||
|
||||
# Let the user edit until Ctrl-G is struck.
|
||||
box.edit()
|
||||
|
||||
# Get resulting contents
|
||||
message = box.gather()
|
||||
|
||||
def main():
|
||||
curses.wrapper(main_textbox)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
from blessed import Terminal
|
||||
import time
|
||||
|
||||
term = Terminal()
|
||||
|
||||
for x in range(10):
|
||||
for y in range(10):
|
||||
print(term.move_xy(x, y) + '*')
|
||||
time.sleep(0.1)
|
|
@ -0,0 +1,86 @@
|
|||
import curses
|
||||
from curses.textpad import Textbox, rectangle
|
||||
|
||||
|
||||
class AdventureGame:
|
||||
def __init__(self, stdscr):
|
||||
# Self.stdrscr is our parent standard screen
|
||||
self.stdscr = stdscr
|
||||
|
||||
# This is just done once at startup to get inital windows sizes
|
||||
# TODO: handle live window resize!
|
||||
self.height, self.width = self.stdscr.getmaxyx()
|
||||
|
||||
self.command = 0
|
||||
|
||||
# Clear and refresh the screen
|
||||
self.stdscr.clear()
|
||||
self.stdscr.refresh()
|
||||
|
||||
# Setup some colors!
|
||||
# Start some curses colors
|
||||
curses.start_color()
|
||||
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
|
||||
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
|
||||
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
|
||||
|
||||
# Setup art_win
|
||||
art_win_width = int((self.width / 3) * 2)
|
||||
art_win_begin_x = int(self.width - art_win_width)
|
||||
art_win_begin_y = 1 # Must be at least 1 (to allow header)
|
||||
art_win_height = int(self.height / 2)
|
||||
|
||||
art_win = curses.newwin(art_win_height, art_win_width, art_win_begin_y, art_win_begin_x)
|
||||
|
||||
# Setup dialogue_win
|
||||
dialogue_win_width = self.width
|
||||
dialogue_win_begin_x = 0
|
||||
dialogue_win_begin_y = art_win_height + 1 # Fits 1 underneath the art box above.
|
||||
dialogue_win_height = self.height - art_win_height - 2 # Fits 2 underneath the bottom box,
|
||||
|
||||
dialogue_win = curses.newwin(dialogue_win_height, dialogue_win_width, dialogue_win_begin_y,
|
||||
dialogue_win_begin_x)
|
||||
|
||||
while self.command != 'q':
|
||||
# Get the height of everything
|
||||
self.height, self.width = self.stdscr.getmaxyx()
|
||||
|
||||
self.title = 'Untitled Adventure Game PRE ALPHA'[:self.width - 1]
|
||||
self.statusbarstr = "Press 'q' to exit "
|
||||
|
||||
# Render status bar
|
||||
self.stdscr.attron(curses.color_pair(3))
|
||||
self.stdscr.addstr(self.height - 1, 0, self.statusbarstr)
|
||||
self.stdscr.addstr(self.height - 1, len(self.statusbarstr), ' ' * (self.width - len(self.statusbarstr) - 1))
|
||||
self.stdscr.attroff(curses.color_pair(3))
|
||||
|
||||
# Render major game windows
|
||||
self.draw_art_window(art_win)
|
||||
self.command = self.draw_dialogue_box(dialogue_win)
|
||||
|
||||
# Refresh the screen
|
||||
self.stdscr.refresh()
|
||||
|
||||
# Wait for next input
|
||||
# self.command = self.stdscr.getch()
|
||||
curses.echo()
|
||||
#self.command = self.stdscr.getstr(0, 0)
|
||||
|
||||
def draw_main_menu(self):
|
||||
pass
|
||||
|
||||
def draw_art_window(self, window):
|
||||
window.addstr(0, 0, "Wip")
|
||||
window.box()
|
||||
window.refresh()
|
||||
|
||||
def draw_dialogue_box(self, window):
|
||||
window.box()
|
||||
window.refresh()
|
||||
|
||||
textbox = Textbox(window)
|
||||
return textbox.gather()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
curses.wrapper(AdventureGame)
|
Loading…
Reference in New Issue