Code: Alles auswählen
import pgzrun
from random import randint
WIDTH = 800
HEIGHT = 600
stickman = Actor("stickman")
stickman.pos = 50, 527
wall = Actor("wall")
wall.pos = randint(800, 1600), 527
game_over = False
score = 0
scores = []
def update_high_scores():
global score, scores
filename = r"/Users/stieb/Desktop/Programmer/FartRun/highscores.txt"
scores = []
with open(filename, "r") as file:
line = file.readline()
high_scores = line.split()
for high_score in high_scores:
if(score > int(high_score)):
scores.append(str(score) + " ")
score = int(high_score)
else:
scores.append(str(high_score) + " ")
with open(filename, "w") as file:
for high_score in scores:
file.write(high_score)
def display_high_scores():
screen.draw.text("HIGHSCORES", (350, 150), color="black")
y = 175
position = 1
for high_score in scores:
screen.draw.text(str(position) + ". " + high_score, (350, y), color="black")
y += 25
position += 1
def draw():
screen.blit("background", (0, 0))
if not game_over:
stickman.draw()
wall.draw()
screen.draw.text("Punkte: " + str(score), (700, 5), color = "black")
else:
display_high_scores()
def update():
global game_over, score, run
if not game_over:
if wall.right > 0:
wall.x -= 2
else:
wall.x = randint(1600, 3200)
score += 1
if stickman.collidepoint(wall.x, wall.y):
game_over = True
update_high_scores()
pgzrun.go()