-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
74 lines (52 loc) · 1.63 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# PyRPG
# File: player.py
# Version: 1.0
# Creator: Joel D. Garrett
# Modified: 3/5/2011
import os.path
from gamestate import *
# Private Attributes
class Player(object):
# Class Attributes
name = ""
level = 1
experience = 0
attack = 10
damage = 0
def __init__(self, name__):
self.name = name__
def health(self):
return 50 + (self.level * 50)
def hitpoints(self):
return self.health() - self.damage
def printStats(self):
# Line padding
stats = "\n "
# Level: Name
stats = stats + "%d: %s" % (self.level, self.name)
# Hitpoints/Health
stats = stats + "\t\t%d/%d\n" % (self.hitpoints(), self.health())
print stats
def load(self):
success = False
path = "players/%s.sav" % (self.name.lower())
if os.path.exists(path):
file = open(path, 'r')
data = file.readline().split(':')
if len(data) == 3:
success = True
self.level = int(data[0])
self.experience = int(data[1])
self.attack = int(data[2])
print "Welcome back %s. Your progress has been restored." % (self.name)
file.close()
return success
def save(self):
if not os.path.exists("players/"):
os.mkdir("players/")
path = "players/%s.sav" % (self.name.lower())
file = open(path, 'w')
data = ":".join((str(self.level), str(self.experience), str(self.attack)))
file.write(data)
print "-- Player data saved --"
file.close()