Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
50
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"] = eShell()
  47.         objDict["eShell"].shellx = self.x
  48.         objDict["eShell"].shelly = self.y
  49.         self.hasShot = False
  50.         self.shellInAir = False
  51.         self.shellVelocity = 5
  52.        
  53.     def move(self):
  54.         if self.x >= 15 and self.prevDirection == 1: #if determines previous direction and new direction
  55.             self.Direction = 0
  56.             if self.x <= 15:
  57.                 self.prevDirection = 0
  58.            
  59.         elif self.x <= 760 and self.prevDirection == 0:
  60.             self.Direction = 1
  61.             if self.x >= 760:
  62.                 self.prevDirection = 1
  63.        
  64.     #move the enemy in the appropriate direction
  65.         if self.Direction == 0:
  66.             self.x = self.x - self.MoveSpeed
  67.             self.rect.topleft = (self.x,self.y)
  68.            
  69.         elif self.Direction == 1:
  70.             self.x = self.x + self.MoveSpeed
  71.             self.rect.topleft = (self.x,self.y)
  72.    
  73.     #enemy combat
  74.     def shoot(self):
  75.        
  76.         self.hasShot = True #did we shoot? is the shell still traveling?
  77.        
  78.         if self.hasShot == True and self.shellInAir == False:
  79.             objDict["Shell"].shelly = objDict["eShell"].shelly - self.shellVelocity
  80.             self.shellInAir = True
  81.    
  82.     def willShoot(self, t): #will i shoot at the player?
  83.         if t == self.x and self.hasShot == False:
  84.             self.shoot()
  85. #################################
  86.  
  87. #player class
  88. class Player(pygame.sprite.Sprite):
  89.     def __init__(self):
  90.        
  91.         self.x = 400 #coordinates of player
  92.         self.y =550
  93.        
  94.         objDict["pShell"].shellx = self.x #initial positioning of projectile
  95.         objDict["pShell"].shelly = self.y
  96.         self.shellVelocity = 5
  97.         self.hasShot = False
  98.         self.shellInAir = False
  99.        
  100.         #lets make this a sprite
  101.         self.image = pygame.image.load ("ship.png").convert()
  102.         self.rect = self.image.get_rect()
  103.         self.rect.topleft = (self.x,self.y)
  104.        
  105.         self.maxX = 760 #maximum distance to right
  106.         self.minX = 15  #minmum distance to left
  107.         self.MoveSpeed = 5 # player move speed
  108.        
  109.     def input(self):
  110.    
  111.         for event in pygame.event.get(): #this si the stuff that makes the window closable
  112.        
  113.             if event.type == pygame.QUIT:
  114.                 exit()
  115.        
  116.             elif event.type == pygame.KEYDOWN:  #this is our basic movement with arrows left and right
  117.                 if event.key == pygame.K_LEFT and self.x >= self.minX:
  118.                     self.x = self.x - self.MoveSpeed
  119.                     self.rect.topleft = (self.x,self.y)
  120.                 if event.key == pygame.K_RIGHT and self.x <= self.maxX:
  121.                     self.x = self.x + self.MoveSpeed
  122.                     self.rect.topleft = (self.x,self.y)
  123.        
  124.                 if event.key == pygame.K_SPACE and self.shellInAir == False:
  125.                     self.shoot()
  126.                    
  127.     def shoot(self):
  128.        
  129.         self.hasShot = True #did we shoot? is the shell still traveling?
  130.        
  131.         if self.hasShot == True and self.shellInAir == False:
  132.             objDict["pShell"].shelly = objDict["pShell"].shelly - self.shellVelocity
  133.             self.shellInAir = True
  134. ##########################
  135.  
  136. #shell classes to keep track of projectiles
  137. class pShell(pygame.sprite.Sprite):
  138.     def __init__():
  139.         self.shellx = objDict["playerA"].x + 6 #the initial placement of the shell
  140.         self.shelly = objDict["playerA"].y - 20
  141.         #lets make this a sprite
  142.         self.image = pygame.image.load ("pShellA.png").convert()
  143.         self.rect = self.image.get_rect()
  144.         self.rect.topleft = (self.shellx,self.shelly)
  145.  
  146.  
  147. #the enemy shell sprite
  148. class eShell(pygame.sprite.Sprite):
  149.     def __init__():
  150.         self.shellx = objDict["enemyA"].x + 6 #the initial placement of the shell
  151.         self.shelly = objDict["enemyA"].y + 20
  152.         #lets make this a sprite
  153.         self.image = pygame.image.load ("eShellA.png").convert()
  154.         self.rect = self.image.get_rect()
  155.         self.rect.topleft = (self.shellx,self.shelly)
  156. ###############################
  157.  
  158. #the game class
  159. class Game():
  160.     #instance the game classes
  161.     def __init__(self):
  162.         objDict["enemyA"] = Enemy() #storing Enemy() as an element in a dict to use everywhere
  163.         objDict["playerA"] = Player()
  164.        
  165.         self.PlayerDead = False
  166.         self.EnemyDead = False
  167.        
  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