Facebook
From A.M, 4 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 203
  1. import pygame
  2. import sys
  3. import random
  4. import os
  5.  
  6. #Predefining Window Dimensions
  7. W = 800
  8. H = 600
  9. width = 60
  10. height = 60
  11. width2 = 50
  12. height2 = 50
  13.  
  14. #starting/initialising pygame
  15. os.environ["SDL_VIDEO_CENTERED"] = "1"
  16. pygame.init()
  17.  
  18. #Setting up our game/window
  19. pygame.display.set_caption("Jungle Defender") #setting the name for our game window
  20. screen = pygame.display.set_mode((W,H)) #creating the dimensions for the window using the predefined variables
  21. background = pygame.image.load("ForestBackground.jpg") #loading the bg image
  22. background2 = pygame.image.load("StartScreen.jpg")
  23. clock = pygame.time.Clock() #clock variable for the fps of the game
  24.  
  25. #Classes
  26. #Player Sprite Class
  27. class Player(object):
  28.     def __init__(self,x,y,width,height):
  29.         self.walkRight = [pygame.image.load('SPR1.png'), pygame.image.load('SPR2.png'), pygame.image.load('SPR3.png'), pygame.image.load('SPR4.png'), pygame.image.load('SPR5.png'), pygame.image.load('SPR6.png'), pygame.image.load('SPR7.png')]
  30.         self.walkLeft = [pygame.image.load('SPL1.png'), pygame.image.load('SPL2.png'), pygame.image.load('SPL3.png'), pygame.image.load('SPL4.png'), pygame.image.load('SPL5.png'), pygame.image.load('SPL6.png'), pygame.image.load('SPL7.png')]
  31.         #self.char = pygame.image.load("SPL1.png")
  32.         self.x = x
  33.         self.y = y
  34.         self.width = width
  35.         self.height = height
  36.         self.vel = 5
  37.         self.isJump = False
  38.         self.jumpCount = 11.5
  39.         self.left = False
  40.         self.right = False
  41.         self.walkCount = 0
  42.         self.standing = True
  43.         self.hitbox = (self.x + 3, self.y+9,self.width+3,self.height+8) # sprite hit box
  44.        
  45.     def draw(self,screen):
  46.         if self.walkCount + 1 >= 21:
  47.             self.walkCount = 0
  48.  
  49.         if not(self.standing):
  50.             if self.left:
  51.                 screen.blit(self.walkLeft[self.walkCount//3], (self.x,self.y))
  52.                 self.walkCount += 1
  53.             elif self.right:
  54.                 screen.blit(self.walkRight[self.walkCount//3],(self.x,self.y))
  55.                 self.walkCount += 1
  56.         else:
  57.             if self.right:
  58.                 screen.blit(self.walkRight[0], (self.x, self.y))
  59.             else:
  60.                 screen.blit(self.walkLeft[0], (self.x, self.y))
  61.  
  62.     def move(self,x,y): #creating the move object so we can call it later and move our sprite
  63.         if x != 0:
  64.             self.move_single_axis(x,0)
  65.         if y != 0:
  66.             self.move_single_axis(0,y)
  67.     def move_single_axis(self, x, y):
  68.         self.rect.x += x
  69.     def update(self):
  70.         if self.y > H - height:
  71.             self.y += 10
  72.  
  73. #Enemy Sprite Class
  74. class Enemy(object):
  75.     def __init__(self,x,y, width, height, end):
  76.         self.x = x
  77.         self.y = y
  78.         self.width = width
  79.         self.height = height
  80.         #self.end = end
  81.         #self.path = [self.x, self.end]
  82.         self.walkCount = 0
  83.         self.vel = 3
  84.         self.hitbox = (self.x + 3, self.y+9,self.width+3,self.height+8)
  85.     def update(self):
  86.         if self.rect.y < (H - height2):
  87.             self.rect.y += 10
  88.            
  89. #Bullet Sprite Class
  90. class Bullet(object):
  91.     def __init__(self,x,y,radius,colour,facing):
  92.         self.x = x
  93.         self.y = y
  94.         self.radius = radius
  95.         self.colour = colour
  96.         self.facing = facing
  97.         self.vel = 8 * facing #multiplies velocity by facing (either 1, or-1, to determine direction of the bullet. -1 = left. 1 =right
  98.  
  99.     def draw(self,screen):
  100.         pygame.draw.circle(screen, self.colour, (self.x,self.y), self.radius)
  101.  
  102. def menu():
  103.     while True:
  104.         screen.fill((0,0,0))
  105.         screen.blit(background2,(0,0))        
  106.         for event in pygame.event.get():
  107.             if event.type == pygame.QUIT:
  108.                 pygame.quit()
  109.                 sys.exit()
  110.             keys = pygame.key.get_pressed()
  111.             if keys[pygame.K_RETURN]:
  112.                 game()
  113.                
  114.         pygame.display.update()
  115.  
  116. def drawGameWindow():
  117.     screen.fill((0,0,0))
  118.     screen.blit(background, (0,0))
  119.     for bullet in bullets:
  120.         bullet.draw(screen)
  121.     all_sprites.update() #updating the sprites group
  122.     player.draw(screen)
  123.     all_sprites.draw(screen)#drawing all sprites to the screen
  124.     player.update()
  125.     pygame.display.update()
  126.  
  127.  
  128.  
  129. #Code to handle all of our sprites.
  130. player = Player(20,500, 60, 60)#adding our player class to a variable and passing necessary variables/dimensions
  131. enemy = Enemy(600,500, 60, 60, 1) #adding enemy class to a variable
  132. all_sprites = pygame.sprite.Group()#creating our sprite group, so that we can add multiple sprites to it and draw them with ease
  133. bullets = []
  134.  
  135. #Main Game Loop
  136. def game():
  137.     running = True #creating the main game loop
  138.     while running == True:
  139.         clock.tick(56)
  140.         for event in pygame.event.get():
  141.             if event.type == pygame.QUIT:                
  142.                 running = False
  143.                 pygame.quit()
  144.  
  145.         for bullet in bullets:
  146.             if bullet.x < 800 and bullet.x > 0: #Making sure the bullet isn't off the screen.
  147.                 bullet.x += bullet.vel #move our bullet according to velocity
  148.             else:
  149.                 bullets.pop(bullets.index(bullet)) #Deleting our bullet by popping (removing) from list.
  150.        
  151.         keys = pygame.key.get_pressed() # creating a variable to check for key presses
  152.  
  153.         if keys [pygame.K_UP]:
  154.             if player.left:
  155.                 facing = -1 #determining direction of bullet (left = -1)
  156.             else:
  157.                 facing = 1 #determining direction of bullet (right = +1)                
  158.             if len(bullets) < 6: #no. of bullets on screen at once.
  159.                 bullets.append(Bullet(round(player.x + player.width // 2), round(player.y + player.height // 2), 6, (0,0,0), facing))
  160.        
  161.         if keys[pygame.K_LEFT] and player.x > player.vel:
  162.             player.x -= player.vel
  163.             player.right = False  
  164.             player.left = True
  165.             player.standing = False
  166.                
  167.         elif keys[pygame.K_RIGHT]:
  168.             player.x += player.vel
  169.             player.right = True
  170.             player.left = False
  171.             player.standing = False
  172.             if player.x >= W-width:
  173.                 player.x = W-width
  174.         else:
  175.             player.standing = True
  176.             player.walkCount = 0
  177.            
  178.  
  179.         if not (player.isJump):
  180.             if keys[pygame.K_SPACE]:
  181.                 player.isJump = True
  182.                 player.right = False
  183.                 player.left = False
  184.                 player.walkCount = 0
  185.         else:
  186.             if player.jumpCount >= -11.5:
  187.                 neg = 1
  188.                 if player.jumpCount < 0:
  189.                     neg = -1
  190.                 player.y -= (player.jumpCount ** 2) * 0.5 * neg
  191.                 player.jumpCount -= 1
  192.             else:
  193.                 player.isJump = False
  194.                 player.jumpCount = 11.5
  195.  
  196.                
  197.         drawGameWindow()
  198.  
  199. menu()
  200. pygame.quit()
  201. #End of our code.
  202.