日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python飞机大战+图片

發(fā)布時間:2023/12/9 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python飞机大战+图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

???????

?

# coding:utf-8 import pygame, sys, random, time, easygui from pygame.locals import * # 初始化pygame環(huán)境 pygame.init()# 創(chuàng)建一個長寬分別為480/650窗口 canvas = pygame.display.set_mode((480, 650)) canvas.fill((255, 255, 255))# 設置窗口標題 pygame.display.set_caption("飛機大戰(zhàn)") bg = pygame.image.load("images/bg1.png") enemy1 = pygame.image.load("images/enemy1.png") enemy2 = pygame.image.load("images/enemy2.png") enemy3 = pygame.image.load("images/enemy3.png") b = pygame.image.load("images/bullet1.png") h = pygame.image.load("images/hero.png") #開始游戲圖片 startgame=pygame.image.load("images/startGame.png") #logo圖片 logo=pygame.image.load("images/LOGO.png") #暫停圖片 pause = pygame.image.load("images/game_pause_nor.png")# 添加時間間隔的方法 def isActionTime(lastTime, interval):if lastTime == 0:return TruecurrentTime = time.time()return currentTime - lastTime >= interval# 定義Sky類 class Sky():def __init__(self):self.width = 480self.height = 852self.img = bgself.x1 = 0self.y1 = 0self.x2 = 0self.y2 = -self.height# 創(chuàng)建paint方法def paint(self):canvas.blit(self.img, (self.x1, self.y1))canvas.blit(self.img, (self.x2, self.y2))# 創(chuàng)建step方法def step(self):self.y1 = self.y1 + 1self.y2 = self.y2 + 1if self.y1 > self.height:self.y1 = -self.heightif self.y2 > self.height:self.y2 = -self.height# 定義父類FlyingObject class FlyingObject(object):def __init__(self, x, y, width, height, life, img):self.x = xself.y = yself.width = widthself.height = heightself.life = lifeself.img = img# 敵飛機移動的時間間隔self.lastTime = 0self.interval = 0.01# 添加刪除屬性self.canDelete = False# 定義paint方法def paint(self):canvas.blit(self.img, (self.x, self.y))# 定義step方法def step(self):# 判斷是否到了移動的時間間隔if not isActionTime(self.lastTime, self.interval):returnself.lastTime = time.time()# 控制移動速度self.y = self.y + 2# 定義hit方法判斷兩個對象之間是否發(fā)生碰撞def hit(self, component):c = componentreturn c.x > self.x - c.width and c.x < self.x + self.width and \c.y > self.y - c.height and c.y < self.y + self.height# 定義bang方法處理對象之間碰撞后的處理def bang(self, bangsign):# 敵機和英雄機碰撞之后的處理if bangsign:if hasattr(self, 'score'):GameVar.score += self.scoreif bangsign == 2:self.life -= 1# 設置刪除屬性為Trueself.canDelete = True# 敵機和子彈碰撞之后的處理else:self.life -= 1if self.life == 0:# 設置刪除屬性為Trueself.canDelete = Trueif hasattr(self, 'score'):GameVar.score += self.score # 定義outOfBounds方法判斷對象是否越界def outOfBounds(self):return self.y > 650# 重構Enemy類 class Enemy(FlyingObject):def __init__(self, x, y, width, height, type, life, score, img):FlyingObject.__init__(self, x, y, width, height, life, img)self.type = typeself.score = score# 重構Hero類 class Hero(FlyingObject):def __init__(self, x, y, width, height, life, img):FlyingObject.__init__(self, x, y, width, height, life, img)self.x = 480 / 2 - self.width / 2self.y = 650 - self.height - 30self.shootLastTime = 0self.shootInterval = 0def shoot(self):if not isActionTime(self.shootLastTime, self.shootInterval):returnself.shootLastTime = time.time()GameVar.bullets.append(Bullet(self.x + self.width / 2 - 5, self.y - 10, 10, 10, 1, b))# 重構Bullet類 class Bullet(FlyingObject):def __init__(self, x, y, width, height, life, img):FlyingObject.__init__(self, x, y, width, height, life, img)def step(self):self.y = self.y - 2# 重寫outOfBounds方法判斷子彈是否越界def outOfBounds(self):return self.y < -self.height# 創(chuàng)建componentEnter方法 def componentEnter():# 隨機生成坐標x = random.randint(0, 480 - 57)x1 = random.randint(0, 480 - 50)x2 = random.randint(0, 480 - 100)# 根據(jù)隨機整數(shù)的值生成不同的敵飛機n = random.randint(0, 9)# 判斷是否到了產(chǎn)生敵飛機的時間if not isActionTime(GameVar.lastTime, GameVar.interval):returnGameVar.lastTime = time.time()if n <= 7:GameVar.enemies.append(Enemy(x, 0, 57, 45, 1, 1, 1, enemy1))elif n == 8:GameVar.enemies.append(Enemy(x1, 0, 50, 68, 2, 3, 5, enemy2))elif n == 9: if len(GameVar.enemies) == 0 or GameVar.enemies[0].type != 3: GameVar.enemies.insert(0, Enemy(x2, 0, 100, 153, 3, 10, 20, enemy3))# 創(chuàng)建畫組件方法 def componentPaint():# 判斷是否到了飛行物重繪的時間if not isActionTime(GameVar.paintLastTime, GameVar.paintInterval):returnGameVar.paintLastTime = time.time()# 調用sky對象的paint方法GameVar.sky.paint()for enemy in GameVar.enemies:enemy.paint()# 畫出英雄機GameVar.hero.paint()# 畫出子彈對象for bullet in GameVar.bullets:bullet.paint()# 寫出分數(shù)和生命值fillText('SCORE:' + str(GameVar.score), (0, 0))fillText('LIFE:' + str(GameVar.heroes), (380, 0))# 創(chuàng)建組件移動的方法 def componentStep():# 調用sky對象的step方法GameVar.sky.step()for enemy in GameVar.enemies:enemy.step()# 使子彈移動for bullet in GameVar.bullets:bullet.step()# 創(chuàng)建刪除組件的方法 def componentDelete():for enemy in GameVar.enemies:if enemy.canDelete or enemy.outOfBounds():GameVar.enemies.remove(enemy)for bullet in GameVar.bullets:if bullet.canDelete or bullet.outOfBounds():GameVar.bullets.remove(bullet)# 從列表中刪除英雄機if GameVar.hero.canDelete == True:GameVar.heroes -= 1if GameVar.heroes == 0:easygui.msgbox('游戲結束')else:GameVar.hero = Hero(0, 0, 60, 75, 1, h)# 定義GameVar類 class GameVar():sky = Sky()enemies = []# 產(chǎn)生敵飛機的時間間隔lastTime = 0interval = 1# 重繪飛行物的時間間隔paintLastTime = 0paintInterval = 0.01# 創(chuàng)建英雄機對象hero = Hero(0, 0, 60, 75, 1, h)# 創(chuàng)建列表存儲子彈對象bullets = []# 添加分數(shù)和生命值score = 0heroes = 3#創(chuàng)建字典存儲游戲狀態(tài)STATES = {'START':1,'RUNNING':2,'PAUSE':3,'GAME_OVER':4}state = STATES['START'] print(GameVar.state)# 定義fillText方法 def fillText(text, position):my_font = pygame.font.SysFont("微軟雅黑", 40)newText = my_font.render(text, True, (255, 255, 255))canvas.blit(newText, position)# 創(chuàng)建游戲退出事件處理方法 def handleEvent():for event in pygame.event.get():if event.type == pygame.QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:pygame.quit()sys.exit() if event.type==MOUSEBUTTONDOWN and event.button==1:if GameVar.state==GameVar.STATES['START']:GameVar.state=GameVar.STATES['RUNNING']# 英雄機跟隨鼠標移動if event.type == MOUSEMOTION:GameVar.hero.x = event.pos[0] - GameVar.hero.width / 2GameVar.hero.y = event.pos[1] - GameVar.hero.height / 2 # 創(chuàng)建方法判斷鼠標移出畫布 def isMouseOut(x, y):if x >= 479 or x <= 0 or y >= 649 or y <= 0:return Trueelse:return False # 創(chuàng)建方法判斷鼠標移入畫布 def isMouseOver(x, y):if x > 1 and x < 479 and y > 1 and y < 649:return Trueelse:return False# 創(chuàng)建checkHit方法 def checkHit():# 判斷英雄機是否與每一架敵飛機發(fā)生碰撞for enemy in GameVar.enemies:if GameVar.hero.hit(enemy):# 敵機和英雄機調用bang方法enemy.bang(1)GameVar.hero.bang(2)# 判斷每一架敵飛機是否與每一顆子彈發(fā)生碰撞for bullet in GameVar.bullets:if enemy.hit(bullet):# 敵機和子彈調用bang方法enemy.bang(0)bullet.bang(0)#創(chuàng)建controlState方法控制游戲狀態(tài) def controlState():#游戲開始狀態(tài)if GameVar.state == GameVar.STATES['START']:GameVar.sky.paint()GameVar.sky.step()canvas.blit(logo,(-40,200))canvas.blit(startgame,(150,400))#游戲運行狀態(tài)elif GameVar.state == GameVar.STATES['RUNNING']:componentEnter()componentPaint()componentStep()checkHit()GameVar.hero.shoot()componentDelete()#游戲暫停狀態(tài)elif GameVar.state == GameVar.STATES['PAUSE']:componentPaint()GameVar.sky.step()canvas.blit(pause,(0,0))#游戲結束狀態(tài)elif GameVar.state == GameVar.STATES['GAME_OVER']:componentPaint()GameVar.sky.step()renderText('gameOver',(180,320))while True:#調用控制游戲狀態(tài)的方法controlState()# 刷新屏幕pygame.display.update()# 調用handleEvent方法handleEvent()# 延遲處理pygame.time.delay(15)

總結

以上是生活随笔為你收集整理的Python飞机大战+图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。