From 15259b983c39cfee2ba888d0a1e72669bcab1ec2 Mon Sep 17 00:00:00 2001 From: 206530234 Date: Thu, 24 Jun 2021 10:09:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'=E8=AE=A9=E5=A4=96?= =?UTF-8?q?=E6=98=9F=E4=BA=BA=E7=BE=A4=E7=A7=BB=E5=8A=A8'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 让外星人群移动 | 153 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 让外星人群移动 diff --git a/让外星人群移动 b/让外星人群移动 new file mode 100644 index 0000000..846e320 --- /dev/null +++ b/让外星人群移动 @@ -0,0 +1,153 @@ +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) \ No newline at end of file