经典 90 坦克大战 Python 版实现(支持单双人模式)

本文介绍了如何使用Python编程实现经典游戏90坦克大战,包括游戏设定、场景、坦克、子弹、食物和大本营的代码实现,并提供玩家操作说明。文章末尾分享了获取完整代码和进阶讨论的途径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

更多内容,请点击上方关注查看!

  • 坦克大战是一个比较经典的小游戏,而 90 坦克大战是一个比较经典的版本,我们来看一下如何利用 Python 实现坦克大战,先睹为快。
    在这里插入图片描述

游戏设定

基本组成
场景 坦克 子弹 食物 大本营
操作规则
玩家一

移动:WASD 射击:J
玩家二

移动:←→↑↓ 射击:0

主要实现

场景实现代码

场景类

import pygame
import random

石头墙

class Brick(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.brick = pygame.image.load('images/scene/brick.png')
		self.rect = self.brick.get_rect()
		self.being = False

钢墙

class Iron(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.iron = pygame.image.load('images/scene/iron.png')
		self.rect = self.iron.get_rect()
		self.being = False

class Ice(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.ice = pygame.image.load('images/scene/ice.png')
		self.rect = self.ice.get_rect()
		self.being = False

河流

class River(pygame.sprite.Sprite):
	def __init__(self, kind=None):
		pygame.sprite.Sprite.__init__(self)
		if kind is None:
			self.kind = random.randint(0, 1)
		self.rivers = ['images/scene/river1.png', 'images/scene/river2.png']
		self.river = pygame.image.load(self.rivers[self.kind])
		self.rect = self.river.get_rect()
		self.being = False

class Tree(pygame.sprite.Sprite):
	def __init__(self):
		pygame.sprite.Sprite.__init__(self)
		self.tree = pygame.image.load('images/scene/tree.png')
		self.rect = self.tree.get_rect()
		self.being = False

地图

class Map():
	def __init__(self, stage):
		self.brickGroup = pygame.sprite.Group()
		self.ironGroup  = pygame.sprite.Group()
		self.iceGroup = pygame.sprite.Group()
		self.riverGroup = pygame.sprite.Group()
		self.treeGroup = pygame.sprite.Group()
		if stage == 1:
			self.stage1()
		elif stage == 2:
			self.stage2()
	# 关卡一
	def stage1(self):
		for x in [2, 3, 6, 7, 18, 19, 22, 23]:
			for y in [2, 3, 4, 5, 6, 7, 8, 9, 10, 17, 18, 19, 20, 21, 22, 23]:
				self.brick = Brick()
				self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
				self.brick.being = True
				self.brickGroup.add(self.brick)
		for x in [10, 11, 14, 15]:
			for y in [2, 3, 4, 5, 6, 7, 8, 11, 12, 15, 16, 17, 18, 19, 20]:
				self.brick = Brick()
				self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
				self.brick.being = True
				self.brickGroup.add(self.brick)
		for x in [4, 5, 6, 7, 18, 19, 20, 21]:
			for y in [13, 14]:
				self.brick = Brick()
				self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
				self.brick.being = True
				self.brickGroup.add(self.brick)
		for x in [12, 13]:
			for y in [16, 17]:
				self.brick = Brick()
				self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
				self.brick.being = True
				self.brickGroup.add(self.brick)
		for x, y in [(11, 23), (12, 23), (13, 23), (14, 23), (11, 24), (14, 24), (11, 25), (14, 25)]:
			self.brick = Brick()
			self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
			self.brick.being = True
			self.brickGroup.add(self.brick)
		for x, y in [(0, 14), (1, 14), (12, 6), (13, 6), (12, 7), (13, 7), (24, 14), (25, 14)]:
			self.iron = Iron()
			self.iron.rect.left, self.iron.rect.top = 3 + x * 24, 3 + y * 24
			self.iron.being = True
			self.ironGroup.add(self.iron)
	# 关卡二
	def stage2(self):
		for x in [2, 3, 6, 7, 18, 19, 22, 23]:
			for y in [2, 3, 4, 5, 6, 7, 8, 9, 10, 17, 18, 19, 20, 21, 22, 23]:
				self.brick = Brick()
				self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
				self.brick.being = True
				self.brickGroup.add(self.brick)
		for x in [10, 11, 14, 15]:
			for y in [2, 3, 4, 5, 6, 7, 8, 11, 12, 15, 16, 17, 18, 19, 20]:
				self.brick = Brick()
				self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
				self.brick.being = True
				self.brickGroup.add(self.brick)
		for x in [4, 5, 6, 7, 18, 19, 20, 21]:
			for y in [13, 14]:
				self.brick = Brick()
				self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
				self.brick.being = True
				self.brickGroup.add(self.brick)
		for x in [12, 13]:
			for y in [16, 17]:
				self.brick = Brick()
				self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
				self.brick.being = True
				self.brickGroup.add(self.brick)
		for x, y in [(11, 23), (12, 23), (13, 23), (14, 23), (11, 24), (14, 24), (11, 25), (14, 25)]:
			self.brick = Brick()
			self.brick.rect.left, self.brick.rect.top = 3 + x * 24, 3 + y * 24
			self.brick.being = True
			self.brickGroup.add(self.brick)
		for x, y in [(0, 14), (1, 14), (12, 6), (13, 6), (12, 7), (13, 7), (24, 14), (25, 14)]:
			self.iron = Iron()
			self.iron.rect.left, self.iron.rect.top = 3 + x * 24, 3 + y * 24
			self.iron.being = True
			self.ironGroup.add(self.iron)
	def protect_home(self):
		for x, y in [(11, 23), (12, 23), (13, 23), (14, 23), (11, 24), (14, 24), (11, 25), (14, 25)]:
			self.iron = Iron()
			self.iron.rect.left, self.iron.rect.top = 3 + x * 24, 3 + y * 24
			self.iron.being = True
			self.ironGroup.add(self.iron)

**

坦克实现代码

**

坦克类

import pygame
import random
from BattleCity.bullet import Bullet

我方坦克类

class myTank(pygame.sprite.Sprite):
	def __init__(self, player):
		pygame.sprite.Sprite.__init__(self)
		# 玩家编号(1/2)
		self.player = player
		# 不同玩家用不同的坦克(不同等级对应不同的图)
		if player == 1:
			self.tanks = ['images/myTank/tank_T1_0.png', 'images/myTank/tank_T1_1.png', 'images/myTank/tank_T1_2.png']
		elif player == 2:
			self.tanks = ['images/myTank/tank_T2_0.png', 'images/myTank/tank_T2_1.png', 'images/myTank/tank_T2_2.png']
		else:
			raise ValueError('myTank class -> player value error.')
		# 坦克等级(初始0)
		self.level = 0
		# 载入(两个tank是为了轮子特效)
		self.tank = pygame.image.load(self
前言: 作者系四川大学计算机科学系毕业,但是毕业后十几年都没有编过程序,干的工作有抄水电表,网管,销售工作,最近发现人渐渐老去,有心愿未了,于是最近跟着网络视频教学,学习了一下VC++和面向对象编程,就自已小时候最爱玩的坦克大战为练习,来熟悉和巩固对VC++的学习。 本作特点: 1. 可能是世界上最接近“坦克大战”原作的VC程序. 几乎99%相似模拟度。 2. 本作还特别包括“坦克90”加强。 3. 即时存档,读档功能。 4. 即时回退,时光倒流功能。 5. 播放战斗录相功能。 6. 智能躲避敌方攻击的功能。(在演示状态) 7. 敌方坦克智能躲避工方攻击的功能。(在TANKE90模式) 8. 对Win7兼容性不好, 运行会变慢 9. 本作是精确到象素级的模拟原作了. 如何编译: 1. 运行VC6. 2. 用打开工作空间的方式, 打开Tank.dsw 3. 如编译出现Diretx方面的错,请下载directx8程序包http://115.com/file/clqzomlm#dx81sdk.zip 加入到你的VC6里面, 如何安装请查网上. 4. 根目录下有Tank.exe已经编译好的了, 你可以试一试, 360可能会误报, 但保证没有病毒. 不信你自已编译好后, 也可能会误报 后记: 本次放出的是DirectX本, 如有其它问题请联系作者. 作者邮箱: romman@163.com 另外,还有一个CFrame本,和一个WIN32本,这两个本效率不高,但兼容性好,有需要的联系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值