Advertisement
furas

PyGame code modification and adding following camera

Jan 3rd, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.89 KB | None | 0 0
  1. # http://stackoverflow.com/questions/34566528/how-do-you-get-the-camera-to-follow-the-player-scrolling
  2.  
  3. import pygame
  4.  
  5. # --- CONSTANS --- UPPERCASE names
  6.  
  7. DISPLAY_WIDTH = 1200
  8. DISPLAY_HEIGHT = 800
  9.  
  10. RED   = (255,  0,  0)
  11. GREEN = (  0,255,  0)
  12.  
  13. BLACK = (  0,  0,  0)
  14.  
  15. BLOCK_WALL = "P"
  16. BLOCK_PLAYER = "$"
  17. #BLOCK_OTHER = ""
  18.  
  19. PLAYER_SIZE = 50
  20.  
  21. # --- CLASSES --- CamelCase names
  22.  
  23. # --- FUNCTIONS --- lower_case names
  24.  
  25. def draw_block(screen, rect, color=RED):
  26.     pygame.draw.rect(screen, color, rect)
  27.  
  28. # --- MAIN ---
  29.  
  30. # --- variables --- lower_case names
  31.  
  32. # two-dimensional list
  33. level =  [
  34.     "PPPPPPPPPPPP", # or sublist [ "P","P","P","P","P","P","P","P","P","P","P" ],
  35.     "P          P", # or sublist [ "P"," "," "," "," "," "," "," "," "," ","P" ],
  36.     "P        P P", # or sublist [ "P"," "," "," "," "," "," "," "," ","P","P" ],
  37.     "P          P", # or sublist [ "P"," "," "," "," "," "," "," "," "," ","P" ],
  38.     "P     P    P", # or sublist [ "P"," "," "," "," "," ","P"," "," "," ","P" ],
  39.     "P   $ P    P", # or sublist [ "P"," "," "," ","$"," ","P"," "," "," ","P" ],
  40.     "P          P", # or sublist [ "P"," "," "," "," "," "," "," "," "," ","P" ],
  41.     "P          P", # or sublist [ "P"," "," "," "," "," "," "," "," "," ","P" ],
  42.     "P   P      P", # or sublist [ "P"," "," "," ","P"," "," "," "," "," ","P" ],
  43.     "PPPPPPPPPPPP", # or sublist [ "P","P","P","P","P","P","P","P","P","P","P" ],
  44. ]
  45.  
  46. # --- init ----
  47.  
  48. pygame.init()
  49.  
  50. game_display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
  51. game_display_rect = game_display.get_rect()
  52.  
  53. pygame.display.set_caption("My Game")
  54.  
  55. # --- player ---
  56.  
  57. # TODO: find player position on map (`level`)
  58.  
  59. player_rect = pygame.Rect(0, 0, PLAYER_SIZE, PLAYER_SIZE)
  60. player_rect.center = game_display_rect.center
  61.  
  62. player_movement = 5
  63. player_change_y = 0
  64. player_change_x = 0
  65.  
  66. # --- walls ---
  67.  
  68. # CALCULATING THE SIZE OF THE BLOCKS SO THEY FIT W/ A VARIETY OF RESOLUTIONS
  69.  
  70. # scale > 1 to make level bigger then screen size - to use following camera
  71. scale = 2
  72.  
  73. block_width = DISPLAY_WIDTH/len(level[0]) * scale
  74. block_height = DISPLAY_HEIGHT/len(level) * scale
  75.  
  76. walls_list = []
  77.  
  78. for y, row in enumerate(level):
  79.     for x, element in enumerate(row):
  80.         if element == BLOCK_WALL:
  81.             rect = pygame.Rect(x*block_width, y*block_height, block_width, block_height)
  82.             walls_list.append( rect )
  83.  
  84. print('walls count:', len(walls_list))
  85.  
  86. # --- offset ---
  87.  
  88. offset_x = 0
  89. offset_y = 0
  90.  
  91. max_offset_x = (len(level[0]) * block_width) - game_display_rect.width
  92. max_offset_y = (len(level) * block_height) - game_display_rect.height
  93.  
  94. print('max_offset_x:', max_offset_x)
  95.  
  96. # --- mainloop ---
  97.  
  98. clock = pygame.time.Clock()
  99.  
  100. game_exit = False
  101.  
  102. while not game_exit:
  103.  
  104.     # --- events handling ---
  105.  
  106.     for event in pygame.event.get():
  107.        
  108.         # --- global events ---
  109.  
  110.         if event.type == pygame.QUIT:
  111.             game_exit = True
  112.  
  113.         if event.type == pygame.KEYDOWN:
  114.             if event.key == pygame.K_ESCAPE:
  115.                 game_exit = True
  116.                
  117.         # --- player events ---
  118.        
  119.         if event.type == pygame.KEYDOWN:
  120.             if event.key == pygame.K_UP:
  121.                 player_change_y -= player_movement
  122.  
  123.             if event.key == pygame.K_DOWN:
  124.                 player_change_y += player_movement
  125.  
  126.             if event.key == pygame.K_LEFT:
  127.                 player_change_x -= player_movement
  128.  
  129.             if event.key == pygame.K_RIGHT:
  130.                 player_change_x += player_movement
  131.  
  132.         elif event.type == pygame.KEYUP:
  133.             if event.key == pygame.K_UP:
  134.                 player_change_y += player_movement
  135.  
  136.             if event.key == pygame.K_DOWN:
  137.                 player_change_y -= player_movement
  138.  
  139.             if event.key == pygame.K_LEFT:
  140.                 player_change_x += player_movement
  141.  
  142.             if event.key == pygame.K_RIGHT:
  143.                 player_change_x -= player_movement
  144.  
  145.  
  146.     # --- calculations (without draws) ---
  147.  
  148.     # move player and check collision
  149.  
  150.     # left-right collision
  151.     if player_change_x:
  152.         player_rect.x += player_change_x
  153.  
  154.         for wall_rect in walls_list:
  155.  
  156.             if player_rect.colliderect(wall_rect):
  157.                 if player_change_x < 0: # move left
  158.                     player_rect.left = wall_rect.right                
  159.                 elif player_change_x > 0: # move right
  160.                     player_rect.right = wall_rect.left
  161.            
  162.     # up-down collision
  163.     if player_change_y:
  164.         player_rect.y += player_change_y
  165.  
  166.         for wall_rect in walls_list:
  167.  
  168.             if player_rect.colliderect(wall_rect):
  169.                 if player_change_y < 0: # move up
  170.                     player_rect.top = wall_rect.bottom
  171.                 elif player_change_y > 0: # move down
  172.                     player_rect.bottom = wall_rect.top
  173.  
  174.     # camera offset
  175.  
  176.     # left side stop
  177.     if player_rect.x > game_display_rect.centerx:
  178.         offset_x = game_display_rect.centerx - player_rect.x
  179.         # right side stop
  180.         if offset_x < -max_offset_x:
  181.             offset_x = -max_offset_x
  182.  
  183.     # top side stop
  184.     if player_rect.y > game_display_rect.centery:
  185.         offset_y = game_display_rect.centery - player_rect.y
  186.         # bottom side stop
  187.         if offset_y < -max_offset_y:
  188.             offset_y = -max_offset_y
  189.        
  190.     # --- draws (without calculations )---
  191.  
  192.     # clear screen
  193.     game_display.fill(BLACK)
  194.  
  195.     # draw walls with offset
  196.     for wall_rect in walls_list:
  197.         draw_block(game_display, wall_rect.move(offset_x, offset_y))
  198.        
  199.     # draw player with offset
  200.     pygame.draw.rect(game_display, GREEN, player_rect.move(offset_x, offset_y))
  201.  
  202.     clock.tick(120) # most monitors refresh screen with 60Hz - it means 60 FPS
  203.  
  204.     # send to monitor
  205.     pygame.display.update()
  206.  
  207. # --- the end ---
  208.  
  209. pygame.quit()
  210. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement