当前位置: 首页 > news >正文

python--产品篇--游戏-坦克

文章目录

  • 准备
  • 代码
    • main.py
    • cfg.py
  • 效果

准备

下载
在这里插入图片描述

代码

main.py

import os
import cfg
import pygame
from modules import *'''主函数'''
def main(cfg):# 游戏初始化pygame.init()pygame.mixer.init()screen = pygame.display.set_mode((cfg.WIDTH, cfg.HEIGHT))pygame.display.set_caption(cfg.TITLE)# 加载游戏素材sounds = {}for key, value in cfg.AUDIO_PATHS.items():sounds[key] = pygame.mixer.Sound(value)sounds[key].set_volume(1)# 开始界面is_dual_mode = gameStartInterface(screen, cfg)# 关卡数levelfilepaths = [os.path.join(cfg.LEVELFILEDIR, filename) for filename in sorted(os.listdir(cfg.LEVELFILEDIR))]# 主循环for idx, levelfilepath in enumerate(levelfilepaths):switchLevelIterface(screen, cfg, idx+1)game_level = GameLevel(idx+1, levelfilepath, sounds, is_dual_mode, cfg)is_win = game_level.start(screen)if not is_win: breakis_quit_game = gameEndIterface(screen, cfg, is_win)return is_quit_game'''run'''
if __name__ == '__main__':while True:is_quit_game = main(cfg)if is_quit_game:break

cfg.py

import os'''字体'''
FONTPATH = os.path.join(os.getcwd(), 'resources/font/font.ttf')
'''图片'''
BULLET_IMAGE_PATHS = {'up': os.path.join(os.getcwd(), 'resources/images/bullet/bullet_up.png'),'down': os.path.join(os.getcwd(), 'resources/images/bullet/bullet_down.png'),'left': os.path.join(os.getcwd(), 'resources/images/bullet/bullet_left.png'),'right': os.path.join(os.getcwd(), 'resources/images/bullet/bullet_right.png')}
ENEMY_TANK_IMAGE_PATHS = {'1': [os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_1_0.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_1_1.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_1_2.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_1_3.png')],'2': [os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_2_0.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_2_1.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_2_2.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_2_3.png')],'3': [os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_3_0.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_3_1.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_3_2.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_3_3.png')],'4': [os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_4_0.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_4_1.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_4_2.png'),os.path.join(os.getcwd(), 'resources/images/enemyTank/enemy_4_3.png')]}
PLAYER_TANK_IMAGE_PATHS = {'player1': [os.path.join(os.getcwd(), 'resources/images/playerTank/tank_T1_0.png'),os.path.join(os.getcwd(), 'resources/images/playerTank/tank_T1_1.png'),os.path.join(os.getcwd(), 'resources/images/playerTank/tank_T1_2.png')],'player2': [os.path.join(os.getcwd(), 'resources/images/playerTank/tank_T2_0.png'),os.path.join(os.getcwd(), 'resources/images/playerTank/tank_T2_1.png'),os.path.join(os.getcwd(), 'resources/images/playerTank/tank_T2_2.png')]}
FOOD_IMAGE_PATHS = {'boom': os.path.join(os.getcwd(), 'resources/images/food/food_boom.png'),'clock': os.path.join(os.getcwd(), 'resources/images/food/food_clock.png'),'gun': os.path.join(os.getcwd(), 'resources/images/food/food_gun.png'),'iron': os.path.join(os.getcwd(), 'resources/images/food/food_iron.png'),'protect': os.path.join(os.getcwd(), 'resources/images/food/food_protect.png'),'star': os.path.join(os.getcwd(), 'resources/images/food/food_star.png'),'tank': os.path.join(os.getcwd(), 'resources/images/food/food_tank.png')}
HOME_IMAGE_PATHS = [os.path.join(os.getcwd(), 'resources/images/home/home1.png'),os.path.join(os.getcwd(), 'resources/images/home/home_destroyed.png')]
SCENE_IMAGE_PATHS = {'brick': os.path.join(os.getcwd(), 'resources/images/scene/brick.png'),'ice': os.path.join(os.getcwd(), 'resources/images/scene/ice.png'),'iron': os.path.join(os.getcwd(), 'resources/images/scene/iron.png'),'river1': os.path.join(os.getcwd(), 'resources/images/scene/river1.png'),'river2': os.path.join(os.getcwd(), 'resources/images/scene/river2.png'),'tree': os.path.join(os.getcwd(), 'resources/images/scene/tree.png')}
OTHER_IMAGE_PATHS = {'appear': os.path.join(os.getcwd(), 'resources/images/others/appear.png'),'background': os.path.join(os.getcwd(), 'resources/images/others/background.png'),'boom_dynamic': os.path.join(os.getcwd(), 'resources/images/others/boom_dynamic.png'),'boom_static': os.path.join(os.getcwd(), 'resources/images/others/boom_static.png'),'gameover': os.path.join(os.getcwd(), 'resources/images/others/gameover.png'),'logo': os.path.join(os.getcwd(), 'resources/images/others/logo.png'),'mask': os.path.join(os.getcwd(), 'resources/images/others/mask.png'),'protect': os.path.join(os.getcwd(), 'resources/images/others/protect.png'),'tip': os.path.join(os.getcwd(), 'resources/images/others/tip.png'),'gamebar': os.path.join(os.getcwd(), 'resources/images/others/gamebar.png')}
'''声音'''
AUDIO_PATHS = {'add': os.path.join(os.getcwd(), 'resources/audios/add.wav'),'bang': os.path.join(os.getcwd(), 'resources/audios/bang.wav'),'blast': os.path.join(os.getcwd(), 'resources/audios/blast.wav'),'fire': os.path.join(os.getcwd(), 'resources/audios/fire.wav'),'Gunfire': os.path.join(os.getcwd(), 'resources/audios/Gunfire.wav'),'hit': os.path.join(os.getcwd(), 'resources/audios/hit.wav'),'start': os.path.join(os.getcwd(), 'resources/audios/start.wav')}
'''屏幕'''
WIDTH = 630
HEIGHT = 630
BORDER_LEN = 3
GRID_SIZE = 24
PANEL_WIDTH = 150
TITLE = '坦克大战'
'''关卡'''
LEVELFILEDIR = os.path.join(os.getcwd(), 'modules/levels')

效果

在这里插入图片描述

相关文章:

  • Combining Buffered I/O and Direct I/O in Distributed File Systems——论文泛读
  • 环境配置、如何安装OpenHarmony HAR
  • 一次电脑感染Synaptics Pointing Device Driver病毒的经历,分享下经验
  • Java 面试题
  • 前端 WebSocket 的一些使用
  • 【Spring底层原理高级进阶】Spring Kafka:实时数据流处理,让业务风起云涌!️
  • 实战解析:打造风控特征变量平台,赋能数据驱动决策
  • Python - getpass
  • 线上问题——学习记录幂等判断失效问题分析
  • Git快速入门
  • 回溯算法01-组合(Java)
  • 数据库分库分表中间件选择
  • 【扩散模型系列1】扩散模型背景|DDPMs|LDM
  • 【理解机器学习算法】之Nearest Shrunken Centroid(纯Python)
  • Redis面试题
  • JS 中的深拷贝与浅拷贝
  • 分享的文章《人生如棋》
  • 【刷算法】从上往下打印二叉树
  • canvas 高仿 Apple Watch 表盘
  • Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
  • js ES6 求数组的交集,并集,还有差集
  • mac修复ab及siege安装
  • Next.js之基础概念(二)
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • 前端知识点整理(待续)
  • 如何选择开源的机器学习框架?
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • 听说你叫Java(二)–Servlet请求
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • 06-01 点餐小程序前台界面搭建
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • #include<初见C语言之指针(5)>
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (8)STL算法之替换
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)ssm跨平台教学系统 毕业设计 280843
  • (规划)24届春招和25届暑假实习路线准备规划
  • (剑指Offer)面试题34:丑数
  • (七)理解angular中的module和injector,即依赖注入
  • (算法二)滑动窗口
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (转) RFS+AutoItLibrary测试web对话框
  • .chm格式文件如何阅读
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .NET Core跨平台微服务学习资源
  • .net 按比例显示图片的缩略图
  • .NET文档生成工具ADB使用图文教程
  • // an array of int
  • /proc/stat文件详解(翻译)
  • ::什么意思
  • [ vulhub漏洞复现篇 ] Hadoop-yarn-RPC 未授权访问漏洞复现