Mini RPG: Just Started

I have started on my mini-RPG! As of now I have a field and character class. The field has three obstacles in it now, and the entire field is represented by the currentMap list shown at the beginning of the source code. A 1 is a wall, 0 is empty space, 2 is an obstacle (not walkable), and 3 is a newline character.


At the moment the obstacles are represented by one of five different characters, and each obstacle is assigned one of these characters during the instantiation (creation) of the game field’s instance. This way, random characters are generated for the obstacles and stay that way the entire game.

For now the only things that this does are create a field and print it when you hit enter, and move the character around. In order to detect collision I created an isValidMove function. This function checks to see if the next move the player can take will be anything but a 0 or not, and returns True or False.

import random

currentMap = [
    [1, 1, 1, 1, 1, 3],
    [1, 0, 0, 0, 1, 3],
    [1, 2, 0, 0, 1, 3],
    [1, 0, 0, 0, 1, 3],
    [1, 0, 0, 2, 1, 3],
    [1, 0, 0, 0, 1, 3],
    [1, 0, 2, 0, 1, 3],
    [1, 0, 0, 0, 1, 3],
    [1, 1, 1, 1, 1, 3]
    ]

class gameField:
    def __init__(self, mapData=[0]):
        self.mapData = mapData
        self.string = ”
        self.blocks = []
        for i in range(len(self.mapData)):
            for j in range(len(self.mapData[i])):
                if self.mapData[i][j] == 2:
                    r = random.randrange(1, 5)
                    if r == 1:
                        self.blocks.append(‘ v ‘)
                    if r == 2:
                        self.blocks.append(‘ x ‘)
                    if r == 3:
                        self.blocks.append(‘ ^ ‘)
                    if r == 4:
                        self.blocks.append(‘ * ‘)
                    if r == 5:
                        self.blocks.append(‘ ~ ‘)

class character:
    def __init__(self, x=0, y=0, field=None):
        self.crds = [x, y]
        self.x = self.crds[0]
        self.y = self.crds[1]
        self.field = field

    def __str__(self):
        s = ”
        r = 0
        for i in range(len(self.field.mapData)):
            for j in range(len(self.field.mapData[i])):
                if self.field.mapData[i][j] == 4:
                    self.field.mapData[i][j] = 0
        self.field.mapData[self.y][self.x] = 4
        for i in range(len(self.field.mapData)):
            for j in range(len(self.field.mapData[i])):
                if self.field.mapData[i][j] == 0:
                    s += ‘   ‘
                elif self.field.mapData[i][j] == 1:
                    s += ‘ / ‘
                elif self.field.mapData[i][j] == 2:
                    r += 1
                    if r == 1:
                        s += self.field.blocks[0]
                    if r == 2:
                        s += self.field.blocks[1]
                    if r == 3:
                        s += self.field.blocks[2]
                elif self.field.mapData[i][j] == 3:
                    s += ‘\n’
                elif self.field.mapData[i][j] == 4:
                    s += ‘ O ‘
        self.field.string = s
        return self.field.string

    def isValidMove(self, move):
        if move == ‘a’:
            if self.field.mapData[self.y][self.x – 1] != 0:
                return False
        if move == ‘a’:
            if self.field.mapData[self.y][self.x – 1] == 0:
                return True
        if move == ‘w’:
            if self.field.mapData[self.y – 1][self.x] != 0:
                return False
        if move == ‘w’:
            if self.field.mapData[self.y – 1][self.x] == 0:
                return True
        if move == ‘s’:
            if self.field.mapData[self.y + 1][self.x] != 0:
                return False
        if move == ‘s’:
            if self.field.mapData[self.y + 1][self.x] == 0:
                return True
        if move == ‘d’:
            if self.field.mapData[self.y][self.x + 1] != 0:
                return False
        if move == ‘d’:
            if self.field.mapData[self.y][self.x + 1] == 0:
                return True

currentGameField = gameField(currentMap)
currentCharacter = character(3, 1, currentGameField)

while True:
    print(currentCharacter)
    char = raw_input()
    if char == ‘a’:
        if currentCharacter.isValidMove(char):
            currentCharacter.x -= 1
    if char == ‘d’:
        if currentCharacter.isValidMove(char):
            currentCharacter.x += 1
    if char == ‘w’:
        if currentCharacter.isValidMove(char):
            currentCharacter.y -= 1
    if char == ‘s’:
        if currentCharacter.isValidMove(char):
            currentCharacter.y += 1

After that is complete, I’ll add in an option to enter your inventory and have a chance of a random battle sequence after each step the player takes.

Hopefully in the future I will have support for having multiple rooms, doors to and from rooms, and a .txt loader that can load maps from a .txt file. We’ll see how things go.

Here is an example of some output text:

 /  /  /  /  /
 /           /
 /  v        /
 /  O        /
 /        ^  /
 /           /
 /     v     /
 /           /
 /  /  /  /  /

d
 /  /  /  /  /
 /           /
 /  v        /
 /     O     /
 /        ^  /
 /           /
 /     v     /
 /           /
 /  /  /  /  /

The character is the O, and the / are walls, the rest are just impassable obstacles. I hit the d key, and moved the character a space right. 

TwitterRedditFacebookShare

4 thoughts on “Mini RPG: Just Started

  1. Anonymous

    I’m a little late to respond, but I finished the card version I had started about 2 weeks ago, without you, I would have never started it (Or even get the idea). I take Java classes and this helps me a lot. Your great!

    Apos

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *