0

I want to make a simple game in Python where you are the '@' sign and can move around a room.

level = """
  ┌-------------------------------------------┐
  |                                           |
  |                                           |
  |                                           |
  |                                           |
  |                                           |
  |                                           |
  |                    @                      |
  |                                           |
  |                                           |
  |                                           |
  |                                           |
  |                                           |
  |                                           |
  |                                           |
  └-------------------------------------------┘
"""
print level
moving = raw_input(' ')
if moving == str('w'):
    level == """
 ┌-------------------------------------------┐
 |                                           |
 |                                           |
 |                                           |
 |                                           |
 |                                           |
 |                    @                      |
 |                                           |
 |                                           |
 |                                           |
 |                                           |
 |                                           |
 |                                           |
 |                                           |
 |                                           |
 └-------------------------------------------┘
   """
    print level 

That is my code so far. I want to make it so that when the user inputs 'w' the first 'level' printed is replaced in the terminal. Is this possible? If so how do I do this? If not how (alternatively) would I do this? [edit] when i input 'w' i want it to replace the first 'level' printed in the program. instead of printing out a new instance of 'level' sorry for not explaining well enough

Dannyboy8899
  • 101
  • 2
  • 7

2 Answers2

4

The curses module provide a way to create TUIs(Terminal User Interfaces). You can find a tutorial here.

A small example related to what you want to do:

import curses
curses.initscr()
curses.noecho()    # don't echo the keys on the screen
curses.cbreak()    # don't wait enter for input

window = curses.newwin(10, 10, 0, 0)  # create a 10x10 window
window.box()       # Draw the box outside the window
cur_x = 1
cur_y = 1
while True:
    window.addch(cur_y, cur_x, '@')
    window.refresh()
    inchar = window.getch()
    window.addch(cur_y, cur_x, ' ')
    # W,A,S,D used to move around the @
    if inchar == ord('w'):
        cur_y -= 1
    elif inchar == ord('a'):
        cur_x -= 1
    elif inchar == ord('d'):
        cur_x += 1
    elif inchar == ord('s'):
        cur_y += 1
    elif inchar == ord('q'):
        # stop the program when Q is entered.
        break
curses.endwin()

Save it in a file test_curses.py then run:

python test_curses.py

In the terminal and use WASD to move around the @.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • and how would i add static text using curses? that stays there even if the program is scrolling down? – Dannyboy8899 Jun 15 '13 at 07:46
  • @user2487886 It depends on what you mean. Probably the cleaniest way is to simply put the text in a different window. – Bakuriu Jun 15 '13 at 08:00
  • but could i display it in the terminal? like on the side? like a rougelike – Dannyboy8899 Jun 15 '13 at 08:03
  • @user2487886 Yes. Curses is *the* library used to write rogue-like programs. – Bakuriu Jun 15 '13 at 08:04
  • so curses isn't in the default python right? where do u download the library? – Dannyboy8899 Jun 15 '13 at 08:05
  • because when i try running your code it says it cannot import curses Traceback (most recent call last): File "C:/Python27/curses_test.py", line 1, in import curses File "C:\Python27\lib\curses\__init__.py", line 15, in from _curses import * ImportError: No module named _curses – Dannyboy8899 Jun 15 '13 at 08:08
  • @user2487886 Which operating system are you using? Python `curses` is a standard library module, but on some OS the `ncurses` C-library isn't installed by default and hence the module cannot be used. If you are on windows you should check [this](http://stackoverflow.com/questions/1244897/curses-like-library-for-cross-platform-console-app-in-python) question and answers, since the normal curses library doesn't work there. – Bakuriu Jun 15 '13 at 08:12
  • Downloads for Windows are available at http://www.lfd.uci.edu/~gohlke/pythonlibs/ – Matthias Jun 15 '13 at 12:47
0

I've never used curses (as mentioned by Bakuriu), but the ability to change a character on the screen without changing anything else on the screen would make all this much easier.

As a workaround, I'd recommend you just clear the screen between instances:

import os
os.system('clear')

EDIT: Curses uses the move(x,y) command to position the cursor at any location required, and if you don't want the cursor to be displayed, you can use curs_set(False). (http://docs.python.org/dev/howto/curses.html)

shashwat
  • 992
  • 1
  • 13
  • 27