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 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
- def action(self):
- pass
- 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
- def action(self):
- pass
- 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
- def action(self):
- pass
- def walkover(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])
- self.next = Room.Space()
- 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)
- field[fut_x][fut_y].action()
- if field[fut_x][fut_y].allow_pass():
- field[self.x][self.y] = self.next #Room.Space() #reset old place to Room.Space()
- self.x = fut_x
- self.y = fut_y
- if field[fut_x][fut_y].walkover():
- self.next = field[fut_x][fut_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
- class Item():
- def __init__(self, datavalue):
- self.symbol = '*'
- self.datavalue = datavalue
- def out_char(self):
- return self.datavalue
- def allow_pass(self):
- return True
- def action(self):
- pass
- def walkover(self):
- return self.datavalue
- def generate():
- 'Generates the playfield randomly for the game'
- board = [[Nothing() for i in range(78)] for j in range(21)]
- item_spawns = [Item(1),Item(0)]
- item_spawnprev = 50 #possibility for items to spawn - higher number is lower chance
- for n in range(1): # Allows repetition of room-making script when that works better
- y_min = randint(0, 60)
- x_min = randint(0, 15)
- y_max = randint(y_min+5, 78)
- x_max = randint(x_min+5, 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:
- if randint(0,item_spawnprev) == 20:
- board[i][j] = item_spawns[randint(0,len(item_spawns)-1)]
- else:
- board[i][j] = Room.Space()
- return board,((x_min+1,x_max-2),(y_min+1,y_max-2))
- 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 ValueError('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