利用Python编写一个坦克大战小游戏-创新互联-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

利用Python编写一个坦克大战小游戏-创新互联

本篇文章给大家分享的是有关利用Python编写一个坦克大战小游戏,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

公司主营业务:成都做网站、网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出老城免费做网站回馈大家。

代码实现如下:

# 石头墙
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)

分享题目:利用Python编写一个坦克大战小游戏-创新互联
文章转载:http://kswsj.cn/article/diijed.html

其他资讯