制作一个这样的pygame小游戏,依赖pygame,制作了一些背景图片,为了保证显示效果,还专门下载了一款阿里巴巴普惠体字体,需要把这些图片都打包放入DEB并在安装后能够正确访问。
上图是准备的图片和字体文件,当然还有sh脚本文件和py程序文件。
sh脚本中特意把图片文件和字体文件复制进去了
#!/bin/bash
# 定义包名
PACKAGE_NAME="wuwangfazhou-calculation-game"
# 创建目录结构
mkdir -p $PACKAGE_NAME/DEBIAN
mkdir -p $PACKAGE_NAME/usr/local/bin
mkdir -p $PACKAGE_NAME/usr/local/share/icons/wuwangfazhou-calculation-game
mkdir -p $PACKAGE_NAME/usr/local/share/images/wuwangfazhou-calculation-game
mkdir -p $PACKAGE_NAME/usr/share/applications
mkdir -p $PACKAGE_NAME/usr/local/share/fonts/wuwangfazhou-calculation-game
# 复制脚本到目录
cp wuwangfazhou-calculation-game.py $PACKAGE_NAME/usr/local/bin/
chmod +x $PACKAGE_NAME/usr/local/bin/wuwangfazhou-calculation-game.py
# 复制图标文件 ######
cp app_icon.png $PACKAGE_NAME/usr/local/share/icons/wuwangfazhou-calculation-game/
# 复制游戏中使用的所有图片文件 #######
echo "正在复制游戏图片文件..."
cp *.png $PACKAGE_NAME/usr/local/share/images/wuwangfazhou-calculation-game/ 2>/dev/null || :
cp *.jpg $PACKAGE_NAME/usr/local/share/images/wuwangfazhou-calculation-game/ 2>/dev/null || :
cp *.jpeg $PACKAGE_NAME/usr/local/share/images/wuwangfazhou-calculation-game/ 2>/dev/null || :
# 复制字体文件
cp AlibabaPuHuiTi-3-65-Medium.ttf $PACKAGE_NAME/usr/local/share/fonts/wuwangfazhou-calculation-game/
# 创建桌面入口文件
cat << EOF > wuwangfazhou-calculation-game.desktop
[Desktop Entry]
Name=武王伐纣算术游戏
Exec=/usr/bin/env python3 /usr/local/bin/wuwangfazhou-calculation-game.py
Icon=/usr/local/share/icons/wuwangfazhou-calculation-game/app_icon.png
Terminal=false
Type=Application
Categories=Utility;
EOF
# 检查桌面入口文件是否存在
if [ -f "wuwangfazhou-calculation-game.desktop" ]; then
# 复制桌面入口文件
cp wuwangfazhou-calculation-game.desktop $PACKAGE_NAME/usr/share/applications/
chmod +x $PACKAGE_NAME/usr/share/applications/wuwangfazhou-calculation-game.desktop
else
echo "错误: wuwangfazhou-calculation-game.desktop文件不存在,请先创建该文件。"
exit 1
fi
# 创建控制文件
cat << EOF > $PACKAGE_NAME/DEBIAN/control
Package: $PACKAGE_NAME
Version: 1.3
Section: utils
Priority: optional
Architecture: all
Depends: python3, python3-pygame
Maintainer: YU Taiyuan <382614949@qq.com>
Description: 武王伐纣算术游戏
This is a game for calculation.
EOF
# 检查控制文件是否存在
if [ ! -f "$PACKAGE_NAME/DEBIAN/control" ]; then
echo "错误: 控制文件 $PACKAGE_NAME/DEBIAN/control 未创建成功。"
exit 1
fi
# 构建DEB包
fakeroot dpkg-deb --build $PACKAGE_NAME
# 检查DEB包是否生成
DEB_FILE="$PACKAGE_NAME.deb"
if [ ! -f "$DEB_FILE" ]; then
echo "错误: DEB包 $DEB_FILE 未生成成功。"
exit 1
fi
echo "DEB包已生成: $DEB_FILE"
# 修改文件权限
chmod 777 "$DEB_FILE"
chmod 777 "wuwangfazhou-calculation-game.desktop"
# 设置图片、字体和图标文件权限
chmod 644 $PACKAGE_NAME/usr/local/share/icons/wuwangfazhou-calculation-game/*
chmod 644 $PACKAGE_NAME/usr/local/share/images/wuwangfazhou-calculation-game/*
chmod 644 $PACKAGE_NAME/usr/local/share/fonts/wuwangfazhou-calculation-game/*
安装后的字体文件和图片文件保存路径:
确实有。
了解了这个路径,我们在py程序文件中也要进行相应的修改,以保证安装后程序能够正常使用
import pygame
import random
import os
# 初始化pygame
pygame.init()
# 设置窗口大小
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('武王伐纣算术过关游戏:消灭纣王全靠你了!!')
# 加载字体
font_path = '/usr/local/share/fonts/wuwangfazhou-calculation-game/AlibabaPuHuiTi-3-65-Medium.ttf' #安装后的字体文件路径
if not os.path.exists(font_path):
raise FileNotFoundError(f"字体文件 {font_path} 未找到,请确保字体文件存在。")
font_normal = pygame.font.Font(font_path, 36)
font_input = pygame.font.Font(font_path, 90)
# 加载各关卡背景图片,假设图片都在当前目录下
image_path = '/usr/local/share/images/wuwangfazhou-calculation-game/' #安装后的图片文件路径
image_files = {
'西岐城': image_path +'1、西岐城.png',
'金鸡岭': image_path +'2、金鸡岭.png',
'汜水关': image_path +'3、汜水关.png',
'界牌关': image_path +'4、界牌关.png',
'穿云关': image_path +'5、穿云关.png',
'潼关': image_path +'6、潼关.png',
'临潼关': image_path +'7、临潼关.png',
'朝歌城': image_path +'8、朝歌城.png'
}
background_images = {}
for name, file in image_files.items():
try:
background_images[name] = pygame.image.load(file)
background_images[name] = pygame.transform.scale(background_images[name], (screen_width, screen_height))
except pygame.error as e:
print(f"加载图片 {file} 时出错: {e}")
# 关卡信息,包含关卡名称和对应的运算类型
levels = [
('西岐城', '一位数加法'),
('金鸡岭', '二位数加法'),
('汜水关', '一位数减法'),
('界牌关', '二位数减法'),
('穿云关', '一位数乘法'),
('潼关', '二位数乘法'),
('临潼关', '一位数除法'),
('朝歌城', '二位数除法')
]
current_level = 0
# 初始化游戏数据
correct_count = 0
wrong_count = 0
score = 0
def generate_question(operation_type):
if '加法' in operation_type:
num_digits = 1 if '一位数' in operation_type else 2
num1 = random.randint(1, 9) if num_digits == 1 else random.randint(10, 50)
num2 = random.randint(1, 9) if num_digits == 1 else random.randint(10, 50)
question = f"{num1} + {num2}"
answer = num1 + num2
elif '减法' in operation_type:
num_digits = 1 if '一位数' in operation_type else 2
num1 = random.randint(1, 9) if num_digits == 1 else random.randint(10, 50)
num2 = random.randint(1, min(9, num1)) if num_digits == 1 else random.randint(10, num1)
question = f"{num1} - {num2}"
answer = num1 - num2
elif '乘法' in operation_type:
num_digits = 1 if '一位数' in operation_type else 2
num1 = random.randint(1, 9) #if num_digits == 1 else random.randint(10, 99)
num2 = random.randint(1, 9) if num_digits == 1 else random.randint(10, 20)
question = f"{num1} × {num2}"
answer = num1 * num2
elif '除法' in operation_type:
num_digits = 1 if '一位数' in operation_type else 2
num2 = random.randint(1, 5) if num_digits == 1 else random.randint(5, 10)
num1 = num2 * random.randint(1, 9) if num_digits == 1 else num2 * random.randint(1, 9)
question = f"{num1} ÷ {num2}"
answer = num1 // num2
return question, answer
question, correct_answer = generate_question(levels[current_level][1])
input_text = ''
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN or event.key == pygame.K_KP_ENTER:
try:
user_answer = int(input_text)
if user_answer == correct_answer:
correct_count += 1
score += 1
result_text = "正确!"
else:
wrong_count += 1
score -= 1
result_text = f"错误!正确答案是 {correct_answer}"
# 生成新问题
question, correct_answer = generate_question(levels[current_level][1])
input_text = ''
# 检查是否进入下一关
if score >= 10:
congratulation_text = f"祝贺你,成功通过 {levels[current_level][0]} 关卡!"
congrats_surface = font_normal.render(congratulation_text, True, (255, 0, 0))
congrats_rect = congrats_surface.get_rect(center=(screen_width // 2, screen_height // 2))
try:
screen.blit(background_images[levels[current_level][0]], (0, 0)) #最后无新背景图可换了报异常
except:
screen.blit(background_images[levels[current_level - 1][0]], (0, 0))
screen.blit(congrats_surface, congrats_rect)
pygame.display.flip()
pygame.time.wait(4000)
current_level += 1
if current_level >= len(levels):
running = False
else:
correct_count = 0
wrong_count = 0
score = 0
question, correct_answer = generate_question(levels[current_level][1])
except ValueError:
result_text = "请输入有效的数字!"
elif event.key == pygame.K_BACKSPACE:
input_text = input_text[:-1]
else:
if event.unicode.isdigit():
input_text += event.unicode
try:
screen.blit(background_images[levels[current_level][0]], (0, 0))
except:
screen.blit(background_images[levels[current_level - 1][0]], (0, 0))
# 在左上角写上图片名
level_name_surface = font_normal.render(levels[current_level][0], True, (255, 0, 0))
screen.blit(level_name_surface, (10, 10))
question_surface = font_input.render(question, True, (255, 0, 0))
question_rect = question_surface.get_rect(center=(screen_width // 2, screen_height // 2 - 50))
screen.blit(question_surface, question_rect)
input_surface = font_normal.render(input_text, True, (255, 0, 0))
input_rect = input_surface.get_rect(center=(screen_width // 2, screen_height // 2 + 50))
screen.blit(input_surface, input_rect)
result_surface = font_normal.render(result_text if'result_text' in locals() else '', True, (255, 0, 0))
result_rect = result_surface.get_rect(center=(screen_width // 2, screen_height // 2 + 100))
screen.blit(result_surface, result_rect)
stats_text = f"得分: {score}, 正确: {correct_count}, 错误: {wrong_count}"
stats_surface = font_normal.render(stats_text, True, (255, 0, 0))
stats_rect = stats_surface.get_rect(center=(screen_width // 2, screen_height - 50))
screen.blit(stats_surface, stats_rect)
pygame.display.flip()
pygame.quit()
最后安装完成,大功告成。
注意:SH文件,努力做到一键操作,让您改改可以用