I need help with a game in (turtle graphics - python)
Peter Otten
__peter__ at web.de
Wed Feb 22 09:27:21 EST 2017
Kasper wrote:
> How can i make the score stop blinking
The following part
> #draws the score on the screen
> mypen.undo()
> mypen.penup()
> mypen.hideturtle()
> mypen.setposition(-290, 310)
> scorestring1 = (name1 + ": %s" + " points ") %score1
> scorestring2 = (name2 + ": %s" + " points") %score2
> mypen.write(scorestring1 + scorestring2, False, align="left",
> font=("Arial",14, "normal"))
of your code removes what was written before and then draws something new,
hence the blinking. One easy improvement is to call the above only when the
score has changed:
# untested
...
# outside the loop:
old_score1 = None
old_score2 = None
...
# in the loop:
if old_score1 != score1 or old_score2 != score2:
mypen.undo()
mypen.penup()
mypen.hideturtle()
mypen.setposition(-290, 310)
scorestring1 = (name1 + ": %s" + " points ") %score1
scorestring2 = (name2 + ": %s" + " points") %score2
mypen.write(scorestring1 + scorestring2, False, align="left",
font=("Arial",14, "normal"))
# make sure the if-suite is not executed again
# unless there was a change
old_score1 = score1
old_score2 = score2
> and how can i make a high score table, in this game made with python?
You can store the (score, name) pairs in a file that you update when the
program terminates. Do you know how to read/write a file?
Given a file containing
10 Jim
13 Sue
5 Dave
you can use the split() method break a line into the score and the name, and
int() to convert the score from a string to an integer.
When you put the entries into a list it is easy to sort them in reverse
order:
>>> highscores = [(10, "Jim"), (13, "Sue"), (5, "Dave")]
>>> for score, name in sorted(highscores, reverse=True):
... print("%3d %s" % (score, name))
...
13 Sue
10 Jim
5 Dave
More information about the Python-list
mailing list