在这篇博客是一个小彩蛋,我们将探索如何使用Python和OpenCV库来制作一个简单的塔防小游戏(虽然OpenCV的设计初衷并不是用来做游戏的)。我们将详细介绍游戏的玩法以及其功能模块的划分。
这个游戏是一个垂直滚动的射击游戏。玩家培养一个位于屏幕底部的可以向上射击的角色,目的是击败出现在屏幕上部的各种怪物。游戏随着时间的推移,难度会逐渐增加。
游戏的代码被划分为几个主要的模块,每个模块都承担着特定的功能。
main.py
)game_objects.py
)game_logic.py
)events_and_rewards.py
)game_ui.py
)main.py
)这段代码展示了如何将不同的游戏组件整合到一个主循环中,以创建一个基本的2D射击游戏。它使用了 OpenCV 来处理图形显示和键盘输入,展示了如何在 Python 中使用 OpenCV 创建简单的游戏。通过这个示例,可以看到如何结合面向对象的设计来构建游戏逻辑,以及如何处理游戏中的不同事件和状态。这个游戏可以进一步扩展,例如增加更多的怪物类型、升级选项、关卡设计等,以提升游戏体验和复杂度。
import cv2
import numpy as np
import random
import time
from game_objects import Player, Monster
from game_ui import GameUI
from game_logic import GameLogic
from events_and_rewards import EventsAndRewards
# 设置窗口大小
window_width, window_height = 400, 800
# 初始化游戏窗口
window = np.zeros((window_height, window_width, 3), dtype=np.uint8)
# 创建游戏实体
player = Player(185, 750, 30) # 位置
# 创建游戏逻辑、界面和事件管理器 初始时没有怪物 没有子弹
game_logic = GameLogic(player, [], [], window_width, window_height)
game_ui = GameUI((window_width, window_height))
events_and_rewards = EventsAndRewards(game_logic)
# 用于控制怪物生成和子弹射击的时间
last_monster_spawn_time = time.time()
last_bullet_shoot_time = time.time()
monster_spawn_interval = 1.0 # 每秒生成一个怪物
bullet_shoot_interval = 1.0 # 每秒射击一次
game_logic.paused = False # 游戏状态
# 主游戏循环
while True:
game_logic.check_for_upgrade(player, window, game_ui)
if not game_logic.paused:
current_time = time.time()
# 更新怪物生成间隔
monster_spawn_interval = game_logic.get_monster_spawn_interval()
bullet_shoot_interval = game_logic.bullet_shoot_interval()
# 每秒生成一个怪物
if current_time - last_monster_spawn_time > monster_spawn_interval:
for i in range(max(1, int(player.level / 3))):
size = random.randint(10, 50) # 假设怪物宽度
x = random.randint(0, window_width - size) # 假设怪物宽度为30
game_logic.monsters.append(Monster(x, 0, "random_type", size)) # 使用随机类型或其他逻辑
last_monster_spawn_time = current_time
# 每秒射击一次
if current_time - last_bullet_shoot_time > bullet_shoot_interval:
player.shoot(game_logic.bullets, game_logic.monsters)
last_bullet_shoot_time = current_time
# 清空窗口
game_ui.clear_window(window, player)
# 绘制玩家
player.draw(window)
# 更新和绘制子弹
for bullet in game_logic.bullets:
bullet.move(monsters=game_logic.monsters)
bullet.draw(window)
# 更新和绘制怪物
for monster in game_logic.monsters:
monster.move(0, 1, player.y - 10) # 假设怪物每次下移1像素
monster.draw(window)
# 处理游戏逻辑
game_logic.update()
if len(game_logic.monsters) > 50:
# 处理事件和奖励
event = events_and_rewards.trigger_random_event()
if event:
# 如果有事件被触发,处理该事件
events_and_rewards.apply_event_effects(event)
# 显示窗口内容
cv2.imshow("Game Window", window)
# 检查是否按下了退出键(例如Esc键)
key = cv2.waitKey(1)
if key == 27: # Esc键的ASCII码
break
# 游戏结束,关闭窗口
cv2.destroyAllWindows()
这段代码集成了下面定义的 Player
, Monster
, GameUI
, GameLogic
, 和 EventsAndRewards
类,以创建一个完整的游戏环境。以下是对代码的主要部分的解析:
GameLogic
的 update
方法来处理子弹移动、子弹和怪物的碰撞以及怪物和墙的碰撞。game_objects.py
)这段代码提供了一个2D游戏的基本结构,包含玩家、怪物和子弹的互动。玩家可以升级子弹,而怪物具有不同的形状和行为。代码还包括子弹的追踪和穿透功能,为游戏增加了更多策略性。通过对这些类和方法的进一步扩展和定制,可以创建更复杂和有趣的游戏体验。
import cv2
import numpy as np
class GameObject:
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self, window):
pass # To be implemented in subclasses
def move(self, dx, dy):
self.x += dx
self.y += dy
class Player:
def __init__(self, x, y, size=30):
self.x = x
self.y = y
self.size = size
self.health = 3000 # 初始生命值
self.score = 0 # 初始分数
self.money = 0 # 初始金钱
self.experience = 0 # 初始经验值
self.level = 1 # 初始等级
self.color = (255, 255, 255) # 玩家颜色
self.bullet_speed = 5 # 子弹速度
self.bullet_size = 5 # 子弹大小
self.shoot_interval = 1.0 # 发射间隔,初始为1秒
self.tracking_bullet = False # 是否发射跟踪弹
self.piercing_bullet = 0 # 是否发射贯穿弹 穿透怪物次数
self.shoot_interval_multiplier = 1 # 发射间隔,升级成本乘数
self.piercing_bullet_multiplier = 1 # 贯穿弹,升级成本乘数
def draw(self, window):
top_left = (self.x, self.y)
bottom_right = (self.x + self.size, self.y + self.size)
cv2.rectangle(window, top_left, bottom_right, self.color, -1, lineType=cv2.LINE_AA)
wall_height = 5 # 假设墙的高度为5像素
wall_color = (127, 127, 127) # 灰色
cv2.rectangle(window, (0, self.y - 10), (window.shape[1], self.y - 10 + wall_height), wall_color, -1)
def shoot(self, bullets, monsters):
# 发射子弹
bullet_x = self.x + self.size // 2
bullet_y = self.y
bullet_direction = (0, -1) # 向上射击
bullets.append(Bullet(bullet_x, bullet_y, bullet_direction,
self.bullet_speed, self.bullet_size,
monsters, self.tracking_bullet, self.piercing_bullet))
def upgrade_bullet(self, upgrade_type):
# 实现升级子弹的逻辑
if upgrade_type == "Increase Bullet":
self.shoot_interval = max(0.1, self.shoot_interval - 0.1) # 减少发射间隔,最小为0.1秒
self.money = self.money - 10 * self.shoot_interval_multiplier # 扣减金币
self.shoot_interval_multiplier = self.shoot_interval_multiplier + 0.5
elif upgrade_type == "Piercing Bullet":
self.piercing_bullet = self.piercing_bullet + 1 # 穿透怪物次数+1
self.money = self.money - 20 * self.piercing_bullet_multiplier # 扣减金币
self.piercing_bullet_multiplier = self.piercing_bullet_multiplier + 0.5
elif upgrade_type == "Tracking Bullet":
self.tracking_bullet = True # 启用跟踪弹
self.money = self.money - 500 # 扣减金币
def upgrade(self):
self.experience = 0 # 重置经验值
self.level += 1 # 提升等级
class Monster(GameObject):
def __init__(self, x, y, monster_type, size=30):
super().__init__(x, y)
self.monster_type = monster_type
self.size = size # 添加怪物的尺寸属性
def draw(self, window):
if self.monster_type == "random_type":
self.monster_type = np.random.choice(["triangle", "ellipse", "diamond"])
half_size = self.size // 2
if self.monster_type == "triangle":
points = np.array([
[self.x, self.y - half_size],
[self.x + half_size, self.y + half_size],
[self.x - half_size, self.y + half_size]
], np.int32)
cv2.polylines(window, [points], True, (0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
elif self.monster_type == "ellipse":
cv2.ellipse(window, (self.x, self.y), (half_size, half_size // 2), 0, 0, 360, (0, 0, 255), -1,
lineType=cv2.LINE_AA)
elif self.monster_type == "diamond":
points = np.array([
[self.x, self.y - half_size],
[self.x + half_size, self.y],
[self.x, self.y + half_size],
[self.x - half_size, self.y]
], np.int32)
cv2.polylines(window, [points], True, (255, 255, 0), thickness=2, lineType=cv2.LINE_AA)
def move(self, dx, dy, wall_y=0):
if self.y + dy + self.size <= wall_y: # 确保怪物不会移动到墙的位置
super().move(dx, dy)
class Bullet(GameObject):
def __init__(self, x, y, direction, speed, size, monsters, tracking=False, piercing_count=0):
super().__init__(x, y)
self.direction = direction # 射击方向
self.speed = speed
self.size = size
self.tracking = tracking # 是否追踪敌人
self.piercing_count = piercing_count # 穿透怪物次数
# 初始化时确定方向
if monsters:
self.direction = self.calculate_direction_to_closest_monster(monsters)
else:
self.direction = (0, -1) # 如果没有怪物,默认向上
def draw(self, window):
cv2.circle(window, (int(self.x), int(self.y)), self.size, (255, 255, 0), -1, lineType=cv2.LINE_AA)
def move(self, dx=0, dy=0, monsters=None):
if self.tracking and monsters:
self.update_direction_to_closest_monster(monsters)
else:
# 如果不追踪或没有怪物,继续向原始方向移动
dx, dy = self.direction[0] * self.speed, self.direction[1] * self.speed
super().move(dx, dy)
def update_direction_to_closest_monster(self, monsters):
if monsters:
closest_monster = min(monsters,
key=lambda monster: np.linalg.norm([monster.x - self.x, monster.y - self.y]))
dx, dy = closest_monster.x - self.x, closest_monster.y - self.y
distance = np.sqrt(dx ** 2 + dy ** 2)
self.direction = (dx / distance, dy / distance)
def calculate_direction_to_closest_monster(self, monsters):
shoot_offset = 15 # 射击提前量
closest_monster = min(monsters, key=lambda monster: np.linalg.norm([monster.x - self.x, monster.y - self.y]))
dx, dy = closest_monster.x - self.x, closest_monster.y - self.y + shoot_offset
distance = np.sqrt(dx ** 2 + dy ** 2)
return dx / distance, dy / distance
GameObject
类x
, y
)。draw(window)
: 在游戏窗口中绘制对象,具体实现在子类中。move(dx, dy)
: 移动对象,改变其坐标。Player
类x
, y
), 大小 (size
), 生命值 (health
), 分数 (score
), 金钱 (money
), 经验 (experience
), 等级 (level
), 玩家颜色 (color
),以及与子弹相关的属性。draw(window)
: 在游戏窗口中绘制玩家。shoot(bullets, monsters)
: 发射子弹。upgrade_bullet(upgrade_type)
: 升级子弹的类型。upgrade()
: 提升玩家等级。Monster
类GameObject
的基本属性外,还包括怪物类型 (monster_type
) 和大小 (size
)。draw(window)
: 根据怪物类型在游戏窗口中绘制不同的图形。move(dx, dy, wall_y)
: 移动怪物,但不允许它们穿过墙。Bullet
类direction
), 速度 (speed
), 大小 (size
), 是否追踪 (tracking
), 穿透次数 (piercing_count
)。draw(window)
: 在游戏窗口中绘制子弹。move(dx, dy, monsters)
: 根据是否追踪怪物来更新子弹的方向和位置。update_direction_to_closest_monster(monsters)
: 更新子弹的方向,使其指向最近的怪物。calculate_direction_to_closest_monster(monsters)
: 计算并返回指向最近怪物的方向。game_logic.py
)GameLogic
类是游戏的核心,负责管理游戏中的动态元素,如玩家、子弹和怪物的行为。它通过更新游戏状态、处理碰撞和升级逻辑来保持游戏的连贯性。这个类可以被进一步扩展,以增加更多的游戏特性和复杂性。通过这种方式,可以创建一个更丰富、更引人入胜的游戏体验。
import cv2
import numpy as np
class GameLogic:
def __init__(self, player, monsters, bullets, window_width, window_height):
self.paused = False
self.player = player
self.monsters = monsters
self.bullets = bullets
self.window_width = window_width
self.window_height = window_height
def update(self):
self.move_bullets()
self.check_bullet_monster_collision()
self.check_monster_wall_collision()
self.remove_offscreen_objects()
def move_bullets(self):
for bullet in self.bullets:
bullet.move()
def check_bullet_monster_collision(self):
for bullet in self.bullets[:]:
for monster in self.monsters[:]:
if self.is_collision(bullet, monster):
self.player.score += int(monster.size / 10) # 加分
self.player.money += np.random.randint(1, max(2, int(monster.size / 10))) # 加金币
self.player.experience += int(monster.size / self.player.level) # 加经验
self.monsters.remove(monster)
if bullet.piercing_count <= 0:
self.bullets.remove(bullet)
else:
bullet.piercing_count = bullet.piercing_count - 1
break # 退出内层循环,避免迭代已移除的子弹
def check_monster_wall_collision(self):
for monster in self.monsters[:]:
if monster.y + monster.size >= (self.player.y-10): # 假设怪物底部触碰到墙
self.player.health -= 1 # 每个怪物每秒减少1点生命值
def remove_offscreen_objects(self):
self.bullets = [bullet for bullet in self.bullets if self.is_on_screen(bullet)]
self.monsters = [monster for monster in self.monsters if self.is_on_screen(monster)]
def is_collision(self, obj1, obj2):
collision_size = obj2.size if obj2.size > 20 else 20
# 简单的碰撞检测逻辑
if (obj1.x < obj2.x + collision_size and
obj1.x + collision_size > obj2.x and
obj1.y < obj2.y + obj2.size and
obj1.y + collision_size > obj2.y):
return True
return False
def is_on_screen(self, obj):
# 检查物体是否还在屏幕中
return 0 <= obj.x <= self.window_width and 0 <= obj.y <= self.window_height
def get_monster_spawn_interval(self):
# 随着等级增加,生成怪物的间隔时间减少
# 例如,每提升一级,时间减少3%
interval_decrease = 0.03 * self.player.level
base_interval = 1.0
new_interval = max(0.1, base_interval - interval_decrease) # 设置一个最小间隔时间
return new_interval
def bullet_shoot_interval(self):
return self.player.shoot_interval
def check_for_upgrade(self, player, window, game_ui):
if player.experience >= 100 and player.level < 20:
player.upgrade()
if player.level >= 20:
# 清空窗口
window.fill(0)
cv2.putText(window, "Victory!!!", (130, 400), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 127, 255), 2, lineType=cv2.LINE_AA)
# 更新窗口
cv2.imshow("Game Window", window)
# 等待按下Esc键
while True:
if cv2.waitKey(1) == 27: # Esc键的ASCII码
break
else:
# 绘制UI
game_ui.clear_window(window, player)
while player.money > 0:
self.paused = True
selected_option = game_ui.show_upgrade_menu(window, player)
# 根据选择的升级选项进行升级
player.upgrade_bullet(selected_option)
# 绘制UI
game_ui.clear_window(window, player)
if selected_option == "Cancel":
break
self.paused = False
该类的主要作用是控制游戏的流程,包括移动子弹、检测碰撞、移除屏幕外的对象、生成怪物和处理玩家升级。以下是对关键功能的解析:
GameLogic
类__init__
): 初始化游戏逻辑,包括玩家、怪物、子弹以及窗口的宽度和高度。paused
: 游戏是否暂停。player
: 玩家对象。monsters
: 怪物列表。bullets
: 子弹列表。window_width
, window_height
: 游戏窗口的尺寸。update
: 更新游戏逻辑,包括移动子弹、检查子弹与怪物的碰撞、检查怪物与墙的碰撞和移除屏幕外的对象。move_bullets
: 移动游戏中的所有子弹。check_bullet_monster_collision
: 检测子弹与怪物之间的碰撞。如果发生碰撞,增加玩家的得分、金币和经验值。若子弹具有穿透能力,减少其穿透次数。check_monster_wall_collision
: 检测怪物是否碰撞到墙。如果碰撞到墙,减少玩家的生命值。remove_offscreen_objects
: 移除屏幕外的子弹和怪物。is_collision
: 简单的碰撞检测逻辑,用于判断两个对象是否碰撞。is_on_screen
: 检查对象是否还在屏幕内。get_monster_spawn_interval
: 计算怪物生成的间隔时间,随玩家等级提高而减少。bullet_shoot_interval
: 返回玩家当前的子弹发射间隔。check_for_upgrade
: 检查玩家是否满足升级条件。如果满足,允许玩家进行升级,并处理游戏胜利逻辑。events_and_rewards.py
)EventsAndRewards
类通过随机事件为游戏增加了不可预测性和多样性。这些事件不仅增加了游戏的趣味性,还为玩家提供了额外的挑战和奖励。这种机制可以进一步扩展,比如增加更多的事件类型、调整奖励规则或修改触发概率,以创造更丰富的游戏体验。通过与 GameLogic
类的交互,EventsAndRewards
类能够有效地融入游戏的总体框架中。
import random
class EventsAndRewards:
def __init__(self, game_logic):
self.events = ["bonus_coin", "sudden_attack", "treasure_chest"]
self.rewards = {"bonus_coin": 10, "sudden_attack": 5, "treasure_chest": 50, }
self.trigger_probability = 0.1 # 设置触发概率为10%
self.game_logic = game_logic
def trigger_random_event(self):
# 使用随机数决定是否触发事件
if random.random() < self.trigger_probability:
# 如果触发,随机选择一个事件
event = random.choice(self.events)
return event
else:
# 如果不触发,返回None或其他标记
return None
def apply_event_effects(self, event):
if event == "bonus_coin":
self.handle_bonus_coin()
elif event == "sudden_attack":
self.handle_sudden_attack()
elif event == "treasure_chest":
self.handle_treasure_chest()
def handle_bonus_coin(self):
self.game_logic.player.money = self.game_logic.player.money + random.randint(2, self.rewards["bonus_coin"])
def handle_sudden_attack(self):
shoot_count = random.randint(1, self.rewards["sudden_attack"])
for i in range(shoot_count):
self.game_logic.player.shoot(self.game_logic.bullets, self.game_logic.monsters)
def handle_treasure_chest(self):
self.game_logic.player.money = self.game_logic.player.money + random.randint(2, self.rewards["treasure_chest"])
这段代码负责在游戏中触发和处理各种随机事件以及给予相应的奖励。以下是对其主要组成部分的解析:
EventsAndRewards
类__init__
): 初始化事件处理器,设置事件类型、对应奖励和触发概率。events
: 可能发生的事件列表(如 “bonus_coin”, “sudden_attack”, “treasure_chest”)。rewards
: 每个事件对应的奖励。trigger_probability
: 事件触发的概率。game_logic
: 游戏逻辑对象,用于访问和修改游戏状态。trigger_random_event
: 根据设定的概率随机决定是否触发一个事件。如果触发,随机选择并返回一个事件;否则返回 None
。apply_event_effects
: 根据触发的事件类型,调用相应的处理方法。handle_bonus_coin
: 处理“bonus_coin”事件,随机增加玩家的金币数量。handle_sudden_attack
: 处理“sudden_attack”事件,使玩家自动发射一定数量的子弹。handle_treasure_chest
: 处理“treasure_chest”事件,随机增加玩家的金币数量。game_ui.py
)GameUI
类负责游戏中所有与用户界面相关的视觉元素的显示,包括分数、生命值、金钱等。它还处理玩家与升级菜单的交互,允许玩家使用金币来选择不同的升级选项。这个类将游戏逻辑与用户界面清晰地分离开来,使得游戏的代码更加模块化和易于管理。通过这种方式,可以更容易地进行UI的更新和改进,以提供更好的玩家体验。
import cv2
import numpy as np
class GameUI:
def __init__(self, window_size):
self.window_size = window_size
self.font = cv2.FONT_HERSHEY_SIMPLEX
self.font_scale = 0.5
self.font_color = (255, 255, 255)
self.line_type = 1
def draw_score(self, window, score):
cv2.putText(window, f'Score: {score}', (10, 20), self.font, self.font_scale, self.font_color, self.line_type,
lineType=cv2.LINE_AA)
def draw_health(self, window, health):
cv2.putText(window, f'Health: {health}', (10, 40), self.font, self.font_scale, self.font_color, self.line_type,
lineType=cv2.LINE_AA)
def draw_money(self, window, money):
cv2.putText(window, f'Coins: {int(money)}', (10, 60), self.font, self.font_scale, self.font_color, self.line_type,
lineType=cv2.LINE_AA)
def draw_experience(self, window, experience):
cv2.putText(window, f'EXP: {experience}', (10, 80), self.font, self.font_scale, self.font_color, self.line_type,
lineType=cv2.LINE_AA)
def draw_level(self, window, level):
cv2.putText(window, f'Level: {level}', (10, 100), self.font, self.font_scale, self.font_color, self.line_type,
lineType=cv2.LINE_AA)
def draw_upgrade_menu(self, window, upgrades):
start_y = 100
cv2.putText(window, 'Upgrades:', (10, start_y), self.font, self.font_scale, self.font_color, self.line_type,
lineType=cv2.LINE_AA)
for i, upgrade in enumerate(upgrades):
text = f"{i + 1}. {upgrade['name']} - Cost: {upgrade['cost']}"
cv2.putText(window, text, (10, start_y + 20 * (i + 1)), self.font, self.font_scale, self.font_color,
self.line_type, lineType=cv2.LINE_AA)
def clear_window(self, window, player):
# 清空窗口
window.fill(0)
# 绘制UI
self.draw_score(window, player.score)
self.draw_health(window, player.health)
self.draw_money(window, player.money)
self.draw_experience(window, player.experience)
self.draw_level(window, player.level)
def show_upgrade_menu(self, window, player):
upgrade_options = ["Increase Bullet", "Piercing Bullet", "Tracking Bullet"]
upgrade_costs = [int(10 * player.shoot_interval_multiplier), int(20 * player.piercing_bullet_multiplier),
500] # 升级成本
return_option = None
text_cord = 200
def mouse_callback(event, x, y, flags, param):
nonlocal return_option
if event == cv2.EVENT_LBUTTONDOWN:
for i, option in enumerate(upgrade_options):
if text_cord - 10 + (i - 1) * 30 <= y <= text_cord + 10 + i * 30: # 按钮位置
if player.money >= upgrade_costs[i]:
if option == "Tracking Bullet" and player.tracking_bullet is not True:
return_option = option
elif option != "Tracking Bullet":
return_option = option
else:
cv2.putText(window, "Not Enough Coins", (50, text_cord + (len(upgrade_options) + 2) * 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.6,
(0, 0, 255), 2, lineType=cv2.LINE_AA)
break
# Cancel位置
if text_cord - 10 + len(upgrade_options) * 30 <= y <= text_cord + 10 + (
len(upgrade_options) + 1) * 30:
return_option = "Cancel"
return return_option
cv2.setMouseCallback("Game Window", mouse_callback)
while return_option is None:
cv2.rectangle(window, (10, text_cord - 40),
(window.shape[1] - 10, text_cord + (len(upgrade_options) + 1) * 30),
(200, 127, 0), -1, lineType=cv2.LINE_AA)
# 显示升级选项
for i, option in enumerate(upgrade_options):
if player.tracking_bullet is True and option == "Tracking Bullet":
continue
text = f"{option} - Coins: {upgrade_costs[i]}"
cv2.putText(window, text, (50, text_cord + i * 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2,
lineType=cv2.LINE_AA)
cv2.putText(window, "Cancel", (50, text_cord + len(upgrade_options) * 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
(255, 255, 255), 2, lineType=cv2.LINE_AA)
cv2.imshow("Game Window", window)
cv2.waitKey(1)
cv2.setMouseCallback("Game Window", lambda *args: None) # 移除鼠标事件
return return_option
这段代码定义了一个名为 GameUI
的类,用于在游戏中显示用户界面(UI),包括得分、生命值、金币、经验值和等级,以及处理升级菜单的显示和交互。以下是对其主要组成部分的解析:
GameUI
类__init__
): 初始化UI的一些基础设置,如窗口大小、字体、字体大小、字体颜色和线型。window_size
: 游戏窗口的大小。font
, font_scale
, font_color
, line_type
: 用于绘制文本的字体设置。draw_score
: 在窗口上绘制玩家的得分。draw_health
: 在窗口上绘制玩家的生命值。draw_money
: 在窗口上绘制玩家的金币数量。draw_experience
: 在窗口上绘制玩家的经验值。draw_level
: 在窗口上绘制玩家的等级。draw_upgrade_menu
: 绘制升级选项的菜单。clear_window
: 清空窗口并重新绘制所有UI元素。show_upgrade_menu
: 显示升级菜单,并处理玩家的升级选择。这个方法包含了一个鼠标回调函数 mouse_callback
,用于处理玩家点击升级选项时的交互。这个游戏在玩法流程上还不够完善,但是也展示了如何使用Python和OpenCV来创建有趣的游戏体验。通过将游戏代码模块化,我们不仅使代码更加清晰易懂,也方便了未来的维护和扩展。这个项目是理解游戏开发基础和练习编程技能的绝佳途径。