最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

面向?qū)ο髮W(xué)習(xí)之pygame坦克大戰(zhàn)

 更新時(shí)間:2019年09月11日 08:37:54   作者:qq_40121643  
這篇文章主要為大家詳細(xì)介紹了面向?qū)ο髮W(xué)習(xí)之pygame坦克大戰(zhàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

經(jīng)過(guò)一天多的奮戰(zhàn),查閱文獻(xiàn),參考別人的代碼等等,完成了第一個(gè)面向?qū)ο蟮男№?xiàng)目,也深深體會(huì)到面向?qū)ο缶幊趟枷朐谟螒蚓幊讨兴缪莸慕巧?/p>

附上代碼,參考了別人的代碼,以及對(duì)他們代碼的完善,又加上了自己的一些東西,收獲頗深。

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)
  #敵方子彈擊中我方坦克
  # 游戲結(jié)束
  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("分?jǐn)?shù):{} 生命:{}".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個(gè)字體文件,主坦克的四個(gè)形態(tài),敵方坦克的四個(gè)形態(tài),以及子彈等,10張圖片。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python爬蟲(chóng)代理池搭建的方法步驟

    Python爬蟲(chóng)代理池搭建的方法步驟

    這篇文章主要介紹了Python爬蟲(chóng)代理池搭建的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Python實(shí)現(xiàn)語(yǔ)音合成功能詳解

    Python實(shí)現(xiàn)語(yǔ)音合成功能詳解

    這篇文章主要為大家介紹了一個(gè)通過(guò)Python制作的小工具,可以實(shí)現(xiàn)語(yǔ)音識(shí)別以及文字轉(zhuǎn)語(yǔ)音的功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的可以動(dòng)手試一試
    2022-01-01
  • 淺談使用Python變量時(shí)要避免的3個(gè)錯(cuò)誤

    淺談使用Python變量時(shí)要避免的3個(gè)錯(cuò)誤

    這篇文章主要介紹了淺談使用Python變量時(shí)要避免的3個(gè)錯(cuò)誤,還是比較不錯(cuò)的,涉及部分代碼分析,以及字典的創(chuàng)建等相關(guān)內(nèi)容,需要的朋友可以參考下。
    2017-10-10
  • 詳解詳解Python中writelines()方法的使用

    詳解詳解Python中writelines()方法的使用

    這篇文章主要介紹了詳解詳解Python中writelines()方法的使用,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Python讀取txt某幾列繪圖的方法

    Python讀取txt某幾列繪圖的方法

    今天小編就為大家分享一篇Python讀取txt某幾列繪圖的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • python中安裝模塊包版本沖突問(wèn)題的解決

    python中安裝模塊包版本沖突問(wèn)題的解決

    這篇文章主要給大家介紹了在python中安裝模塊包版本沖突問(wèn)題的解決方法,文中介紹了該問(wèn)題的原因與解決方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-05-05
  • Python為Excel單元格設(shè)置填充\背景色的操作方法(純色、漸變、圖案)

    Python為Excel單元格設(shè)置填充\背景色的操作方法(純色、漸變、圖案)

    在使用Excel進(jìn)行數(shù)據(jù)處理和分析時(shí),對(duì)特定單元格進(jìn)行背景顏色填充不僅能夠提升工作表的視覺(jué)吸引力,還能幫助用戶(hù)快速識(shí)別和區(qū)分不同類(lèi)別的數(shù)據(jù),本文將通過(guò)三個(gè)示例詳細(xì)介紹如何使用Python在Excel中設(shè)置不同的單元格背景,需要的朋友可以參考下
    2024-08-08
  • Pandas.DataFrame刪除指定行和列(drop)的實(shí)現(xiàn)

    Pandas.DataFrame刪除指定行和列(drop)的實(shí)現(xiàn)

    本文主要介紹了Pandas.DataFrame刪除指定行和列(drop)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • 我用Python做個(gè)AI出牌器斗地主把把贏

    我用Python做個(gè)AI出牌器斗地主把把贏

    這篇文章主要介紹了我是如何用Python做的AI出牌器,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • Python圖片驗(yàn)證碼降噪和8鄰域降噪

    Python圖片驗(yàn)證碼降噪和8鄰域降噪

    這篇文章主要介紹了Python圖片驗(yàn)證碼降噪和8鄰域降噪的相關(guān)內(nèi)容,需要的小伙伴可以參考下面文章
    2021-08-08

最新評(píng)論

洞口县| 分宜县| 台东县| 凉城县| 德保县| 威宁| 秦皇岛市| 昂仁县| 宿迁市| 闸北区| 天门市| 三台县| 郸城县| 吉首市| 佳木斯市| 沁水县| 承德县| 比如县| 周至县| 贵州省| 菏泽市| 瑞丽市| 湘潭市| 饶阳县| 长春市| 崇阳县| 凯里市| 塔城市| 洪江市| 浑源县| 天台县| 永川市| 高淳县| 马尔康县| 阜南县| 广安市| 读书| 西昌市| 永泰县| 寿宁县| 天镇县|