欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python版飛機大戰(zhàn)代碼分享

 更新時間:2018年11月20日 10:45:02   作者:wangbowj123  
這篇文章主要為大家詳細(xì)介紹了python版飛機大戰(zhàn)的實現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

利用pygame實現(xiàn)了簡易版飛機大戰(zhàn)。源代碼如下:

# -*- coding:utf-8 -*-
import pygame
import sys
from pygame.locals import *
from pygame.font import *
import time
import random

class Hero(object):
 #玩家 英雄類
 def __init__(self, screen_temp):
 self.x = 210
 self.y = 700
 self.life = 21
 # self.life = 100
 self.image = pygame.image.load("./feiji/hero1.png")
 self.screen = screen_temp
 self.bullet_list = []#用來存儲子彈對象的引用
 #爆炸效果用的如下屬性
 self.hit = False #表示是否要爆炸
 self.bomb_list = [] #用來存儲爆炸時需要的圖片
 self.__create_images() #調(diào)用這個方法向bomb_list中添加圖片
 self.image_num = 0 #用來記錄while True的次數(shù),當(dāng)次數(shù)達(dá)到一定值時才顯示一張爆炸的圖,然后清空,,當(dāng)這個次數(shù)再次達(dá)到時,再顯示下一個爆炸效果的圖片
 self.image_index = 0#用來記錄當(dāng)前要顯示的爆炸效果的圖片的序號

 def __create_images(self):
 #添加爆炸圖片
 self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n1.png"))
 self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n2.png"))
 self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n3.png"))
 self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n4.png"))

 def display(self):
 #顯示玩家的飛機
 #如果被擊中,就顯示爆炸效果,否則顯示普通的飛機效果
 if self.hit == True:
  self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))#(self.x, self.y)是指當(dāng)前英雄的位置
  #blit方法 (一個對象,左上角位置)
  self.image_num += 1
  print(self.image_num)
  if self.image_num == 7:
  self.image_num = 0
  self.image_index += 1
  print(self.image_index) #這里子彈打住英雄時沒有被清除掉,所以打一下,就死了
  if self.image_index > 3:
  time.sleep(1)
  exit()#調(diào)用exit讓游戲退出
  #self.image_index = 0
 else:
  if self.x< 0: #控制英雄,不讓它跑出界面
  self.x = 0
  elif self.x > 382:
  self.x = 382
  if self.y < 0:
  self.y = 0
  elif self.y > 750:
  self.y = 750
  self.screen.blit(self.image,(self.x, self.y))#z這里是只要沒有被打中,就一直是剛開始的樣子

 #不管玩家飛機是否被擊中,都要顯示發(fā)射出去的子彈
 for bullet in self.bullet_list:
  bullet.display()
  bullet.move()

 def move(self, move_x,move_y):
 self.x += move_x
 self.y += move_y

 def fire(self):
 #通過創(chuàng)建一個子彈對象,完成發(fā)射子彈
 bullet = Bullet(self.screen, self.x, self.y)#創(chuàng)建一個子彈對象
 self.bullet_list.append(bullet)

 def bomb(self):
 self.hit = True

 def judge(self):
 global life
 if life <= 0:
  self.bomb()

class Bullet(object):
 #玩家子彈類
 def __init__(self, screen_temp, x_temp, y_temp):
 self.x = x_temp + 40
 self.y = y_temp - 20
 self.image = pygame.image.load("./feiji/bullet.png")
 self.screen = screen_temp

 def display(self):
 self.screen.blit(self.image, (self.x, self.y))

 def move(self):
 self.y -= 10

class Bullet_Enemy(object):
 #敵機子彈類
 def __init__(self, screen_temp, x_temp, y_temp):
 self.x = x_temp + 25
 self.y = y_temp + 30
 self.image = pygame.image.load("./feiji/bullet1.png")
 self.screen = screen_temp

 def display(self):
 self.screen.blit(self.image,(self.x,self.y))

 def move(self, hero):
 self.y += 10
 global life
 if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
 #if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):
  life -= 10
  #self.bullet_list.remove()
  print("---judge_enemy---")
  return True
  if life<=0:
  hero.bomb()
 return False

class Bullet_Boss(object):
 #boss子彈類1
 def __init__(self, screen_temp, x_temp, y_temp):
 self.x = x_temp + 80
 self.y = y_temp + 230
 self.image = pygame.image.load("./feiji/bullet2.png")
 self.screen = screen_temp

 def display(self):
 self.screen.blit(self.image, (self.x, self.y))

 def move(self, hero):
 self.y += 6
 self.x += 2
 global life
 if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
 #if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):
  life -= 20
  #self.bullet_list.remove()
  print("---judge_boss---")
  return True
  if life<=0:
  hero.bomb()
 return False

class Bullet_Boss1(object):
 #boss子彈類2
 def __init__(self, screen_temp, x_temp, y_temp):
 self.x = x_temp + 80
 self.y = y_temp + 230
 self.image = pygame.image.load("./feiji/bullet2.png")
 self.screen = screen_temp

 def display(self):
 self.screen.blit(self.image, (self.x, self.y))

 def move(self, hero):
 self.y += 6
 self.x -= 2
 global life
 if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
 #if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):
  life -= 20
  #self.bullet_list.remove()
  print("---judge_boss---")
  return True
  if life<=0:
  hero.bomb()
 return False

class Bullet_Boss2(object):
 #boss子彈類3
 def __init__(self, screen_temp, x_temp, y_temp):
 self.x = x_temp + 80
 self.y = y_temp + 230
 self.image = pygame.image.load("./feiji/bullet2.png")
 self.screen = screen_temp

 def display(self):
 self.screen.blit(self.image, (self.x, self.y))

 def move(self, hero):
 self.y += 6
 global life
 if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
 #if self.y in range(hero.y, hero.y + 40) and self.x in range(hero.x, hero.x + 40):
  life -= 20 
  #self.bullet_list.remove()
  print("---judge_boss---")
  return True
  if life<=0:
  hero.bomb()
 return False

class Base(object):
 #基類 類似于抽象類
 def __init__(self, screen_temp, x, y, image_name):
 self.x = x
 self.y = y 
 self.screen = screen_temp
 self.image = pygame.image.load(image_name)
 self.alive = True

 def display(self):
 if self.alive == True:
  self.screen.blit(self.image, (self.x, self.y))

 def move(self):
 self.y += 5

class bomb_bullet(Base):
 #炸彈類
 def __init__(self, screen_temp):
 Base.__init__(self, screen_temp, random.randint(45, 400), 0, "./feiji/bomb.png")

 def judge(self, hero):
 if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
  self.alive = False
  hero.bomb()

 if self.y >= 850:
  #self.alive = False
  self.y = 0
  self.x = random.randint(45, 400)
  #print("bomb.y = %d"%self.y)

class supply(Base):
 #補給類
 def __init__(self, screen_temp):
 Base.__init__(self, screen_temp, random.randint(45, 400), -300, "./feiji/bomb-1.gif")

 def judge(self, hero):
 global life
 if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
  self.alive = False
  life += 10

 if self.y >= 1500:
  self.y = 0
  self.x = random.randint(45, 400)
  self.alive = True

class clear_bullet(Base):
 def __init__(self, screen_temp):
 Base.__init__(self, screen_temp, random.randint(45, 400), 0, "./feiji/bomb-2.gif")
 self.alive = False

 def judge(self, hero, enemies):
 global q
 q += 1
 #self.move()
 if q == 20:
  #self.move()
  self.alive = True
  q = 0
  if (hero.y <= self.y and self.y <= hero.y + 40) and (hero.x <= self.x and self.x <= hero.x + 100):
  self.alive = False
  for enemy in enemies:
   enemy.hit == True


class EnemyPlane(object):
 #敵機類
 def __init__(self, screen_temp):
 self.x = random.randint(15, 480)
 self.y = 0
 self.image = pygame.image.load("./feiji/enemy0.png")
 self.screen = screen_temp
 self.bullet_list = []#用來存儲子彈對象的引用
 #self.direction = "right"#用來設(shè)置這個飛機默認(rèn)的移動方向
 self.hit = False
 self.bomb_list = []
 self.__create_images()
 self.image_num = 0
 self.image_index = 0
 #利用產(chǎn)生的隨機數(shù),隨機確定飛機初始移動方向
 self.k = random.randint(1, 20)
 if self.k <= 10:
  self.direction = "right"
 elif self.k > 10:
  self.direction = "left"

 def display(self, hero):
 #顯示敵人的飛機
 if not self.hit:
  self.screen.blit(self.image, (self.x,self.y))
 else:
  self.screen.blit(self.bomb_list[self.image_index], (self.x,self.y))
  self.image_num += 1
  if self.image_num == 3 and self.image_index < 3:
  self.image_num = 0
  self.image_index += 1
  #print(self.image_index)
  # if self.image_index > 2:
  # time.sleep(0.1)

 for bullet in self.bullet_list:
  bullet.display()
  if(bullet.move(hero)):
  self.bullet_list.remove(bullet)

 def move(self):
 #利用隨機數(shù)來控制飛機移動距離,以及移動范圍
 d1 = random.uniform(1,3)
 d2 = random.uniform(0.2,3)
 p1 = random.uniform(50,100)
 p2 = random.uniform(-200,0)
 if self.direction == "right":
  self.x += d1
 elif self.direction == "left":
  self.x -= d1

 if self.x > 480 - p1:
  #480 - 50
  self.direction="left"
 elif self.x < p2:
  self.direction = "right"
 self.y += d2

 def bomb(self):
 self.hit = True

 def __create_images(self):
 self.bomb_list.append(pygame.image.load("./feiji/enemy0_down1.png"))
 self.bomb_list.append(pygame.image.load("./feiji/enemy0_down2.png"))
 self.bomb_list.append(pygame.image.load("./feiji/enemy0_down3.png"))
 self.bomb_list.append(pygame.image.load("./feiji/enemy0_down4.png"))

 def fire(self):
 #利用隨機數(shù)來控制敵機的開火,1/80的概率
 s = random.randint(0,800)
 bullet1 = Bullet_Enemy(self.screen, self.x, self.y)
 if s < 10:
  self.bullet_list.append(bullet1)

class EnemyPlanes(EnemyPlane):
 #敵機群類 繼承自EnemyPlane類 
 def __init__(self, screen_temp):
 EnemyPlane.__init__(self, screen_temp)
 self.num = 0
 self.enemy_list = [] #用列表存儲產(chǎn)生的多架敵機
 self.screen = screen_temp

 def add_enemy(self, num): 
 #產(chǎn)生多架敵機的函數(shù)
 self.num = num
 for i in range(num):
  enemy = EnemyPlane(self.screen)
  self.enemy_list.append(enemy)

 def display(self, hero):
 for i in range(self.num):
  self.enemy_list[i].display(hero)

 def move(self):
 for i in range(self.num): 
  self.enemy_list[i].move()

 def fire(self):
 #s = random.randint(0,1000)
 for i in range(self.num):
  self.enemy_list[i].fire()

class Boss(EnemyPlane):
 #boss敵機類 繼承自EnemyPlane類
 def __init__(self,screen_temp):
 EnemyPlane.__init__(self,screen_temp)
 self.x = 150
 self.y = 0
 self.bomb_list = []
 self.__create_images()
 self.image = pygame.image.load("./feiji/enemy2.png")
 self.screen = screen_temp
 self.bullet_list = []

 def __create_images(self):
 #self.bomb_list.append(pygame.image.load("./feiji/enemy2.png"))
 self.bomb_list.append(pygame.image.load("./feiji/enemy2.png"))
 self.bomb_list.append(pygame.image.load("./feiji/enemy2_down1.png"))
 self.bomb_list.append(pygame.image.load("./feiji/enemy2_down2.png"))
 self.bomb_list.append(pygame.image.load("./feiji/enemy2_down3.png"))
 self.bomb_list.append(pygame.image.load("./feiji/enemy2_down4.png"))
 self.bomb_list.append(pygame.image.load("./feiji/enemy2_down5.png"))
 self.bomb_list.append(pygame.image.load("./feiji/enemy2_down6.png"))

 def display(self, hero):
 #顯示敵人的飛機
 global g
 #print(g)
 self.screen.blit(self.bomb_list[g], (self.x,self.y))
 for bullet in self.bullet_list:
  bullet.display()
  if(bullet.move(hero)):
  self.bullet_list.remove(bullet)

 def move(self):
 d1 = 0
 self.y += 0

 def fire(self):
 global s
 s += 1
 bullet1 = Bullet_Boss(self.screen, self.x, self.y)
 bullet2 = Bullet_Boss1(self.screen, self.x, self.y)
 bullet3 = Bullet_Boss2(self.screen, self.x, self.y)
 if s == 20:
  s = 0 
  self.bullet_list.append(bullet1)
  self.bullet_list.append(bullet2)
  self.bullet_list.append(bullet3)

def judge1(hero,enemy):
 #判斷敵機的炸毀
 for bullet1 in hero.bullet_list:
 if bullet1.y in range(int(enemy.y),int(enemy.y + 30)) and bullet1.x in range(int(enemy.x-10),int(enemy.x + 50)):
  hero.bullet_list.remove(bullet1)
  enemy.bomb()
 if bullet1.y < 0 or bullet1.x < 0 or bullet1.x > 480: #刪除越界的玩家子彈
  hero.bullet_list.remove(bullet1) 

def judge3(hero,boss):
 #判斷boss的炸毀
 global goal, g, goal0
 for bullet3 in hero.bullet_list:
 if bullet3.y in range(int(boss.y), int(boss.y + 60)) and bullet3.x in range(int(boss.x), int(boss.x + 100)):
  hero.bullet_list.remove(bullet3)
  g += 1
  boss.image = boss.bomb_list[g]
  print("g = %d"%g)
  if g >= 6:
  boss.y, g, goal = 0, 0, 0
  boss.bomb()
  goal0 += 10 

def clear_enemy(enemies):
 #清除敵機群類中被炸毀的敵機
 global goal, goal0
 for enemy in enemies.enemy_list:
 if enemy.hit == True and enemy.image_index == 3:
  enemies.enemy_list.remove(enemy)
  enemies.num -= 1
  goal += 1
  goal0 += 5
  print("goal = %d"%goal)
 if enemy.y >= 850:
  enemies.enemy_list.remove(enemy)
  enemies.num -= 1

def judge_num(enemies):
 #判斷頻幕上敵人的數(shù)量,如果為零,繼續(xù)添加敵人
 n = random.randint(1,5)
 if len(enemies.enemy_list) == 0:
 enemies.add_enemy(n)

def show_text(screen_temp):
 #在屏幕上顯示文字
 text = "GOAL:" + str(goal0) + "Life:" + str(life) 
 font_size = 50
 pos = (0,0)
 color = (0,255,0)
 cur_font = pygame.font.SysFont("宋體",font_size)
 text_fmt = cur_font.render(text, 1, color)
 screen_temp.blit(text_fmt, pos)

def creat_bomb(screen_temp):
 bomb = bomb_bullet(screen_temp)
 bomb_list = []
 bomb_list.apend(bomb)

#定義的全局變量
goal = 0 #玩家得分
goal0 = 0
g = 0 #擊中boss的次數(shù)
life = 100#生命值
s = 0 #判斷大boss是否發(fā)射子彈
q = 0

def main():
 #主函數(shù)執(zhí)行
 #獲取事件,比如按鍵等
 bb = False
 move_x = 0
 move_y = 0
 pygame.init()
 screen = pygame.display.set_mode((480,852),0,32)
 #     210,400
 background = pygame.image.load("./feiji/background.png")
 pygame.display.set_caption("飛機大戰(zhàn)")
 atlas = pygame.image.load("./feiji/New Atlas.png")
 #創(chuàng)建玩家飛機
 hero = Hero(screen)
 #創(chuàng)建敵機群
 enemis = EnemyPlanes(screen)
 enemis.add_enemy(5)
 #創(chuàng)建boss對象
 boss = Boss(screen)
 #創(chuàng)建炸彈對象
 bomb = bomb_bullet(screen)
 #創(chuàng)建補給對象
 supply0 = supply(screen)
 clear = clear_bullet(screen)
 left_key, right_key, up_key, down_key, done = 0, 0, 0, 0, 0
 # mark = 0#用來判斷boss發(fā)射子彈
 while True:
 if done:
  if done % 8 == 0:
  done = 1
  hero.fire()
  else:
  done += 1
 for event in pygame.event.get():
  #判斷是否是點擊了退出按鈕
  if event.type == QUIT:
  print("exit")
  exit()
  #判斷是否是按下了鍵
  if event.type == KEYDOWN :
  #down
  #檢測按鍵是否是a或者left

  if event.key == K_a or event.key == K_LEFT:
   #print('left')
   move_x = -5
   left_key += 1 

  #檢測按鍵是否是d或者right
  elif event.key == K_d or event.key == K_RIGHT:
   #print('right')
   move_x = 5
   right_key += 1

  elif event.key == K_w or event.key == K_UP:
   move_y = -5
   up_key += 1

  elif event.key == K_s or event.key == K_DOWN:
   move_y = 5
   down_key += 1

  #檢測按鍵是否是空格鍵
  elif event.key == K_SPACE:
   #print('space')
   hero.fire()
   done = 1
   #enemis.fire()

  elif event.key == K_b:
   print('b')
   hero.bomb()

  if event.type == KEYUP:
  if event.key == K_a or event.key == K_LEFT:
   left_key -= 1
   if right_key == 0:
   move_x = 0
   else:
   move_x = 5

  if event.key == K_d or event.key == K_RIGHT:
   right_key -= 1
   if left_key == 0:
   move_x = 0
   else:
   move_x = -5

  if event.key == K_w or event.key == K_UP:
   up_key -= 1
   if down_key == 0:
   move_y = 0
   else:
   move_y = 5 

  if event.key == K_s or event.key == K_DOWN:
   down_key -= 1
   if up_key == 0:
   move_y = 0
   else:
   move_y = -5

  if event.key == K_SPACE:
   done = 0 

 screen.blit(background, (0, 0))
 hero.move(move_x, move_y)
 hero.display()
 hero.judge()
 enemis.display(hero)
 enemis.move()
 enemis.fire()
 bomb.display()
 bomb.judge(hero)
 bomb.move()
 supply0.display()
 supply0.judge(hero)
 supply0.move()
 #clear.display()
 #clear.judge(hero, enemis)
 #clear.move()
 for i in range(enemis.num):
  judge1(hero, enemis.enemy_list[i])
  #enemis.enemy_list[i].judge(hero)
 clear_enemy(enemis)
 judge_num(enemis)
 show_text(screen)
 if goal >= 15:
  boss.display(hero)
  boss.move()
  # mark+=1
  # if mark==8:
  boss.fire()
  # mark = 0
  #boss.judge
  judge3(hero, boss)
 pygame.display.update()

if __name__ == "__main__":
 main()

方法是利用面向?qū)ο蟮乃枷?,寫了基本的敵機類、英雄類、武器類等,利用繼承關(guān)系產(chǎn)生多架敵機。

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

相關(guān)文章

  • NLTK的安裝教程及安裝錯誤解決方案

    NLTK的安裝教程及安裝錯誤解決方案

    NLTK是一款用于自然語言處理的Python庫,安裝過程簡單易懂,只需要使用pip安裝即可。若安裝過程中出現(xiàn)錯誤,可以通過更新pip、安裝必要的依賴項、更換鏡像源等方式解決。
    2023-04-04
  • centos 自動運行python腳本和配置 Python 定時任務(wù)

    centos 自動運行python腳本和配置 Python 定時任務(wù)

    這篇文章主要介紹了centos 自動運行python腳本和配置 Python 定時任務(wù),文章內(nèi)容介紹詳細(xì),需要的小伙伴可以參考一下,希望對你有所幫助
    2022-03-03
  • python將圖片轉(zhuǎn)為矢量圖的方法步驟

    python將圖片轉(zhuǎn)為矢量圖的方法步驟

    這篇文章主要介紹了python將圖片轉(zhuǎn)為矢量圖的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Python機器學(xué)習(xí)實戰(zhàn)之k-近鄰算法的實現(xiàn)

    Python機器學(xué)習(xí)實戰(zhàn)之k-近鄰算法的實現(xiàn)

    k-近鄰算法采用測量不同特征值之間的距離方法進行分類。這篇文章主要為大家介紹了如何通過python實現(xiàn)K近鄰算法,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • Python時間序列數(shù)據(jù)的預(yù)處理方法總結(jié)

    Python時間序列數(shù)據(jù)的預(yù)處理方法總結(jié)

    這篇文章主要介紹了Python時間序列數(shù)據(jù)的預(yù)處理方法總結(jié),時間序列數(shù)據(jù)隨處可見,要進行時間序列分析,我們必須先對數(shù)據(jù)進行預(yù)處理。時間序列預(yù)處理技術(shù)對數(shù)據(jù)建模的準(zhǔn)確性有重大影響
    2022-07-07
  • python字符串替換第一個字符串的方法

    python字符串替換第一個字符串的方法

    這篇文章主要介紹了python字符串替換第一個字符串的方法,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • Python pypinyin注音庫輕松絲滑實現(xiàn)漢字轉(zhuǎn)換成拼音

    Python pypinyin注音庫輕松絲滑實現(xiàn)漢字轉(zhuǎn)換成拼音

    pypinyin 庫,能像功夫熊貓那樣,輕松、快捷地幫你把漢字轉(zhuǎn)換成拼音,有了 pypinyin,不僅可以節(jié)省寶貴的時間,還可以更準(zhǔn)確地展示中文字符的讀音,使文化交流更為順暢,本文帶大家一起探索 pypinyin 庫的魅力
    2024-01-01
  • Python實現(xiàn)深度遍歷和廣度遍歷的方法

    Python實現(xiàn)深度遍歷和廣度遍歷的方法

    今天小編就為大家分享一篇Python實現(xiàn)深度遍歷和廣度遍歷的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python處理鍵映射值操作詳解

    Python處理鍵映射值操作詳解

    這篇文章主要為大家詳細(xì)介紹了Python中的處理鍵映射值操作的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下
    2022-11-11
  • Python多模塊引用由此引發(fā)的相對路徑混亂問題

    Python多模塊引用由此引發(fā)的相對路徑混亂問題

    這篇文章主要介紹了Python多模塊引用由此引發(fā)的相對路徑混亂問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論