Donté Stewart
Donté Stewart

Reputation: 1

Move between floors

I'm trying to make a sort of nethack inspired type game and I'm trying to figure out the moving up and down(I know the current code is probably terribly inefficient)(also sorry if this question is formatted weird this is my first ask)

import random
from pynput.keyboard import Key, Listener
import time
import os


clear = lambda: os.system('cls')
floor1 = ["_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"]
floor2 = ["_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"]
floor3 = ["_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"]
floor4 = ["_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"]
floor5 = ["_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_"]
def allFloor():
    print("".join(floor1))
    print("".join(floor2))
    print("".join(floor3))
    print("".join(floor4))
    print("".join(floor5))
char = ("0")



spaceAmount = len(floor1)
ranStart = random.randint(0, (spaceAmount - 1))

floor1[ranStart] = char

charLoc = floor1.index("0")
moveLeft = charLoc - 1
moveRight = charLoc + 1

allFloor()


def on_release(key):
    if 'char' in dir(key):
        charLoc = floor1.index("0")
        moveLeft = charLoc - 1
        moveRight = charLoc + 1
        if moveRight >= spaceAmount:
            moveRight = moveRight - 1
        if key.char == "a" and moveLeft >= 0:
            time.sleep(0.1)
            floor1[charLoc] = "_"
            floor1[moveLeft] = char
            clear()
            allFloor()
        elif key.char == "d" and moveRight <= spaceAmount:
            time.sleep(0.1)
            floor1[charLoc] = "_"
            floor1[moveRight] = char
            clear()
            allFloor()
    if key == Key.esc:
            # Stop listener
        return False


with Listener(
        on_release=on_release) as listener:
    listener.join()

I was gonna originally just have it check if it's on a specific line and if so when pressing up or down it only goes to the line that is above or below that line

Upvotes: 0

Views: 63

Answers (1)

ukBaz
ukBaz

Reputation: 8004

Have variables to track the location of the sprite/player. An option would be to have one for which floor and then another for the location within the floor.

You don't need to store all the characters to be output, that only needs to happen when being output.

It might be helpful to store this in a class and have methods to move the sprite/player around. These methods would do the checking for valid moves.

The class can also have a method for displaying the information.

An example of that might be:

from dataclasses import dataclass
import random


@dataclass
class Building:
    width: int = 20
    height: int = 5
    sprite: chr = "X"
    background: chr = "-"
    width_loc: int = 0
    height_loc: int = 0
    move_size: int = 1

    def show(self):
        print(" 01234567890123456789")
        for floor in range(self.height, 0, -1):
            print(floor, end="")
            for loc in  range(self.width):
                if all((floor == self.height_loc,
                        loc == self.width_loc)):
                    print(self.sprite, end="")
                else:
                    print(self.background, end="")
            print()

    def move_left(self):
        self.width_loc = max(self.width_loc - self.move_size, 0)

    def move_down(self):
        self.height_loc = max(self.height_loc - self.move_size, 1)

    def move_right(self):
        self.width_loc = min(self.width_loc + self.move_size, self.width)

    def move_up(self):
        self.height_loc = min(self.height_loc + self.move_size, self.height)

    def on_release(self, key):
        if key.char == "a":
            self.move_left()
        elif key.char == "d":
            self.move_right()
        elif key.char == "j":
            self.move_up()
        elif key.char == "k":
            self.move_down()
        self.show()

def main():
    # initial location
    ver_loc = random.randint(1, Building.height -1)
    hor_loc = random.randint(0, Building.width - 1)
    print("initial location:", ver_loc, hor_loc)
    building = Building(width_loc=hor_loc, height_loc=ver_loc, move_size=3)

    building.show()
    # New location
    building.move_left()
    building.show()
    building.move_up()
    building.show()


if __name__ == '__main__':
    main()

An example output was:

initial location: 3 4
 01234567890123456789
5--------------------
4--------------------
3----X---------------
2--------------------
1--------------------
 01234567890123456789
5--------------------
4--------------------
3-X------------------
2--------------------
1--------------------
 01234567890123456789
5-X------------------
4--------------------
3--------------------
2--------------------
1--------------------

I haven't implemented the keyboard listener, but the main method might look like the following with it:

def main():
    # initial location
    ver_loc = random.randint(1, building_height -1)
    hor_loc = random.randint(0, floor_width - 1)
    print(ver_loc, hor_loc)
    building = Building(width_loc=hor_loc, height_loc=ver_loc, move_size=3)
    with Listener(on_release=building.on_release) as listener:
        listener.join()

Upvotes: 0

Related Questions