Python Forum
using class functions in roguelike game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using class functions in roguelike game
#1
I'm struggling to know an exercise from my python book on game architecture. This is for roguelike game structure.

It is an follows:


define a class of type Room, with an __init__ function which takes two values, a width and a height, and uses those to define the object's room size. You should also define class variables x,y which give the initial location of the user in the room (best to make this location (0,0) to start with).

write a helper function/method for your class which allows the room to draw itself

write class functions/methods named left, right, up, down which change the location of the user in the room - taking proper account of the room walls!

Your main program should then be the following to allow keyboard control of the user in the room:
myRoom = Room(4,4)
myRoom.draw()

while True:
s = input()
if s=='a': myRoom.left()
if s=='s': myRoom.right()
if s=='q': myRoom.up()
if s=='z': myRoom.down()
if s=='x':
print("all done")
break
myRoom.draw()
Reply
#2
What have you already tried?
Reply
#3
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()
This was my code to make a map using functions but not sure how to translate to class
Reply
#4
If you don't know where to start, I would suggest a tutorial on classes. We have one on the basics here, and you can check the tutorial section for more tutorials on classes.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Good class design - with a Snake game as an example bear 1 1,825 Jan-24-2024, 08:36 AM
Last Post: annakenna
  Calling functions from within a class: PYQT6 Anon_Brown 4 3,759 Dec-09-2021, 12:40 PM
Last Post: deanhystad
  Why built in functions are defined as class? quazirfan 5 2,776 Oct-23-2021, 01:20 PM
Last Post: Gribouillis
  should I ... go class or stick with functions? 3Pinter 4 2,080 Nov-14-2020, 10:40 AM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

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