Python Arcade – 显示分数
在本文中,我们将学习如何在Python中的 Arcade 中显示分数。
显示分数
在这个例子中,我们将简单地在屏幕上显示我们的分数,我们将增加每个玩家跳跃的分数。为此,我们将使用一些函数:
draw_text():该函数用于使用 Pyglet 的标签在屏幕上绘制文本。
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(): Camera 类用于控制可见视口。
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():表示场景对象的类。
Syntax: arcade.Scene(sprite_lists , name_mapping)
Parameters:
- sprite_lists: A list of SpriteList objects
- name_mapping: A dictionary of SpriteList objects
PhysicsEnginePlatformer():用于平台游戏的简单物理引擎。
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
使用的精灵():
在下面的示例中,我们将创建一个 MainGame() 类。首先在这个类中,我们将初始化一些变量,用于速度、相机、分数、级别和玩家的精灵,然后我们将在这个类中创建 6 个函数。
- on_draw():在这个函数,我们将使用我们的相机在屏幕上绘制场景和我们的分数文本。
- setup():在这个函数,我们将初始化我们的相机和场景对象,然后我们将加载我们的播放器和平台的精灵。之后,我们将调用 PhysicsEnginePlatformer()函数。
- on_update():在这个函数,我们将更新玩家的精灵、相机和物理引擎的 x 坐标。我们还将在此函数更改我们的级别。
- on_key_press()和 on_key_release():在这个函数,我们将根据按下或释放的键盘键来改变速度变量的值。如果玩家按下向上箭头键,那么我们将增加分数的值。
- camera_move():在这个函数,我们将根据玩家当前的位置移动我们的相机。
下面是实现:
Python3
# Importing arcade module
import arcade
# Creating MainGame class
class MainGame(arcade.Window):
def __init__(self):
super().__init__(600, 600,
title="Player Movement")
# Initializing a variable to store
# the velocity of the player
self.vel_x = 0
# Creating variable for Camera
self.camera = None
# Creating variable to store current score
self.score = 0
# Creating scene object
self.scene = None
# Creating variable to store player sprite
self.player = None
# Creating variable for our game engine
self.physics_engine = None
# Creating on_draw() function to draw on the screen
def on_draw(self):
arcade.start_render()
# Drawing the text
arcade.draw_text('Score :- '+str(self.score),
self.player_sprite.center_x - 500 / 2,
self.player_sprite.center_y + 200,
arcade.color.WHITE, 30, 5000, 'left')
# Using the camera
self.camera.use()
# Drawing our scene
self.scene.draw()
def setup(self):
# Initialize Scene object
self.scene = arcade.Scene()\
# Using Camera() function
self.camera = arcade.Camera(600, 600)
# Creating different sprite lists
self.scene.add_sprite_list("Player")
self.scene.add_sprite_list("Platforms",
use_spatial_hash=True)
# Adding player sprite
self.player_sprite = arcade.Sprite("Player.png", 1)
# Adding coordinates for the center of the sprite
self.player_sprite.center_x = 64
self.player_sprite.center_y = 60
# Adding Sprite in our scene
self.scene.add_sprite("Player", self.player_sprite)
# Adding platform sprite according to level
platform = arcade.Sprite(f"Platform.png", 1)
# Adding coordinates for the center of the platform
platform.center_x = 300
platform.center_y = 32
self.scene.add_sprite("Platforms", platform)
# Creating Physics engine
self.physics_engine = arcade.PhysicsEnginePlatformer(
self.player_sprite, self.scene.get_sprite_list("Platforms"), 0.5
)
# Creating on_update function to
# update the x coordinate
def on_update(self, delta_time):
# Changing x coordinate of player
self.player_sprite.center_x += self.vel_x * delta_time
# Updating the physics engine to move the player
self.physics_engine.update()
# Calling the camera_move function
self.camera_move()
# Creating function to change the velocity
# when button is pressed
def on_key_press(self, symbol, modifier):
# Checking the button pressed
# and changing the value of velocity
if symbol == arcade.key.LEFT:
self.vel_x = -300
elif symbol == arcade.key.RIGHT:
self.vel_x = 300
elif symbol == arcade.key.UP:
if self.physics_engine.can_jump():
self.player_sprite.change_y = 15
# Increasing the score
self.score += 10
# Creating function to change the velocity
# when button is released
def on_key_release(self, symbol, modifier):
# Checking the button released
# and changing the value of velocity
if symbol == arcade.key.LEFT:
self.vel_x = 0
elif symbol == arcade.key.RIGHT:
self.vel_x = 0
def camera_move(self):
# Getting the x coordinate for the center of camera
screen_x = self.player_sprite.center_x - \
(self.camera.viewport_width / 2)
# Getting the y coordinate for the center of camera
screen_y = self.player_sprite.center_y - \
(self.camera.viewport_height / 2)
# Moving the camera
self.camera.move_to([screen_x, screen_y])
# Calling MainGame class
game = MainGame()
game.setup()
arcade.run()
Python3
# Importing arcade module
import arcade
# Creating MainGame class
class MainGame(arcade.Window):
def __init__(self):
super().__init__(600, 600,
title = "Score")
# Initializing a variable to store
# the velocity of the player
self.vel_x = 0
# Creating variable for Camera
self.camera = None
# Creating variable to store current score
self.score = 0
# Creating scene object
self.scene = None
# Creating variable to store player sprite
self.player = None
# Creating variable for our game engine
self.physics_engine = None
# Creating on_draw() function to draw on the screen
def on_draw(self):
arcade.start_render()
# Drawing the text
arcade.draw_text('Score :- '+str(self.score),
self.player_sprite.center_x -500 / 2,
self.player_sprite.center_y +200,
arcade.color.WHITE,30,5000,'left')
# Using the camera
self.camera.use()
# Drawing our scene
self.scene.draw()
def setup(self):
# Initialize Scene object
self.scene = arcade.Scene()
# Using Camera() function
self.camera = arcade.Camera(600, 600)
# Creating different sprite lists
self.scene.add_sprite_list("Player")
self.scene.add_sprite_list("Platforms",
use_spatial_hash=True)
# Adding player sprite
self.player_sprite = arcade.Sprite("Player.png", 1)
# Adding coordinates for the center of the sprite
self.player_sprite.center_x = 64
self.player_sprite.center_y = 60
# Adding Sprite in our scene
self.scene.add_sprite("Player", self.player_sprite)
# Adding platform sprite according to level
platform = arcade.Sprite(f"Platform.png", 1)
# Adding coordinates for the center of the platform
platform.center_x = 300
platform.center_y = 32
self.scene.add_sprite("Platforms", platform)
# Creating Physics engine
self.physics_engine = arcade.PhysicsEnginePlatformer(
self.player_sprite, self.scene.get_sprite_list("Platforms"), 0.5
)
# Creating on_update function to
# update the x coordinate
def on_update(self, delta_time):
# Changing x coordinate of player
self.player_sprite.center_x += self.vel_x * delta_time
# Updating the physics engine to move the player
self.physics_engine.update()
# Setting the score to 0 whenever
# player falss from the platform
if self.player_sprite.center_y < -20:
self.score = 0
self.setup()
# Calling the camera_move function
self.camera_move()
# Creating function to change the velocity
# when button is pressed
def on_key_press(self, symbol, modifier):
# Checking the button pressed
# and changing the value of velocity
if symbol == arcade.key.LEFT:
self.vel_x = -300
elif symbol == arcade.key.RIGHT:
self.vel_x = 300
elif symbol == arcade.key.UP:
if self.physics_engine.can_jump():
self.player_sprite.change_y = 15
# Increasing the score
self.score += 10
# Creating function to change the velocity
# when button is released
def on_key_release(self, symbol, modifier):
# Checking the button released
# and changing the value of velocity
if symbol == arcade.key.LEFT:
self.vel_x = 0
elif symbol == arcade.key.RIGHT:
self.vel_x = 0
def camera_move(self):
# Getting the x coordinate for the center of camera
screen_x = self.player_sprite.center_x - \
(self.camera.viewport_width / 2)
# Getting the y coordinate for the center of camera
screen_y = self.player_sprite.center_y - \
(self.camera.viewport_height / 2)
# Moving the camera
self.camera.move_to([screen_x, screen_y])
# Calling MainGame class
game = MainGame()
game.setup()
arcade.run()
输出:
重置分数
在这个例子中,每当玩家从平台跌落并从起始位置重新开始游戏时,我们将把分数重置为 0。为此,我们将简单地在 on_update()函数添加以下几行。
if self.player_sprite.center_y < -20:
self.score = 0
self.setup()
如果玩家从平台上掉下来,则将 score 变量的值设置为 0 并调用我们的 setup()函数以重新启动游戏。
下面是实现:
蟒蛇3
# Importing arcade module
import arcade
# Creating MainGame class
class MainGame(arcade.Window):
def __init__(self):
super().__init__(600, 600,
title = "Score")
# Initializing a variable to store
# the velocity of the player
self.vel_x = 0
# Creating variable for Camera
self.camera = None
# Creating variable to store current score
self.score = 0
# Creating scene object
self.scene = None
# Creating variable to store player sprite
self.player = None
# Creating variable for our game engine
self.physics_engine = None
# Creating on_draw() function to draw on the screen
def on_draw(self):
arcade.start_render()
# Drawing the text
arcade.draw_text('Score :- '+str(self.score),
self.player_sprite.center_x -500 / 2,
self.player_sprite.center_y +200,
arcade.color.WHITE,30,5000,'left')
# Using the camera
self.camera.use()
# Drawing our scene
self.scene.draw()
def setup(self):
# Initialize Scene object
self.scene = arcade.Scene()
# Using Camera() function
self.camera = arcade.Camera(600, 600)
# Creating different sprite lists
self.scene.add_sprite_list("Player")
self.scene.add_sprite_list("Platforms",
use_spatial_hash=True)
# Adding player sprite
self.player_sprite = arcade.Sprite("Player.png", 1)
# Adding coordinates for the center of the sprite
self.player_sprite.center_x = 64
self.player_sprite.center_y = 60
# Adding Sprite in our scene
self.scene.add_sprite("Player", self.player_sprite)
# Adding platform sprite according to level
platform = arcade.Sprite(f"Platform.png", 1)
# Adding coordinates for the center of the platform
platform.center_x = 300
platform.center_y = 32
self.scene.add_sprite("Platforms", platform)
# Creating Physics engine
self.physics_engine = arcade.PhysicsEnginePlatformer(
self.player_sprite, self.scene.get_sprite_list("Platforms"), 0.5
)
# Creating on_update function to
# update the x coordinate
def on_update(self, delta_time):
# Changing x coordinate of player
self.player_sprite.center_x += self.vel_x * delta_time
# Updating the physics engine to move the player
self.physics_engine.update()
# Setting the score to 0 whenever
# player falss from the platform
if self.player_sprite.center_y < -20:
self.score = 0
self.setup()
# Calling the camera_move function
self.camera_move()
# Creating function to change the velocity
# when button is pressed
def on_key_press(self, symbol, modifier):
# Checking the button pressed
# and changing the value of velocity
if symbol == arcade.key.LEFT:
self.vel_x = -300
elif symbol == arcade.key.RIGHT:
self.vel_x = 300
elif symbol == arcade.key.UP:
if self.physics_engine.can_jump():
self.player_sprite.change_y = 15
# Increasing the score
self.score += 10
# Creating function to change the velocity
# when button is released
def on_key_release(self, symbol, modifier):
# Checking the button released
# and changing the value of velocity
if symbol == arcade.key.LEFT:
self.vel_x = 0
elif symbol == arcade.key.RIGHT:
self.vel_x = 0
def camera_move(self):
# Getting the x coordinate for the center of camera
screen_x = self.player_sprite.center_x - \
(self.camera.viewport_width / 2)
# Getting the y coordinate for the center of camera
screen_y = self.player_sprite.center_y - \
(self.camera.viewport_height / 2)
# Moving the camera
self.camera.move_to([screen_x, screen_y])
# Calling MainGame class
game = MainGame()
game.setup()
arcade.run()
输出: