飞机大战

飞机大战1
import pygame
from pygame.locals import *
import os,random,time
def getPath(path):
return os.path.join(“C:\Users\Administrator\Desktop\IT研究院-Python\New_Stydy\img”,path)
class Biu():
def init(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
self.image=pygame.image.load(getPath(“bullet.png”))
def draw(self):
self.windows.blit(self.image,(self.x,self.y))
self.move()
def move(self):
self.y-=5
class HeroPlane():
def init(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
self.height=124
self.width=100
self.normalImageList=[“hero1.png”,“hero2.png”]
self.normalImageIndex=0
self.bombImageList=[“hero_blowup_n1.png”,“hero_blowup_n2.png”,“hero_blowup_n3.png”,“hero_blowup_n4.png”]
self.bombImageIndex=0
self.isBomb=False
self.biuList=[]
def draw(self):
if not self.isBomb:
image=pygame.image.load(getPath(self.normalImageList[self.normalImageIndex]))
self.normalImageIndex=(self.normalImageIndex+1)%len(self.normalImageList)
self.windows.blit(image,(self.x,self.y))
else:
if len(self.bombImageList)self.bombImageIndex:
time.sleep(0.5)
exit(0)
image=pygame.image.load(getPath(self.bombImageList[self.bombImageIndex]))
self.bombImageIndex+=1
self.windows.blit(image,(self.x,self.y))
time.sleep(0.3)
for zd in self.biuList:
zd.draw()
def dealEvent(self,eventList):
for event in eventList:
if event.type
QUIT:
exit(0)
elif event.typeKEYDOWN:
if event.key
K_LEFT:
self.x=self.x-5 if self.x>5 else 0
elif event.keyK_RIGHT:
self.x=self.x+5 if self.x<375 else 380
elif event.key == K_UP:
self.y=self.y-5 if self.y>5 else 0
elif event.key == K_DOWN:
self.y=self.y+5 if self.y<523 else 528
elif event.key
K_SPACE:
zd = Biu(self.x + 100 // 2 - 22 // 2, self.y - 22, windows)
self.biuList.append(zd)
def pzjc(self, bList):
eRect = Rect(self.x, self.y, 100, 124)
for zd in bList:
zdRect = Rect(zd.x, zd.y, 9, 21)
if zdRect.colliderect(eRect):
self.isBomb = True
bList.remove(zd)
class EnemyBiu():
def init(self, x, y, windows):
self.x = x
self.y = y
self.windows = windows
self.image = pygame.image.load(getPath(“bullet1.png”))
def draw(self):
self.windows.blit(self.image, (self.x, self.y))
self.move()
def move(self):
self.y += 5
class EnemyPlane():
def init(self, x, y, windows):
self.x = x
self.y = y
self.windows = windows
self.height = 89
self.width = 69
self.normalImageList = [“enemy1.png”]
self.normalImageIndex = 0
self.biuList = []
self.bombImageList = [“enemy1_down1.png”, “enemy1_down2.png”, “enemy1_down3.png”,
“enemy1_down4.png”]
self.bombImageIndex = 0
self.isBomb = False
self.direct = “左”
def draw(self):
if not self.isBomb:
image = pygame.image.load(getPath(self.normalImageList[self.normalImageIndex]))
self.normalImageIndex = (self.normalImageIndex + 1) % len(self.normalImageList)
self.windows.blit(image, (self.x, self.y))
for zd in self.biuList:
zd.draw()
self.move()
self.fire()
else:
if len(self.bombImageList) == self.bombImageIndex:
time.sleep(0.5)
exit(0)
image = pygame.image.load(getPath(self.bombImageList[self.bombImageIndex]))
self.bombImageIndex += 1
self.windows.blit(image, (self.x, self.y))
time.sleep(0.3)
def move(self):
if self.direct == “左”:
self.x -= 2
if self.x <= 0:
self.direct = “右”
else:
self.x += 2
if self.x >= 480 - 69:
self.direct = “左”
def fire(self):
x = random.randint(1, 100)
if x == 3 or x == 67:
zd = EnemyBiu(self.x + 69 // 2 - 9 // 2, self.y + 89, self.windows)
self.biuList.append(zd)
def pzjc(self, bList):
eRect = Rect(self.x, self.y, 69, 89)
for zd in bList:
zdRect = Rect(zd.x, zd.y, 22, 22)
if zdRect.colliderect(eRect):
self.isBomb = True
bList.remove(zd)
windows = pygame.display.set_mode((480, 652), 0, 32)
pygame.display.set_caption(“飞机大战”)
backGround = pygame.image.load(getPath(“background.png”))
icon = pygame.image.load(getPath(“icon72x72.png”))
pygame.display.set_icon(icon)
pygame.key.set_repeat(20, 20)
heroPlane = HeroPlane(480 // 2 - 100 // 2, 652 - 124, windows)
enemyPlane = EnemyPlane(480 // 2 - 69 // 2, 0, windows)
while True:
windows.blit(backGround, (0, 0))
heroPlane.draw()
enemyPlane.draw()
enemyPlane.pzjc(heroPlane.biuList)
heroPlane.pzjc(enemyPlane.biuList)
heroPlane.dealEvent(pygame.event.get())
pygame.display.update()
飞机大战2
import pygame
from pygame.locals import *
import os,random,time
def getPath(path):
return os.path.join(“C:\Users\Administrator\Desktop\IT研究院-Python\New_Stydy\img”,path)
class Zb():
def init(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
class Zd(Zb):
def draw(self):
self.windows.blit(self.image, (self.x, self.y))
self.move()
class Plane(Zb):
def init(self,x,y,windows):
super().init(x,y,windows)
self.normalImageIndex = 0
self.bombImageIndex = 0
self.isBomb = False
self.biuList = []
def draw(self):
if not self.isBomb:
image = pygame.image.load(getPath(self.normalImageList[self.normalImageIndex]))
self.normalImageIndex = (self.normalImageIndex + 1) % len(self.normalImageList)
self.windows.blit(image, (self.x, self.y))
else:
if len(self.bombImageList) == self.bombImageIndex:
time.sleep(0.5)
exit(0)
image = pygame.image.load(getPath(self.bombImageList[self.bombImageIndex]))
self.bombImageIndex += 1
self.windows.blit(image, (self.x, self.y))
time.sleep(0.3)
class Biu(Zd):
def init(self, x, y, windows):
super().init(x,y,windows)
self.image=pygame.image.load(getPath(“bullet.png”))
def move(self):
self.y-=5
class EnemyBiu(Zd):
def init(self, x, y, windows):
super().init(x, y, windows)
self.image = pygame.image.load(getPath(“bullet1.png”))
def move(self):
self.y += 5
class HeroPlane(Plane):
def init(self,x,y,windows):
super().init(x,y,windows)
self.normalImageList=[“hero1.png”,“hero2.png”]
self.bombImageList=[“hero_blowup_n1.png”,“hero_blowup_n2.png”,“hero_blowup_n3.png”,“hero_blowup_n4.png”]
def draw(self):
super().draw()
for zd in self.biuList:
zd.draw()
def dealEvent(self,eventList):
for event in eventList:
if event.typeQUIT:
exit(0)
elif event.type
KEYDOWN:
if event.keyK_LEFT:
self.x=self.x-5 if self.x>5 else 0
elif event.key
K_RIGHT:
self.x=self.x+5 if self.x<375 else 380
elif event.key == K_UP:
self.y=self.y-5 if self.y>5 else 0
elif event.key == K_DOWN:
self.y=self.y+5 if self.y<523 else 528
elif event.key==K_SPACE:
zd = Biu(self.x + 100 // 2 - 22 // 2, self.y - 22, windows)
self.biuList.append(zd)
def pzjc(self, bList):
eRect = Rect(self.x, self.y, 100, 124)
for zd in bList:
zdRect = Rect(zd.x, zd.y, 9, 21)
if zdRect.colliderect(eRect):
self.isBomb = True
bList.remove(zd)
class EnemyPlane(Plane):
def init(self, x, y, windows):
super().init(x, y, windows)
self.normalImageList = [“enemy1.png”]
self.bombImageList = [“enemy1_down1.png”, “enemy1_down2.png”, “enemy1_down3.png”,“enemy1_down4.png”]
self.direct = “左”
def draw(self):
super().draw()
if not self.isBomb:
self.move()
self.fire()
for zd in self.biuList:
zd.draw()
self.biuList.remove(zd) if zd.y>652 else “”
def move(self):
if self.direct == “左”:
self.x -= 2
if self.x <= 0:
self.direct = “右”
else:
self.x += 2
if self.x >= 480 - 69:
self.direct = “左”
def fire(self):
x = random.randint(1, 100)
if x == 3 or x == 67:
zd = EnemyBiu(self.x + 69 // 2 - 9 // 2, self.y + 89, self.windows)
self.biuList.append(zd)
def pzjc(self, bList):
eRect = Rect(self.x, self.y, 69, 89)
for zd in bList:
zdRect = Rect(zd.x, zd.y, 22, 22)
if zdRect.colliderect(eRect):
self.isBomb = True
bList.remove(zd)
windows = pygame.display.set_mode((480, 652), 0, 32)
pygame.display.set_caption(“飞机大战”)
backGround = pygame.image.load(getPath(“background.png”))
icon = pygame.image.load(getPath(“icon72x72.png”))
pygame.display.set_icon(icon)
pygame.key.set_repeat(20, 20)
heroPlane = HeroPlane(480 // 2 - 100 // 2, 652 - 124, windows)
enemyPlane = EnemyPlane(480 // 2 - 69 // 2, 0, windows)
while True:
windows.blit(backGround, (0, 0))
heroPlane.draw()
enemyPlane.draw()
enemyPlane.pzjc(heroPlane.biuList)
heroPlane.pzjc(enemyPlane.biuList)
heroPlane.dealEvent(pygame.event.get())
pygame.display.update()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值