import pygame
import sys
import random
class zhuji(pygame.sprite.DirtySprite):
def __init__(self,image,locatian):
pygame.sprite.DirtySprite.__init__(self)
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.rect.topleft = locatian
class enemy(pygame.sprite.DirtySprite):
def __init__(self,image,locatian):
pygame.sprite.DirtySprite.__init__(self)
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.rect.topleft = locatian
def update(self):
self.rect.topleft = (self.rect.topleft[0],self.rect.topleft[1]+1)
root.blit(self.image,self.rect)
if self.rect.topleft[1]>650:
self.kill()
class bullet(pygame.sprite.DirtySprite):
def __init__(self,image,locatian):
pygame.sprite.DirtySprite.__init__(self)
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.rect.topleft = locatian
def update(self):
self.rect.topleft = (self.rect.topleft[0],self.rect.topleft[1]-2)
root.blit(self.image,self.rect)
if self.rect.topleft[1]<0:
self.kill()
bb = pygame.sprite.Group()
ee = pygame.sprite.Group()
po = (150,520)
po2 =(0,0)
speed = 0.0
k=0
pygame.init()
root = pygame.display.set_mode((400,650))
bg = pygame.image.load("background.jpeg")
plane = zhuji("AI改图-plane-100x102.png",po)
plane_enemy = enemy("plane-enemy.png",po2)
ee.add(plane_enemy)
def update():
root.blit(bg,(0,0))
root.blit(plane.image,po)
bb.update()
ee.update()
pygame.display.update()
pygame.display.update()
c=pygame.time.Clock()
while 1:
c.tick(300)
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_a:
speed=-1
#po = (po[0]-10,po[1])
if e.key == pygame.K_d:
speed=1
#po = (po[0] + 10, po[1])
if e.key == pygame.K_SPACE:
b = bullet("bullet.png", (po[0]+43,po[1]))
root.blit(b.image, b.rect)
bb.add(b)
if e.type==pygame.KEYUP:
speed=0
po = (po[0] + speed, po[1])
if k == 400:
po2 = (random.randint(0,400),0)
plane_enemy = enemy("plane-enemy.png", po2)
ee.add(plane_enemy)
k=0
k+=1
pygame.sprite.groupcollide(ee,bb,True,True)
XY = pygame.sprite.spritecollideany(plane,ee)
if XY:
root.blit(bg,(0,0))
f = pygame.font.Font(None,40)
xy = f.render("GAME OVER",True,(255,0,0))
root.blit(xy,(110,325))
pygame.display.update()
else:
update()
61234