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

news2024/9/29 19:24:28

文章目录

  • 准备
  • 代码
    • 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: break
	is_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')

效果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1491451.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

仙宫云:细节控ComfyUI AI写实摄影+视频镜像

在使用comfyui工作流时经常遇到插件安装,模型下载的问题,为了方便大家使用和体验comfyui,我在仙宫云上部署了一个云端comfyui镜像包,开放给大家使用。 细节控ComfyUI AI写实摄影视频工作流: 镜像主页:仙宫…

python之双端队列deque

普通队列只能进行队尾插入和出队头的操作,双端队列可以对队头和队尾进行操作,而且相比于list实现的队更具有优越性,list实现在删除和插入时的时间复杂度大约为O(n),而deque的时间复杂度是O(1) 队头操作:append()、pop(…

三整数排序问题的解题逻辑

【题目描述】 输入3个整数,从小到大排序后输出。 【样例输入】 20 7 33 【样例输出】 7 20 33 【解析】 本题解法大概有3种: 1、穷举条件法。 此方法先判断a、b、c大小的所有可能,再根据各种可能性输出不同的排序。 思路是先判断a、…

微信小程序开发:循环定时删除阿里云oss上传的文件

上文有说到我们开发了定时删除阿里云oss的功能,但是一次只能删除10条。 本文我们做到一次删除全部过期的文件。 实现:使用while循环,在循环里获取是否还有已过期的,没有就break掉,有就走删除逻辑。 开始代码部分&am…

云原生团队如何实现加量不加价

随着云原生技术的快速发展,越来越多的业务实现了上云,云原生团队在工作量增大的同时也随之变成了所有问题对接的入口,如何承担这个保姆的角色成为了一道难题,故障的定界和问题证据的交接更是让人头疼的问题。在这种情况下需要有工…

2024年租用阿里云服务器多少钱?阿里云服务器租用价格表(最新版)

2024年租用阿里云服务器一年多少钱?不同时期阿里云服务器的租用价格不同,随着2024年阿里云上云采购季活动的开启和阿里云最新一轮的云产品降价调整,阿里云服务器租用价格也做了一些调整,配置最低的1核1G云服务器收费标准为22.8/月…

【中间件】RabbitMQ入门

📝个人主页:五敷有你 🔥系列专栏:中间件 ⛺️稳中求进,晒太阳 MQ的优劣: 优势 应用解耦:提升了系统容错性和可维护性异步提速:提升用户体验和系统吞吐量消峰填谷&#xff1…

【Spring云原生】Spring官宣,干掉原生JVM,推出 Spring Native!整体提升性能!Native镜像技术在Spring中的应用

🎉🎉欢迎光临🎉🎉 🏅我是苏泽,一位对技术充满热情的探索者和分享者。🚀🚀 🌟特别推荐给大家我的最新专栏《Spring 狂野之旅:从入门到入魔》 🚀 本…

c++的队列的用法

基本介绍 c的队列就是std::queue。 需要包含的头文件&#xff1a; #include<queue>queue就是先进先出队列 queue,就是队列&#xff0c;队列是一种容器适配器&#xff0c;专门设计用于在FIFO上下文中操作(先进先出)&#xff0c;其中将元素插入容器的一端并从另一端提…

LangChain 教程:构建 LLM 支持的应用程序的指南

作者&#xff1a;Aditya Tripathi GPT-4 和 LLaMA 等大型语言模型 (LLM) 在过去几年中创造了一个充满可能性的世界。 它预示着人工智能工具和应用程序的繁荣&#xff0c;ChatGPT 似乎一夜之间成为家喻户晓的名字。 但如果没有为促进新一代应用程序而创建的强大工具和框架&#…

【面试题】webpack的五大核心、构建流程、性能优化

【面试题】webpack的五大核心、webpack的构建流程、webpack的性能优化 webpack是什么?webpack的五大核心webpack的构建流程webpack性能优化 webpack是什么? js静态模块打包工具。 功能 将多个文件打包成更小的文件&#xff0c;(压缩)翻译 babal-loader es6进行降级兼容。 …

【pyinstaller打包记录】Linux系统打包可执行文件后,onnxruntime报警告(Init provider bridge failed)

简介 PyInstaller 是一个用于将 Python 程序打包成可执行文件&#xff08;可执行程序&#xff09;的工具。它能够将 Python 代码和其相关的依赖项&#xff08;包括 Python 解释器、依赖的模块、库文件等&#xff09;打包成一个独立的可执行文件&#xff0c;方便在不同环境中运行…

如何根据企业司法涉诉大数据合理规避风险?

在当前的商业环境中&#xff0c;企业司法涉诉的信息越来越成为衡量一家企业信誉和运营风险的重要标准。大数据时代的到来&#xff0c;让我们有了更加丰富的手段对这些信息进行挖掘与分析&#xff0c;从而对企业可能面临的风险进行预警。本文将探讨如何通过对企业司法涉诉的大数…

服务器硬件监控,保障系统稳健运行的关键策略

服务器硬件在运维中扮演着至关重要的角色&#xff0c;超过一半的网络中断是由硬件故障引起的&#xff0c;这使得硬件性能监控成为运维中不可或缺的一部分。对于一个组织或企业的信息技术基础设施而言&#xff0c;重要性不言而喻&#xff1a; 1. 安全性&#xff1a;服务器硬…

虚拟内存地址动静态库

前言 大家好我是jiantaoyab&#xff0c;这是我所总结作为学习的笔记第5篇,在这里分享给大家,还有一些书籍《[深入理解计算机系统》《计算机组成&#xff1a;结构化方法》《计算机体系结构&#xff1a;量化研究方法》《程序员的自我修养》&#xff0c;今天我们来了解程序的虚拟…

聚观早报 | 2024款腾势D9将发布;岚图汽车2月销量

聚观早报每日整理最值得关注的行业重点事件&#xff0c;帮助大家及时了解最新行业动态&#xff0c;每日读报&#xff0c;就读聚观365资讯简报。 整理丨Cutie 3月2日消息 2024款腾势D9将发布 岚图汽车2月销量 苹果Vision Pro防汗新专利 真我12 Pro正式开售 Redmi K70/Pro…

wvp-gb28181-pro国标设备录像下载

点击【国标设备】&#xff0c;进入设备通道 每个通道右边都有对应的操作&#xff0c; 点击操作栏中的【设备录像】按钮 点击【设备录像】进入录像查看页面&#xff0c;选择要查看的日期即可对录像进行播放和下载 播放&#xff1a;双击录像名称 下载&#xff1a;点击下载按钮 下…

SpringBoot实现分页模糊查询

1. Navicat查询数据 Navicat中查询所有数据 SELECT * FROM sys_user;Navicat中查询前两条数据&#xff08;俩种方式&#xff09; SELECT * FROM sys_user LIMIT 2; //从0开始&#xff0c;第一个参数是起始位置即(pageNum-1)*pageSize&#xff0c;第二个参数是步长 SELECT * …

一文搞定Pytorch CUDA Toolkit与Driver的关系

1. 在我们使用Pytorch不同的版本时&#xff0c;有这样一个大致的对应关系&#xff0c;即&#xff1a;Pytorch版本CUDA Toolkit版本NVIDIA Driver 版本。 难点在于CUDA版本与Driver版本的关系&#xff0c;简单通过NVIDIA官网解决&#xff1a;CUDA Compatibility 这里既有关于Li…

JavaScript的`call`方法:实现函数间的调用!

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…