using the example import csv def touch_ground(sprite, hit_sprite): """ Specifies what happens when ball hits the ground """ global file_name hit_sprite.set_x_speed(0) hit_sprite.set_y_speed(0) hit_sprite.go_to(0,0) hit_sprite.say("GAME OVER") stage.wait(1) hit_sprite.hide() show_scores(file_name) def set_stage(): """ Sets up the stage for the game """ stage.set_background("soccerfield") stage.disable_floor() def add_player(): """ Adds a player to the stage for the user to control """ player = codesters.Sprite("player1") player.go_to(0, -155) return player def add_ball(): """ Adds a ball to the stage and sets its attributes """ ball = codesters.Sprite("soccerball") ball.set_y_speed(-8) def head_ball(sprite, hit_sprite): """ Detects collisions between the player and ball """ global score score += 1 sprite.say(score) my_var = hit_sprite.get_y_speed() hit_sprite.set_y_speed(-my_var + 1) my_var = hit_sprite.get_x_speed() hit_sprite.set_x_speed(my_var + 1) def move_left(sprite): """ Moves the player left """ sprite.move_left(25) def move_right(sprite): """ Moves the player right """ sprite.move_right(25) def show_scores(file_name): """ Calls all CSV-related functions """ global score, player_name high_scores = get_high_scores(file_name) high_scores.append([player_name, score]) sort_by_score(high_scores) update_high_scores(file_name, high_scores) new_high_scores = get_high_scores(file_name) display_scores(new_high_scores) def get_high_scores(file_name): """ Returns data from CSV as list of lists """ my_file = open(file_name, "r") csv_contents = [] my_data = csv.reader(my_file) for row in my_data: row[1] = int(row[1]) csv_contents.append(row) my_file.close() return csv_contents def update_high_scores(file_name, my_data): """ Uses list of lists to write to new file """ my_file = open(file_name, "w") data_writer = csv.writer(my_file) for row in my_data: data_writer.writerow(row) my_file.close() def sort_by_score(data): """ Sort a list of lists by it's second column """ data.sort(reverse = True, key = lambda x: x[1]) def display_scores(data): """ Display the list of high scores """ for row in data: sprite = codesters.Text(row[0] + ": " + str(row[1]), -350, 200, "black") sprite.set_speed(10) sprite.move_right(350) stage.wait(1) sprite.hide() def main(): """ Sets up the program and calls other functions """ global score, player_name, file_name file_name = "headball_highscores.csv" score = 0 ground = codesters.Rectangle(0,-260, 500, 10, "green") ground.set_physics_off() ground.event_collision(touch_ground) set_stage() player = add_player() player_name = player.ask("What's your name?") add_ball() player.event_collision(head_ball) player.event_key("left", move_left) player.event_key("right", move_right) main()
add it to



import csv

# Global Variables
score = 0
player_name = ''
file_name = "headball_highscores.csv"

# Function to handle game over scenario and display high scores
def touch_ground(sprite, hit_sprite):
    hit_sprite.set_x_speed(0)
    hit_sprite.set_y_speed(0)
    hit_sprite.go_to(0, 0)
    hit_sprite.say("GAME OVER")
    stage.wait(1)
    hit_sprite.hide()
    show_scores(file_name)

# Function to set up the game stage
def set_stage():
    stage.set_background("soccerfield")
    stage.disable_floor()

# Function to add a player to the stage
def add_player():
    player = codesters.Sprite("player1")
    player.go_to(0, -155)
    return player

# Function to add a ball to the stage
def add_ball():
    ball = codesters.Sprite("soccerball")
    ball.set_y_speed(-8)

# Function to handle collisions between player and ball
def head_ball(sprite, hit_sprite):
    global score
    score += 1
    sprite.say(score)
    my_var = hit_sprite.get_y_speed()
    hit_sprite.set_y_speed(-my_var + 1)
    my_var = hit_sprite.get_x_speed()
    hit_sprite.set_x_speed(my_var + 1)

# Function to move the player left
def move_left(sprite):
    sprite.move_left(25)

# Function to move the player right
def move_right(sprite):
    sprite.move_right(25)

# Function to show high scores
def show_scores(file_name):
    global score, player_name
    high_scores = get_high_scores(file_name)
    high_scores.append([player_name, score])
    sort_by_score(high_scores)
    update_high_scores(file_name, high_scores)
    new_high_scores = get_high_scores(file_name)
    display_scores(new_high_scores)

# Function to get high scores from the CSV file
def get_high_scores(file_name):
    csv_contents = []
    with open(file_name, "r") as my_file:
        my_data = csv.reader(my_file)
        for row in my_data:
            row[1] = int(row[1])
            csv_contents.append(row)
    return csv_contents

# Function to update high scores to the CSV file
def update_high_scores(file_name, my_data):
    with open(file_name, "w") as my_file:
        data_writer = csv.writer(my_file)
        for row in my_data:
            data_writer.writerow(row)

# Function to sort high scores by score
def sort_by_score(data):
    data.sort(reverse=True, key=lambda x: x[1])

# Function to display high scores on the stage
def display_scores(data):
    y_pos = 200
    for idx, row in enumerate(data):
        sprite = codesters.Text(row[0] + ": " + str(row[1]), -350, y_pos, "black")
        sprite.set_speed(10)
        sprite.move_right(350)
        stage.wait(1)
        sprite.hide()
        y_pos -= 20

# Main function to set up the game and call other functions
def main():
    global score, player_name, file_name
    set_stage()
    player = add_player()
    player_name = player.ask("What's your name?")
    player.event_collision(head_ball)
    player.event_key("left", move_left)
    player.event_key("right", move_right)
    ground = codesters.Rectangle(0, -260, 500, 10, "green")
    ground.set_physics_off()
    ground.event_collision(touch_ground)
    main()

main()