使用 Arcade Library 入门 2D 游戏(第9部分):显示得分
I’m happy to translate the article for you, but it looks like the body of the post wasn’t included in your message—only the source link was provided. Could you please paste the text you’d like translated (excluding any code blocks or URLs you want to keep unchanged)? Once I have the content, I’ll deliver a Simplified‑Chinese version while preserving the original formatting.
显示得分
在本文中,我们将在屏幕上显示收集到的硬币数量作为得分。
1. 创建文本对象
在 main.py 中 GameView 类的构造函数里,准备一个数值型得分变量,并使用 arcade.Text() 创建一个文本对象。
将以下信息作为参数传入:
- 要显示的文本
- X / Y 坐标
- 文本颜色
- 字体大小
- X 锚点(使用文本的水平中心)
- Y 锚点(使用文本的顶部)
# main.py (excerpt)
# Score
self.score = 0
# Text object
self.score_text = arcade.Text(
"SCORE: {}".format(self.score),
self.w / 2, self.h - 20,
arcade.color.BLACK,
16,
anchor_x="center",
anchor_y="top"
)
2. 增加得分并更新文本
在 main.py 中 GameView 类的 on_update() 方法里,我们已经实现了碰撞检测。
此时同时增加得分并更新文本。如果玩家一次触碰到多个硬币,得分会相应增加。
# main.py (excerpt)
def on_update(self, delta_time):
self.players.update(delta_time)
self.coins.update(delta_time)
# Player x coin list
hit_coins = arcade.check_for_collision_with_list(
self.player,
self.coins
)
for coin in hit_coins:
coin.remove_from_sprite_lists()
# Add to score
self.score += 1
# Update the text of the text object
self.score_text.text = "SCORE: {}".format(self.score)
3. 绘制文本
最后,在 main.py 中 GameView 类的 on_draw() 方法里绘制文本对象。
(这一步常常被意外忘记 😅)
# main.py (excerpt)
def on_draw(self):
self.clear() # Clear
self.backgrounds.draw()
self.players.draw()
self.coins.draw()
self.score_text.draw() # Draw the text object
完整代码
下面是截至目前已实现全部功能的完整代码。
你可以直接复制并运行。
sprite.py (完整代码)
import arcade
import math
class BaseSprite(arcade.Sprite):
def __init__(self, filename, x, y):
super().__init__(filename)
# Position
self.center_x = x
self.center_y = y
# Velocity
self.vx = 0
self.vy = 0
# Animation
self.anim_counter = 0
self.anim_interval = 4
self.anim_index = 0
self.anim = []
def update(self, delta_time):
""" Update """
self.center_x += self.vx * delta_time
self.center_y += self.vy * delta_time
# Animation
self.update_animation()
def move(self, spd, deg):
""" Move Sprite """
rad = deg * math.pi / 180
self.vx = spd * math.cos(rad)
self.vy = spd * math.sin(rad)
def stop(self):
""" Stop Sprite """
self.vx = 0
self.vy = 0
def update_animation(self):
""" Animation """
if not self.anim:
return
self.anim_counter += 1
if self.anim_counter < self.anim_interval:
return
self.anim_counter = 0
self.anim_index += 1
if len(self.anim) <= self.anim_index:
self.anim_index = 0
self.texture = self.anim[self.anim_index]
class Player(BaseSprite):
def __init__(self, filename, x, y):
super().__init__(filename, x, y)
self.anim.append(arcade.load_texture("images/ninja/front_01.png"))
self.anim.append(arcade.load_texture("images/ninja/front_02.png"))
self.anim.append(arcade.load_texture("images/ninja/front_03.png"))
self.anim.append(arcade.load_texture("images/ninja/front_04.png"))
self.anim.append(arcade.load_texture("images/ninja/front_05.png"))
class Coin(BaseSprite):
def __init__(self, filename, x, y):
super().__init__(filename, x, y)
self.anim.append(ar
cade.load_texture("images/coin/coin_01.png"))
self.anim.append(arcade.load_texture("images/coin/coin_02.png"))
self.anim.append(arcade.load_texture("images/coin/coin_03.png"))
self.anim.append(arcade.load_texture("images/coin/coin_04.png"))
self.anim.append(arcade.load_texture("images/coin/coin_05.png"))
main.py (complete code)
import arcade
import sprite
import random
class GameView(arcade.View):
def __init__(self, window):
super().__init__()
self.window = window
self.w = self.window.width
self.h = self.window.height
# Background color
self.background_color = arcade.color.PAYNE_GREY
# Background sprites
self.backgrounds = arcade.SpriteList()
bkg = arcade.Sprite("images/bg_temple.png")
bkg.center_x = self.w / 2
bkg.center_y = self.h / 2
self.backgrounds.append(bkg)
# Player sprite
self.players = arcade.SpriteList()
self.player = sprite.Player(
"images/ninja/front_01.png",
x=self.w / 2,
y=self.h / 2
)
self.players.append(self.player)
# Coin sprites
self.coins = arcade.SpriteList()
for i in range(10):
x = random.random() * self.w
y = random.random() * self.h
coin = sprite.Coin(
"images/coin/coin_01.png",
x,
y
)
self.coins.append(coin)
# Score
self.score = 0
# Text object
self.score_text = arcade.Text(
"SCORE: {}".format(self.score),
self.w / 2,
self.h - 20,
arcade.color.BLACK,
16,
anchor_x="center",
anchor_y="top"
)
def on_show(self):
arcade.set_background_color(self.background_color)
def on_draw(self):
self.clear()
self.backgrounds.draw()
self.players.draw()
self.coins.draw()
self.score_text.draw()
def on_update(self, delta_time):
self.players.update(delta_time)
self.coins.update(delta_time)
# Player x coin list
hit_coins = arcade.check_for_collision_with_list(
self.player,
self.coins
)
for coin in hit_coins:
coin.remove_from_sprite_lists()
# Add to score
self.score += 1
# Update the text of the text object
self.score_text.text = "SCORE: {}".format(self.score)
def on_key_press(self, key, modifiers):
# Example movement controls (optional)
if key == arcade.key.LEFT:
self.player.move(200, 180)
elif key == arcade.key.RIGHT:
self.player.move(200, 0)
elif key == arcade.key.UP:
self.player.move(200, 90)
elif key == arcade.key.DOWN:
self.player.move(200, 270)
def on_key_release(self, key, modifiers):
if key in (arcade.key.LEFT, arcade.key.RIGHT,
arcade.key.UP, arcade.key.DOWN):
self.player.stop()
def main():
window = arcade.Window(800, 600, "Coin Collector")
game_view = GameView(window)
window.show_view(game_view)
arcade.run()
if __name__ == "__main__":
main()
全屏控制
- 进入全屏模式
- 退出全屏模式
结果预览
下次…
感谢阅读!
下一个标题将是 “Playing Sound Effects”。
敬请期待 👍

