面向对象学习之pygame坦克大战


Posted in Python onSeptember 11, 2019

经过一天多的奋战,查阅文献,参考别人的代码等等,完成了第一个面向对象的小项目,也深深体会到面向对象编程思想在游戏编程中所扮演的角色。

附上代码,参考了别人的代码,以及对他们代码的完善,又加上了自己的一些东西,收获颇深。

import pygame
import sys
import time
from pygame.locals import *
from random import randint
MOVE_SLEEP = 0.01
class MyTank:
 width = 600
 heights = 500
 speed = 10
 screen = 0
 myshells = []
 enemylist = []
 enemyshells = []
 grade = 0
 life = 3
 cnt = 0
 def startgame(self):
 pygame.init()
 self.screen = pygame.display.set_mode((self.width,self.heights),0,32)
 pygame.display.set_caption("bit tank")
 self.tank = Tank(self.screen,275,450)
 for i in range(6):
  self.enemylist.append(EnmeyTank(self.screen))
 while True:
  key = pygame.key.get_pressed()
  self.screen.fill((0,0,0))
  if key[K_LEFT]:
  self.tank.move('L')
  elif key[K_RIGHT]:
  self.tank.move('R')
  elif key[K_UP]:
  self.tank.move('U')
  elif key[K_DOWN]:
  self.tank.move('D')
  self.get_event()
  for shell in self.myshells:
  if shell.move() == True:
   self.myshells.remove(shell)
  shell.display()
  a = shell.hitTank()
  #子弹碰撞
  if a == True:
   if self.life >0:
   self.myshells.remove(shell)
   self.grade += 1
  #mytank碰撞
  if self.tank.live == True:
  if self.tank.hitTank():
   self.life -= 1
   if self.life <=0:
   self.tank.live =False
   else:self.tank = Tank(self.screen,275,450)
  #mytanke 碰撞子弹
  if self.tank.live == True:
  if self.tank.hitShell():
   self.life -= 1
   if self.life <=0:
   self.tank.live = False
   else:self.tank=Tank(self.screen,275,450)
  #敌方子弹击中我方坦克
  # 游戏结束
  if self.life <=0:
  self.gotGamePrint()
  for enemy in self.enemylist:
  enemy.move()
  print('move')
  enemy.display()
  # 添加敌方子弹
  self.cnt += 1
  if self.cnt % 100 ==0:
  for enemy in self.enemylist:
   self.enemyshells.append(enemy.fire())
  #判断敌方子弹碰撞
  for enemyshell in self.enemyshells:
  f = enemyshell.move()
  enemyshell.display()
  if f:
   self.enemyshells.remove(enemyshell)
  if len(self.enemylist)<6:
  self.enemylist.append(EnmeyTank(self.screen))
  self.screen.blit(self.getGrade(),(5,5))
  self.tank.display()
  pygame.display.update()
  time.sleep(0.02)
 def get_event(self):
 for event in pygame.event.get():
  if event.type == KEYDOWN:
  if event.key == K_SPACE:
   self.myshells.append(self.tank.fire())
  if event.key == K_ESCAPE:
   pass
 def getGrade(self):
 text = pygame.font.Font('./font/msyhbd.ttc',20).render("分数:{} 生命:{}".format(self.grade,self.life),True,(0,255,0))
 return text
 def gotGamePrint(self):
 text = pygame.font.Font('./font/msyh.ttc',70).render('game over!',True,(0,255,0))
 self.screen.blit(text,(100,200))
class Shell:
 width = 48
 height = 48
 live = True
 speed = 3
 def __init__(self,screen,tank):
 self.screen = screen
 self.image = pygame.image.load('./images/3.png')
 self.direction = tank.direction
 self.rect = self.image.get_rect()
 self.rect.left = tank.rect.left + (tank.width-self.width)/2.0+18
 # print(tank.rect.left,tank.width,self.width)
 self.rect.top = tank.rect.top + (tank.height - self.height)/2.0
 self.live = True
 def move(self):
 tag = self.isObstacle()
 if self.live == True:
  if self.direction == 'L' and self.direction not in tag:
  self.rect.left -= self.speed
  elif self.direction == 'R' and self.direction not in tag:
  self.rect.left += self.speed
  elif self.direction == 'U' and self.direction not in tag:
  self.rect.top -= self.speed
  elif self.direction == 'D' and self.direction not in tag:
  self.rect.top += self.speed
  else:
  pass
  if self.direction in tag:
  return True
  else:
  return False
 else:
  pass
 def display(self):
 # print(self.rect.left,self.rect.top)
 if self.live == True:
  self.screen.blit(self.image,self.rect)
 def isObstacle(self):
 tag = []
 if self.rect.left <=0:tag.append('L')
 if self.rect.left + self.width >= MyTank.width:tag.append('R')
 if self.rect.top <=0:tag.append('U')
 if self.rect.top + self.height >=MyTank.heights:tag.append('D')
 return tag
 def hitTank(self):
 hitList = pygame.sprite.spritecollide(self,MyTank.enemylist,False)
 for e in hitList:
  e.live = False
  MyTank.enemylist.remove(e)
  self.live = False
  return True
 return False
 def hitMytank(self):
 hitList = pygame.sprite.spritecollide(self,MyTank.tank,False)
 for e in hitList:
  e.live = False
  MyTank.life -= 1
  return True
class BaseTank:
 width = 50
 height = 50
 direction = 'U'
 live = True
 time = 0
 images = {}
 def __init__(self,screen,left,top):
 self.screen = screen
 self.images['L'] = pygame.image.load("images/04.jpg")
 self.images['R'] = pygame.image.load("images/02.jpg")
 self.images['U'] = pygame.image.load("images/01.jpg")
 self.images['D'] = pygame.image.load("images/03.jpg")
 self.image = self.images[self.direction]
 self.rect = self.image.get_rect()
 self.rect.left = left
 self.rect.top = top
 self.live = True # 坦克是否被消灭
 def isObstacle(self):
 tag = []
 if self.rect.left <= 0: tag.append('L')
 if self.rect.left + self.width >= MyTank.width: tag.append('R')
 if self.rect.top <= 0: tag.append('U')
 if self.rect.top + self.height >= MyTank.heights: tag.append('D')
 return tag
 def display(self):
 if self.live == True:
  self.image = self.images[self.direction]
  self.screen.blit(self.image, self.rect)
 def fire(self):
 m = Shell(self.screen,self)
 return m
class Tank(BaseTank):
 images = {}
 def __init__(self,screen,left,top):
 super().__init__(screen,275,450)
 self.screen = screen
 self.speed = 2
 self.images['L'] = pygame.image.load('./images/4.jpg')
 self.images['R'] = pygame.image.load('./images/2.jpg')
 self.images['U'] = pygame.image.load('./images/1.jpg')
 self.images['D'] = pygame.image.load('./images/3.jpg')
 self.image = self.images[self.direction]
 self.rect = self.image.get_rect()
 self.rect.top = top
 self.rect.left = left
 def move(self, direction):
 if self.live == True:
  tag = self.isObstacle()
  if direction == self.direction:
  if self.direction == 'L' and self.direction not in tag:
   self.rect.left -= self.speed
  elif self.direction == 'R' and self.direction not in tag:
   self.rect.left += self.speed
  elif self.direction == 'U' and self.direction not in tag:
   self.rect.top -= self.speed
  elif self.direction == 'D' and self.direction not in tag:
   self.rect.top += self.speed
  else:
   pass
  else:
  self.direction = direction
 def hitTank(self):
 hitList = pygame.sprite.spritecollide(self,MyTank.enemylist,False)
 for e in hitList:
  self.live = False
  return True
 return False
 def hitShell(self):
 hitlist = pygame.sprite.spritecollide(self, MyTank.enemyshells, False)
 for e in hitlist:
  self.live = False
  return True
 return False
class EnmeyTank(BaseTank):
 speed = 1
 def __init__(self,screen):
 super().__init__(screen,randint(1,5)*100,0)
 self.getdirection()
 self.step = 0
 def getdirection(self):
 self.direction = ['L','R','U','D'][randint(0,3)]
 def move(self):
 if self.live == True:
  if self.step == 0 or (self.direction in self.isObstacle()):
  self.getdirection()
  self.step = randint(0, 200)
  else:
  tag = self.isObstacle()
  if self.direction == 'L' and self.direction not in tag:
   self.rect.left -= self.speed
  elif self.direction == 'R' and self.direction not in tag:
   self.rect.left += self.speed
  elif self.direction == 'U' and self.direction not in tag:
   self.rect.top -= self.speed
  elif self.direction == 'D' and self.direction not in tag:
   self.rect.top += self.speed
  else:
   pass
  self.step -= 1
if __name__ == '__main__':
 main = MyTank()
 main.startgame()

文件主要有10张图片和2个字体文件,主坦克的四个形态,敌方坦克的四个形态,以及子弹等,10张图片。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现代码行数统计示例分享
Feb 10 Python
Python远程桌面协议RDPY安装使用介绍
Apr 15 Python
python实现将元祖转换成数组的方法
May 04 Python
Python实现删除文件但保留指定文件
Jun 21 Python
Python 迭代器工具包【推荐】
May 06 Python
python实现批量解析邮件并下载附件
Jun 19 Python
python动态进度条的实现代码
Jul 03 Python
在python Numpy中求向量和矩阵的范数实例
Aug 26 Python
pytorch .detach() .detach_() 和 .data用于切断反向传播的实现
Dec 27 Python
Python基于requests库爬取网站信息
Mar 02 Python
Opencv 图片的OCR识别的实战示例
Mar 02 Python
python中Tkinter 窗口之输入框和文本框的实现
Apr 12 Python
Python整数与Numpy数据溢出问题解决
Sep 11 #Python
python中通过selenium简单操作及元素定位知识点总结
Sep 10 #Python
用Python画一个LinkinPark的logo代码实例
Sep 10 #Python
Pytorch修改ResNet模型全连接层进行直接训练实例
Sep 10 #Python
django drf框架自带的路由及最简化的视图
Sep 10 #Python
Pytorch中accuracy和loss的计算知识点总结
Sep 10 #Python
python3.7环境下安装Anaconda的教程图解
Sep 10 #Python
You might like
PHP 和 MySQL 基础教程(二)
2006/10/09 PHP
PHP @ at 记号的作用示例介绍
2014/10/10 PHP
PHP多进程通信-消息队列使用
2019/03/08 PHP
JavaScript获取GridView选择的行内容
2009/04/14 Javascript
jQuery图片切换插件jquery.cycle.js使用示例
2014/06/16 Javascript
jQuery中的read和JavaScript中的onload函数的区别
2014/08/27 Javascript
JavaScript 经典实例日常收集整理(常用经典)
2016/03/30 Javascript
js实现select二级联动下拉菜单
2020/04/17 Javascript
基于cssSlidy.js插件实现响应式手机图片轮播效果
2016/08/30 Javascript
Vuejs第十三篇之组件——杂项
2016/09/09 Javascript
javascript创建对象的3种方法
2016/11/02 Javascript
BootStrap表单宽度设置方法
2017/03/10 Javascript
JavaScript条件判断_动力节点Java学院整理
2017/06/26 Javascript
jQuery中过滤器的基本用法示例
2017/10/11 jQuery
使用vue实现grid-layout功能实例代码
2018/01/05 Javascript
vue :src 文件路径错误问题的解决方法
2018/05/15 Javascript
使用VUE+iView+.Net Core上传图片的方法示例
2019/01/04 Javascript
Vue.js使用axios动态获取response里的data数据操作
2020/09/08 Javascript
关于IDEA中的.VUE文件报错 Export declarations are not supported by current JavaScript version
2020/10/17 Javascript
vue集成一个支持图片缩放拖拽的富文本编辑器
2021/01/29 Vue.js
[40:55]Liquid vs LGD 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
[46:55]完美世界DOTA2联赛决赛 FTD vs Phoenix 第三场 11.08
2020/11/11 DOTA
Python时区设置方法与pytz查询时区教程
2013/11/27 Python
kafka-python 获取topic lag值方式
2019/12/23 Python
关于ZeroMQ 三种模式python3实现方式
2019/12/23 Python
python Matplotlib模块的使用
2020/09/16 Python
行政经理的岗位职责
2013/11/23 职场文书
硕士研究生就业推荐信
2014/05/18 职场文书
网络技术专业求职信
2014/07/13 职场文书
三八活动策划方案
2014/08/17 职场文书
村当支部个人对照检查材料思想汇报
2014/10/06 职场文书
大学生翘课检讨书范文
2014/10/06 职场文书
绵山导游词
2015/02/05 职场文书
如何写辞职信
2015/05/13 职场文书
致接力运动员加油稿
2015/07/21 职场文书
SQL Server删除表中的重复数据
2022/05/25 SQL Server