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
#1
I'm looking to create something like this

thisA function where I can draw a grid with an outline and specify the location of a body within the grid

for example in this image the function would read:

drawRoom(14,8,8,5)

the first two numbers being the grid dimensions and the last two being the location of the body.

I presume I would need three functions. One for +---+, one for line, ....., and one for the line with an object, ...@.

Any ideas?
Reply
#2
You could do separate functions like you say, but I don't think you need to. You would print the top border, then loop through the rows of the grid. If it's the row with the @, print that, otherwise just print a row of dots. Then (after the loop) print the bottom border.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Combine Two Recursive Functions To Create OneĀ Recursive Selection Sort Function Jeremy7 12 7,191 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Dynamically create functions from Json Clement_2000 5 3,829 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