Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys, random
- pygame.init()
- screen_width = 800
- screen_height = 600
- screen = pygame.display.set_mode([screen_width, screen_height])
- all_sprites_list = pygame.sprite.Group()
- block_list = pygame.sprite.Group()
- bullet_list = pygame.sprite.Group()
- player_list = pygame.sprite.Group()
- BLACK = ( 0, 0, 0)
- WHITE = ( 255, 255, 255)
- background = pygame.image.load('space.jpg')
- pygame.display.set_caption("Alien Invasion!")
- explosion = pygame.mixer.Sound('explosion.wav')
- score = 0
- class Block(pygame.sprite.Sprite):
- def __init__(self, color):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load("spaceship.png")
- self.rect = self.image.get_rect()
- def update(self):
- self.rect.y += 1
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.x=0
- self.y=0
- self.image = pygame.image.load("alien.png")
- self.rect = self.image.get_rect()
- def update(self):
- pos = pygame.mouse.get_pos()
- self.rect.x = pos[0]
- def render(self):
- if (self.currentImage==0):
- screen.blit(self.image, (self.x, self.y))
- class Bullet(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load("bullet.png")
- self.rect = self.image.get_rect()
- def update(self):
- self.rect.y -= 9
- for i in range(15):
- block = Block(BLACK)
- block.rect.x = random.randrange(screen_width)
- block.rect.y = random.randrange(150)
- block_list.add(block)
- all_sprites_list.add(block)
- player = Player()
- all_sprites_list.add(player)
- done = False
- clock = pygame.time.Clock()
- player.rect.y = 480
- k_space = False
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
- pygame.quit()
- sys.exit()
- elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
- k_space = True
- if k_space:
- bullet = Bullet()
- bullet.rect.x = player.rect.x
- bullet.rect.y = player.rect.y
- all_sprites_list.add(bullet)
- bullet_list.add(bullet)
- k_space = False
- all_sprites_list.update()
- for block in block_list:
- player_hit_list = pygame.sprite.spritecollide(block, player_list, True)
- for player in player_hit_list:
- explosion.play()
- block_list.remove(block)
- player_list.remove(player)
- all_sprites_list.remove(block)
- all_sprites_list.remove(player)
- if block.rect.y < +10:
- block_list.remove(block)
- all_sprites_list.remove(block)
- for bullet in bullet_list:
- block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
- for block in block_hit_list:
- explosion.play()
- bullet_list.remove(bullet)
- all_sprites_list.remove(bullet)
- score += 10
- if bullet.rect.y < -10:
- bullet_list.remove(bullet)
- all_sprites_list.remove(bullet)
- font = pygame.font.Font(None, 36)
- text = font.render("Score: " + str(score), True, WHITE)
- textpos = text.get_rect(centerx=screen.get_width()/12)
- screen.blit(background, (0,0))
- screen.blit(text, textpos)
- all_sprites_list.draw(screen)
- pygame.display.update()
- clock.tick(80)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement