153 lines
2.0 KiB
Plaintext
153 lines
2.0 KiB
Plaintext
import random
|
|
|
|
class spaceInvader(pygame.sprite.Sprite):
|
|
|
|
def __init__(self):
|
|
|
|
self.image = pygame.image.load("spaceInvader.png")
|
|
|
|
self.x = 200
|
|
|
|
self.y = 390
|
|
|
|
self.shots = []
|
|
|
|
def handle_keys(self):
|
|
|
|
key = pygame.key.get_pressed()
|
|
|
|
dist = 5
|
|
|
|
if key[pygame.K_RIGHT]:
|
|
|
|
self.x+=dist
|
|
|
|
elif key[pygame.K_LEFT]:
|
|
|
|
self.x-=dist
|
|
|
|
def draw(self, surface):
|
|
|
|
surface.blit(self.image,(self.x,self.y))
|
|
|
|
for s in self.shots:
|
|
|
|
s.draw(screen)
|
|
|
|
class Alien(pygame.sprite.Sprite):
|
|
|
|
def __init__(self,x,y,direction,alienType):
|
|
|
|
pygame.sprite.Sprite.__init__(self)
|
|
|
|
self.AlienType = alienType
|
|
|
|
self.Direction = direction
|
|
|
|
if alienType == 1:
|
|
|
|
alienImage = pygame.image.load("alien1.png")
|
|
|
|
self.Speed = 1
|
|
|
|
self.Score = 5
|
|
|
|
if alienType == 2:
|
|
|
|
alienImage = pygame.image.load("alien2.png")
|
|
|
|
self.Score = 15
|
|
|
|
self.Speed = 1
|
|
|
|
if alienType == 3:
|
|
|
|
alienImage = pygame.image.load("alien3.png")
|
|
|
|
self.Score = 10
|
|
|
|
self.Speed = 1
|
|
|
|
if alienType == 4:
|
|
|
|
alienImage = pygame.image.load("alien4.png")
|
|
|
|
self.Score = 20
|
|
|
|
self.Speed = 1
|
|
|
|
if alienType == 5:
|
|
|
|
alienImage = pygame.image.load("alien5.png")
|
|
|
|
self.Score = 25
|
|
|
|
self.Speed = 1
|
|
|
|
self.image = pygame.Surface([26, 50])
|
|
|
|
self.image.set_colorkey(black)
|
|
|
|
self.image.blit(alienImage,(0,0))
|
|
|
|
self.rect = self.image.get_rect()
|
|
|
|
self.rect.x = x
|
|
|
|
self.rect.y = y
|
|
|
|
def moveAliens(self):
|
|
|
|
if self.Direction == "right":
|
|
|
|
self.rect.x += self.Speed
|
|
|
|
if self.Direction == "left":
|
|
|
|
self.rect.x -= self.Speed
|
|
|
|
pygame.init()
|
|
|
|
pygame.mouse.set_visible(False)
|
|
|
|
screen = pygame.display.set_mode([400,400])
|
|
|
|
allAliens = pygame.sprite.Group()
|
|
|
|
spaceInvader = spaceInvader()
|
|
|
|
pygame.display.set_caption("Space Attack")
|
|
|
|
background_image = pygame.image.load("Galaxy.png").convert()
|
|
|
|
pygame.mouse.set_visible(True)
|
|
|
|
done = False
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
black =( 0, 0, 0)
|
|
|
|
white =( 255,255,255)
|
|
|
|
red = (255, 0, 0)
|
|
|
|
score = 0
|
|
|
|
enemies = []
|
|
|
|
#For X coords
|
|
|
|
spawnPositions = [50,100,150,200,250,300,350]
|
|
|
|
yCoord = 10
|
|
|
|
for n in range(5):
|
|
|
|
for i in range(len(spawnPositions)):
|
|
|
|
xCoord = spawnPositions[i]
|
|
|
|
alienType = random.randint(1,5)
|
|
|
|
alien = Alien(xCoord) |