Python Forum
How to create a basic grid with functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a basic grid with functions
#3
I had some spare time. How i´d do it.
def create_room(width, height, px, py):
    # fill room with dots
    room = [['.' for x in range(width+2)] for y in range(height+2)]
    # place horizontal '-'
    for i in range(width+2):
        room[0][i] = '-'
        room[height+1][i] = '-'
    # place vertical '|'
    for i in range(1, height+2):
        room[i][0] = '|'
        room[i][width+1] = '|'
    # place corners '+'
    for cy, cx in [(0, 0), (0, width+1), (height+1, 0), (height+1, width+1)]:
        room[cy][cx] = '+'
    # place player '@'
    room[py+1][px+1] = '@'
    return room

def draw(room):
    for y in range(len(room)):
        for x in range(len(room[0])):
            print(room[y][x], end='')
        print()

room = create_room(14, 8, 8, 5)
draw(room)
Reply


Messages In This Thread
RE: How to create a basic grid with functions - by ThomasL - Nov-22-2019, 04:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,640 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Dynamically create functions from Json Clement_2000 5 4,081 Feb-22-2019, 06:43 PM
Last Post: Clement_2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020