Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # http://stackoverflow.com/questions/34566528/how-do-you-get-the-camera-to-follow-the-player-scrolling
- import pygame
- # --- CONSTANS --- UPPERCASE names
- DISPLAY_WIDTH = 1200
- DISPLAY_HEIGHT = 800
- RED = (255, 0, 0)
- GREEN = ( 0,255, 0)
- BLACK = ( 0, 0, 0)
- BLOCK_WALL = "P"
- BLOCK_PLAYER = "$"
- #BLOCK_OTHER = ""
- PLAYER_SIZE = 50
- # --- CLASSES --- CamelCase names
- # --- FUNCTIONS --- lower_case names
- def draw_block(screen, rect, color=RED):
- pygame.draw.rect(screen, color, rect)
- # --- MAIN ---
- # --- variables --- lower_case names
- # two-dimensional list
- level = [
- "PPPPPPPPPPPP", # or sublist [ "P","P","P","P","P","P","P","P","P","P","P" ],
- "P P", # or sublist [ "P"," "," "," "," "," "," "," "," "," ","P" ],
- "P P P", # or sublist [ "P"," "," "," "," "," "," "," "," ","P","P" ],
- "P P", # or sublist [ "P"," "," "," "," "," "," "," "," "," ","P" ],
- "P P P", # or sublist [ "P"," "," "," "," "," ","P"," "," "," ","P" ],
- "P $ P P", # or sublist [ "P"," "," "," ","$"," ","P"," "," "," ","P" ],
- "P P", # or sublist [ "P"," "," "," "," "," "," "," "," "," ","P" ],
- "P P", # or sublist [ "P"," "," "," "," "," "," "," "," "," ","P" ],
- "P P P", # or sublist [ "P"," "," "," ","P"," "," "," "," "," ","P" ],
- "PPPPPPPPPPPP", # or sublist [ "P","P","P","P","P","P","P","P","P","P","P" ],
- ]
- # --- init ----
- pygame.init()
- game_display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
- game_display_rect = game_display.get_rect()
- pygame.display.set_caption("My Game")
- # --- player ---
- # TODO: find player position on map (`level`)
- player_rect = pygame.Rect(0, 0, PLAYER_SIZE, PLAYER_SIZE)
- player_rect.center = game_display_rect.center
- player_movement = 5
- player_change_y = 0
- player_change_x = 0
- # --- walls ---
- # CALCULATING THE SIZE OF THE BLOCKS SO THEY FIT W/ A VARIETY OF RESOLUTIONS
- # scale > 1 to make level bigger then screen size - to use following camera
- scale = 2
- block_width = DISPLAY_WIDTH/len(level[0]) * scale
- block_height = DISPLAY_HEIGHT/len(level) * scale
- walls_list = []
- for y, row in enumerate(level):
- for x, element in enumerate(row):
- if element == BLOCK_WALL:
- rect = pygame.Rect(x*block_width, y*block_height, block_width, block_height)
- walls_list.append( rect )
- print('walls count:', len(walls_list))
- # --- offset ---
- offset_x = 0
- offset_y = 0
- max_offset_x = (len(level[0]) * block_width) - game_display_rect.width
- max_offset_y = (len(level) * block_height) - game_display_rect.height
- print('max_offset_x:', max_offset_x)
- # --- mainloop ---
- clock = pygame.time.Clock()
- game_exit = False
- while not game_exit:
- # --- events handling ---
- for event in pygame.event.get():
- # --- global events ---
- if event.type == pygame.QUIT:
- game_exit = True
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- game_exit = True
- # --- player events ---
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_UP:
- player_change_y -= player_movement
- if event.key == pygame.K_DOWN:
- player_change_y += player_movement
- if event.key == pygame.K_LEFT:
- player_change_x -= player_movement
- if event.key == pygame.K_RIGHT:
- player_change_x += player_movement
- elif event.type == pygame.KEYUP:
- if event.key == pygame.K_UP:
- player_change_y += player_movement
- if event.key == pygame.K_DOWN:
- player_change_y -= player_movement
- if event.key == pygame.K_LEFT:
- player_change_x += player_movement
- if event.key == pygame.K_RIGHT:
- player_change_x -= player_movement
- # --- calculations (without draws) ---
- # move player and check collision
- # left-right collision
- if player_change_x:
- player_rect.x += player_change_x
- for wall_rect in walls_list:
- if player_rect.colliderect(wall_rect):
- if player_change_x < 0: # move left
- player_rect.left = wall_rect.right
- elif player_change_x > 0: # move right
- player_rect.right = wall_rect.left
- # up-down collision
- if player_change_y:
- player_rect.y += player_change_y
- for wall_rect in walls_list:
- if player_rect.colliderect(wall_rect):
- if player_change_y < 0: # move up
- player_rect.top = wall_rect.bottom
- elif player_change_y > 0: # move down
- player_rect.bottom = wall_rect.top
- # camera offset
- # left side stop
- if player_rect.x > game_display_rect.centerx:
- offset_x = game_display_rect.centerx - player_rect.x
- # right side stop
- if offset_x < -max_offset_x:
- offset_x = -max_offset_x
- # top side stop
- if player_rect.y > game_display_rect.centery:
- offset_y = game_display_rect.centery - player_rect.y
- # bottom side stop
- if offset_y < -max_offset_y:
- offset_y = -max_offset_y
- # --- draws (without calculations )---
- # clear screen
- game_display.fill(BLACK)
- # draw walls with offset
- for wall_rect in walls_list:
- draw_block(game_display, wall_rect.move(offset_x, offset_y))
- # draw player with offset
- pygame.draw.rect(game_display, GREEN, player_rect.move(offset_x, offset_y))
- clock.tick(120) # most monitors refresh screen with 60Hz - it means 60 FPS
- # send to monitor
- pygame.display.update()
- # --- the end ---
- pygame.quit()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement