A curses-game I need help with.

Rob Wolfe rw at smsnet.pl
Mon Oct 9 22:35:43 CEST 2006


"Gasten" <operagasten at gmail.com> writes:

> Can you help?

I'm not a guru on curses by any means, but I've moved one line
of your code and IMHO it works fine now.
Look below for the code I've changed.

> I'll just append all my code here (for anyone to test), so you who
> doesn't want to read this humble beginners code can skip to another
> message. Thank you  for your time.
>
> (So far I've understood, is that iMsg() and updateMsg() is wrong. Line
> 71 and 92.)
>
>
> try:
>     import curses
> except ImportError:
>     print "Missing the Curses-library."
>     print "Please install the curses-library correctly."
>     SystemExit
>
> #---------------------------------------------------------------
>
> class Game:
>     """The Game-object. Controls the windows, global settings and so
> on."""
>     def __init__(self):
>         self.mainwin = curses.initscr()
>
>         ## Some basic settings
>         curses.noecho()
>         curses.cbreak()
>         self.mainwin.keypad(1)
>         curses.curs_set(0)
>
>         self.msg = Msg()
>         self.msg.msg(["Welcome to The Game! Lets watch another fearless
> samurai", "get beaten bloody in the arena!"])
>
>         self.mainwin.addstr(3,15, '+' + '-' * 62 + '+')
>         self.mainwin.addstr(22,15, '+' + '-' * 62 + '+')
>         self.mainwin.vline(4,15, '|', 18)
>         self.mainwin.vline(4,78, '|', 18)
>
>
>     movedir = {
>         'up':           {'x':0, 'y':-1},
>         'down':         {'x':0, 'y':+1},
>         'right':        {'x':+1, 'y':0},
>         'left':         {'x':-1, 'y':0},
>         'downright':    {'x':+1, 'y':+1},
>         'upright':      {'x':+1, 'y':-1},
>         'downleft':     {'x':-1, 'y':+1},
>         'upleft':       {'x':-1, 'y':-1}
>         }
>
>     walls = (
>         ord('|'),
>         ord('+'),
>         ord('-')
>         )
>
> #---------------------------------------------------------------
>
> class Msg:
>     """The messagehandeling and -bar system."""
>     def __init__(self):
>         self.msgs = []
>
>     def printMore(self):
>         # Print the little #More#-sign
>         game.mainwin.addch(1, 71, curses.ACS_BLOCK, curses.A_REVERSE)
>         game.mainwin.addstr(1, 72, "More", curses.A_REVERSE)
>         game.mainwin.addch(1, 76, curses.ACS_BLOCK, curses.A_REVERSE)
>
>     def msg(self, msg):
>         if isinstance(msg, str):
>             self.msgs.append(msg)
>         else:
>             self.msgs = self.msgs + msg
>
>     def ereasMsg(self):
>         for count in range(0,2):
>                 game.mainwin.move(count,0)
>                 game.mainwin.clrtoeol()
>
>     def updateMsg(self):
>         count = len(self.msgs)
>         for msg in self.msgs:               # Print one msg after
> another
>             game.mainwin.addstr(0,0, msg)   # Print the actual message
>             if count > 1:                   # If there are two or more
> laft, print a #More#-sign.
>                 count -= 1
>                 self.printMore()
>                 game.mainwin.refresh()
>                 while 1:
>                     next = game.mainwin.getch()
>                     if next == ord(' '):
>                         self.ereasMsg()
>                         break
>                     else:
>                         continue
>             else:
>                 game.mainwin.refresh()
>
>         self.msgs = [] # When done, kill all messages
>         return True
>
>     def iMsg(self, msg):
>         """Same as msg(), but the screen will refresh instantly.
>         'i' is standing for Interface or Instant message."""
>         if isinstance(msg, str):
>             msg = [msg]             # List'it!
>         count = len(msg)
>         for item in msg:
>             if count > 1:
>                 count -= 1
>                 self.printMore()
>                 game.mainwin.addstr(0,0, item)
>                 game.mainwin.refresh()
>                 while 1:
>                     next = game.mainwin.getch()
>                     if next == ord(' '):
>                         self.ereasMsg()
>                         break
>                     else:
>                         continue
>             else:
>                 game.mainwin.addstr(0,0, item)
>
>         return
>
>
> #---------------------------------------------------------------
>
> class Samurai:
>     """This is the object of the mighty Samurai. Phe4r him. He can
> move.
>     He can spawn. He will be able to kick som robo butt too,
>     but not now."""
>     def __init__(self):
>         ## Configuring game values.
>         self.loc = {
>             "x": 20,
>             "y": 8
>             }
>         self.samuraichar = ord("X")
>
>         # spawn
>         game.mainwin.addch(self.loc['y'], self.loc['x'],
> self.samuraichar)
>
>     def move(self, dir, speed=1):
>         # Move in dir speed number of times.
>         for count in range(1, speed + 1):
>             newX = self.loc['x'] + game.movedir[dir]['x']
>             newY = self.loc['y'] + game.movedir[dir]['y']
>
>             # Checks that all is clear.
>             if game.mainwin.inch(newY, newX) not in game.walls:
>
>                 # Delete old
>                 game.mainwin.addch(self.loc['y'], self.loc['x'], ' ')
>
>                 self.loc['x'] =  newX
>                 self.loc['y'] =  newY
>                 # Create a new character.
>                 game.mainwin.addch(self.loc['y'], self.loc['x'],
> self.samuraichar)
>
>             else:
>                 game.msg.iMsg("You can't go there.")
>                 return
>
> #---------------------------------------------------------------
>
> def main():
>     global game, player
>     game = Game()
>     player = Samurai()
>
>     running = True
>
>
>     while running:
>         key_command = game.mainwin.getch()

          # I've moved erasing messages here
          game.msg.ereasMsg()

>
>         if key_command == ord('8'):  ## Movement-commands
>             player.move('up')
>
>         elif key_command == ord('2'):
>             player.move('down')
>
>         elif key_command == ord('4'):
>             player.move('left')
>
>         elif key_command == ord('6'):
>             player.move('right')
>
>         elif key_command == ord('7'):
>             player.move('upleft')
>
>         elif key_command == ord('9'):
>             player.move('upright')
>
>         elif key_command == ord('1'):
>             player.move('downleft')
>
>         elif key_command == ord('3'):
>             player.move('downright')
>
>         elif key_command == ord('Q'):  ## Exit
>             break
>
>         elif key_command == ord('5'): ## Wait
>             pass
>
>         elif key_command == ord('x'): ## print location
>             game.msg.iMsg("X: " + str(player.loc['x']) + ", Y: " +
> str(player.loc['y']))
>
>         elif key_command == ord('z'):
>             strlist = ""
>             for i in game.walls:
>                 strlist = strlist + str(i) + " "
>             game.msg.iMsg(strlist)
>
>         elif key_command == ord('c'):
>             for count in range(0,2):
>                 game.mainwin.move(count,0)
>                 game.mainwin.clrtoeol()
>
>         else:
>             game.msg.iMsg("That is not a valid command.")
>
>         if len(game.msg.msgs) == 0:     # If there are any msgs.
>             game.mainwin.refresh()
>         else:
>             game.msg.updateMsg()
>
>         game.msg.ereasMsg()

          # I've moved this line
          # game.msg.eraseMsg

>
>
> if __name__ == "__main__":
>     try:
>         import psyco
>         psyco.full()
>     except ImportError:
>         # Don't use something that doesn't exists.
>         pass
>
>     main()
>
> curses.nocbreak(); game.mainwin.keypad(0); curses.echo()
> curses.endwin()
>
>
> (The code is GPL'ed, Martin Ahnelöv 06)
>

-- 
HTH,
Rob



More information about the Python-list mailing list