Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MAIN CODE FILE:
- #importing features
- import pygame
- import time
- import random
- from Player_Final_Project import Player_function
- import Final_Project_Levels
- #setting screen dimentions
- screen_width = 1280
- screen_height = 640
- #Createing my main program
- def main():
- pygame.init()
- background_colour = (255,255,255)
- #Displaying the screen
- screen = pygame.display.set_mode((screen_width, screen_height))
- pygame.display.set_caption('Final Project Game')
- #This creates the player for the game
- player = Player_function()
- #This code creates all of the levels I have made
- level_array = []
- level_array.append(Final_Project_Levels.level_1(player))
- #This sets the current level for the program
- active_level_number = 0
- active_level = level_array[active_level_number]
- current_sprite_list = pygame.sprite.Group()
- player.level = active_level
- #This sets the postion for the player at the start of the game
- # player.rect.x = 340
- # player.rect.y = screen_height - player.rect.height
- player.rect.x = 100
- player.rect.y = 100
- current_sprite_list.add(player)
- #This is used for a while true loop (untill the user clicks off)
- close = False
- #This is basically used to manage the updates of the screen (frames)
- time = pygame.time.Clock()
- #This is the while true loop fo the main program (everyone has to do this in pygame)
- while not close:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- close = True
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_w:
- player.walk_right()
- if event.key == pygame.K_a:
- player.walk_left()
- if event.key == pygame.K_w:
- player.jump_physics()
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_w and player.change_in_x > 0:
- player.stop_walk()
- if event.key == pygame.K_a and player.change_in_x < 0:
- player.stop_walk()
- #This code just updates a few things as the game progresses
- current_sprite_list.update()
- active_level.update_level()
- #This code stops the player from moving off the screen or hitting the walls
- if player.rect.x >= 500:
- difference = 500 - player.rect.x
- player.rect.x = 500
- active_level.snap_world(-difference)
- if player.rect.x <= 120:
- difference = 120 - player.rect.x
- player.rect.x = 120
- active_level.snap_world(difference)
- #This code basically draws the thigns to the screen
- active_level.output(screen)
- #current_sprite_list.output(screen)
- #This code basically limits the FPS to 60
- time.tick(60)
- screen.fill(background_colour)
- #Outputting the screen to the user
- pygame.display.flip()
- pygame.quit()
- #Callng the main function so the game runs
- if __name__ == "__main__":
- main()
- lEVELS FILE:
- import pygame
- import Final_Project_Platforms
- class Levels():
- world_snap = 0
- #This is a list of sprites used within the game
- platform_array = None
- #This is the variable for the background image
- level_background = None
- #This controls the movement of the screen with the level
- world_scroll = 0
- level_handleing = -1000
- def __init__ (self, player):
- #This code is used when a player collides with a platform
- self.platform_array = pygame.sprite.Group()
- self.player = player
- def update_level(self):
- #This code updates the level throughout
- self.platform_array.update()
- def output(self, screen):
- color = (255, 0, 0)
- #This code draws the background
- screen.fill(color)
- screen.blit(self.background, (self.world_scroll // 3,0))
- #This code draws the sprite lists
- self.platform_array.draw(screen)
- def snap_world(self, snap_x):
- #This code takes into account the amount of shift in the world
- self.world_snap += snap_x
- #This shifts all of the sprite lists
- for platform in self.platform_array:
- platform.rect.x += snap_x
- #Creating the first level
- class level_1(Levels):
- def __init__(self, player):
- #This code calls the parent constructor so the attribute player is known
- Levels.__init__(self, player)
- background_color = (255, 255, 255)
- self.background = pygame.image.load("better_background.jpg").convert()
- self.background.set_colorkey(background_color)
- self.level_limit = -2500
- #This code puts the x and y values of the platform into the array
- level = [ [Final_Project_Platforms.grass_on_left, 500, 500],
- [Final_Project_Platforms.grass_in_middle, 570, 500],
- [Final_Project_Platforms.grass_on_right, 640, 500],
- [Final_Project_Platforms.grass_on_left, 800, 400],
- [Final_Project_Platforms.grass_in_middle, 870, 400],
- [Final_Project_Platforms.grass_on_right, 940, 400],
- [Final_Project_Platforms.grass_on_left, 1000, 500],
- [Final_Project_Platforms.grass_in_middle, 1070, 500],
- [Final_Project_Platforms.grass_on_right, 1140, 500],
- ]
- #This code goes through the array and adds the platforms
- for platforms in level:
- tile = Final_Project_Platforms.platforms(platforms[0])
- tile.rect.x = platforms[1]
- tile.rect.y = platforms[2]
- tile.player = self.player
- self.platform_array.add(tile)
- PLATFORMS FILE:
- import pygame
- from Sprite_Sheet_Program_Final_Project import Sprites
- grass_on_left = (576, 720, 70, 70)
- grass_on_right = (576, 576, 70, 70)
- grass_in_middle = (504, 576, 70, 70)
- stone_left = (432, 720, 70, 40)
- stone_middle = (648, 648, 70, 40)
- stone_right = (792, 648, 70, 40)
- class platforms(pygame.sprite.Sprite):
- def __init__(self, sprite_sheet_data):
- pygame.sprite.Sprite.__init__(self)
- sprite_tile = Sprites("platforms_spritesheet.png")
- self.blank = sprite_tile.grab_image(sprite_sheet_data[0],
- sprite_sheet_data[1],
- sprite_sheet_data[2],
- sprite_sheet_data[3])
- #color = pygame.Color ( 0,20,20)
- #self.image = pygame.Surface([10, 10])
- #self.image.fill(color)
- #self.rect = self.image.get_rect()
- self.rect = self.blank.get_rect()
- PLAYER FILE:
- import pygame
- #Importing the relevent class
- from Sprite_Sheet_Program_Final_Project import Sprites
- #Setting the player calss as a sprite
- class Player_function(pygame.sprite.Sprite):
- #Setting the speed
- change_in_x = 0
- change_in_y = 0
- #A list that holds all of the sprite images for the relevent walking direction
- walking_sprite_left = []
- walking_sprite_right = []
- #Setting the direction of the player in the game
- direction_facing = "Right"
- level = None
- #Setting the method
- def __init__(self):
- #Calling the attributes
- pygame.sprite.Sprite.__init__(self)
- #Setting where the images are pulled from
- sprite_character = Sprites("running_for_game.png")
- #Load all the right facing images into a list
- blank = sprite_character.grab_image(0, 0, 64, 64)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(66, 0, 64, 64)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(132, 0, 64, 64)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(0, 93, 64, 64)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(66, 93, 64, 64)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(132, 93, 64, 64)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(0, 186, 64, 64)
- self.walking_sprite_right.append(blank)
- #Load all the right facing images again and then flip them so they are now left
- blank = sprite_character.grab_image(0, 0, 64, 64)
- blank = pygame.transform.flip(blank, True, False)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(66, 0, 64, 64)
- blank = pygame.transform.flip(blank, True, False)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(132, 0, 64, 64)
- blank = pygame.transform.flip(blank, True, False)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(0, 93, 64, 64)
- blank = pygame.transform.flip(blank, True, False)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(66, 93, 64, 64)
- blank = pygame.transform.flip(blank, True, False)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(132, 93, 64, 64)
- blank = pygame.transform.flip(blank, True, False)
- self.walking_sprite_right.append(blank)
- blank = sprite_character.grab_image(0, 186, 64, 64)
- blank = pygame.transform.flip(blank, True, False)
- self.walking_sprite_right.append(blank)
- #Setting the image for the player to start on
- self.blank = self.walking_sprite_right[0]
- #Referencing to the image rect
- self.rect = self.blank.get_rect()
- def movement(self):
- #Defines the physics in our game (gravity)
- self.gravity()
- #Creates movement for the player (left/right)
- self.rect.x += self.change_in_x
- position = self.rect.x + self.level.world_snap
- if self.direction == "R":
- current_frame = (position // 30) % len(self.walking_sprite_right)
- self.blank = self.walking_sprite_right[current_frame]
- else:
- current_frame = (position // 30) % len(self.walking_sprite_left)
- self.blank = self.walking_sprite_left[current_frame]
- #Seeing if we have collided with anything
- hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
- for block in hit_list:
- #Set the right side to the left side of the item we hit
- if self.change_x > 0:
- self.rect.right = block.rect.left
- elif self.change_x < 0:
- #Set the left side to the right of the item we hit.
- self.rect.left = block.rect.right
- #Enables movement up and down
- self.rect.y += self.change_in_y
- #Seeing if we have collided with anything
- hit_list = pygame.sprite.spritecollide(self, self.level.platform_list,False)
- for block in hit_list:
- #re-orienting position based on the top/bottom of the object
- if self.change_in_y > 0:
- self.rect.bottom = block.rect.top
- elif self.change_in_y < 0:
- self.rect.top = block.rect.bottom
- #Stopping the vertical movement
- self.change_in_y = 0
- #If collision with a platform chnage in the x direction
- if isinstance(block, MovingPlatform):
- self.rect.x += block.change_in_x
- def gravity_physics(self):
- if self.change_in_y == 0:
- self.change_in_y = 1
- else:
- self.change_in_y += .35
- #If we are going to hit the ground then this if statement will kick in
- if self.rect.y >= screen_height - self.rect.height and self.change_in_y >= 0:
- self.change_in_y = 0
- self.rect.y = screen_height - self.rect.height
- def jump_physics(self):
- #This function moves the character downwards when the user has hit the jump button
- self.rect.y += 2
- platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_array, False)
- self.rect.y -= 2
- def walk_right(self):
- self.change_in_x = 6
- self.direction = "R"
- def walk_left(self):
- self.change_in_x = -6
- self.direction = "L"
- def stop_walk(self):
- self.change_in_x = 0
- SPRITE SHEET FILE:
- import pygame
- class Sprites(object):
- #Setting the Sprite sheet image
- sprite_character = None
- #Setting the Constructor with the file name that the sprite sheet is located in
- def __init__(self, images_for_game):
- #Load the sprtie sheet images
- self.sprite_character = pygame.image.load(images_for_game).convert()
- def grab_image(self, x, y, width, height):
- white = (255, 255, 255)
- #Create a blank image first of all
- blank = pygame.Surface([width, height]).convert()
- #Copying the image into a smaller state
- blank.blit(self.sprite_character, (0, 0), (x, y, width, height))
- #Set black to the transparent colour
- blank.set_colorkey(white)
- #Returning the finished product
- return blank
Advertisement
Add Comment
Please, Sign In to add comment