Jul-30-2019, 09:01 AM
Object of type Scoreboard is not JSON serializable
IS ANY OTHER WAY TO serialize this OBJECT ... ? trying a scoreboard for a game...
>>> %Run snake-points-pause-sounds-highscores2.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 424, in <module>
drawGameOver(surface, data.username, data.points)
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 115, in drawGameOver
board = scoreRecord(username, points)
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 355, in scoreRecord
serialize("hightscores.txt", board)
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 285, in serialize
json.dump(players, f)
File "C:\Users\lwdls\Thonny\lib\json\__init__.py", line 179, in dump
for chunk in iterable:
File "C:\Users\lwdls\Thonny\lib\json\encoder.py", line 438, in _iterencode
o = _default(o)
File "C:\Users\lwdls\Thonny\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Scoreboard is not JSON serializable
IS ANY OTHER WAY TO serialize this OBJECT ... ? trying a scoreboard for a game...
>>> %Run snake-points-pause-sounds-highscores2.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 424, in <module>
drawGameOver(surface, data.username, data.points)
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 115, in drawGameOver
board = scoreRecord(username, points)
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 355, in scoreRecord
serialize("hightscores.txt", board)
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 285, in serialize
json.dump(players, f)
File "C:\Users\lwdls\Thonny\lib\json\__init__.py", line 179, in dump
for chunk in iterable:
File "C:\Users\lwdls\Thonny\lib\json\encoder.py", line 438, in _iterencode
o = _default(o)
File "C:\Users\lwdls\Thonny\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Scoreboard is not JSON serializable
# hghscores - start
def deserialize(fileName):
exists = os.path.isfile(fileName)
if exists:
# Store configuration file values
f = open(fileName, 'r')
board = json.load(f)
f.close()
else:
# Keep presets
board = Scoreboard()
return board
def serialize(fileName, players):
f = open(fileName, 'w')
json.dump(players, f)
f.close()
class GameEntry:
"""Represents one entry of a list of high scores."""
def __init__(self, name, score):
"""Create an entry with given name and score."""
self._name = name
self._score = score
def get_name(self):
"""Return the name of the person for this entry."""
return self._name
def get_score(self):
"""Return the score of this entry."""
return self._score
def __str__(self):
"""Return string representation of the entry."""
return '({0}, {1})'.format(self._name, self._score) # e.g., '(Bob, 98)'
class Scoreboard:
"""Fixed-length sequence of high scores in nondecreasing order."""
def __init__(self, capacity=10):
"""Initialize scoreboard with given maximum capacity.
All entries are initially None.
"""
self._board = [None] * capacity # reserve space for future scores
self._n = 0 # number of actual entries
def __getitem__(self, k):
"""Return entry at index k."""
return self._board[k]
def __str__(self):
"""Return string representation of the high score list."""
return '\n'.join(str(self._board[j]) for j in range(self._n))
def add(self, entry):
"""Consider adding entry to high scores."""
score = entry.get_score()
# Does new entry qualify as a high score?
# answer is yes if board not full or score is higher than last entry
good = self._n < len(self._board) or score > self._board[-1].get_score()
if good:
if self._n < len(self._board): # no score drops from list
self._n += 1 # so overall number increases
# shift lower scores rightward to make room for new entry
j = self._n - 1
while j > 0 and self._board[j-1].get_score() < score:
self._board[j] = self._board[j-1] # shift entry from j-1 to j
j -= 1 # and decrement j
self._board[j] = entry # when done, add new entry
def scoreRecord(username, points):
board = deserialize("hightscores.txt")
entry = GameEntry(username, points)
board.add(entry)
serialize("hightscores.txt", board)
return board
