3

Hi I am 13 and I have been programming for 1 1/2 yrs now (since 12 yrs old is what I really mean). I need some advice on how to make your character move with keyboard without any packages or pygame. I need the character to actually move in the map I have create with the special character keys and stuff, I also need help with creating a menu system so that it actually launches the game. Here is my code.

class Map(self, keyboard):
    """ View of characted being moved to new location. """    

class Character():
    def Character(object):
        C = player(hero)
        hero = {'name' : 'Hero',
                 'lvl' : 1,
                 'xp' : 0,
                 'lvlNext' : 25,  
                 'stats' : {'str' : 1,
                            'dex' : 1,
                            'int' : 1,
                            'hp' : 30,
                            'atk' : [5, 12]}}
    def update(self, C, map):
        """ Move player based on keys pressed. """
        if keyboard.is_pressed(K_w, C, map):
            self.C
            self.map
            self.y -= 1
        if keyboard.is_pressed(K_s, C, map):
            self.C
            self.map
            self.y += 1
        if keyboard.is_pressed(K_a, C, map):
            self.C
            self.map 
            self.x -= 1
        if keyboard.is_preseed(K_d, C, map):
            self.C
            self.map
            self.x += 1

        else:
            print(" Waiting for your move. ")

class Enemy():
    def Enemy(object):
        f = enemy(fly)
        fly = {'name' : 'Fly',
                 'lvl' : 1,
                 'reward' : 25,
                 'xp' : 0,
                 'lvlNext' : 25,
                 'stats' : {'str': 1,
                            'int' : 1,
                            'hp' : 30,
                            'atk' : [5, 12]}}

    def move(self, f, map):
        """ Move """


class Wall()
    def Wall(object):
        P = wall(solid)
        solid = ['name' : 'Solid',
                 'Function' : 








    c = chest
    d = door
    DD = door




map = """

     PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
     P|-----------------------------------------------------|P 
     P|  |--------| |-------|------------|  |---------------|P
     P|  |      |PPDPPPPPPPP|            |  |               |P
     P| C|      |P         P|  f         |  |               |P
     P|  |      |P   f     P|            |  |               |P
     P|  |      |P         P|            d  d               |P
     P|  |      |P         P|            |  |               |P
     P|  |      |P         P|            |  |               |P 
     P|  |      |P         P|            |  |    cccc       |P
     P|  |      |PPPPPPPPPPP|            |  |    c  c       |P
     P|__d_______|P|_________d___________|  |____cccc_______|P       
     PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDPPPPPPPPPPPPPPPPPP

      """




class Keyboard(self, character, map):
    """ Key board controls. """
    def update(self):
        """ Move hero based on keys pressed. """
        if keyboard.is_pressed(K_w, p, map):
            self.y -= 1
        if keyboard.is_pressed(K_s, p, map):
            self.y += 1
        if keyboard.is_pressed(K_a, p, map):
            self.x -= 1
        if keyboard.is_preseed(K_d, p, map):
            self.x += 1

hero = {'name' : 'Hero',
                 'lvl' : 1,
                 'xp' : 0,
                 'lvlNext' : 25,
                 'stats' : {'str' : 1,
                            'dex' : 1,
                            'int' : 1,
                            'hp' : 30,
                            'atk' : [5, 12]}}

fly = {'name' : 'Fly',
                 'lvl' : 1,
                 'reward' : 25,
                 'xp' : 0,
                 'lvlNext' : 25,
                 'stats' : {'str': 1,
                            'int' : 1,
                            'hp' : 30,
                            'atk' : [5, 12]}}

def level(char):
    nStr, nDex, nInt = 0, 0, 0
    while char['xp'] >= char['lvlNext']:
        char['lvl'] += 1
        char['xp'] = char['xp'] - char['lvlNext']
        char['lvlNext'] = round(char['lvlNext'] * 1.5)
        nStr += 1
        nDex += 1
        nInt += 1

    print('level:', char['lvl'])

    print('STR {} +{} DEX {} +{} INT {} +{}'.format(char['stats']['str'], nStr,
                                                    char['stats']['dex'], nDex,
                                                    char['stats']['int'], nInt))
    char['stats']['str'] += nStr
    char['stats']['dex'] += nDex
    char['stats']['int'] += nInt

from random import randint

def takeDmg(attacker, defender):
    dmg = randint(attacker['stats']['atk'][0], attacker['stats']['atk'][1])
    defender['stats']['hp'] = defender['stats']['hp'] - dmg
    if defender['stats']['hp'] <= 0:
        print('{} has been slain'.format(defender['name']))
        hero['xp'] += fly['reward']
        level(hero)
        input('Press any key to quite.')
        exit(0)

So as you can see what I am trying to do at least. I am needing the map, and the "Game_Sys" which equals menu, and I need the menu to respond to any key function I have. Now I built the level system it actually works. I built the enemy with reward system to add to my level once I finish killing the enemy, my xp gets filled up else I level up if it gets over my max xp for current lvl. But now I want to move "p = player blablabla" across map without PYGAME, and without a package. It is text based so hopefully I get this program to run somehow. Anyways, if any see any errors and give me some help and advice on this game thank you. My goal is to get "C" to move around the map I created, and if I get close to f, I get attacked, or i start a battle, and I go from there.

Can I use livewires to open up a game window or do I need pygame for a game window to open? Can I have a partner to help out? I do need someone.

WarrenT
  • 4,502
  • 19
  • 27
  • I fixed the formatting so it looks better. Your game is very interesting and well-formed, good luck!! – Luigi Apr 22 '14 at 03:30

1 Answers1

3

A text-based adventure game with a map would be difficult to implement in Python (at best). Livewires is not a GUI display itself, but many of the examples used in the course require pygame. I actually didn't know it was still functional/maintained; it's a fairly old tool.

So, your difficulty is in map display. The problem with most Python interpreters is that, to show movement on a map, you'd have to literally print your map for every single movement.

my_map = """

 PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
 P|-----------------------------------------------------|P 
 P|  |--------| |-------|------------|  |---------------|P
 P|  |      |PPDPPPPPPPP|            |  |               |P
 P| C|      |P         P|  f         |  |               |P
 P|  |      |P   f     P|            |  |               |P
 P|  |      |P         P|            d  d               |P
 P|  |      |P         P|            |  |               |P
 P|  |      |P         P|            |  |               |P 
 P|  |      |P         P|            |  |    cccc       |P
 P|  |      |PPPPPPPPPPP|            |  |    c  c       |P
 P|__d_______|P|_________d___________|  |____cccc_______|P       
 PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDPPPPPPPPPPPPPPPPPP

"""

And that would be a lot of times, which would be a huge memory hit. Using this map and Pygame, though, you could generate a game that uses most (if not all) of the code you've already written.

Things you would have to do:

  • Convert your map into an array. In pygame, you could easily generate a map grid from images (i.e. this is door, this is player, this is fly, this is empty floor, this is wall). Since you have your map already made, if you converted it to an array, you could easily generate a map. You'd have an array of arrays, which you could then access as an (x,y) grid.

Example psuedocode:

for y in final_rows:
    for x in y:
        if x == 'P':
            render_P()
        elif x == 'f':
            render_fly()
        #... etc.
  • Get user input. Right now, you don't get user input. Pygame makes it easy to do so, check out the documentation and this answer for more.
  • Create menus and everything else that makes a game. You already know that you need to do this, I just include it here for the sake of completeness.

If you can't tell, I highly recommend pygame for all GUI things, but if you find out that it doesn't work for you, check out this page for an exhaustive list of Python GUIs and other game-making things.

Good luck!

EDIT: This is what I mean when I say to make an array/list for your map:

my_map = """
 PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
 P|-----------------------------------------------------|P 
 P|  |--------| |-------|------------|  |---------------|P
 P|  |      |PPDPPPPPPPP|            |  |               |P
 P| C|      |P         P|  f         |  |               |P
 P|  |      |P   f     P|            |  |               |P
 P|  |      |P         P|            d  d               |P
 P|  |      |P         P|            |  |               |P
 P|  |      |P         P|            |  |               |P 
 P|  |      |P         P|            |  |    cccc       |P
 P|  |      |PPPPPPPPPPP|            |  |    c  c       |P
 P|__d_______|P|_________d___________|  |____cccc_______|P       
 PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDPPPPPPPPPPPPPPPPPP
"""

#split map into rows (y-values)
unprocessed_rows = my_map.split('\n')

#initialize list
final_rows = []

#process rows
for row in unprocessed_rows:

    #remove spaces on either side
    row = row.strip()

    #only add the row if it is not empty
    #(the first and last are in the variable my_map)
    if len(row)>0:
        final_rows.append(row)

#see what this created:
print final_rows

    ['PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP', 'P|-----------------------------------------------------|P', 'P|  |--------| |-------|------------|  |---------------|P', 'P|  |      |PPDPPPPPPPP|            |  |               |P', 'P| C|      |P         P|  f         |  |               |P', 'P|  |      |P   f     P|            |  |               |P', 'P|  |      |P         P|            d  d               |P', 'P|  |      |P         P|            |  |               |P', 'P|  |      |P         P|            |  |               |P', 'P|  |      |P         P|            |  |    cccc       |P', 'P|  |      |PPPPPPPPPPP|            |  |    c  c       |P', 'P|__d_______|P|_________d___________|  |____cccc_______|P', 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDPPPPPPPPPPPPPPPPPP']

#it's obviously a list of strings, but it's hard to read.
#Let's print it row by row to make sure everything turned out okay.
for r in final_rows:
    print r

PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
P|-----------------------------------------------------|P
P|  |--------| |-------|------------|  |---------------|P
P|  |      |PPDPPPPPPPP|            |  |               |P
P| C|      |P         P|  f         |  |               |P
P|  |      |P   f     P|            |  |               |P
P|  |      |P         P|            d  d               |P
P|  |      |P         P|            |  |               |P
P|  |      |P         P|            |  |               |P
P|  |      |P         P|            |  |    cccc       |P
P|  |      |PPPPPPPPPPP|            |  |    c  c       |P
P|__d_______|P|_________d___________|  |____cccc_______|P
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDPPPPPPPPPPPPPPPPPP

#good, no extra lines or random spaces.

#we can now access specific map coordinates like this:
#my_map[y][x] (starting at 0)
print final_rows[0][0]
 #prints P
print final_rows[4][3]
 #prints C

Once you've converted your map to a list, you can access it with the psuedocode I gave above.

Community
  • 1
  • 1
Luigi
  • 4,129
  • 6
  • 37
  • 57