LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2011, 10:18 AM   #1
daweefolk
Member
 
Registered: Sep 2009
Distribution: raspbian
Posts: 94

Rep: Reputation: 15
python: writing to a curses window from a class


I'm trying to make a game in python with a nethack-style map, I'm using the curses module to organize my screen.
I have a class in a module (characters.py) called "Character", which will control npc movement. How can I make it so, when an npc moves or is created, the curses window mapwin in the main file (main.py) updates?
Something like (in the Characters class)
Code:
def __init__(self,y,x):
  self.y=y#track character's
  self.x=x#y and x location
  main.mapwin.addstr(self.y,self.x,"Q")
  #when created, place new character on 
  #map with the symbol "Q"
  main.mapwin.refresh()
I can't figure out how to access the curses windows from an external class. Is it even possible?
 
Old 03-24-2011, 11:24 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
You'd have to pass it to the class. What you could do is add another variable:
Code:
class Characters:
    window = None
    def __init__(self,y,x,window=None):
        self.y = y
        self.x = x

        if window != None:
            Characters.window = window
        Characters.window.addstr(self.y,self.x,"Q")
        Characters.window.refresh()
You would then at some point need to initialise a character and pass the 'window' argument, so it sets the variable for all characters. Alternatively, you could remove it from the constructor, and make a new function called "setClassWindow(window)" or something similar, which just sets "Characters.window = window"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] python and curses : unable to getch() for a curses.KEY event mangec Programming 1 11-09-2010 04:21 PM
[SOLVED] Python n00b: promoting a derived class to base class? CoderMan Programming 2 03-11-2010 01:46 PM
Python Curses under Windows XP? introuble Programming 1 12-26-2006 07:59 AM
Messed up Python Curses happygiraffe98 Programming 2 12-02-2005 08:53 AM
python - make curses play nice w/ os.system() datadriven Programming 0 02-17-2005 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration