Eh, read at your own leisure when you have time. Even if you are not interested, python is a very popular language for getting into making games thanks to pygame. I'm sure somebody out there can always use the help.
Anway, part 2!
The important thing to realise is that objects are types, just like ints or strings, we can have multiple objects of the same class with different properties. For example...
class Player:
def __init__ (self):
self.name = raw_input('Please enter player name: ')
player1 = Player()
player2 = Player()
print player1.name, 'vs' player2.name
Try that out. As you can see, the first and second player are separate objects, meaning that player1's name is not the same name as player2's name. Now why don't we add that other thing we needed our player class to have.
import random
class Player:
def __init__ (self):
self.name = raw_input('Please enter player name: ')
self.target = random.randint(1, 100)
player1 = Player()
player2 = Player()
print player1.name, 'vs', player2.name
Now just like each player has their own separate name, each player also has their own target to guess. We can declare what ever values we need in the __init__ method for the class, but we can also do something else for our players. We can give them functions.
import random
class Player:
def __init__ (self):
self.name = raw_input('Please enter player name: ')
self.target = random.randint(1, 100)
def guess(self):
print self.name, 'is up to guess.'
guess = int(raw_input('Please enter your guess: '))
if guess < self.target:
print 'Sorry, that is too low.'
return False
if guess > self.target:
print 'Sorry, that is too high.'
return False
print 'Well done, that is correct!'
return True
player1 = Player()
player2 = Player()
print player1.name, 'vs', player2.name
player1.guess()
There, we have our guess method, and with that, our player class is pretty much done! It is now a fully functional class that can preform everything required by our project, but let's explain how that a little more.
Once again, we start by defining a function inside the class, give it a name, and once again we make sure the first parameter is self. Almost the same as __init__, but this time we give a more meaningful name to the function. After that, you need to remember that we are working inside an object, thus if we want to reference to anything in the object, we are referencing to itself, or self, as the case may be. Might sound a little tricky, but just think that when you call the function with object.function(), it is object that is put into 'self', thus using 'self' will use the object.
The rest should be straight forward enough, and all things you have seen before. If not, feel free to ask.
That is it for objects, as far as this lesson goes, but we might as well finish the game off for fun. It should be all old news by now anyway, but once again, please tell me if anything seems strange to you.
import random
from collections import deque
class Player:
def __init__ (self, name):
self.name = name
self.target = random.randint(1, 100)
print self.name, "takes a seat at the table."
def guess(self):
print self.name, 'is up to guess.'
guess = int(raw_input('Please enter your guess: '))
if guess < self.target:
print 'Sorry, that is too low.'
return False
if guess > self.target:
print 'Sorry, that is too high.'
return False
print 'Well done, that is correct!'
return True
players = deque([])
name = raw_input('Please enter player name or press enter to start the game: ')
while name != "":
players.append(Player(name))
name = raw_input('Please enter player name or press enter to start the game: ')
while len(players) > 0:
activePlayer = players.popleft()
if(activePlayer.guess()):
print activePlayer.name, "Has won and left the game"
else:
players.append(activePlayer)
print
print "Game over!"
There we are! In just 37 lines, counting white space and imports, we made a fully functional game. Such is the power of objects, to keep logic where it needs to be, and encapsulate relevant data.