Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Can someone help me with python?  (Read 1441 times)

bostltch

  • Bay Watcher
    • View Profile
Can someone help me with python?
« on: February 07, 2018, 06:11:31 am »

So I felt like trying to learn code on my own and make a very basic text based game in python as my little learner project, Up to this point things were going well. At first I was following a YouTube tutorial but it was just the very basics for the game and I've out paced and and am continuing to add things. The current problem is with my simple fight mechanic with factoring player and enemy defense. (also I realize that for some reason in the code I used the British spelling of "defence" rather than "defense" but *oh well*)

Spoiler (click to show/hide)

That's the whole thing and I get this error

Spoiler (click to show/hide)

If someone could tell me what I'm doing wrong I'd be very grateful because the player attack works fine, it bugs out with the enemy attack only and I don't get it, I'm probably just missing something simple. Like I said  though, I'm very new to this so I realize this is very simple and most likely not very optimized but my hope is that if I keep doing this it'll get better. Also if more code is needed to actually solve the problem let me know and I'll post that too.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: Can someone help me with python?
« Reply #1 on: February 07, 2018, 06:26:04 am »

"Method" means a function that's part of a class. In this case, playerc.defence is not a value, it's a function. I'm guessing here, since you haven't shown that part of the code.

When you want to use a value that's returned from a function, you add () after the name. e.g. you write something like

edamage -= playerc.defence()

The reason for this is because "playerc.defence" refers to the function's address in memory (for e.g. passing a function around as a parameter) but "playerc.defence()" means "execute the function, then give me the result".



bostltch

  • Bay Watcher
    • View Profile
Re: Can someone help me with python?
« Reply #2 on: February 07, 2018, 07:14:18 am »

In this case, playerc.defence is not a value, it's a function.

Ah! Yes now I see. It's because the player's defense is defined in a property under the player class so they could equip armor and have it change but the defense for enemy classes is just a flat value because it will never change. I didn't realize that and got confused by it working on the enemy but not the player. Thank you so much!
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: Can someone help me with python?
« Reply #3 on: February 07, 2018, 07:16:07 am »

As a tip, you probably want to make both the player class and the enemy class be the same class. It's much easier to improve your game if you do things like that.

So, say sometime in the future, you decide you want an enemy to pick up armor, or cast a buff spell on themselves, it becomes much more trivial to do that if you've built a single creature class that's used by both players and enemies: you can co-opt any ability designed for a player for an enemy, and vice-versa: all the things that a player or an enemy could do become creature abilities which any creature can access. This also makes building boss fights easier, since you can have the boss use player moves.

Then, you have a controller object which takes input from the player, and sends commands to the creature you're controlling. Controllers can then become an object type: you have one controller for the player-creatures and various AI controllers for the enemy-creatures. But the game engine doesn't need to know which is which, it treats them all the same, meaning the code ends up much simpler.
« Last Edit: February 07, 2018, 07:27:10 am by Reelya »
Logged

bostltch

  • Bay Watcher
    • View Profile
Re: Can someone help me with python?
« Reply #4 on: February 07, 2018, 04:08:27 pm »

As a tip, you probably want to make both the player class and the enemy class be the same class. It's much easier to improve your game if you do things like that.

I see by your explanation how that would obviously be better than me having separate classes for each creature but the problem with that is that I would have to rewrite a lot of the initial code to do it, parts that were covered by the videos I watched to start the project. The guy didn't exactly fully explain why or how the things in the classes worked, I just know that they do the way that they are and if I touch them things will break. So if I get better I will keep this in mind, thanks!
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: Can someone help me with python?
« Reply #5 on: February 07, 2018, 11:49:26 pm »

Rewriting code can be a lot of fun actually, and there are ways to do it safely. e.g. incrementally.

- make an object "creature" which is used by both the player and enemy objects

- *gradually* move stats inside the "creature" object, test it each time you do. The low-hanging fruit is to find what's the *same* in Player and Enemy (both values and code), and move those features into the shared class. But after that, you can find things that are *different* and work out ways to encode them into the same structure.

- eventually, you will hit a point where there's nothing left in the Player and Enemy class, and they're just a thin wrapper around a creature object each. Then, only a tiny bit of rewriting is needed to remove the wrappers and have the program use the Creature objects directly.
« Last Edit: February 07, 2018, 11:54:56 pm by Reelya »
Logged

bloop_bleep

  • Bay Watcher
    • View Profile
Re: Can someone help me with python?
« Reply #6 on: March 23, 2018, 10:24:30 pm »

As a sort of compromise between your current code and Reelya's suggestion, you could create a Creature class that contains the common functionality of Player and Enemy objects, and make those two classes inherit from that. This is because you might have some asymmetry between Player and Enemy objects, I.e. most of the code is the same, but there's might be something one class can do that the other class can't do.
Logged
Quote from: KittyTac
The closest thing Bay12 has to a flamewar is an argument over philosophy that slowly transitioned to an argument about quantum mechanics.
Quote from: thefriendlyhacker
The trick is to only make predictions semi-seriously.  That way, I don't have a 98% failure rate. I have a 98% sarcasm rate.