Making a Roguelike while chillin out part II
Draculinio
Posted on July 26, 2024
Let's continue with the development of the roguelike in the chill & python sessions.
As I mentioned earlier, when I have some free time and want to relax, I will turn on my youtube channel live streaming, put some cool music and start coding without pressure, just for fun.
All what I am going to talk about is in the game repo
First I wanted to clear a little the code in the main zone, something like this, kind of standard in curses libraries.
def main(stdscr):
# my code here
wrapper(main)
From there it is way easier to code stuff, because even if now you see a lot of code there, in the future it will be easier to send the code to functions/classes.
The second change that I did was changing the way you send commands. Instead of writing a command, I will expect an ascii code, so, depending on which key the player press, it will do different stuff.For keypad there are special curses commands
while command != 101:
stdscr.addstr(p.posy,p.posx,'@',curses.color_pair(1)) #WRITE THE CHARACTER
#We need to put any enemy that is available in the room
for i in room.enemies:
stdscr.addstr(i.posy,i.posx,i.symbol)
print_player(p, stdscr)
print_board(stdscr)
print_room(room,stdscr)
command = stdscr.getch() #Interesting, this returns an ASCII char...
stdscr.erase()
if command == curses.KEY_LEFT: #Interesting, if I want to have arrows I should not convert ascii...
if p.posx>1:
p.posx -=1
if command == curses.KEY_RIGHT:
if p.posx<79:
p.posx +=1
if command == curses.KEY_UP:
if p.posy>2:
p.posy -=1
if command == curses.KEY_DOWN:
if p.posy<22:
p.posy +=1 #Yes, terrible...
#Move enemies
room.move_enemies()
Now, I don't expect 'exit' or 'e', I just expect ASCII code 101 (e) to finish the main loop.
For movement of the character, now I have a position in x and y in the character class, so everytime the player press an arrow key, it moves.
This is what I added in the player constructor:
def __init__(self, name, creature):
self.name = name
self.creature = creature #let's start with human or elf.
self.strength = 0
self.defense = 0
self.gold = 0
self.room = '' #here is a question... do the character need to know where he is or the room should know who is there????
self.posx = 10
self.posy = 10
The ifs that I put in arrow movement are because I assume that a terminal is 80x25 (there might be other configurations but, as I have to have fixed positions, I want to make it possible to be run in almost any terminal).
Then is the enemies movement. I also added that position stuff to enemies in the room and gave them random movement in the room class.
def move_enemies(self):
for i in self.enemies:
move = random.randint(1,4)
if move == 1:
if i.posx>1:
i.posx -=1
if move == 2:
if i.posx<79:
i.posx +=1
if move == 3:
if i.posy>2:
i.posy -=1
if move == 4:
if i.posy<22:
i.posy +=1
I know there might be better ways to do this but for now, this works and as logic starts to be harder, I will move all this.
Then added some colors and functions for the board and...
As I mentioned, in the article you have the repo, feel free to do whatever you want and subscribe to the channel!
Posted on July 26, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.