圆月山庄资源网 Design By www.vgjia.com
经过一天多的奋战,查阅文献,参考别人的代码等等,完成了第一个面向对象的小项目,也深深体会到面向对象编程思想在游戏编程中所扮演的角色。
附上代码,参考了别人的代码,以及对他们代码的完善,又加上了自己的一些东西,收获颇深。
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张图片。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
pygame,坦克大战
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月05日
2024年11月05日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]