当前位置:   article > 正文

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

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

准备

下载
在这里插入图片描述

代码

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

效果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/221627
推荐阅读
相关标签
  

闽ICP备14008679号