Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. """
  2. This game was created by a kid
  3. so all things that you think are
  4. not nice to be in this code, I'm
  5. okay with it because i'm still
  6. learning
  7. """
  8.  
  9. import pygame, sys
  10.  
  11. clock = pygame.time.Clock()
  12.  
  13. from pygame.locals import *
  14.  
  15. pygame.init() # initiates pygame
  16.  
  17. pygame.display.set_caption('Pygame Platformer')
  18.  
  19. WINDOW_SIZE = (600, 400)
  20.  
  21. screen = pygame.display.set_mode(WINDOW_SIZE, 0, 32) # initiate the window
  22.  
  23. display = pygame.Surface((300, 200)) # used as the surface for rendering, which is scaled
  24.  
  25. print("Score = 0")
  26.  
  27. moving_right = False
  28. moving_left = False
  29. vertical_momentum = 0
  30. air_timer = 0
  31.  
  32. scroll = [0, 0]
  33.  
  34. def load_map(path):
  35. f = open(path + '.txt', 'r')
  36. data = f.read()
  37. f.close()
  38. data = data.split('\n')
  39. game_map = []
  40. for row in data:
  41. game_map.append(list(row))
  42. return game_map
  43.  
  44. global animation_frames
  45. animation_frames = {}
  46.  
  47. def load_animation(path, frame_dusrations):
  48. global animation_frames
  49. animation_name = path.split('/')[-1]
  50. animation_frame_data = []
  51. n = 0
  52. for frame in frame_dusrations:
  53. animation_frame_id = animation_name + '_' + str(n)
  54. img_loc = path + "/" + animation_frame_id + '.png'
  55. animation_image = pygame.image.load(img_loc).convert()
  56. animation_image.set_colorkey((255, 255, 255))
  57. animation_frames[animation_frame_id] = animation_image.copy()
  58. for i in range(frame):
  59. animation_frame_data.append(animation_frame_id)
  60. n += 1
  61. return animation_frame_data
  62.  
  63. def change_action(action_var, frame, new_value):
  64. if action_var != new_value:
  65. action_var = new_value
  66. frame = 0
  67. return action_var, frame
  68.  
  69. animation_database = {}
  70.  
  71. animation_database['run'] = load_animation('player_animations/run', [7, 7])
  72. animation_database['idle'] = load_animation('player_animations/idle', [7, 7, 40])
  73.  
  74. player_action = 'idle'
  75. player_frame = 0
  76. player_flip = False
  77.  
  78. game_map = load_map('map')
  79.  
  80. score = 0
  81.  
  82. grass_img = pygame.image.load('grass.png')
  83. dirt_img = pygame.image.load('dirt.png')
  84. stone_img = pygame.image.load('stone.png')
  85. hepler_img = pygame.image.load('robot.png')
  86. tree_img = pygame.image.load('tree.png')
  87. tree2_img = pygame.image.load('tree.png')
  88. huge_tree_img = pygame.image.load('huge_tree.png')
  89. coin_img = pygame.image.load('coin.png')
  90. coin_pos = [100, 99]
  91. tree_pos = [200, 42]
  92. tree2_pos = [110, -6]
  93. huge_tree_pos = [500, 6]
  94.  
  95. player_pos = [100, 100]
  96. player_rect = pygame.Rect(100, 100, 5, 13)
  97.  
  98. background_objects = [[0.25, [120, 10, 70, 400]], [0.25, [280, 30, 40, 400]], [0.5, [30, 40, 40, 400]], [0.5, [130, 90, 100, 400]], [0.5, [300, 80, 120, 400]], [0.5, [500, 10, 60, 500]], [0.25, [600, 50, 60, 358]], [0.25, [600, 0, 60, 500]], [0.5, [800, 10, 80, 800]]]
  99.  
  100. def collision_test(rect, tiles):
  101. hit_list = []
  102. for tile in tiles:
  103. if rect.colliderect(tile):
  104. hit_list.append(tile)
  105. return hit_list
  106.  
  107. def move(rect, movement, tiles):
  108. collision_types = {'top': False, 'bottom': False, 'right': False, 'left': False}
  109. rect.x += movement[0]
  110. hit_list = collision_test(rect, tiles)
  111. for tile in hit_list:
  112. if movement[0] > 0:
  113. rect.right = tile.left
  114. collision_types['right'] = True
  115. elif movement[0] < 0:
  116. rect.left = tile.right
  117. collision_types['left'] = True
  118. rect.y += movement[1]
  119. hit_list = collision_test(rect, tiles)
  120. for tile in hit_list:
  121. if movement[1] > 0:
  122. rect.bottom = tile.top
  123. collision_types['bottom'] = True
  124. elif movement[1] < 0:
  125. rect.top = tile.bottom
  126. collision_types['top'] = True
  127. return rect, collision_types
  128.  
  129. while True: # game loop
  130.  
  131. display.fill((146, 244, 255)) # clear screen by filling it with blue
  132.  
  133. scroll[0] += (player_rect.x - scroll[0] - 152)
  134. scroll[1] += (player_rect.y - scroll[1] - 106)
  135.  
  136. pygame.draw.rect(display, (7, 80, 75), pygame.Rect(0, 120, 300, 80))
  137. for background_object in background_objects:
  138. obj_rect = pygame.Rect(background_object[1][0]-scroll[0]*background_object[0],background_object[1][1]-scroll[1]*background_object[0],background_object[1][2],background_object[1][3])
  139. if background_object[0] == 0.5:
  140. pygame.draw.rect(display, (14, 222, 150), obj_rect)
  141. else:
  142. pygame.draw.rect(display, (9, 91, 85), obj_rect)
  143.  
  144. tile_rects = []
  145. y = 0
  146. for layer in game_map:
  147. x = 0
  148. for tile in layer:
  149. if tile == '1':
  150. display.blit(dirt_img, (x * 16 - scroll[0], y * 16 - scroll[1]))
  151. if tile == '2':
  152. display.blit(grass_img, (x * 16 - scroll[0], y * 16 - scroll[1]))
  153. if tile == '3':
  154. display.blit(stone_img, (x * 16 - scroll[0], y * 16 - scroll[1]))
  155. if tile != '0':
  156. tile_rects.append(pygame.Rect(x * 16, y * 16, 16, 16))
  157. x += 1
  158. y += 1
  159.  
  160. display.blit(hepler_img, (130, 100))
  161. display.blit(coin_img, (coin_pos[0] - scroll[0], coin_pos[1] - scroll[1]))
  162. display.blit(tree_img, (tree_pos[0] - scroll[0], tree_pos[1] - scroll[1]))
  163. display.blit(tree2_img, (tree2_pos[0] - scroll[0], tree2_pos[1] - scroll[1]))
  164. display.blit(huge_tree_img, (huge_tree_pos[0] - scroll[0], huge_tree_pos[1] - scroll[1]))
  165.  
  166. player_movement = [0, 0]
  167. if moving_right == True:
  168. player_movement[0] += 2
  169. if moving_left == True:
  170. player_movement[0] -= 2
  171. player_movement[1] += vertical_momentum
  172. vertical_momentum += 0.2
  173. if vertical_momentum > 3:
  174. vertical_momentum = 3
  175.  
  176. if player_movement[0] > 0:
  177. player_action, player_frame = change_action(player_action, player_frame, 'run')
  178. player_flip = False
  179. if player_movement[0] == 0:
  180. player_action, player_frame = change_action(player_action, player_frame, 'idle')
  181. if player_movement[0] < 0:
  182. player_action, player_frame = change_action(player_action, player_frame, 'run')
  183. player_flip = True
  184.  
  185. player_rect, collisions = move(player_rect, player_movement, tile_rects)
  186.  
  187. if collisions['bottom'] == True:
  188. air_timer = 0
  189. vertical_momentum = 0
  190. else:
  191. air_timer += 1
  192.  
  193. player_frame += 1
  194. if player_frame >= len(animation_database[player_action]):
  195. player_frame = 0
  196. player_img_id = animation_database[player_action][player_frame]
  197. player_img = animation_frames[player_img_id]
  198. display.blit(pygame.transform.flip(player_img, player_flip, False), (player_rect.x - scroll[0], player_rect.y - scroll[1]))
  199.  
  200. display.blit(player_img, (player_rect.x - scroll[0], player_rect.y - scroll[1]))
  201.  
  202. if player_rect.x > coin_pos[0] and player_rect.y == coin_pos[1]:
  203. coin_pos[0] = 10000
  204. print("Score = 1")
  205.  
  206. for event in pygame.event.get(): # event loop
  207. if event.type == QUIT:
  208. pygame.quit()
  209. sys.exit()
  210. if event.type == KEYDOWN:
  211. if event.key == K_RIGHT:
  212. moving_right = True
  213. if event.key == K_LEFT:
  214. moving_left = True
  215. if event.key == K_UP:
  216. if air_timer < 6:
  217. vertical_momentum = -5
  218. if event.type == KEYUP:
  219. if event.key == K_RIGHT:
  220. moving_right = False
  221. if event.key == K_LEFT:
  222. moving_left = False
  223.  
  224. screen.blit(pygame.transform.scale(display, WINDOW_SIZE), (0, 0))
  225. pygame.display.update()
  226. clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement