kaliboi

Untitled

Jan 31st, 2018
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.60 KB | None | 0 0
  1. MAIN CODE FILE:
  2.  
  3.  
  4. #importing features
  5. import pygame
  6. import time
  7. import random
  8. from Player_Final_Project import Player_function
  9. import Final_Project_Levels
  10.  
  11. #setting screen dimentions
  12. screen_width = 1280
  13. screen_height = 640
  14.  
  15. #Createing my main program
  16. def main():
  17.  
  18. pygame.init()
  19.  
  20. background_colour = (255,255,255)
  21.  
  22. #Displaying the screen
  23. screen = pygame.display.set_mode((screen_width, screen_height))
  24. pygame.display.set_caption('Final Project Game')
  25.  
  26.  
  27. #This creates the player for the game
  28. player = Player_function()
  29.  
  30.  
  31. #This code creates all of the levels I have made
  32. level_array = []
  33. level_array.append(Final_Project_Levels.level_1(player))
  34.  
  35.  
  36. #This sets the current level for the program
  37. active_level_number = 0
  38. active_level = level_array[active_level_number]
  39.  
  40.  
  41. current_sprite_list = pygame.sprite.Group()
  42. player.level = active_level
  43.  
  44.  
  45. #This sets the postion for the player at the start of the game
  46. # player.rect.x = 340
  47. # player.rect.y = screen_height - player.rect.height
  48. player.rect.x = 100
  49. player.rect.y = 100
  50. current_sprite_list.add(player)
  51.  
  52. #This is used for a while true loop (untill the user clicks off)
  53. close = False
  54.  
  55. #This is basically used to manage the updates of the screen (frames)
  56. time = pygame.time.Clock()
  57.  
  58.  
  59. #This is the while true loop fo the main program (everyone has to do this in pygame)
  60. while not close:
  61. for event in pygame.event.get():
  62. if event.type == pygame.QUIT:
  63. close = True
  64. if event.type == pygame.KEYDOWN:
  65. if event.key == pygame.K_w:
  66. player.walk_right()
  67. if event.key == pygame.K_a:
  68. player.walk_left()
  69. if event.key == pygame.K_w:
  70. player.jump_physics()
  71. if event.type == pygame.KEYUP:
  72. if event.key == pygame.K_w and player.change_in_x > 0:
  73. player.stop_walk()
  74. if event.key == pygame.K_a and player.change_in_x < 0:
  75. player.stop_walk()
  76.  
  77.  
  78.  
  79. #This code just updates a few things as the game progresses
  80. current_sprite_list.update()
  81. active_level.update_level()
  82.  
  83.  
  84. #This code stops the player from moving off the screen or hitting the walls
  85. if player.rect.x >= 500:
  86. difference = 500 - player.rect.x
  87. player.rect.x = 500
  88. active_level.snap_world(-difference)
  89. if player.rect.x <= 120:
  90. difference = 120 - player.rect.x
  91. player.rect.x = 120
  92. active_level.snap_world(difference)
  93.  
  94.  
  95. #This code basically draws the thigns to the screen
  96. active_level.output(screen)
  97. #current_sprite_list.output(screen)
  98.  
  99. #This code basically limits the FPS to 60
  100. time.tick(60)
  101.  
  102.  
  103.  
  104. screen.fill(background_colour)
  105.  
  106. #Outputting the screen to the user
  107. pygame.display.flip()
  108.  
  109.  
  110. pygame.quit()
  111.  
  112.  
  113.  
  114. #Callng the main function so the game runs
  115. if __name__ == "__main__":
  116. main()
  117.  
  118.  
  119.  
  120. lEVELS FILE:
  121.  
  122.  
  123. import pygame
  124. import Final_Project_Platforms
  125.  
  126. class Levels():
  127.  
  128. world_snap = 0
  129.  
  130. #This is a list of sprites used within the game
  131. platform_array = None
  132.  
  133. #This is the variable for the background image
  134. level_background = None
  135.  
  136. #This controls the movement of the screen with the level
  137. world_scroll = 0
  138. level_handleing = -1000
  139.  
  140. def __init__ (self, player):
  141.  
  142. #This code is used when a player collides with a platform
  143. self.platform_array = pygame.sprite.Group()
  144. self.player = player
  145.  
  146.  
  147. def update_level(self):
  148. #This code updates the level throughout
  149. self.platform_array.update()
  150.  
  151.  
  152. def output(self, screen):
  153. color = (255, 0, 0)
  154.  
  155. #This code draws the background
  156. screen.fill(color)
  157. screen.blit(self.background, (self.world_scroll // 3,0))
  158.  
  159. #This code draws the sprite lists
  160. self.platform_array.draw(screen)
  161.  
  162.  
  163. def snap_world(self, snap_x):
  164.  
  165. #This code takes into account the amount of shift in the world
  166. self.world_snap += snap_x
  167.  
  168. #This shifts all of the sprite lists
  169. for platform in self.platform_array:
  170. platform.rect.x += snap_x
  171.  
  172.  
  173. #Creating the first level
  174. class level_1(Levels):
  175.  
  176. def __init__(self, player):
  177.  
  178. #This code calls the parent constructor so the attribute player is known
  179. Levels.__init__(self, player)
  180.  
  181. background_color = (255, 255, 255)
  182.  
  183. self.background = pygame.image.load("better_background.jpg").convert()
  184. self.background.set_colorkey(background_color)
  185. self.level_limit = -2500
  186.  
  187. #This code puts the x and y values of the platform into the array
  188. level = [ [Final_Project_Platforms.grass_on_left, 500, 500],
  189. [Final_Project_Platforms.grass_in_middle, 570, 500],
  190. [Final_Project_Platforms.grass_on_right, 640, 500],
  191. [Final_Project_Platforms.grass_on_left, 800, 400],
  192. [Final_Project_Platforms.grass_in_middle, 870, 400],
  193. [Final_Project_Platforms.grass_on_right, 940, 400],
  194. [Final_Project_Platforms.grass_on_left, 1000, 500],
  195. [Final_Project_Platforms.grass_in_middle, 1070, 500],
  196. [Final_Project_Platforms.grass_on_right, 1140, 500],
  197. ]
  198.  
  199. #This code goes through the array and adds the platforms
  200. for platforms in level:
  201. tile = Final_Project_Platforms.platforms(platforms[0])
  202. tile.rect.x = platforms[1]
  203. tile.rect.y = platforms[2]
  204. tile.player = self.player
  205. self.platform_array.add(tile)
  206.  
  207.  
  208.  
  209. PLATFORMS FILE:
  210.  
  211.  
  212. import pygame
  213.  
  214. from Sprite_Sheet_Program_Final_Project import Sprites
  215.  
  216. grass_on_left = (576, 720, 70, 70)
  217. grass_on_right = (576, 576, 70, 70)
  218. grass_in_middle = (504, 576, 70, 70)
  219. stone_left = (432, 720, 70, 40)
  220. stone_middle = (648, 648, 70, 40)
  221. stone_right = (792, 648, 70, 40)
  222.  
  223. class platforms(pygame.sprite.Sprite):
  224.  
  225. def __init__(self, sprite_sheet_data):
  226.  
  227. pygame.sprite.Sprite.__init__(self)
  228.  
  229.  
  230. sprite_tile = Sprites("platforms_spritesheet.png")
  231.  
  232. self.blank = sprite_tile.grab_image(sprite_sheet_data[0],
  233. sprite_sheet_data[1],
  234. sprite_sheet_data[2],
  235. sprite_sheet_data[3])
  236. #color = pygame.Color ( 0,20,20)
  237. #self.image = pygame.Surface([10, 10])
  238. #self.image.fill(color)
  239. #self.rect = self.image.get_rect()
  240. self.rect = self.blank.get_rect()
  241.  
  242.  
  243. PLAYER FILE:
  244.  
  245.  
  246. import pygame
  247.  
  248. #Importing the relevent class
  249. from Sprite_Sheet_Program_Final_Project import Sprites
  250.  
  251. #Setting the player calss as a sprite
  252. class Player_function(pygame.sprite.Sprite):
  253.  
  254. #Setting the speed
  255. change_in_x = 0
  256. change_in_y = 0
  257.  
  258. #A list that holds all of the sprite images for the relevent walking direction
  259. walking_sprite_left = []
  260. walking_sprite_right = []
  261.  
  262. #Setting the direction of the player in the game
  263. direction_facing = "Right"
  264.  
  265. level = None
  266.  
  267. #Setting the method
  268. def __init__(self):
  269.  
  270. #Calling the attributes
  271. pygame.sprite.Sprite.__init__(self)
  272.  
  273. #Setting where the images are pulled from
  274. sprite_character = Sprites("running_for_game.png")
  275.  
  276. #Load all the right facing images into a list
  277. blank = sprite_character.grab_image(0, 0, 64, 64)
  278. self.walking_sprite_right.append(blank)
  279. blank = sprite_character.grab_image(66, 0, 64, 64)
  280. self.walking_sprite_right.append(blank)
  281. blank = sprite_character.grab_image(132, 0, 64, 64)
  282. self.walking_sprite_right.append(blank)
  283. blank = sprite_character.grab_image(0, 93, 64, 64)
  284. self.walking_sprite_right.append(blank)
  285. blank = sprite_character.grab_image(66, 93, 64, 64)
  286. self.walking_sprite_right.append(blank)
  287. blank = sprite_character.grab_image(132, 93, 64, 64)
  288. self.walking_sprite_right.append(blank)
  289. blank = sprite_character.grab_image(0, 186, 64, 64)
  290. self.walking_sprite_right.append(blank)
  291.  
  292.  
  293. #Load all the right facing images again and then flip them so they are now left
  294. blank = sprite_character.grab_image(0, 0, 64, 64)
  295. blank = pygame.transform.flip(blank, True, False)
  296. self.walking_sprite_right.append(blank)
  297. blank = sprite_character.grab_image(66, 0, 64, 64)
  298. blank = pygame.transform.flip(blank, True, False)
  299. self.walking_sprite_right.append(blank)
  300. blank = sprite_character.grab_image(132, 0, 64, 64)
  301. blank = pygame.transform.flip(blank, True, False)
  302. self.walking_sprite_right.append(blank)
  303. blank = sprite_character.grab_image(0, 93, 64, 64)
  304. blank = pygame.transform.flip(blank, True, False)
  305. self.walking_sprite_right.append(blank)
  306. blank = sprite_character.grab_image(66, 93, 64, 64)
  307. blank = pygame.transform.flip(blank, True, False)
  308. self.walking_sprite_right.append(blank)
  309. blank = sprite_character.grab_image(132, 93, 64, 64)
  310. blank = pygame.transform.flip(blank, True, False)
  311. self.walking_sprite_right.append(blank)
  312. blank = sprite_character.grab_image(0, 186, 64, 64)
  313. blank = pygame.transform.flip(blank, True, False)
  314. self.walking_sprite_right.append(blank)
  315.  
  316.  
  317. #Setting the image for the player to start on
  318. self.blank = self.walking_sprite_right[0]
  319.  
  320. #Referencing to the image rect
  321. self.rect = self.blank.get_rect()
  322.  
  323. def movement(self):
  324.  
  325. #Defines the physics in our game (gravity)
  326. self.gravity()
  327.  
  328. #Creates movement for the player (left/right)
  329. self.rect.x += self.change_in_x
  330. position = self.rect.x + self.level.world_snap
  331. if self.direction == "R":
  332. current_frame = (position // 30) % len(self.walking_sprite_right)
  333. self.blank = self.walking_sprite_right[current_frame]
  334. else:
  335. current_frame = (position // 30) % len(self.walking_sprite_left)
  336. self.blank = self.walking_sprite_left[current_frame]
  337.  
  338. #Seeing if we have collided with anything
  339. hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
  340. for block in hit_list:
  341. #Set the right side to the left side of the item we hit
  342. if self.change_x > 0:
  343. self.rect.right = block.rect.left
  344. elif self.change_x < 0:
  345. #Set the left side to the right of the item we hit.
  346. self.rect.left = block.rect.right
  347.  
  348. #Enables movement up and down
  349. self.rect.y += self.change_in_y
  350.  
  351. #Seeing if we have collided with anything
  352. hit_list = pygame.sprite.spritecollide(self, self.level.platform_list,False)
  353. for block in hit_list:
  354.  
  355. #re-orienting position based on the top/bottom of the object
  356. if self.change_in_y > 0:
  357. self.rect.bottom = block.rect.top
  358. elif self.change_in_y < 0:
  359. self.rect.top = block.rect.bottom
  360.  
  361. #Stopping the vertical movement
  362. self.change_in_y = 0
  363.  
  364. #If collision with a platform chnage in the x direction
  365. if isinstance(block, MovingPlatform):
  366. self.rect.x += block.change_in_x
  367.  
  368. def gravity_physics(self):
  369.  
  370. if self.change_in_y == 0:
  371. self.change_in_y = 1
  372. else:
  373. self.change_in_y += .35
  374.  
  375. #If we are going to hit the ground then this if statement will kick in
  376. if self.rect.y >= screen_height - self.rect.height and self.change_in_y >= 0:
  377. self.change_in_y = 0
  378. self.rect.y = screen_height - self.rect.height
  379.  
  380. def jump_physics(self):
  381.  
  382. #This function moves the character downwards when the user has hit the jump button
  383. self.rect.y += 2
  384. platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_array, False)
  385. self.rect.y -= 2
  386.  
  387.  
  388. def walk_right(self):
  389.  
  390. self.change_in_x = 6
  391. self.direction = "R"
  392.  
  393. def walk_left(self):
  394.  
  395. self.change_in_x = -6
  396. self.direction = "L"
  397.  
  398. def stop_walk(self):
  399.  
  400. self.change_in_x = 0
  401.  
  402.  
  403.  
  404.  
  405. SPRITE SHEET FILE:
  406.  
  407. import pygame
  408.  
  409. class Sprites(object):
  410.  
  411. #Setting the Sprite sheet image
  412. sprite_character = None
  413.  
  414. #Setting the Constructor with the file name that the sprite sheet is located in
  415. def __init__(self, images_for_game):
  416.  
  417. #Load the sprtie sheet images
  418. self.sprite_character = pygame.image.load(images_for_game).convert()
  419.  
  420. def grab_image(self, x, y, width, height):
  421.  
  422. white = (255, 255, 255)
  423.  
  424. #Create a blank image first of all
  425. blank = pygame.Surface([width, height]).convert()
  426.  
  427. #Copying the image into a smaller state
  428. blank.blit(self.sprite_character, (0, 0), (x, y, width, height))
  429.  
  430. #Set black to the transparent colour
  431. blank.set_colorkey(white)
  432.  
  433. #Returning the finished product
  434. return blank
Advertisement
Add Comment
Please, Sign In to add comment