Advertisement
tyler569

roguelike prog 1

Apr 23rd, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.05 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 Nothing():
  14.     'Class describing cells outside rooms - these are not traversable'
  15.     def __init__(self):
  16.         self.symbol = ' '
  17.     def out_char(self):
  18.         return self.symbol
  19.     def allow_pass(self):
  20.         return False
  21.     def action(self):
  22.         pass
  23.  
  24.  
  25. class Room():
  26.     'Class describing rooms, walls, and spaces'
  27.     # !!! Room appears not to pass self to methods - check why
  28.     class Wall():
  29.         'Class describing room walls'
  30.         def __init__(self):
  31.             self.symbol = '#'
  32.         def out_char(self):
  33.             return self.symbol
  34.         def allow_pass(self):
  35.             return False
  36.         def action(self):
  37.             pass
  38.  
  39.     class Space():
  40.         'Class describing empty cells inside rooms - these are traversable'
  41.         def __init__(self):
  42.             self.symbol = '.'
  43.         def out_char(self):
  44.             return self.symbol
  45.         def allow_pass(self):
  46.             return True
  47.         def action(self):
  48.             pass
  49.         def walkover(self):
  50.             return True
  51.  
  52.        
  53. class Player():
  54.     'Class containing data and methods about the player charachter'
  55.     def __init__(self, startpos):
  56.         self.symbol = '@'
  57.         self.x = randint(startpos[0][0],startpos[0][1])
  58.         self.y = randint(startpos[1][0],startpos[1][1])
  59.         self.next = Room.Space()
  60.     def out_char(self):
  61.         return self.symbol
  62.     def move(self, field):
  63.         'Moves the player after checking the user has indicated a valid space to move into'
  64.         direction = (0,0)
  65.         inchar = getch()
  66.         if inchar == b'\xe0':
  67.             input = getch()
  68.             input_table = {b'P':(1,0),b'M':(0,1),b'H':(-1,0),b'K':(0,-1)}
  69.             direction = input_table[input]
  70.             fut_x = fixpos(self.x + direction[0], 21)
  71.             fut_y = fixpos(self.y + direction[1], 78)
  72.            
  73.             field[fut_x][fut_y].action()
  74.            
  75.             if field[fut_x][fut_y].allow_pass():
  76.                 field[self.x][self.y] = self.next #Room.Space()  #reset old place to Room.Space()
  77.                 self.x = fut_x
  78.                 self.y = fut_y
  79.                 if field[fut_x][fut_y].walkover():
  80.                     self.next = field[fut_x][fut_y]       #save whatever was ehere player is now
  81.                 field[self.x][self.y] = self        #new place references player
  82.            
  83.         elif inchar == b'\x03':
  84.             exit()
  85.         else:
  86.             #Manage other input cases and develop a blanket 'does nothing'
  87.             pass
  88.        
  89.  
  90. class Item():
  91.     def __init__(self, datavalue):
  92.         self.symbol = '*'
  93.         self.datavalue = datavalue
  94.     def out_char(self):
  95.         return self.datavalue
  96.     def allow_pass(self):
  97.         return True
  98.     def action(self):
  99.         pass
  100.     def walkover(self):
  101.         return self.datavalue
  102.        
  103.        
  104. def generate():
  105.     'Generates the playfield randomly for the game'
  106.     board = [[Nothing() for i in range(78)] for j in range(21)]
  107.     item_spawns = [Item(1),Item(0)]
  108.     item_spawnprev = 50    #possibility for items to spawn - higher number is lower chance
  109.    
  110.     for n in range(1):  # Allows repetition of room-making script when that works better
  111.         y_min = randint(0, 60)
  112.         x_min = randint(0, 15)
  113.         y_max = randint(y_min+5, 78)
  114.         x_max = randint(x_min+5, 21)
  115.        
  116.         for i in range(x_min,x_max):
  117.             for j in range(y_min,y_max):
  118.                 if i == x_min or i == x_max - 1 or j == y_min or j == y_max - 1:
  119.                     board[i][j] = Room.Wall()
  120.                 else:
  121.                     if randint(0,item_spawnprev) == 20:
  122.                         board[i][j] = item_spawns[randint(0,len(item_spawns)-1)]
  123.                     else:
  124.                         board[i][j] = Room.Space()
  125.  
  126.     return board,((x_min+1,x_max-2),(y_min+1,y_max-2))
  127.    
  128. def render(field):
  129.     'Takes the playfield as input and renders it to the console screen'
  130.     system('cls')
  131.     print()
  132.     for h in field:
  133.         for i in h:
  134.             print(i.out_char(),end='')
  135.         print()
  136.     print()
  137.     sleep(0.1)
  138.  
  139. def fixpos(pos, max):
  140.     'Raises an error if the player goes out of bounds'
  141.     if pos >= 0 and pos < max:
  142.         return pos
  143.     else:
  144.         raise ValueError('Out of bounds')
  145.  
  146. def main():
  147.     'Main runtime of this roguelike'
  148.     GameContinues = True
  149.    
  150.     field, startpos = generate()
  151.     player = Player(startpos)
  152.     field[player.x][player.y] = player
  153.  
  154.     while GameContinues:
  155.         render(field)
  156.         player.move(field)
  157.    
  158.    
  159. if __name__ == '__main__':
  160.     main()
  161. pass # Simply to make Notepad++ stop being mad at me - does absolutly nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement