import pygame
from random import randint
import random
pygame.init()
screen = pygame.display.set_mode((480, 700))
done = False
class Plane():
def __init__(self,x,y,imagefile):
self.x=x
self.y=y
image=pygame.image.load(imagefile)
self.icon = pygame.transform.scale(image,(80,80))
def move(self,fx):
if fx=='left':
self.x=self.x-2
elif fx=='right':
self.x=self.x+2
else:
pass
class Bullet():
def __init__(self,x,y,imagefile):
self.x=x
self.y=y
self.icon=image = pygame.image.load(imagefile).convert_alpha()
def move(self):
self.y=self.y-3
screen.blit(self.icon,(self.x,self.y))
def __del__(self):
del self
class Enemy():
def __init__(self,x,y,step,imagefile):
self.x=x
self.y=y
self.step=step
self.icon = pygame.image.load(imagefile).convert_alpha()
def move(self):
self.y=self.y+self.step+0.2
screen.blit(self.icon,(self.x,self.y))
def __del__(self):
del self
def initdestrop():
for i in range(10):
enemy=Enemy(randint(50,430),100,random.random()*2,r'images/enemy1_down2.png')
destroy_list.append(enemy)
import time
myPlane=None
bullet=None
bullet_list=list()
enemy_list=list()
destroy_list=list()
destroy_list=[]
jishi=0
initdestrop()
baozha_sound=pygame.mixer.Sound('mp3/baozha.mp3')
bullet_sound=pygame.mixer.Sound('mp3/bullet.mp3')
for i in range(randint(3,8)):
enemy=Enemy(randint(50,430),100,random.random()*2,r'images/enemy1.png')
enemy_list.append(enemy)
myPlane=Plane(240,600,r'images\me1.png')
while not done:
jishi=jishi+1
if jishi==200:
jishi=0
print('add enemy')
enemy=Enemy(randint(50,430),0,random.random()*2,r'images/enemy1.png')
enemy_list.append(enemy)
image = pygame.image.load(r'images\background.png').convert_alpha()
screen.blit(image,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type==pygame.KEYUP:
if event.key==pygame.K_UP:
bullet_sound.play()
bullet=Bullet(myPlane.x+(myPlane.icon.get_width())/2,myPlane.y-10,r'images\bullet1.png')
bullet_list.append(bullet)
keyvalue = pygame.key.get_pressed()
if keyvalue[pygame.K_LEFT]:
myPlane.move('left')
if keyvalue[pygame.K_RIGHT]:
myPlane.move('right')
for i,item in enumerate(enemy_list):
item.move()
if item.y>650:
print('del enemy')
baozha_sound.play()
for baozhao in destroy_list:
screen.blit(baozhao.icon,(item.x,item.y))
del enemy_list[i]
del item
else:
pass
for i,item in enumerate(bullet_list):
item.move()
if item.y<=0:
print('del')
del bullet_list[i]
del item
else:
for e,enemy in enumerate(enemy_list):
if abs(enemy.x+(enemy.icon.get_width())/2-item.x)<(enemy.icon.get_width()/2+item.icon.get_width()/2) and abs(enemy.y+enemy.icon.get_height()/2-item.y)<(enemy.icon.get_height()/2+item.icon.get_height()/2):
for baozhao in destroy_list:
screen.blit(baozhao.icon,(enemy.x,enemy.y))
baozha_sound.play()
del enemy_list[e]
del e
print('enemy fall')
screen.blit(myPlane.icon,(myPlane.x,myPlane.y))
pygame.display.update()
|