您好,登錄后才能下訂單哦!
基于對面向對象編程的思想完成簡單的坦克大戰游戲。主要目的鍛煉面相對象編程思想
同樣的在使用python進行游戲編寫時需要安裝pygame模塊
安裝方法:
pycharm安裝方式:File --> setting
游戲中的主要對象有:
坦克父類:BaseTank
我方坦克:HeroTank
敵方坦克:EnemyTank
子彈類:Bullet
爆炸類:Explode
墻類:Wall
主進程:MainGame
定義一個精靈類:
# 定義一個精靈類 class BaseItem(Sprite): def __init__(self, color, width, height): # Call the parent class (Sprite) constructor pygame.sprite.Sprite.__init__(self)
坦克父類:
# 坦克父類 class BaseTank(BaseItem): # 定義類屬性,所有坦克對象高和寬都是一樣 width = 60 height = 60 def __init__(self, left, top): self.direction = 'U' # 坦克的方向默認向上 # 存放圖片的字典 self.images = { 'U': pygame.image.load('tank_img/p1tankU.gif'), 'D': pygame.image.load('tank_img/p1tankD.gif'), 'L': pygame.image.load('tank_img/p1tankL.gif'), 'R': pygame.image.load('tank_img/p1tankR.gif') } self.image = self.images[self.direction] # 坦克的圖片由方向決定 self.speed = 5 # 坦克的速度 self.rect = self.image.get_rect() # 設置放置的位置 self.rect.left = left self.rect.top = top self.stop = True # 坦克是否停止 self.live = True # 決定坦克是否消滅了 # 保持原來的位置 self.oldLeft = self.rect.left self.oldTop = self.rect.top # 射擊方法 def shot(self): return Bullet(self) # 坦克的移動 def move(self): # 保持原來的狀態 self.oldLeft = self.rect.left self.oldTop = self.rect.top # 判斷坦克的移動方向 if self.direction == 'U': if self.rect.top > 0: self.rect.top -= self.speed elif self.direction == 'D': if self.rect.top + self.rect.height < WINDOW_HEIGHT: self.rect.top += self.speed elif self.direction == 'L': if self.rect.left > 0: self.rect.left -= self.speed elif self.direction == 'R': if self.rect.left+self.rect.height < WINDOW_WIDTH: self.rect.left += self.speed # 加載坦克 def displayTank(self): self.image = self.images[self.direction] MainGame.window.blit(self.image, self.rect) # 撞墻處理 def hitWall(self): for wall in MainGame.wallList: if pygame.sprite.collide_rect(wall, self): self.stay() # 處理位置不變 def stay(self): self.rect.left = self.oldLeft self.rect.top = self.oldTop
我方坦克類():
# 我方坦克 class HeroTank(BaseTank): def __init__(self, left, top): super().__init__(left, top) # 我方坦克碰撞敵方坦克 def myTank_hit_enemyTank(self): for enemyTank in MainGame.EnemyTankList: if pygame.sprite.collide_rect(enemyTank, self): self.stay()
敵方坦克類():
# 敵方坦克 class EnemyTank(BaseTank): def __init__(self, left, top, speed): super(EnemyTank, self).__init__(left, top) self.images = { 'U': pygame.image.load('tank_img/enemy1U.gif'), 'D': pygame.image.load('tank_img/enemy1D.gif'), 'L': pygame.image.load('tank_img/enemy1L.gif'), 'R': pygame.image.load('tank_img/enemy1R.gif') } self.direction = self.RandomDirection() self.image = self.images[self.direction] self.rect = self.image.get_rect() self.rect.left = left self.rect.top = top self.speed = speed self.step = 60 self.enemy_flag = False # 坦克出生隨機方向 def RandomDirection(self): num = random.randint(1, 4) if num == 1: return 'U' elif num == 2: return 'D' elif num == 3: return 'L' else: return 'R' # 坦克隨機移動 def randomMove(self): if self.step < 0: self.direction = self.RandomDirection() self.step = 60 else: self.move() self.step -= 1 # 坦克射擊 def shot(self): num = random.randint(1, 100) if num < 4: return Bullet(self) # 敵方坦克碰撞我方坦克 def enemyTank_hit_MyTank(self): for enemy in MainGame.EnemyTankList: if MainGame.my_tank and MainGame.my_tank.live: if pygame.sprite.collide_rect(MainGame.my_tank, enemy): self.stay()
子彈類():
# 子彈類 class Bullet(BaseItem): def __init__(self, tank): self.image = pygame.image.load('tank_img/tankmissile.gif') self.direction = tank.direction self.rect = self.image.get_rect() # 根據坦克方向,生成子彈位置 if self.direction == 'U': self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2 self.rect.top = tank.rect.top - self.rect.height elif self.direction == 'D': self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2 self.rect.top = tank.rect.top + tank.rect.height elif self.direction == 'L': self.rect.left = tank.rect.left - self.rect.width / 2 - self.rect.width / 2 self.rect.top = tank.rect.top + tank.rect.height / 2 - self.rect.width / 2 elif self.direction == 'R': self.rect.left = tank.rect.left + tank.rect.width self.rect.top = tank.rect.top + tank.rect.height / 2 - self.rect.width / 2 # 子彈的速度 self.speed = 6 # 子彈狀態 self.live = True # 加載子彈 def displayBullet(self): MainGame.window.blit(self.image, self.rect) # 子彈的移動 def move(self): if self.direction == 'U': if self.rect.top > 0: self.rect.top -= self.speed else: self.live = False elif self.direction == 'R': if self.rect.left + self.rect.width < WINDOW_WIDTH: self.rect.left += self.speed else: self.live = False elif self.direction == 'D': if self.rect.top + self.rect.height < WINDOW_HEIGHT: self.rect.top += self.speed else: self.live = False elif self.direction == 'L': if self.rect.left > 0: self.rect.left -= self.speed else: self.live = False # 我方子彈擊中敵方坦克 def myBullet_hit_enemy(self): for enemytank in MainGame.EnemyTankList: if pygame.sprite.collide_rect(enemytank, self): enemytank.live = False self.live = False # 創建爆炸對象 explode = Explode(enemytank) MainGame.explodeList.append(explode) # 敵方坦克擊中我方坦克 def enemyBullet_hit_myTank(self): if MainGame.my_tank and MainGame.my_tank.live: if pygame.sprite.collide_rect(MainGame.my_tank, self): MainGame.my_tank.live = False self.live = False # 創建爆炸對象 explode = Explode(MainGame.my_tank) MainGame.explodeList.append(explode) # 射擊墻壁 def wall_bullet(self): for wall in MainGame.wallList: if pygame.sprite.collide_rect(wall, self): wall.hg -= 1 self.live = False if wall.hg <= 0: wall.live = False
墻壁類():
# 墻壁類 class Wall: def __init__(self, left, top): self.image = pygame.image.load('tank_img/steels.gif') self.rect = self.image.get_rect() self.rect.left = left self.rect.top = top self.live = True self.hg = 100000000000000 # 加載墻壁 def displayWall(self): if self.live: MainGame.window.blit(self.image, self.rect)
爆炸類:
# 爆炸類 class Explode: def __init__(self, tank): # 爆炸的位置由坦克決定 self.rect = tank.rect self.images = [ pygame.image.load('tank_img/blast0.gif'), pygame.image.load('tank_img/blast1.gif'), pygame.image.load('tank_img/blast2.gif'), pygame.image.load('tank_img/blast3.gif'), pygame.image.load('tank_img/blast4.gif'), pygame.image.load('tank_img/blast5.gif'), pygame.image.load('tank_img/blast6.gif'), pygame.image.load('tank_img/blast7.gif') ] self.step = 0 self.image = self.images[self.step] self.live = True # 加載爆炸類 def displayExplode(self): if self.step < len(self.images): self.image = self.images[self.step] self.step += 1 MainGame.window.blit(self.image, self.rect) else: self.live = False self.step = 0
主進程():
# 游戲類 class MainGame: # 類屬性 window = None my_tank = None # 敵方坦克初始化 EnemyTankList = [] EnemyTankCount = 5 # 存儲我方子彈列表 myBulleList = [] # 存儲敵方子彈的列表 EnemyBulletList = [] # 創建爆炸對象列表 explodeList = [] # 創建墻壁列表 wallList = [] # 游戲開始方法 def start_game(self): # 初始化展示模塊 pygame.display.init() # 調用創建窗口的方法 self.creat_window() # 設置游戲窗口標題 pygame.display.set_caption('坦克大戰') # 初始化我方坦克 self.createMyTank() # 初始化敵方坦克 self.creatEnemyTank() # 初始化墻壁 self.creatWall() # 程序持續進行 while True: # 更改背景顏色 MainGame.window.fill(COLOR_GREEN) # 背景音樂 # 獲取事件 self.getEvent() # 調用我方坦克進行顯示 if MainGame.my_tank and MainGame.my_tank.live: MainGame.my_tank.displayTank() if not MainGame.my_tank.stop: MainGame.my_tank.move() MainGame.my_tank.hitWall() MainGame.my_tank.myTank_hit_enemyTank() else: del MainGame.my_tank MainGame.my_tank = None # 加載我方子彈 self.biltMyBullet() # 顯示敵方坦克 self.biltEnemyTank() # 顯示敵方子彈 self.biltEnemyBullet() # 顯示墻壁 self.blitWall() # 顯示爆炸效果 self.blitExplode() self.put_more_enemytank() # 窗口持續刷新 pygame.display.update() time.sleep(0.02) # 重復添加敵方坦克 def put_more_enemytank(self): while len(MainGame.EnemyTankList) < 5: self.more() # 創建游戲窗口方法: def creat_window(self): if not MainGame.window: # 創建窗口 MainGame.window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) return MainGame.window # 創建我方坦克 def createMyTank(self): MainGame.my_tank = HeroTank((WINDOW_WIDTH - HeroTank.width)/2, WINDOW_HEIGHT - HeroTank.height) music = Music('tank_img/start.wav') music.play() # 創建墻壁 def creatWall(self): for i in range(60, WINDOW_WIDTH, 60): top = WINDOW_WIDTH // 3 left = i wall = Wall(left, top) MainGame.wallList.append(wall) # 顯示墻壁 def blitWall(self): for b in MainGame.wallList: if b.live: b.displayWall() else: MainGame.wallList.remove(b) # 加載我方子彈 def biltMyBullet(self): for bullet in MainGame.myBulleList: if bullet.live: bullet.displayBullet() bullet.move() bullet.myBullet_hit_enemy() bullet.wall_bullet() else: MainGame.myBulleList.remove(bullet) # 后續坦克的添加 def more(self): top = 0 for i in range(5 - len(MainGame.EnemyTankList)): left = random.randint(0, 750) speed = random.randint(1, 4) enemy = EnemyTank(left, top, speed) MainGame.EnemyTankList.append(enemy) # 創建敵方坦克 def creatEnemyTank(self): top = 0 for i in range(MainGame.EnemyTankCount): left = random.randint(0, 750) speed = random.randint(1, 4) enemy = EnemyTank(left, top, speed) MainGame.EnemyTankList.append(enemy) # 循環遍歷顯示敵方坦克 def biltEnemyTank(self): for enemytank in MainGame.EnemyTankList: if enemytank.live: enemytank.displayTank() EnemyBullet = enemytank.shot() enemytank.randomMove() enemytank.hitWall() enemytank.enemyTank_hit_MyTank() # 存儲敵方子彈 if EnemyBullet: MainGame.EnemyBulletList.append(EnemyBullet) else: MainGame.EnemyTankList.remove(enemytank) MainGame.EnemyTankCount -= 1 # 加載敵方子彈 def biltEnemyBullet(self): for bullet in MainGame.EnemyBulletList: if bullet.live: bullet.displayBullet() bullet.move() bullet.enemyBullet_hit_myTank() bullet.wall_bullet() else: MainGame.EnemyBulletList.remove(bullet) # 加載爆炸效果 def blitExplode(self): for explode in MainGame.explodeList: if explode.live: explode.displayExplode() else: MainGame.explodeList.remove(explode) # 獲取游戲中的所有事件 def getEvent(self): # 獲取游戲中的事件列表 even_list = pygame.event.get() for e in even_list: # 點擊窗口的叉號實現游戲結束 if e.type == pygame.QUIT: sys.exit() # 通過上下左右鍵控制坦克的移動 if e.type == pygame.KEYDOWN: if MainGame.my_tank and MainGame.my_tank.live: if e.key == pygame.K_DOWN or e.key == pygame.K_s: MainGame.my_tank.direction = 'D' MainGame.my_tank.stop = False print("按下向下的鍵,向下移動") elif e.key == pygame.K_UP or e.key == pygame.K_w: MainGame.my_tank.direction = 'U' MainGame.my_tank.stop = False print("按下向上的鍵,向上移動") elif e.key == pygame.K_LEFT or e.key == pygame.K_a: MainGame.my_tank.direction = 'L' MainGame.my_tank.stop = False print("按下向左的鍵,向左移動") elif e.key == pygame.K_RIGHT or e.key == pygame.K_d: MainGame.my_tank.direction = 'R' MainGame.my_tank.stop = False print("按下向右的鍵,向右移動") elif e.key == pygame.K_SPACE: print('發射子彈') # 創建我方子彈 if len(MainGame.myBulleList) < 10: mybullet = Bullet(MainGame.my_tank) MainGame.myBulleList.append(mybullet) # 射擊音效 Shot_music = Music('tank_img/fire.wav') Shot_music.play() elif e.type == pygame.KEYUP: if e.key == pygame.K_UP or e.key == pygame.K_DOWN or e.key == pygame.K_LEFT or e.key == pygame.K_RIGHT \ or e.key == pygame.K_w or e.key == pygame.K_s or e.key == pygame.K_a or e.key == pygame.K_d: if MainGame.my_tank and MainGame.my_tank.live: MainGame.my_tank.stop = True
程序運行結果:
源碼下載:python實現簡單坦克大戰
更多關于python游戲的精彩文章請點擊查看以下專題:
python俄羅斯方塊游戲集合
python經典小游戲匯總
python微信跳一跳游戲集合
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。