Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.74 KB | None | 0 0
  1. #this is basicly a galaga clone.
  2. import os, sys, time, pygame
  3.  
  4.  
  5. #setup pygame window
  6. pygame.init()
  7.  
  8. windowSize = (800,600)
  9. screen = pygame.display.set_mode(windowSize)
  10. pygame.key.set_repeat(25,25)
  11.  
  12. ###################################
  13.  
  14. #load all images to memory
  15. backGround = pygame.image.load("bg.png").convert()
  16.  
  17. #######################################
  18. #global variables
  19. loopVar = 1
  20.  
  21. Deathwait = 300
  22. StartGameWait = 10000
  23. Clock = pygame.time.Clock()
  24.  
  25. ###################################
  26. #dictionary so my classes can share objects
  27. objDict = {}
  28. ###################################
  29.  
  30. #enemy AI
  31. class Enemy(pygame.sprite.Sprite):
  32.     def __init__(self): #instance the classes
  33.  
  34.         self.x = 400 #enemy coordinates
  35.         self.y = 45
  36.         self.MoveSpeed = .5 #speed of enemy ship
  37.         self.Direction = 1 #direction of enemy ship 0 is left 1 is right 2 is null
  38.         self.prevDirection = 0 # keep track of previous direction
  39.        
  40.         #lets make this a sprite
  41.         self.image = pygame.image.load ("enemyA.png").convert()
  42.         self.rect = self.image.get_rect()
  43.         self.rect.topleft = (self.x,self.y)
  44.        
  45.         #enemy projectile stuff
  46.         objDict["eShell"].shellx = self.x
  47.         objDict["eShell"].shelly = self.y
  48.         self.hasShot = False
  49.         self.shellInAir = False
  50.         self.shellVelocity = 5
  51.        
  52.     def move(self):
  53.         if self.x >= 15 and self.prevDirection == 1: #if determines previous direction and new direction
  54.             self.Direction = 0
  55.             if self.x <= 15:
  56.                 self.prevDirection = 0
  57.            
  58.         elif self.x <= 760 and self.prevDirection == 0:
  59.             self.Direction = 1
  60.             if self.x >= 760:
  61.                 self.prevDirection = 1
  62.        
  63.     #move the enemy in the appropriate direction
  64.         if self.Direction == 0:
  65.             self.x = self.x - self.MoveSpeed
  66.             self.rect.topleft = (self.x,self.y)
  67.            
  68.         elif self.Direction == 1:
  69.             self.x = self.x + self.MoveSpeed
  70.             self.rect.topleft = (self.x,self.y)
  71.    
  72.     #enemy combat
  73.     def shoot(self):
  74.        
  75.         self.hasShot = True #did we shoot? is the shell still traveling?
  76.        
  77.         if self.hasShot == True and self.shellInAir == False:
  78.             objDict["Shell"].shelly = objDict["eShell"].shelly - self.shellVelocity
  79.             self.shellInAir = True
  80.    
  81.     def willShoot(self, t): #will i shoot at the player?
  82.         if t == self.x and self.hasShot == False:
  83.             self.shoot()
  84. #################################
  85.  
  86. #player class
  87. class Player(pygame.sprite.Sprite):
  88.     def __init__(self):
  89.        
  90.         self.x = 400 #coordinates of player
  91.         self.y =550
  92.        
  93.         objDict["pShell"].shellx = self.x #initial positioning of projectile
  94.         objDict["pShell"].shelly = self.y
  95.         self.shellVelocity = 5
  96.         self.hasShot = False
  97.         self.shellInAir = False
  98.        
  99.         #lets make this a sprite
  100.         self.image = pygame.image.load ("ship.png").convert()
  101.         self.rect = self.image.get_rect()
  102.         self.rect.topleft = (self.x,self.y)
  103.        
  104.         self.maxX = 760 #maximum distance to right
  105.         self.minX = 15  #minmum distance to left
  106.         self.MoveSpeed = 5 # player move speed
  107.        
  108.     def input(self):
  109.    
  110.         for event in pygame.event.get(): #this si the stuff that makes the window closable
  111.        
  112.             if event.type == pygame.QUIT:
  113.                 exit()
  114.        
  115.             elif event.type == pygame.KEYDOWN:  #this is our basic movement with arrows left and right
  116.                 if event.key == pygame.K_LEFT and self.x >= self.minX:
  117.                     self.x = self.x - self.MoveSpeed
  118.                     self.rect.topleft = (self.x,self.y)
  119.                 if event.key == pygame.K_RIGHT and self.x <= self.maxX:
  120.                     self.x = self.x + self.MoveSpeed
  121.                     self.rect.topleft = (self.x,self.y)
  122.        
  123.                 if event.key == pygame.K_SPACE and self.shellInAir == False:
  124.                     self.shoot()
  125.                    
  126.     def shoot(self):
  127.        
  128.         self.hasShot = True #did we shoot? is the shell still traveling?
  129.        
  130.         if self.hasShot == True and self.shellInAir == False:
  131.             objDict["pShell"].shelly = objDict["pShell"].shelly - self.shellVelocity
  132.             self.shellInAir = True
  133. ##########################
  134.  
  135. #shell classes to keep track of projectiles
  136. class pShell(pygame.sprite.Sprite):
  137.     def __init__(self, game):
  138.         self.shellx = game.playerA.x + 6 #the initial placement of the shell
  139.         self.shelly = game.playerA.y - 20
  140.         #lets make this a sprite
  141.         self.image = pygame.image.load ("pShellA.png").convert()
  142.         self.rect = self.image.get_rect()
  143.         self.rect.topleft = (self.shellx,self.shelly)
  144.  
  145.  
  146. #the enemy shell sprite
  147. class eShell(pygame.sprite.Sprite):
  148.     def __init__(self, game):
  149.         self.shellx = game.enemyA.x + 6 #the initial placement of the shell
  150.         self.shelly = game.enemyA.y + 20
  151.         #lets make this a sprite
  152.         self.image = pygame.image.load ("eShellA.png").convert()
  153.         self.rect = self.image.get_rect()
  154.         self.rect.topleft = (self.shellx,self.shelly)
  155. ###############################
  156.  
  157. #the game class
  158. class Game():
  159.     #instance the game classes
  160.     def __init__(self):
  161.         objDict["enemyA"] = Enemy() #storing Enemy() as an element in a dict to use everywhere
  162.         obDict["playerA"] = Player()
  163.        
  164.         self.PlayerDead = False
  165.         self.EnemyDead = False
  166.        
  167.         objDict["eShell"] = eShell(self)
  168.         objDict["pShell"] = pShell(self)
  169.        
  170.         #delay before gameplay begins
  171.         Clock.tick(StartGameWait)
  172.        
  173.         while loopVar == 1:
  174.            
  175.             self.playerA.input()
  176.             self.enemyA.move() #move the enemy to align with the player's x coordinate
  177.             self.enemyA.willShoot(self.playerA.x) #if the enemy has a shot he will take it.
  178.  
  179.             self.update()
  180.             #fpsClock.tick(FPS) #waits
  181.     def combat(self):
  182.         #keep track of player shell
  183.         if self.playerA.shellInAir:
  184.             screen.blit(self.pShell.image,(self.pShell.rect))
  185.             self.playerA.shelly = self.playerA.shelly - self.playerA.shellVelocity
  186.            
  187.             if self.playerA.shelly <= 0: #is the shell at the top of the screen?
  188.                 self.playerA.shellInAir = False
  189.                 self.playerA.hasShot = False
  190.             #keep track of enemy shell
  191.         if self.enemyA.shellInAir:
  192.             screen.blit(self.eShell.image,(self.eShell.rect))
  193.             self.enemyA.shelly = self.enemyA.shelly + self.enemyA.shellVelocity
  194.            
  195.             if self.enemyA.shelly >= 600: #is the shell at the top of the screen?
  196.                 self.enemyA.shellInAir = False
  197.                 self.enemyA.hasShot = False
  198.    
  199.     def hitCheck(self):
  200.         #lets see if the bullet hit the enemy
  201.         if self.pShell.rect.colliderect(self.enemyA.rect) and self.playerA.shellInAir:
  202.             print ("enemy has been hit!")
  203.         #check to see if enemy shot player
  204.         if self.eShell.rect.colliderect(self.playerA.rect) and self.enemyA.shellInAir:
  205.             print ("player has been hit!")
  206.        
  207.     #setup the game board
  208.     def update(self):
  209.            
  210.         screen.blit(backGround, (0,0))
  211.         screen.blit(self.playerA.image,(self.playerA.rect)) #draw player ship in initial spot
  212.         screen.blit(self.enemyA.image,(self.enemyA.rect)) #draw enemy ship in initial position
  213.        
  214.         self.combat()
  215.         self.hitCheck()
  216.        
  217.         pygame.display.update()
  218.    
  219. #the actual game takes place below
  220. game = Game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement