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

New contributor

Donté Stewart is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

7

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()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật