import pygame import sys import random import os #Predefining Window Dimensions W = 800 H = 600 width = 60 height = 60 width2 = 50 height2 = 50 #starting/initialising pygame os.environ["SDL_VIDEO_CENTERED"] = "1" pygame.init() #Setting up our game/window pygame.display.set_caption("Jungle Defender") #setting the name for our game window screen = pygame.display.set_mode((W,H)) #creating the dimensions for the window using the predefined variables background = pygame.image.load("ForestBackground.jpg") #loading the bg image background2 = pygame.image.load("StartScreen.jpg") clock = pygame.time.Clock() #clock variable for the fps of the game #Classes #Player Sprite Class class Player(object): def __init__(self,x,y,width,height): 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')] 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')] #self.char = pygame.image.load("SPL1.png") self.x = x self.y = y self.width = width self.height = height self.vel = 5 self.isJump = False self.jumpCount = 11.5 self.left = False self.right = False self.walkCount = 0 self.standing = True self.hitbox = (self.x + 3, self.y+9,self.width+3,self.height+8) # sprite hit box def draw(self,screen): if self.walkCount + 1 >= 21: self.walkCount = 0 if not(self.standing): if self.left: screen.blit(self.walkLeft[self.walkCount//3], (self.x,self.y)) self.walkCount += 1 elif self.right: screen.blit(self.walkRight[self.walkCount//3],(self.x,self.y)) self.walkCount += 1 else: if self.right: screen.blit(self.walkRight[0], (self.x, self.y)) else: screen.blit(self.walkLeft[0], (self.x, self.y)) def move(self,x,y): #creating the move object so we can call it later and move our sprite if x != 0: self.move_single_axis(x,0) if y != 0: self.move_single_axis(0,y) def move_single_axis(self, x, y): self.rect.x += x def update(self): if self.y > H - height: self.y += 10 #Enemy Sprite Class class Enemy(object): def __init__(self,x,y, width, height, end): self.x = x self.y = y self.width = width self.height = height #self.end = end #self.path = [self.x, self.end] self.walkCount = 0 self.vel = 3 self.hitbox = (self.x + 3, self.y+9,self.width+3,self.height+8) def update(self): if self.rect.y < (H - height2): self.rect.y += 10 #Bullet Sprite Class class Bullet(object): def __init__(self,x,y,radius,colour,facing): self.x = x self.y = y self.radius = radius self.colour = colour self.facing = facing self.vel = 8 * facing #multiplies velocity by facing (either 1, or-1, to determine direction of the bullet. -1 = left. 1 =right def draw(self,screen): pygame.draw.circle(screen, self.colour, (self.x,self.y), self.radius) def menu(): while True: screen.fill((0,0,0)) screen.blit(background2,(0,0)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() keys = pygame.key.get_pressed() if keys[pygame.K_RETURN]: game() pygame.display.update() def drawGameWindow(): screen.fill((0,0,0)) screen.blit(background, (0,0)) for bullet in bullets: bullet.draw(screen) all_sprites.update() #updating the sprites group player.draw(screen) all_sprites.draw(screen)#drawing all sprites to the screen player.update() pygame.display.update() #Code to handle all of our sprites. player = Player(20,500, 60, 60)#adding our player class to a variable and passing necessary variables/dimensions enemy = Enemy(600,500, 60, 60, 1) #adding enemy class to a variable all_sprites = pygame.sprite.Group()#creating our sprite group, so that we can add multiple sprites to it and draw them with ease bullets = [] #Main Game Loop def game(): running = True #creating the main game loop while running == True: clock.tick(56) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.quit() for bullet in bullets: if bullet.x < 800 and bullet.x > 0: #Making sure the bullet isn't off the screen. bullet.x += bullet.vel #move our bullet according to velocity else: bullets.pop(bullets.index(bullet)) #Deleting our bullet by popping (removing) from list. keys = pygame.key.get_pressed() # creating a variable to check for key presses if keys [pygame.K_UP]: if player.left: facing = -1 #determining direction of bullet (left = -1) else: facing = 1 #determining direction of bullet (right = +1) if len(bullets) < 6: #no. of bullets on screen at once. bullets.append(Bullet(round(player.x + player.width // 2), round(player.y + player.height // 2), 6, (0,0,0), facing)) if keys[pygame.K_LEFT] and player.x > player.vel: player.x -= player.vel player.right = False player.left = True player.standing = False elif keys[pygame.K_RIGHT]: player.x += player.vel player.right = True player.left = False player.standing = False if player.x >= W-width: player.x = W-width else: player.standing = True player.walkCount = 0 if not (player.isJump): if keys[pygame.K_SPACE]: player.isJump = True player.right = False player.left = False player.walkCount = 0 else: if player.jumpCount >= -11.5: neg = 1 if player.jumpCount < 0: neg = -1 player.y -= (player.jumpCount ** 2) * 0.5 * neg player.jumpCount -= 1 else: player.isJump = False player.jumpCount = 11.5 drawGameWindow() menu() pygame.quit() #End of our code.