rishabbansal21

Day-7

Jan 14th, 2021 (edited)
459
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #bit.ly/3dC6rF6
  2.  
  3. import pygame
  4. import random
  5. import math
  6.  
  7. #INITIALISING PYGAME
  8. pygame.init()
  9.  
  10. #SCREEN
  11. screen = pygame.display.set_mode((800,600))
  12.  
  13. #TITLE
  14. pygame.display.set_caption("SPACE INVADERS")
  15.  
  16. #BACKGROUND
  17. background = pygame.image.load('imgs/background.png')
  18. pygame.mixer.music.load('sounds/background.wav')
  19. pygame.mixer.music.play(-1)
  20.  
  21. #PLAYER
  22. playerimg = pygame.image.load('imgs/si.png')
  23. pX = 360
  24. pY = 480
  25. pXchange = 0
  26. pYchange = 0
  27. speed = 5
  28. def player(x,y):
  29.     screen.blit(playerimg, (x,y))
  30.  
  31. #ENEMY
  32. eSound = pygame.mixer.Sound('sounds/explosion.wav')
  33. '''enemyimg = pygame.image.load('imgs/alien.png')
  34. eX = 400
  35. eY = 100
  36. eXchange = 3
  37. eYchange = 3'''
  38. enemyimg = []
  39. eX = []
  40. eY = []
  41. eXchange = []
  42. eYchange = []
  43. num_of_enemies = 5
  44.  
  45. for i in range(num_of_enemies):
  46.     enemyimg.append(pygame.image.load('imgs/alien.png'))
  47.     eX.append(random.randint(100,700))
  48.     eY.append(random.randint(50,300))
  49.     eXchange.append(3)
  50.     eYchange.append(30)
  51.  
  52. def enemy(x,y,i):
  53.     screen.blit(enemyimg[i], (x,y))
  54.  
  55. #BULLET
  56. bSound = pygame.mixer.Sound('sounds/laser.wav')
  57. bulletimg = pygame.image.load('imgs/bullet.png')
  58. bX = pX
  59. bY = pY
  60. bYchange = -10
  61. bState = 0 #READY
  62. def fire_bullet(x,y):
  63.     global bState
  64.     bState = 1 #FIRE
  65.     screen.blit(bulletimg, (x+15,y-30))
  66.  
  67. #COLLISION DETECTION
  68. def isCollision(EX,EY,BX,BY):
  69.     distance = math.sqrt( (BX-EX)**2 + (BY-EY)**2 )
  70.     if distance < 30:
  71.         return True
  72.     return False
  73.  
  74. #SCORE
  75. score = 0
  76. font = pygame.font.Font('freesansbold.ttf', 32)
  77. sCoord = (10,10)
  78. def print_score(sc):
  79.     screen.blit(font.render("SCORE: " + str(sc), True, (255,255,255)), sCoord)
  80.  
  81. #GAME OVER
  82. def game_over_text(sc):
  83.     msg = pygame.font.Font('freesansbold.ttf', 64)
  84.     mCoord = (180,200)
  85.     screen.blit(msg.render("GAME OVER!!", True, (255,255,255)), mCoord)
  86.  
  87.     fs = pygame.font.Font('freesansbold.ttf', 32)
  88.     fCoord = (280,300)
  89.     screen.blit(fs.render("FINAL SCORE: " + str(sc), True, (255,255,255)), fCoord)
  90.  
  91.  
  92. running = True
  93.  
  94. while running:
  95.     #screen.fill((100,0,200))
  96.     screen.blit(background, (0,0))
  97.     for event in pygame.event.get():
  98.         if event.type == pygame.QUIT:
  99.             running = False
  100.  
  101.         if event.type == pygame.KEYDOWN:
  102.             if event.key == pygame.K_LEFT: pXchange -= speed
  103.             if event.key == pygame.K_RIGHT: pXchange += speed
  104.             if event.key == pygame.K_UP: pYchange -= speed
  105.             if event.key == pygame.K_DOWN: pYchange += speed
  106.             if event.key == pygame.K_SPACE:
  107.                 bSound.play()
  108.                 bX,bY = pX, pY
  109.                 fire_bullet(bX,bY)
  110.  
  111.         if event.type == pygame.KEYUP:
  112.             if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  113.                 pXchange = 0
  114.             if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
  115.                 pYchange = 0
  116.    
  117.     '''
  118.    collision = isCollision(eX,eY,bX,bY)
  119.    if collision:
  120.        eSound.play()
  121.        score += 1
  122.        eX = random.randint(100,700)
  123.        eY = random.randint(50,250)
  124.        bState = 0
  125.    '''
  126.  
  127.     #PLAYER MOVEMENT
  128.     pX += pXchange
  129.     pY += pYchange
  130.     if pX <= 0:
  131.         pX = 800
  132.     elif pX >= 800:
  133.         pX = 0
  134.     if pY < 0:
  135.         pY = 0
  136.     elif pY>536:
  137.         pY = 536
  138.     player(pX,pY)
  139.  
  140.     #ENEMY MOVEMENT
  141.     for i in range(num_of_enemies):
  142.  
  143.         #GAME OVER
  144.         if eY[i] > 400:
  145.             for j in range(num_of_enemies):
  146.                 eY[j] = 800
  147.             game_over_text(score)
  148.             break
  149.  
  150.  
  151.         eX[i] += eXchange[i]
  152.         if eX[i] > 736:
  153.             eY[i] += 30
  154.             eXchange[i] = -eXchange[i]
  155.         if eX[i] < 0:
  156.             eY[i] += 30
  157.             eXchange[i] = -eXchange[i]
  158.  
  159.         collision = isCollision(eX[i],eY[i],bX,bY)
  160.         if collision:
  161.             eSound.play()
  162.             score += 1
  163.             eX[i] = random.randint(100,700)
  164.             eY[i] = random.randint(50,250)
  165.             bState = 0
  166.  
  167.         enemy(eX[i],eY[i],i)
  168.  
  169.  
  170.     '''
  171.    eX += eXchange
  172.    if eX > 736:
  173.        eY += 30
  174.        eXchange = -eXchange
  175.    elif eX < 0:
  176.        eY += 30
  177.        eXchange = -eXchange
  178.    enemy(eX,eY)
  179.    '''
  180.     #BULLET MOVEMENT
  181.     if bState == 1:
  182.         fire_bullet(bX,bY)
  183.         bY += bYchange
  184.         if bY <= 0:
  185.             bState = 0
  186.  
  187.     print_score(score)
  188.     pygame.display.update()
  189.  
RAW Paste Data