Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2]

Author Topic: Let's Python! (2.7.2) Slowly headed towards Amazons and Barbarians.  (Read 2784 times)

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #15 on: September 18, 2011, 09:54:28 am »

I think I'll take a break today for now.
Here's the code so far, and it works fine up until it needs to display the name. For some reason, that function refuses to want to work.
Code: [Select]
class Player:
def __init__(self):
self.name = 'somebody'
def namePlayer():
playerName = raw_input()
print(playerName)
def displayName():
print(self.name)

def main():
Me = Player()
print ('YOU ARE A MIGHTY ADVENTURER, WHAT NAME HAVE YOU!?')
Me.name = raw_input()
Me.displayName
return 0
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #16 on: September 18, 2011, 10:01:02 am »

Well, for one, your namePlayer method uses a variable called playerName that doesn't get used anywhere else.

Of course that cannot be it because that's why the 'somebody' at initialization is there for.

Try this?
Code: [Select]
class Player:
   def __init__(self):
      self.name = 'somebody'
   def displayName(self):
      print(self.name)

def main():
   Me = Player()
   print ('YOU ARE A MIGHTY ADVENTURER, WHAT NAME HAVE YOU!?')
   Me.name = raw_input()
   Me.displayName
   return 0
Logged

Chris_24

  • Bay Watcher
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #17 on: September 18, 2011, 10:13:14 am »

Code: [Select]
Me.displayName
should be
Code: [Select]
Me.displayName()
:)
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #18 on: September 18, 2011, 10:15:28 am »

Right.

Why do I never notice the most obvious things <_<
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #19 on: September 18, 2011, 10:36:28 am »

Ah, so you have to use self for functions that affect themselves. Go figure. Well, I went with Darvi's suggestion and it worked, then I went ahead and tried to make naming restricted to using a function, which works too. Here's the cleaned up code.

Code: [Select]
class Player:
def __init__(self):
self.name = 'somebody'
def namePlayer(self):
playerName = raw_input()
print(playerName)

def main():
Me = Player()
print ('YOU ARE A MIGHTY ADVENTURER, WHAT NAME HAVE YOU!?')
Me.namePlayer()
return 0
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #20 on: September 18, 2011, 05:49:06 pm »

Try the following. Like I said, model view separation.

Code: [Select]
class Player:
def __init__(self, name):
self.name = name

def main():
print ('YOU ARE A MIGHTY ADVENTURER, WHAT NAME HAVE YOU!?')
Me = Player(raw_input())
return 0

Biag

  • Bay Watcher
  • Huzzah!
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #21 on: September 18, 2011, 10:40:39 pm »

You can put the input prompt inside the raw_input() statement, by the way. Like so:

Code: [Select]
def main():
        me = Player(raw_input('YOU ARE A MIGHTY ADVENTURER! NAME THYSELF: ')

Also, it's good programming practice for all your variables to start with a lowercase letter, and save the capitalized words for classes. In a game, for example, you would have the "Player" class and the "player" variable, which stores an instance of that class.
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #22 on: September 19, 2011, 12:14:42 pm »

And back, time to post an even more cleaned up version of the code. And yeah, I know about naming variables and stuff, I just slipped there, so oops.
Anyway, this time, the code is about 9 lines, with 3 of those lines more or less there for debugging (the displayName bits):

Code: [Select]
class Player:
def __init__(self,name):
self.name = name
def displayName(self):
print(self.name)

def main():
player = Player(raw_input('YOU RE A MIGHT ADVENTURER, WHAT NAME HAVE YOU!? : '))
player.displayName()
return 0

Now, I'm fairly sure that improving on this code is very unlikely, as this is as simple as it gets. So what next? We have a player, and he has a mighty name, but not much else.
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Let's Python! (2.7.2)
« Reply #23 on: September 19, 2011, 01:30:00 pm »

Make some sort of combat? Pit him against an enemy and see how it works out.

Bonus points if you can get your character to shout a battle cry on a critical hit >:3
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Let's Python! (2.7.2) Slowly headed towards Amazons and Barbarians.
« Reply #24 on: September 19, 2011, 01:45:49 pm »

Okay, so before I move on to implement that, it seems complex enough that it'd need some planning out before hand. So here we go.

We want you, the fighter-guy-dude(tte) to fight monsters for the glory of (insert god/ideal here). Now, this is much more complicated than a program that gives you a nametag then shuts off.

So we'll need the following:
-Hitpoints, for both the player and the enemy
-Attack, in order to hit stuff
-Damage, in order to hurt stuff
-Random numbers, in order to randomly miss and stuff.
And so on.

First, we need to define an enemy class. Then we need to go ahead and give it some stats, starting with hitpoints. Once we have hitpoints, we'll work on erasing them and seeing what happens with the code we have in place. Now we can't cheat and only have one instance of the enemy, that'd be stupid, since it'd interfere with us later on.

That's the goal so far, tings having hitpoints and dying, to be replaced with new things.
Logged

chaoticag

  • Bay Watcher
  • All Natural Pengbean
    • View Profile
Re: Let's Python! (2.7.2) Slowly headed towards Amazons and Barbarians.
« Reply #25 on: September 24, 2011, 07:43:28 am »

And now, for something completely different.

Spoiler (click to show/hide)

So, that code is something I cooked up while tutoring my little sister on division. Sadly, she never got to be my Ginny pig, but it works!

And then, there is something else. A friend of mine wrote this code:
Bonus points if you can decipher what it does (I take no responsibility on what happens if you actually run it)
Spoiler (click to show/hide)
Logged
Pages: 1 [2]