|
|

楼主 |
发表于 2019-9-6 12:51:02
|
显示全部楼层
- import pygame
- from random import *
- class Smallenemy(pygame.sprite.Sprite):
- def __init__(self,bg_size):
- pygame.sprite.Sprite.__init__(self)
- self.image=pygame.image.load('images/enemy1.png').convert_alpha()
- self.destoryimages=[]
- self.destoryimages.extend([\
- pygame.image.load('images/enemy1_down1.png').convert_alpha(),\
- pygame.image.load('images/enemy1_down2.png').convert_alpha(),\
- pygame.image.load('images/enemy1_down3.png').convert_alpha(),\
- pygame.image.load('images/enemy1_down4.png').convert_alpha()])
- self.rect=self.image.get_rect()
- self.width,self.height=bg_size[0],bg_size[1]
- self.speed=2
- self.active=True
- self.rect.left,self.rect.top=\
- randint(0,self.width-self.rect.width),\
- randint(-5*self.height,0)#self.rect.width .自身宽度
- self.mask=pygame.mask.from_surface(self.image)
- def move(self):
- if self.rect.top<self.height:
- self.rect.top+=self.speed
- else:
- self.rest()
- def rest(self):
- self.rect.left,self.rect.top=\
- randint(0,self.width-self.rect.width),\
- randint(-5*self.height,0)
- self.active=True
- class Middleenemy(pygame.sprite.Sprite):
- def __init__(self,bg_size):
- pygame.sprite.Sprite.__init__(self)
- self.image=pygame.image.load('images/enemy2.png').convert_alpha()
- self.destoryimages=[]
- self.destoryimages.extend([\
- pygame.image.load('images/enemy2_down1.png').convert_alpha(),\
- pygame.image.load('images/enemy2_down2.png').convert_alpha(),\
- pygame.image.load('images/enemy2_down3.png').convert_alpha(),\
- pygame.image.load('images/enemy2_down4.png').convert_alpha()])
- self.hitimage= pygame.image.load('images/enemy2_hit.png').convert_alpha()
- self.rect=self.image.get_rect()
- self.width,self.height=bg_size[0],bg_size[1]
- self.speed=1
- self.active=True
- self.rect.left,self.rect.top=\
- randint(0,self.width-self.rect.width),\
- randint(-15*self.height,0)#self.rect.width .自身宽度
- self.mask=pygame.mask.from_surface(self.image)
- def move(self):
- if self.rect.top<self.height:
- self.rect.top+=self.speed
- else:
- self.rest()
- def rest(self):
- self.rect.left,self.rect.top=\
- randint(0,self.width-self.rect.width),\
- randit(-50*self.height,0)
- self.active=True
-
-
-
- class Bigenemy(pygame.sprite.Sprite):
- def __init__(self,bg_size):
- pygame.sprite.Sprite.__init__(self)
- self.image1=pygame.image.load('images/enemy3_n1.png').convert_alpha()
- self.image2=pygame.image.load('images/enemy3_n2.png').convert_alpha()
- self.destoryimages=[]
- self.destoryimages.extend([\
- pygame.image.load('images/enemy3_down1.png').convert_alpha(),\
- pygame.image.load('images/enemy3_down2.png').convert_alpha(),\
- pygame.image.load('images/enemy3_down3.png').convert_alpha(),\
- pygame.image.load('images/enemy3_down4.png').convert_alpha(),\
- pygame.image.load('images/enemy3_down5.png').convert_alpha(),\
- pygame.image.load('images/enemy3_down6.png').convert_alpha()])
- self.hitimage= pygame.image.load('images/enemy3_hit.png').convert_alpha()
- self.rect=self.image1.get_rect()
- self.width,self.height=bg_size[0],bg_size[1]
- self.speed=1
- self.active=True
- self.rect.left,self.rect.top=\
- randint(0,self.width-self.rect.width),\
- randint(-5*self.height,0)#self.rect.width .自身宽度
- self.mask=pygame.mask.from_surface(self.image1)
- def move(self):
- if self.rect.top<self.height:
- self.rect.top+=self.speed
- else:
- self.rest()
- def rest(self):
- self.rect.left,self.rect.top=\
- randint(0,self.width-self.rect.width),\
- randint(-5*self.height,0)
- self.active=True
复制代码 |
|