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

python pygame模塊編寫(xiě)飛機(jī)大戰(zhàn)

 更新時(shí)間:2018年11月20日 11:17:39   作者:Great__emperor  
這篇文章主要為大家詳細(xì)介紹了python pygame模塊編寫(xiě)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python pygame模塊編寫(xiě)飛機(jī)大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下

該程序沒(méi)有使用精靈組,而是用列表存儲(chǔ)對(duì)象來(lái)替代精靈組的動(dòng)畫(huà)效果。用矩形對(duì)象的重疊來(lái)判斷相撞事件。該程序可以流暢運(yùn)行,注釋較為詳細(xì),希望可以幫助大家。

import pygame
from pygame.locals import *
from sys import exit
import time
import random

# 創(chuàng)建子彈類(lèi),把子彈的圖片轉(zhuǎn)化為圖像對(duì)象,設(shè)定固定的移動(dòng)速度
class Bullet():
 def __init__(self,bulletfilename,bulletpos):
  self.bulletimg = pygame.image.load(bulletfilename)
  self.bullet_rect = self.bulletimg.get_rect()
  self.bullet_image = self.bulletimg.subsurface(self.bullet_rect)
  self.bullet_rect.midbottom = bulletpos
  self.speed = 2
 def move(self):
  self.bullet_rect.top -= self.speed

# 創(chuàng)建玩家飛機(jī)類(lèi),用面向?qū)ο蟮乃枷雭?lái)對(duì)待
class play_plane_fly():
 def __init__(self,play_image_filename,play_pos):
  self.image = pygame.image.load(play_image_filename)
  self.plane_rect = self.image.get_rect()
  self.play_image = self.image.subsurface(self.plane_rect)
  self.plane_rect.midtop = play_pos
  self.speed = 2
  # 子彈是由玩家飛機(jī)發(fā)射的,所以創(chuàng)建列表,存儲(chǔ)子彈對(duì)象,使該列表變?yōu)樵擃?lèi)的屬性
  self.bullets = []
  self.is_hitted = False

 # 生成函數(shù),完成發(fā)射子彈動(dòng)作,同時(shí)將每個(gè)子彈對(duì)象存在列表中
 def shoot(self,bullet_filename):
  bulletobj = Bullet(bullet_filename,self.plane_rect.midtop)
  self.bullets.append(bulletobj)

 # 向上移動(dòng),當(dāng)飛機(jī)移動(dòng)到邊框位置時(shí),無(wú)法移動(dòng)
 def moveup(self):
  if self.plane_rect.top <= 0:
   self.plane_rect.top = 0
  else:
   self.plane_rect.top -= self.speed

 # 向下移動(dòng),當(dāng)飛機(jī)移動(dòng)到邊框位置時(shí),無(wú)法移動(dòng)
 def movedown(self):
  if self.plane_rect.top >= 950 - self.plane_rect.height:
   self.plane_rect.top = 950 - self.plane_rect.height
  else:
   self.plane_rect.top += self.speed

 # 向右移動(dòng),當(dāng)飛機(jī)移動(dòng)到邊框位置時(shí),無(wú)法移動(dòng)
 def moveleft(self):
  if self.plane_rect.left <= -40:
   self.plane_rect.left = -40
  else:
   self.plane_rect.left -= self.speed

 # 向左移動(dòng),當(dāng)飛機(jī)移動(dòng)到邊框位置時(shí),無(wú)法移動(dòng)
 def moveright(self):
  if self.plane_rect.left >= 700 - self.plane_rect.width:
   self.plane_rect.left = 700 - self.plane_rect.width
  else:
   self.plane_rect.left += self.speed

# 生成敵機(jī)類(lèi),設(shè)定固定的移動(dòng)速度
class Enemy():
 def __init__(self,enemyfilename,enemypos):

  self.img = pygame.image.load(enemyfilename)
  self.enemy_rect = self.img.get_rect()
  self.enemy_image = self.img.subsurface(self.enemy_rect)
  self.enemy_rect.midbottom = enemypos
  self.speed = 1

 def move(self):
  self.enemy_rect.bottom += self.speed

clock = pygame.time.Clock()
def main():
 # 初始化文字屏幕
 pygame.font.init()
 # 初始化圖像屏幕
 pygame.init()
 # 設(shè)定游戲幀
 clock.tick(50)
 # 設(shè)定游戲屏幕大小
 screen = pygame.display.set_mode((660,950))
 # 設(shè)定游戲名稱(chēng)
 pygame.display.set_caption('飛機(jī)大戰(zhàn)')
 # 加載背景圖片,生成圖像對(duì)象
 background = pygame.image.load('image/background.png').convert()
 backgroundsurface = pygame.transform.scale(background, (660, 950))
 # 加載游戲結(jié)束圖片,生成圖像對(duì)象
 gameover = pygame.image.load('image/gameover.png').convert()
 gameoversurface = pygame.transform.scale(gameover,(660, 950))
 playplanefilename = 'image/myself.png'
 planepos = [330,600]
 player = play_plane_fly(playplanefilename,planepos)
 bulletfilename = 'image/bullet.png'
 # 按頻率生成子彈,初始化數(shù)字為0
 bullet_frequency = 0
 enemyfilename = 'image/airplane.png'
 # 按頻率生成敵機(jī),初始化數(shù)字為0
 enemy_frequency = 0
 enemys = []
 myfont = pygame.font.SysFont("arial", 40)
 textImage = myfont.render("Score ", True, (0,0,0))
 # 初始化得分為0
 Score = 0
 # 敵機(jī)被子彈擊中時(shí)的動(dòng)畫(huà),將每張圖片的圖像對(duì)象存在列表中
 enenys_down = []
 enemy0_down = pygame.image.load('image/airplane_ember0.png')
 enemy0_down_rect = enemy0_down.get_rect()
 enemydown0 = enemy0_down.subsurface(enemy0_down_rect)
 enenys_down.append(enemydown0)
 enemy1_down = pygame.image.load('image/airplane_ember1.png')
 enemy1_down_rect = enemy1_down.get_rect()
 enemydown1 = enemy1_down.subsurface(enemy1_down_rect)
 enenys_down.append(enemydown1)
 enemy2_down = pygame.image.load('image/airplane_ember2.png')
 enemy2_down_rect = enemy2_down.get_rect()
 enemydown2 = enemy2_down.subsurface(enemy2_down_rect)
 enenys_down.append(enemydown2)
 enemy3_down = pygame.image.load('image/airplane_ember3.png')
 enemy3_down_rect = enemy3_down.get_rect()
 enemydown3 = enemy3_down.subsurface(enemy3_down_rect)
 enenys_down.append(enemydown3)


 while True:
  # 動(dòng)態(tài)顯示得分
  score = str(Score)
  myscore = pygame.font.SysFont("arial", 40)
  scoreImage = myscore.render(score, True, (0, 0, 0))
  # 判斷事件,防止卡頓或者意外退出
  for event in pygame.event.get():
   if event.type == pygame.QUIT:
    pygame.quit()
    exit()
  key_pressed = pygame.key.get_pressed()
  if key_pressed[K_UP] or key_pressed[K_w]:
   player.moveup()
  if key_pressed[K_DOWN] or key_pressed[K_s]:
   player.movedown()
  if key_pressed[K_LEFT] or key_pressed[K_a]:
   player.moveleft()
  if key_pressed[K_RIGHT] or key_pressed[K_d]:
   player.moveright()

  screen.blit(backgroundsurface, (0, 0))

  if not player.is_hitted:
   # 按頻率生成子彈
   if bullet_frequency % 30 == 0:
    player.shoot(bulletfilename)
   bullet_frequency += 1
   if bullet_frequency >= 30:
    bullet_frequency = 0
   # 讓子彈動(dòng)起來(lái)
   for i in player.bullets:
    i.move()
    screen.blit(i.bullet_image,i.bullet_rect)
    # 當(dāng)子彈飛出屏幕,刪除子彈對(duì)象
    if i.bullet_rect.bottom <= 0:
     player.bullets.remove(i)
   # 按頻率生成敵機(jī)
   if enemy_frequency % 100 == 0:
    enemypos = [random.randint(30, 630), 0]
    enemyplane = Enemy(enemyfilename, enemypos)
    #將敵機(jī)對(duì)象添加到列表中
    enemys.append(enemyplane)
   enemy_frequency += 1
   if enemy_frequency >= 100:
    enemy_frequency = 0
   # 讓敵機(jī)動(dòng)起來(lái)
   for i in enemys:
    i.move()
    screen.blit(i.enemy_image,i.enemy_rect)
    # 當(dāng)敵機(jī)飛出屏幕,刪除敵機(jī)對(duì)象
    if i.enemy_rect.bottom >= 950:
     enemys.remove(i)
    # 遍歷子彈對(duì)象,判斷子彈是否擊中敵機(jī)
    for j in player.bullets:
     # 如果擊中,分?jǐn)?shù)增加,同時(shí)移除該子彈和敵機(jī)對(duì)象
     if pygame.Rect.colliderect(j.bullet_rect,i.enemy_rect):
      Score += 100
      enemys.remove(i)
      player.bullets.remove(j)
      for k in enenys_down:
       screen.blit(k,i.enemy_rect)
    # 遍歷敵機(jī)對(duì)象,判斷玩家是否和敵機(jī)相撞
    if pygame.Rect.colliderect(player.plane_rect,i.enemy_rect):
     # 修改is_hitted的值,跳出該層循環(huán)
     player.is_hitted = True
     break


   screen.blit(player.play_image,player.plane_rect)
   screen.blit(textImage, (0,0))
   screen.blit(scoreImage, (110, 0))
   pygame.display.update()
  # 玩家退出時(shí)顯示分?jǐn)?shù)和游戲結(jié)束
  else:
   screen.blit(gameoversurface,(0,0))
   screen.blit(textImage, (0, 0))
   screen.blit(scoreImage, (110, 0))
   pygame.display.update()
   time.sleep(2)
   break

main()

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

相關(guān)文章

  • Python的Tornado框架實(shí)現(xiàn)圖片上傳及圖片大小修改功能

    Python的Tornado框架實(shí)現(xiàn)圖片上傳及圖片大小修改功能

    Tornado是一個(gè)異步的Python Web開(kāi)發(fā)框架,同時(shí)也是一個(gè)優(yōu)秀的異步服務(wù)器開(kāi)發(fā)庫(kù),這里我們將來(lái)講解一下Python的Tornado框架實(shí)現(xiàn)圖片上傳及圖片大小修改功能方面的一些重點(diǎn):
    2016-06-06
  • Python函數(shù)的參數(shù)類(lèi)型和使用技巧詳解

    Python函數(shù)的參數(shù)類(lèi)型和使用技巧詳解

    這篇文章主要介紹了Python函數(shù)的參數(shù)類(lèi)型和使用技巧詳解,函數(shù)指通過(guò)專(zhuān)門(mén)的代碼組織,用來(lái)實(shí)現(xiàn)特定功能的代碼段,具有相對(duì)的獨(dú)立性,可以被其他代碼重復(fù)調(diào)用,需要的朋友可以參考下
    2023-08-08
  • Python讀取文件內(nèi)容的三種常用方式及效率比較

    Python讀取文件內(nèi)容的三種常用方式及效率比較

    這篇文章主要介紹了Python讀取文件內(nèi)容的三種常用方式及效率比較,結(jié)合具體實(shí)例形式給出了三種文件讀取的常見(jiàn)方法并對(duì)比分析了讀取速度,需要的朋友可以參考下
    2017-10-10
  • python 內(nèi)置庫(kù)wsgiref的使用(WSGI基礎(chǔ)入門(mén))

    python 內(nèi)置庫(kù)wsgiref的使用(WSGI基礎(chǔ)入門(mén))

    WSGI(web服務(wù)器網(wǎng)關(guān)接口)主要規(guī)定了服務(wù)器端和應(yīng)用程序之間的接口,即規(guī)定了請(qǐng)求的URL到后臺(tái)處理函數(shù)之間的映射該如何實(shí)現(xiàn)。wsgiref是一個(gè)幫助開(kāi)發(fā)者開(kāi)發(fā)測(cè)試的Python內(nèi)置庫(kù),程序員可以通過(guò)這個(gè)庫(kù)了解WSGI的基本運(yùn)行原理,但是不能把它用在生產(chǎn)環(huán)境上。
    2021-06-06
  • Python實(shí)現(xiàn)繪制M2貨幣供應(yīng)率曲線

    Python實(shí)現(xiàn)繪制M2貨幣供應(yīng)率曲線

    這篇文章主要為大家詳細(xì)介紹了如何利用Python語(yǔ)言實(shí)現(xiàn)繪制M2貨幣供應(yīng)率曲線,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • Python NumPy灰度圖像的壓縮原理講解

    Python NumPy灰度圖像的壓縮原理講解

    在本篇文章里小編給大家整理的是一篇關(guān)于Python NumPy灰度圖像的壓縮原理講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-08-08
  • python實(shí)現(xiàn)在內(nèi)存中讀寫(xiě)str和二進(jìn)制數(shù)據(jù)代碼

    python實(shí)現(xiàn)在內(nèi)存中讀寫(xiě)str和二進(jìn)制數(shù)據(jù)代碼

    這篇文章主要介紹了python實(shí)現(xiàn)在內(nèi)存中讀寫(xiě)str和二進(jìn)制數(shù)據(jù)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • python使用opencv resize圖像不進(jìn)行插值的操作

    python使用opencv resize圖像不進(jìn)行插值的操作

    這篇文章主要介紹了python使用opencv resize圖像不進(jìn)行插值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Jupyter打開(kāi)圖形界面并畫(huà)出正弦函數(shù)圖像實(shí)例

    Jupyter打開(kāi)圖形界面并畫(huà)出正弦函數(shù)圖像實(shí)例

    這篇文章主要介紹了Jupyter打開(kāi)圖形界面并畫(huà)出正弦函數(shù)圖像實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Python與shell的3種交互方式介紹

    Python與shell的3種交互方式介紹

    這篇文章主要介紹了Python與shell的3種交互方式介紹,本文講解了os.system、os.popen、subprocess模塊等3種方法,需要的朋友可以參考下
    2015-04-04

最新評(píng)論

措美县| 阳泉市| 平江县| 杭州市| 子长县| 临城县| 五大连池市| 酒泉市| 团风县| 志丹县| 日土县| 德昌县| 陆河县| 衡东县| 福安市| 阿鲁科尔沁旗| 雷山县| 三台县| 河南省| 宁阳县| 柳州市| 田东县| 江西省| 锡林郭勒盟| 湄潭县| 临猗县| 社旗县| 平安县| 桐梓县| 佛学| 开封县| 西吉县| 揭阳市| 微山县| 龙门县| 长子县| 吉安县| 隆子县| 岐山县| 绥滨县| 泰宁县|