python实现坦克大战的方法-创新互联
这篇文章主要讲解了python实现坦克大战的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

下面来看看然后利用python做一个坦克大战游戏
创建子弹类
import pygame
class Bullet(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.bullet_up = pygame.image.load(r"..\image\bullet_up.png")
self.bullet_down = pygame.image.load(r"..\image\bullet_down.png")
self.bullet_left = pygame.image.load(r"..\image\bullet_left.png")
self.bullet_right = pygame.image.load(r"..\image\bullet_right.png")
# 子弹方向 速度 生命 碎石
self.dir_x, self.dir_y = 0, 0
self.speed = 6
self.life = False
self.strong = False
self.bullet = self.bullet_up
self.rect = self.bullet.get_rect()
self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24
def changeImage(self, dir_x, dir_y):
self.dir_x, self.dir_y = dir_x, dir_y
if self.dir_x == 0 and self.dir_y == -1:
self.bullet = self.bullet_up
elif self.dir_x == 0 and self.dir_y == 1:
self.bullet = self.bullet_down
elif self.dir_x == -1 and self.dir_y == 0:
self.bullet = self.bullet_left
elif self.dir_x == 1 and self.dir_y == 0:
self.bullet = self.bullet_right
def move(self):
self.rect = self.rect.move(self.speed * self.dir_x,
self.speed * self.dir_y)
# 碰撞地图边缘
if self.rect.top < 3:
self.life = False
# self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24
if self.rect.bottom > 630 - 3:
self.life = False
# self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24
if self.rect.left < 3:
self.life = False
# self.rect.left, self.rect.right = 3 + 12 * 24, 3 + 24 * 24
if self.rect.right > 630 - 3:
self.life = False另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
新闻标题:python实现坦克大战的方法-创新互联
标题链接:http://www.jxjierui.cn/article/degcdd.html


咨询
建站咨询
