VOOZH about

URL: https://www.geeksforgeeks.org/python/python-arcade-display-score/

⇱ Python Arcade - Display Score - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Arcade - Display Score

Last Updated : 23 Sep, 2021

In this article, we will learn how to display scores in Arcade in Python.

Displaying the Score

In this example, we are going to simply display our score on the screen and we will increase the score every player jumps. For this, we will use some functions:

draw_text(): This function is used to draw text to the screen using Pyglet’s label.

Syntax: arcade.draw_text(text, x, y, color, size, width, align, font_name)

Parameters:

  • text: Text we want to display
  • x : x coordinate
  • y : y coordinate
  • color : color of the text
  • size : Size of the font
  • width : Width of the text
  • align : Alignment of the text
  • font_name : Name of the font

Camera(): The Camera class is used for controlling the visible viewport.

Syntax: arcade.Camera( width , height, window)

Parameters:

  • width: width of the viewport
  • height: height of the viewport
  • window: Window to associate with this camera

Scene(): A class that represents a scene object.

Syntax: arcade.Scene(sprite_lists , name_mapping)

Parameters:

  • sprite_lists: A list of SpriteList objects
  • name_mapping: A dictionary of SpriteList objects

PhysicsEnginePlatformer(): Simplistic physics engine for use in a platformer.

Syntax: arcade.PhysicsEnginePlatformer( player_sprite , platforms, gravity, ladders)

Parameters:

  • player_sprite: sprite of the player
  • platforms: The sprites it can’t move through
  • gravity: Downward acceleration per frame
  • ladders: Ladders the user can climb on

Sprites Used():

πŸ‘ Image
πŸ‘ Image

In the below example, we are going to create a MainGame() class. Inside this class first, we are going to initialize some variables for velocity, camera, score, level, and player's sprite then we will create 6 functions inside this class.

  • on_draw(): Inside this function, we will use our camera and draw the scene and our score text on the screen.
  • setup(): In this function, we will initialize our camera and scene object then we will load our player and platform's sprites. After that, we will call the  PhysicsEnginePlatformer() function.
  • on_update(): In this function, we will update the x coordinates of the player's sprite, camera, and physics engine. We will also change our level in this function.
  • on_key_press() and on_key_release(): In this function, we will change the value of the velocity variable according to the keyboard key that is pressed or released. And if the player presses the UP arrow key then we will increase the value of the score.
  • camera_move(): In this function, we will move our camera according to our player's current position.

Below is the implementation:

Output:

πŸ‘ Image

Resetting the Score

In this example, we will reset our score to 0 whenever the player falls from the platform and restart the game from starting position. For this, we will simply add the below lines in our on_update() function.

if self.player_sprite.center_y < -20:
 self.score = 0
 self.setup()

If the player falls from the platform then ware setting the value of the score variable to 0 and calling our setup() function to restart the game.

Below is the implementation:

Output:

πŸ‘ Image
Comment
Article Tags:
Article Tags: