• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Arnold // Golden Donkey Productions
      Where to get started in making a text based MUD?
       
      So I have been making small games for over 3 years now and I'm comfortable in the programming languages that follow:
      1. Python (I need a bit of practice with this)
      2. C# (I am the most comfortable in this)
      3. Java (I know quite a bit but might need to do a tad of research)
       
      and I have also done a bit in:
      4. JS (I know pretty much nothing here)
      5. And some other stuff
       
      Anyway, so now you know all that rubbish here goes. What I wan't to do is program a text based MMORPG or a text based MUD. I have basically no clue where to begin so I what I need is some places to start. If anyone has any information on the subject, anything at all, please post it bellow. I will be really appreciated.
    • By Vansh Gupta
      A simple arcade game in python.I am currently in class 9th and made this game using python
    • By RidiculousName
      I've made a very simple map generator as a demo for combat encounters in my game. I plan to have the trees and rocks be cover, while the player's bandits ambush a caravan on the road in the middle. Please, let me know what you think. Criticism is welcome.
      """ Generates a basic map for combat """ __Author__ = "RidiculousName" __date__ = "3/21/18" import pygame as pg import copy import random def createMap(width, height, trees, rocks, roadWidth): """ creates a combat map :param width: int; 20-60 width of map in squares :param height: int; 20-60 height of map in squares :param trees: int; 0 to (width*height)//5 # of trees in map :param rocks: int; 0 to (width*height)//5 # of rocks in map :param roadWidth: int; 0-10 width of road in map (if value=0, will not have a road) :return: tuple matrix of map """ # variable declarations mapMatrix = [] rowList = [0] * width treeLocations = [] rockLocations = [] colIndex = random.randint(0, height) rowIndex = random.randint(0, width) #error checking if trees > (width * height) // 3: print("ERROR: TOO MANY TREES") return 0 elif rocks > (width * height) // 3: print("ERROR: TOO MANY ROCKS") return 0 # create a blank map full of grass for i in range(height): row = copy.copy(rowList) mapMatrix.append(row) # add trees for i in range(trees): while (rowIndex, colIndex) in treeLocations: colIndex = random.randint(0, height - 1) rowIndex = random.randint(0, width - 1) mapMatrix[rowIndex][colIndex] = 1 treeLocations.append((rowIndex, colIndex)) # add rocks for i in range(rocks): while (rowIndex, colIndex) in treeLocations \ or (rowIndex, colIndex) in rockLocations: colIndex = random.randint(0, height - 1) rowIndex = random.randint(0, width - 1) mapMatrix[rowIndex][colIndex] = 2 rockLocations.append((rowIndex, colIndex)) # add the road if roadWidth > 0: ct = int(roadWidth // 2) road = int(height // 2) while ct > 0: mapMatrix[road + ct] = [3] * width mapMatrix[road - ct] = [3] * width ct -= 1 mapMatrix[road] = [3] * width # convert to tuple for i in range(height): mapMatrix[i] = tuple(mapMatrix[i]) # return return tuple(mapMatrix) def showMap(screen, mapMatrix): """ :param screen: pygame screen object images are blitted to this :param mapMatrix: list matrix contains the map matrix :return: none """ # variable declarations height = pg.display.Info().current_h width = pg.display.Info().current_w x_pos = 0 y_pos = 0 grass = pg.image.load("grass.png").convert() tree = pg.image.load("tree.png").convert() rock = pg.image.load("rock.png").convert() road = pg.image.load("road.png").convert() for i in range(len(mapMatrix)): for j in range(len(mapMatrix[i])): if mapMatrix[i][j] == 0: screen.blit(grass, [x_pos, y_pos]) elif mapMatrix[i][j] == 1: screen.blit(tree, [x_pos, y_pos]) elif mapMatrix[i][j] == 2: screen.blit(rock, [x_pos, y_pos]) elif mapMatrix[i][j] == 3: screen.blit(road, [x_pos, y_pos]) x_pos += 16 y_pos += 16 x_pos = 0 def main(): """ calls functions to allow the game to run """ # variable declarations done = False # initialize pygame pg.init() # make screen object size = (1600, 900) screen = pg.display.set_mode(size) # set window caption pg.display.set_caption("Bandit King") #manages FPS clock = pg.time.Clock() #creates map mapMatrix = createMap(30, 30, 140, 20, 2) while not done: # --- main event loop for event in pg.event.get(): if event.type == pg.QUIT: done = True # --- game logic # --- drawing code showMap(screen, mapMatrix) # --- update screen pg.display.flip() # --- limit to 60 FPS clock.tick(60) #print("height: ", pg.display.Info().current_h, "width: ", pg.display.Info().current_w) pg.quit() if __name__ == "__main__": main()
    • By adambiser
      Hello all,
      I have teamed up with TheGameCreators for them to trial a Python version of AppGameKit in the form of a PYD file that makes AppGameKit commands available to Python 3.6 and greater.  Currently this PYD is compiled only for use with the Windows x86 version of Python.  It is a free, unlimited version of their commercial product, but it does show the AppGameKit logo for a few seconds on game startup and AppGameKit is mentioned in the window title when in windowed mode.  The PYD works with PyInstaller for easy distribution.
      If you haven't heard of AppGameKit, I would recommend looking over their website.  They also have documentation online and an active community forum.  I should point out that method names have been changed to follow PEP 8 naming conventions, so they will be similar but different from the online help.  I also had to make a few changes mentioned on the project's itch.io page.  There is also a PYI file there that can be used with PyCharm for easier access to the same information.
      As mentioned, this is a trial for them, but if there's a positive response they will extend the project and include a way of compiling an executable without the intro logo and window title text as well as support other OS.
      Please take a look and feel free to ask me any questions about it here or on the project's community forum on itch.io.
      AppGameKit for Python project page at itch.io: https://fascimania.itch.io/appgamekit-for-python
      Original announcement url: https://www.thegamecreators.com/post/announcing-appgamekit-for-python
    • By RidiculousName
      I want to make a random map generator in python 3, but I have no idea how to do it. Could someone recommend me a good starting point?
      What I want to make is a flat 2D overworld-map with roads, rivers, settlements, farmlands, forests, and plains.
      I don't want to model specific buildings. This is just for a game I'm making where I'd like the player to be able to travel from point to point.
  • Advertisement
  • Advertisement
Sign in to follow this  

Dealing with Multiple Characters with Different Attributes

Recommended Posts

I'm trying to make a game where the characters are randomly generated and retain individual attributes over time. Right now, I'm working on the combat system. I've made it so the roles of "Attacker" and "Defender" will switch between the player character and the NPC they are fighting. Problem is, I'm unsure of how to organize it in an elegant way. All I can think of is doing a bunch of if & else statements every time I need to check. Could you suggest ways of doing this in an elegant way? If there is anything in my code that could be significantly simplified don't be afraid to point that out either.

 

Here is an example of one of the methods I could use help with. In it, I am taking the int type variable, "overall action score", or OAS, from a tuple, which then helps to determine the force of any attacks that land. On if and else statements, I am checking who is attacking, and calling one of their respective physical attributes.

    def forceFinder(self):
        """
        Determines force of strike from
        OAS and alacrity.
        """
        #subtract attacker OAS from defender OAS
        force = self._oasTuple[0] - self._oasTuple[1]
        #exponentially adds to force according to alacrity and
        #margin between attacker, and defender OAS.
        for i in range(0, force, 10):
            force *= 1.10
            if Combat._attacker == "pc":
                force += Combat._pcAttr["alacrity"] * 3
            else:
                force += Combat._npcAttr["alacrity"] * 3
        return int(force)

 

 

Share this post


Link to post
Share on other sites
Advertisement

Python is not my strong suit so I apologize if this is vague, but...

What you want to do is store a reference to the PC (or NPC) in a variable. Then instead of having an if check, you just ask the variable for its alacrity stat directly. When the turn switches, you can point that variable to the "other" character instead, and off you go.

This way the code uses the same exact checks for everyone, but it can be "configured" to point to one character or another on the fly.

 

Hope that made some semblance of sense :-)

Share this post


Link to post
Share on other sites

The typical approach is to have data that gets swapped out.

You might pass an index to a table for the source or target, you might pass an object for the source and the source for the target, or something else.

For example, adding that information to the object itself and accessing it generically:

force += Combat._attacker.Attr["alacrity"];

or perhaps:

force += Combat._attacker.CurrentAlacrity(Combat._defender);

or accessing it through another table:

force += CombatAttribs[Combat._attacker].["alacrity"];

 

I prefer the function version above, since it more easily allows you to add modifications based on the source and the target, such as a bonus versus dragons or penalty versus shopkeepers or whatever fits your game.

Share this post


Link to post
Share on other sites
1 hour ago, ApochPiQ said:

Python is not my strong suit so I apologize if this is vague, but...

What you want to do is store a reference to the PC (or NPC) in a variable. Then instead of having an if check, you just ask the variable for its alacrity stat directly. When the turn switches, you can point that variable to the "other" character instead, and off you go.

This way the code uses the same exact checks for everyone, but it can be "configured" to point to one character or another on the fly.

 

Hope that made some semblance of sense :-)

So basically, you have an array for each character and switch the identifiers of the arrays based on who fills each role? If that's what you're saying I get it. It seems fairly simple, and would work. I'd like to examine other possibilities still.

22 minutes ago, frob said:

The typical approach is to have data that gets swapped out.

You might pass an index to a table for the source or target, you might pass an object for the source and the source for the target, or something else.

For example, adding that information to the object itself and accessing it generically:

force += Combat._attacker.Attr["alacrity"];

or perhaps:

force += Combat._attacker.CurrentAlacrity(Combat._defender);

or accessing it through another table:

force += CombatAttribs[Combat._attacker].["alacrity"];

 

I prefer the function version above, since it more easily allows you to add modifications based on the source and the target, such as a bonus versus dragons or penalty versus shopkeepers or whatever fits your game.

 

Thank you for the quick, and in-depth response. I'm sorry, but I don't understand Python 2. I'm guessing that's what these examples are since we never seem to end lines in Python 3.6 with semi-colons. I'm not sure how to make these examples work since I don't understand their formatting. I've never seen class-level variables called in that way, with two periods, to reach specific data. Could you please explain it or link to a website/video that can help me understand how to do the same thing in Python 3? It seems like some of your ways might be better than how I've interpreted ApochiPiQ's suggestion.

 

 

Share this post


Link to post
Share on other sites
8 hours ago, RidiculousName said:

So basically, you have an array for each character and switch the identifiers of the arrays based on who fills each role? If that's what you're saying I get it. It seems fairly simple, and would work. I'd like to examine other possibilities still.

You can wrap the access code in a function

def get_property(combat, property):
    if combat._attacker == "pc":
      return combat._pcAttr[property]
    else:
      return combat._npcAttr[property]

which then gives

force += get_property(Combat, "alacrity") * 3

 

Frob folds the selection into the Combat object (his Pythoneese has some C++ influences, just ignore the semi-colons and the dots that look weird :) )

Instead of making "Combat.attacker" a string, make it the object you point to:

# Inside Combat

def assignAttacker(self, attacker_type):
    if attacker_type == "pc":
        self._attackerAttr = self._pcAttr
        self._defenderAttr = self._npcAttr
    else:
        self._attackerAttr = self._npcAttr
        self._defenderAttr = self._pcAttr

And now you can access Combat._attackerAttr[...]

 

As a note, the "_" prefix denotes a private variable (by convention) in Python, which basically means you should not access those variables from outside the methods of the class (like you do with Combat._pcAttr for example). Other Python programmers will get confused about it.

Unlike eg Java and C++, in Python an object variable is always prefixed by the containing object, that is, you cannot write "_pcAttr" to access the variable in Combat, you must always write something like "Combat._pcAttr" or "self._pcAttr". As such, there is no real need to differentiate between normal variables and object member variables, the "Combat." or "self." prefixes in front of the latter already do this.

 

 

Edited by Alberth
fixed code example

Share this post


Link to post
Share on other sites

Again, Alberth!? Every time I post a question you're there with amazingly well-reasoned and detailed answers. Thank you!

I'll take your advice. Only one thing still puzzles me bit.

Quote

As a note, the "_" prefix denotes a private variable (by convention) in Python, which basically means you should not access those variables from outside the methods of the class (like you do with Combat._pcAttr for example). Other Python programmers will get confused about it.

The method I gave as an example is held within the Combat class. Have I misunderstood you?

Share this post


Link to post
Share on other sites
8 hours ago, RidiculousName said:

Again, Alberth!? Every time I post a question you're there with amazingly well-reasoned and detailed answers. Thank you!

Hmm, yeah, sorry about that. Likely that will continue to happen until you start asking questions about complicated component systems, data-oriented-design, and pretty much anything about GPUs or detailed performance stuff.

 

9 hours ago, RidiculousName said:

The method I gave as an example is held within the Combat class. Have I misunderstood you?

Good point, missed that. It would be fine then I guess, since it's only a convention anyway.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Advertisement