Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #this is basicly a galaga clone.
- import os, sys, time, pygame
- #setup pygame window
- pygame.init()
- windowSize = (800,600)
- screen = pygame.display.set_mode(windowSize)
- pygame.key.set_repeat(25,25)
- ###################################
- #load all images to memory
- backGround = pygame.image.load("bg.png").convert()
- #######################################
- #global variables
- loopVar = 1
- Deathwait = 300
- StartGameWait = 10000
- Clock = pygame.time.Clock()
- ###################################
- #dictionary so my classes can share objects
- objDict = {}
- ###################################
- #enemy AI
- class Enemy(pygame.sprite.Sprite):
- def __init__(self): #instance the classes
- self.x = 400 #enemy coordinates
- self.y = 45
- self.MoveSpeed = .5 #speed of enemy ship
- self.Direction = 1 #direction of enemy ship 0 is left 1 is right 2 is null
- self.prevDirection = 0 # keep track of previous direction
- #lets make this a sprite
- self.image = pygame.image.load ("enemyA.png").convert()
- self.rect = self.image.get_rect()
- self.rect.topleft = (self.x,self.y)
- #enemy projectile stuff
- objDict["eShell"].shellx = self.x
- objDict["eShell"].shelly = self.y
- self.hasShot = False
- self.shellInAir = False
- self.shellVelocity = 5
- def move(self):
- if self.x >= 15 and self.prevDirection == 1: #if determines previous direction and new direction
- self.Direction = 0
- if self.x <= 15:
- self.prevDirection = 0
- elif self.x <= 760 and self.prevDirection == 0:
- self.Direction = 1
- if self.x >= 760:
- self.prevDirection = 1
- #move the enemy in the appropriate direction
- if self.Direction == 0:
- self.x = self.x - self.MoveSpeed
- self.rect.topleft = (self.x,self.y)
- elif self.Direction == 1:
- self.x = self.x + self.MoveSpeed
- self.rect.topleft = (self.x,self.y)
- #enemy combat
- def shoot(self):
- self.hasShot = True #did we shoot? is the shell still traveling?
- if self.hasShot == True and self.shellInAir == False:
- objDict["Shell"].shelly = objDict["eShell"].shelly - self.shellVelocity
- self.shellInAir = True
- def willShoot(self, t): #will i shoot at the player?
- if t == self.x and self.hasShot == False:
- self.shoot()
- #################################
- #player class
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- self.x = 400 #coordinates of player
- self.y =550
- objDict["pShell"].shellx = self.x #initial positioning of projectile
- objDict["pShell"].shelly = self.y
- self.shellVelocity = 5
- self.hasShot = False
- self.shellInAir = False
- #lets make this a sprite
- self.image = pygame.image.load ("ship.png").convert()
- self.rect = self.image.get_rect()
- self.rect.topleft = (self.x,self.y)
- self.maxX = 760 #maximum distance to right
- self.minX = 15 #minmum distance to left
- self.MoveSpeed = 5 # player move speed
- def input(self):
- for event in pygame.event.get(): #this si the stuff that makes the window closable
- if event.type == pygame.QUIT:
- exit()
- elif event.type == pygame.KEYDOWN: #this is our basic movement with arrows left and right
- if event.key == pygame.K_LEFT and self.x >= self.minX:
- self.x = self.x - self.MoveSpeed
- self.rect.topleft = (self.x,self.y)
- if event.key == pygame.K_RIGHT and self.x <= self.maxX:
- self.x = self.x + self.MoveSpeed
- self.rect.topleft = (self.x,self.y)
- if event.key == pygame.K_SPACE and self.shellInAir == False:
- self.shoot()
- def shoot(self):
- self.hasShot = True #did we shoot? is the shell still traveling?
- if self.hasShot == True and self.shellInAir == False:
- objDict["pShell"].shelly = objDict["pShell"].shelly - self.shellVelocity
- self.shellInAir = True
- ##########################
- #shell classes to keep track of projectiles
- class pShell(pygame.sprite.Sprite):
- def __init__(self, game):
- self.shellx = game.playerA.x + 6 #the initial placement of the shell
- self.shelly = game.playerA.y - 20
- #lets make this a sprite
- self.image = pygame.image.load ("pShellA.png").convert()
- self.rect = self.image.get_rect()
- self.rect.topleft = (self.shellx,self.shelly)
- #the enemy shell sprite
- class eShell(pygame.sprite.Sprite):
- def __init__(self, game):
- self.shellx = game.enemyA.x + 6 #the initial placement of the shell
- self.shelly = game.enemyA.y + 20
- #lets make this a sprite
- self.image = pygame.image.load ("eShellA.png").convert()
- self.rect = self.image.get_rect()
- self.rect.topleft = (self.shellx,self.shelly)
- ###############################
- #the game class
- class Game():
- #instance the game classes
- def __init__(self):
- objDict["enemyA"] = Enemy() #storing Enemy() as an element in a dict to use everywhere
- obDict["playerA"] = Player()
- self.PlayerDead = False
- self.EnemyDead = False
- objDict["eShell"] = eShell(self)
- objDict["pShell"] = pShell(self)
- #delay before gameplay begins
- Clock.tick(StartGameWait)
- while loopVar == 1:
- self.playerA.input()
- self.enemyA.move() #move the enemy to align with the player's x coordinate
- self.enemyA.willShoot(self.playerA.x) #if the enemy has a shot he will take it.
- self.update()
- #fpsClock.tick(FPS) #waits
- def combat(self):
- #keep track of player shell
- if self.playerA.shellInAir:
- screen.blit(self.pShell.image,(self.pShell.rect))
- self.playerA.shelly = self.playerA.shelly - self.playerA.shellVelocity
- if self.playerA.shelly <= 0: #is the shell at the top of the screen?
- self.playerA.shellInAir = False
- self.playerA.hasShot = False
- #keep track of enemy shell
- if self.enemyA.shellInAir:
- screen.blit(self.eShell.image,(self.eShell.rect))
- self.enemyA.shelly = self.enemyA.shelly + self.enemyA.shellVelocity
- if self.enemyA.shelly >= 600: #is the shell at the top of the screen?
- self.enemyA.shellInAir = False
- self.enemyA.hasShot = False
- def hitCheck(self):
- #lets see if the bullet hit the enemy
- if self.pShell.rect.colliderect(self.enemyA.rect) and self.playerA.shellInAir:
- print ("enemy has been hit!")
- #check to see if enemy shot player
- if self.eShell.rect.colliderect(self.playerA.rect) and self.enemyA.shellInAir:
- print ("player has been hit!")
- #setup the game board
- def update(self):
- screen.blit(backGround, (0,0))
- screen.blit(self.playerA.image,(self.playerA.rect)) #draw player ship in initial spot
- screen.blit(self.enemyA.image,(self.enemyA.rect)) #draw enemy ship in initial position
- self.combat()
- self.hitCheck()
- pygame.display.update()
- #the actual game takes place below
- game = Game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement