打飛機(jī)代碼:敵機(jī)發(fā)射子彈
day10_python應(yīng)用打飛機(jī)-01.png
day10_python應(yīng)用打飛機(jī)-02.png
day10_python應(yīng)用打飛機(jī)-03.png
#coding=utf-8
import time
import random
import pygame
from pygame.locals import *
class HeroPlane(object):
def __init__(self,screen):
#設(shè)置飛機(jī)默認(rèn)的位置
self.x = 230
self.y = 600
#設(shè)置要顯示內(nèi)容的窗口
self.screen = screen
self.imageName = "./feiji/hero.gif"
self.image = pygame.image.load(self.imageName).convert()
#用來存儲(chǔ)英雄飛機(jī)發(fā)射的所有子彈
self.bulletList = []
def display(self):
#更新飛機(jī)的位置
self.screen.blit(self.image,(self.x,self.y))
#判斷一下子彈的位置是否越界,如果是许帐,那么就要?jiǎng)h除這顆子彈
#
#這種方法會(huì)漏掉很多需要?jiǎng)h除的數(shù)據(jù)
# for i in self.bulletList:
# if i.y<0:
# self.bulletList.remove(i)
#存放需要?jiǎng)h除的對(duì)象信息
needDelItemList = []
for i in self.bulletList:
if i.judge():
needDelItemList.append(i)
for i in needDelItemList:
self.bulletList.remove(i)
# del needDelItemList
#更新及這架飛機(jī)發(fā)射出的所有子彈的位置
for bullet in self.bulletList:
bullet.display()
bullet.move()
#修改所有子彈的位置
# for bullet in self.bulletList:
# bullet.y -= 2
def moveLeft(self):
self.x -= 10
def moveRight(self):
self.x += 10
def sheBullet(self):
newBullet = Bullet(self.x,self.y,self.screen)
self.bulletList.append(newBullet)
class Bullet(object):
def __init__(self,x,y,screen):
self.x = x+40
self.y = y-20
self.screen = screen
self.image = pygame.image.load("./feiji/bullet-3.gif").convert()
def move(self):
self.y -= 2
def display(self):
self.screen.blit(self.image,(self.x,self.y))
def judge(self):
if self.y<0:
return True
else:
return False
class EnemyPlane(object):
def __init__(self,screen):
#設(shè)置飛機(jī)默認(rèn)的位置
self.x = 0
self.y = 0
#設(shè)置要顯示內(nèi)容的窗口
self.screen = screen
self.imageName = "./feiji/enemy-1.gif"
self.image = pygame.image.load(self.imageName).convert()
#用來存儲(chǔ)敵人飛機(jī)發(fā)射的所有子彈
self.bulletList = []
self.direction = "right"
def display(self):
#更新飛機(jī)的位置
self.screen.blit(self.image,(self.x,self.y))
#判斷一下子彈的位置是否越界劳坑,如果是,那么就要?jiǎng)h除這顆子彈
#
#這種方法會(huì)漏掉很多需要?jiǎng)h除的數(shù)據(jù)
# for i in self.bulletList:
# if i.y<0:
# self.bulletList.remove(i)
#存放需要?jiǎng)h除的對(duì)象信息
needDelItemList = []
for i in self.bulletList:
if i.judge():
needDelItemList.append(i)
for i in needDelItemList:
self.bulletList.remove(i)
# del needDelItemList
#更新及這架飛機(jī)發(fā)射出的所有子彈的位置
for bullet in self.bulletList:
bullet.display()
bullet.move()
def move(self):
#如果碰到了右邊的邊界成畦,那么就往左走泡垃,如果碰到了左邊的邊界,那么就往右走
if self.direction == "right":
self.x += 2
elif self.direction == "left":
self.x -= 2
if self.x>480-50:
self.direction = "left"
elif self.x<0:
self.direction = "right"
def sheBullet(self):
num = random.randint(1,100)
if num == 88:
newBullet = EnemyBullet(self.x,self.y,self.screen)
self.bulletList.append(newBullet)
class EnemyBullet(object):
def __init__(self,x,y,screen):
self.x = x+30
self.y = y+30
self.screen = screen
self.image = pygame.image.load("./feiji/bullet-1.gif").convert()
def move(self):
self.y += 2
def display(self):
self.screen.blit(self.image,(self.x,self.y))
def judge(self):
if self.y>890:
return True
else:
return False
if __name__ == "__main__":
#1\. 創(chuàng)建一個(gè)窗口羡鸥,用來顯示內(nèi)容
screen = pygame.display.set_mode((480,890),0,32)
#2\. 創(chuàng)建一個(gè)和窗口大小的圖片蔑穴,用來充當(dāng)背景
background = pygame.image.load("./feiji/background.png").convert()
#3\. 創(chuàng)建一個(gè)飛機(jī)對(duì)象
heroPlane = HeroPlane(screen)
#4\. 創(chuàng)建一個(gè)敵人飛機(jī)
enemyPlane = EnemyPlane(screen)
#3\. 把背景圖片放到窗口中顯示
while True:
screen.blit(background,(0,0))
heroPlane.display()
enemyPlane.move()
enemyPlane.sheBullet()
enemyPlane.display()
#判斷是否是點(diǎn)擊了退出按鈕
for event in pygame.event.get():
# print(event.type)
if event.type == QUIT:
print("exit")
exit()
elif event.type == KEYDOWN:
if event.key == K_a or event.key == K_LEFT:
print('left')
heroPlane.moveLeft()
#控制飛機(jī)讓其向左移動(dòng)
elif event.key == K_d or event.key == K_RIGHT:
print('right')
heroPlane.moveRight()
elif event.key == K_SPACE:
print("space")
heroPlane.sheBullet()
#通過延時(shí)的方式,來降低這個(gè)while循環(huán)的循環(huán)速度惧浴,從而降低了cpu占用率
time.sleep(0.01)
pygame.display.update()
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者