SHARE
TWEET

Untitled

a guest Jun 16th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2.  
  3. class Invaders:
  4.     def __init__(self,  display, position=[100,100]):
  5.         self.display = display
  6.         self.position = position
  7.         self.height = 40
  8.         self.width = self.height
  9.         self.colors = colors
  10.     #this will draw an invader onto the screen, you could load an image instead
  11.     def draw_invader(self):
  12.         pygame.draw.rect(self.display, self.colors['grey'], (self.position[0], self.position[1], self.height, self.width))
  13.  
  14. class Player:
  15.     def __init__(self, display, position):
  16.         self.display = display
  17.         self.width = 50
  18.         self.height = 25
  19.         self.position = position
  20.         self.colors = colors
  21.  
  22.     def draw_player(self):
  23.         pygame.draw.rect(self.display, self.colors['green'], (self.position[0], self.position[1], self.width, self.height))
  24.  
  25.  
  26. pygame.init()
  27.  
  28. width = 1000
  29. height = width
  30. display = pygame.display.set_mode((width, height))
  31. colors = {'white': (255, 255, 255), 'black': (0,0,0), 'grey': (160, 160, 160), 'green': (0,255,0)}
  32.  
  33. clock = pygame.time.Clock()
  34.  
  35. def create_invader_positions():
  36.     positions = []
  37.     for x in range(100, width-100,50):
  38.         for y in range(50, 50*3, 50):
  39.             positions.append([x,y])
  40.     return positions
  41.  
  42. def create_invaders(display):
  43.     listOfInvaders = []
  44.     for position in create_invader_positions():
  45.         listOfInvaders.append(Invaders(display, position))
  46.     return listOfInvaders
  47.  
  48. def move_invaders(invaders):
  49.     xPositions = [invader.position[0] for invader in invaders]
  50.     if min(xPositions) > 0:
  51.         return 50
  52.     if max(xPositions) == width:
  53.         return -50
  54.  
  55. def move_player(event):
  56.     playerSpeed = 20
  57.     if event.type == pygame.KEYDOWN:
  58.         if event.key == pygame.K_LEFT:
  59.             return -playerSpeed
  60.         if event.key == pygame.K_RIGHT:
  61.             return playerSpeed
  62.     if event.type == pygame.KEYUP:
  63.         return 0
  64.  
  65. def space_invaders(display):
  66.     # invaderOne = invaders(display)
  67.     event = pygame.event.poll()
  68.     invaders = create_invaders(display)
  69.     player = Player(display, [int(width/2), height-50])
  70.     #attacked willl be true if the invader collides with the player
  71.     attacked = False
  72.     event = pygame.event.poll()
  73.     #sets the speed of the space invader
  74.     invaderSpeed = 2
  75.     moveInvaderX = invaderSpeed
  76.     moveInvaderY = 0
  77.  
  78.     movePlayer = 0
  79.     while attacked == False:
  80.         for event in pygame.event.get():
  81.             if event.type == pygame.QUIT:
  82.                 pygame.quit()
  83.                 quit()
  84.             if event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
  85.                 movePlayer = move_player(event)
  86.  
  87.         display.fill(colors['black'])
  88.         invaderPositionsX = [invader.position[0] for invader in invaders]
  89.         invaderPositionsY = [invader.position[1] for invader in invaders]
  90.         if min(invaderPositionsX) == 0:
  91.             moveInvaderX = invaderSpeed
  92.             moveInvaderY = invaderSpeed
  93.         if max(invaderPositionsX) >= width-50:
  94.             moveInvaderX = -invaderSpeed
  95.             moveInvaderY = invaderSpeed
  96.  
  97.         player.position[0] += movePlayer
  98.         player.draw_player()
  99.  
  100.         for invader in invaders:
  101.             invader.position[0] += moveInvaderX
  102.             invader.position[1] += moveInvaderY
  103.             invader.draw_invader()
  104.         moveInvaderY = 0
  105.         if player.position[0] < player.width or player.position[0] > width-player.width*2:
  106.             movePlayer = 0
  107.         pygame.display.update()
  108.         clock.tick(60)
  109.  
  110. def start_screen():
  111.     # display.fill(colors['white'])
  112.     space_invaders(display)
  113.     pygame.display.update()
  114.  
  115. start_screen()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top