Advertisement
tyler569

roguelike start

Apr 23rd, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1. from sys import exit
  2. from os import system
  3. from time import sleep
  4. from msvcrt import getch
  5. from random import randint
  6.  
  7. # Cell classes all have some common methods:
  8. #   __init__ initialises class variables, esp the symbol
  9. #   out_char return the charachter printed by the renderer for the object
  10. #   allow_pass defines whether the player can walk over this cell or not
  11.  
  12.  
  13. class PositionError(Exception):
  14.     'Error thrown if player manages to get outside play area'
  15.     pass
  16.  
  17.  
  18. class Nothing():
  19.     'Class describing cells outside rooms - these are not traversable'
  20.     def __init__(self):
  21.         self.symbol = ' '
  22.     def out_char(self):
  23.         return self.symbol
  24.     def allow_pass(self):
  25.         return False
  26.  
  27.  
  28. class Room():
  29.     'Class describing rooms, walls, and spaces'
  30.     # !!! Room appears not to pass self to methods - check why
  31.     class Wall():
  32.         'Class describing room walls'
  33.         def __init__(self):
  34.             self.symbol = '#'
  35.         def out_char(self):
  36.             return self.symbol
  37.         def allow_pass(self):
  38.             return False
  39.  
  40.     class Space():
  41.         'Class describing empty cells inside rooms - these are traversable'
  42.         def __init__(self):
  43.             self.symbol = '.'
  44.         def out_char(self):
  45.             return self.symbol
  46.         def allow_pass(self):
  47.             return True
  48.  
  49.        
  50. class Player():
  51.     'Class containing data and methods about the player charachter'
  52.     def __init__(self, startpos):
  53.         self.symbol = '@'
  54.         self.x = randint(startpos[0][0],startpos[0][1])
  55.         self.y = randint(startpos[1][0],startpos[1][1])
  56.     def out_char(self):
  57.         return self.symbol
  58.     def move(self, field):
  59.         'Moves the player after checking the user has indicated a valid space to move into'
  60.         direction = (0,0)
  61.         inchar = getch()
  62.         if inchar == b'\xe0':
  63.             input = getch()
  64.             input_table = {b'P':(1,0),b'M':(0,1),b'H':(-1,0),b'K':(0,-1)}
  65.             direction = input_table[input]
  66.             fut_x = fixpos(self.x + direction[0], 21)
  67.             fut_y = fixpos(self.y + direction[1], 78)
  68.            
  69.             if field[fut_x][fut_y].allow_pass():
  70.                 field[self.x][self.y] = Room.Space()  #reset old place to Room.Space()
  71.                 self.x = fut_x
  72.                 self.y = fut_y
  73.                 next = field[self.x][self.y]       #save whatever was ehere player is now
  74.                 field[self.x][self.y] = self        #new place references player
  75.            
  76.         elif inchar == b'\x03':
  77.             exit()
  78.         else:
  79.             #Manage other input cases and develop a blanket 'does nothing'
  80.             pass
  81.        
  82.    
  83. def generate():
  84.     'Generates the playfield randomly for the game'
  85.     y_min = randint(0, 60)
  86.     x_min = randint(0, 15)
  87.     y_max = randint(y_min+5, 78)
  88.     x_max = randint(x_min+5, 21)
  89.    
  90.     board = [[Nothing() for i in range(78)] for j in range(21)]
  91.    
  92.     for i in range(x_min,x_max):
  93.         for j in range(y_min,y_max):
  94.             if i == x_min or i == x_max - 1 or j == y_min or j == y_max - 1:
  95.                 board[i][j] = Room.Wall()
  96.             else:
  97.                 board[i][j] = Room.Space()
  98.     return board,((x_min+1,x_max-1),(y_min+1,y_max-1))
  99.    
  100. def render(field):
  101.     'Takes the playfield as input and renders it to the console screen'
  102.     system('cls')
  103.     print()
  104.     for h in field:
  105.         for i in h:
  106.             print(i.out_char(),end='')
  107.         print()
  108.     print()
  109.     sleep(0.1)
  110.  
  111. def fixpos(pos, max):
  112.     'Raises an error if the player goes out of bounds'
  113.     if pos >= 0 and pos < max:
  114.         return pos
  115.     else:
  116.         raise PositionError('Out of bounds')
  117.  
  118. def main():
  119.     'Main runtime of this roguelike'
  120.     GameContinues = True
  121.    
  122.     field, startpos = generate()
  123.     player = Player(startpos)
  124.     field[player.x][player.y] = player
  125.  
  126.     while GameContinues:
  127.         render(field)
  128.         player.move(field)
  129.    
  130.    
  131. if __name__ == '__main__':
  132.     main()
  133. pass # Simply to make Notepad++ stop being mad at me - does absolutly nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement