Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import exit
- from os import system
- from time import sleep
- from msvcrt import getch
- from random import randint
- # Cell classes all have some common methods:
- # __init__ initialises class variables, esp the symbol
- # out_char return the charachter printed by the renderer for the object
- # allow_pass defines whether the player can walk over this cell or not
- class PositionError(Exception):
- 'Error thrown if player manages to get outside play area'
- pass
- class Nothing():
- 'Class describing cells outside rooms - these are not traversable'
- def __init__(self):
- self.symbol = ' '
- def out_char(self):
- return self.symbol
- def allow_pass(self):
- return False
- class Room():
- 'Class describing rooms, walls, and spaces'
- # !!! Room appears not to pass self to methods - check why
- class Wall():
- 'Class describing room walls'
- def __init__(self):
- self.symbol = '#'
- def out_char(self):
- return self.symbol
- def allow_pass(self):
- return False
- class Space():
- 'Class describing empty cells inside rooms - these are traversable'
- def __init__(self):
- self.symbol = '.'
- def out_char(self):
- return self.symbol
- def allow_pass(self):
- return True
- class Player():
- 'Class containing data and methods about the player charachter'
- def __init__(self, startpos):
- self.symbol = '@'
- self.x = randint(startpos[0][0],startpos[0][1])
- self.y = randint(startpos[1][0],startpos[1][1])
- def out_char(self):
- return self.symbol
- def move(self, field):
- 'Moves the player after checking the user has indicated a valid space to move into'
- direction = (0,0)
- inchar = getch()
- if inchar == b'\xe0':
- input = getch()
- input_table = {b'P':(1,0),b'M':(0,1),b'H':(-1,0),b'K':(0,-1)}
- direction = input_table[input]
- fut_x = fixpos(self.x + direction[0], 21)
- fut_y = fixpos(self.y + direction[1], 78)
- if field[fut_x][fut_y].allow_pass():
- field[self.x][self.y] = Room.Space() #reset old place to Room.Space()
- self.x = fut_x
- self.y = fut_y
- next = field[self.x][self.y] #save whatever was ehere player is now
- field[self.x][self.y] = self #new place references player
- elif inchar == b'\x03':
- exit()
- else:
- #Manage other input cases and develop a blanket 'does nothing'
- pass
- def generate():
- 'Generates the playfield randomly for the game'
- y_min = randint(0, 60)
- x_min = randint(0, 15)
- y_max = randint(y_min+5, 78)
- x_max = randint(x_min+5, 21)
- board = [[Nothing() for i in range(78)] for j in range(21)]
- for i in range(x_min,x_max):
- for j in range(y_min,y_max):
- if i == x_min or i == x_max - 1 or j == y_min or j == y_max - 1:
- board[i][j] = Room.Wall()
- else:
- board[i][j] = Room.Space()
- return board,((x_min+1,x_max-1),(y_min+1,y_max-1))
- def render(field):
- 'Takes the playfield as input and renders it to the console screen'
- system('cls')
- print()
- for h in field:
- for i in h:
- print(i.out_char(),end='')
- print()
- print()
- sleep(0.1)
- def fixpos(pos, max):
- 'Raises an error if the player goes out of bounds'
- if pos >= 0 and pos < max:
- return pos
- else:
- raise PositionError('Out of bounds')
- def main():
- 'Main runtime of this roguelike'
- GameContinues = True
- field, startpos = generate()
- player = Player(startpos)
- field[player.x][player.y] = player
- while GameContinues:
- render(field)
- player.move(field)
- if __name__ == '__main__':
- main()
- pass # Simply to make Notepad++ stop being mad at me - does absolutly nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement