SHARE
TWEET

Untitled

a guest Jul 17th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # by Robert Pfeiffer (Blubberquark Software) based on code by DaFluffyPotato
  2. import pygame, sys
  3.  
  4. clock = pygame.time.Clock()
  5.  
  6. from pygame.locals import *
  7. pygame.init() # initiates pygame
  8.  
  9. pygame.display.set_caption('Pygame Platformer')
  10.  
  11. TILE=16
  12. display = pygame.display.set_mode((300,200),pygame.SCALED) # initiate the window
  13.  
  14. moving_right = False
  15. moving_left = False
  16. vertical_momentum = 0
  17. air_timer = 0
  18.  
  19. grass_img = pygame.image.load('grass.png')
  20. dirt_img = pygame.image.load('dirt.png')
  21.  
  22. game_map_ascii="""
  23. ...................
  24. ...................
  25. ...................
  26. ...................
  27. .......#####.......
  28. ...................
  29. ##...............##
  30. EE###############EE
  31. EEEEEEEEEEEEEEEEEEE
  32. EEEEEEEEEEEEEEEEEEE
  33. EEEEEEEEEEEEEEEEEEE
  34. EEEEEEEEEEEEEEEEEEE
  35. EEEEEEEEEEEEEEEEEEE
  36. """
  37.  
  38. mapping={".": (False, None),
  39.          "#": (True, grass_img),
  40.          "E": (True, dirt_img)}
  41.  
  42. game_map=[]
  43. for line in game_map_ascii.split("\n"):
  44.     if len(line)==0:
  45.         continue
  46.     game_map.append([mapping[char] for char in line])
  47.  
  48. map_width=len(game_map[0])
  49. map_height=len(game_map)
  50.  
  51. player_img = pygame.image.load('player.png').convert()
  52. player_img.set_colorkey((255,255,255))
  53.  
  54. player_rect = pygame.Rect(100,100,5,13)
  55.  
  56. def collision_test(rect, tiles):
  57.     tile_left=rect.left//TILE
  58.     tile_right=(rect.right-1)//TILE
  59.     tile_top=rect.top//TILE
  60.     tile_bottom=(rect.bottom-1)//TILE
  61.     result=[]
  62.     for x in range(tile_left, tile_right+1):
  63.         for y in range(tile_top, tile_bottom+1):
  64.             try:
  65.                 if tiles[y][x][0]:
  66.                     result.append(pygame.Rect(x*TILE, y*TILE, TILE,TILE))
  67.             except:
  68.                 result.append(pygame.Rect(x*TILE, y*TILE, TILE,TILE))
  69.     return result
  70.  
  71. def move(rect,movement,tiles):
  72.     collision_types = {'top':False,'bottom':False,'right':False,'left':False}
  73.     dx, dy = movement
  74.     rect.x += dx
  75.     hit_list = collision_test(rect,tiles)
  76.     for tile in hit_list:
  77.         if dx > 0:
  78.             rect.right = tile.left
  79.             collision_types['right'] = True
  80.         elif dx < 0:
  81.             rect.left = tile.right
  82.             collision_types['left'] = True
  83.     rect.y += dy
  84.     hit_list = collision_test(rect,tiles)
  85.     for tile in hit_list:
  86.         if dy > 0:
  87.             rect.bottom = tile.top
  88.             collision_types['bottom'] = True
  89.         elif dy < 0:
  90.             rect.top = tile.bottom
  91.             collision_types['top'] = True
  92.     return rect, collision_types
  93.  
  94.  
  95. while True: # game loop
  96.     display.fill((146,244,255)) # clear screen by filling it with blue
  97.  
  98.     for x in range(map_width):
  99.         for y in range(map_height):
  100.             _, img = game_map[y][x]
  101.             if img:
  102.                 display.blit(img,(x*TILE,y*TILE))
  103.  
  104.     player_movement = [0,0]
  105.     if moving_right:
  106.         player_movement[0] += 2
  107.     if moving_left:
  108.         player_movement[0] -= 2
  109.     player_movement[1] += vertical_momentum
  110.     vertical_momentum += 0.2
  111.     if vertical_momentum > 3:
  112.         vertical_momentum = 3
  113.  
  114.     player_rect,collisions = move(player_rect, player_movement, game_map)
  115.  
  116.     if collisions['bottom']:
  117.         air_timer = 0
  118.         vertical_momentum = 0
  119.     else:
  120.         air_timer += 1
  121.  
  122.     display.blit(player_img,(player_rect.x,player_rect.y))
  123.  
  124.  
  125.     for event in pygame.event.get(): # event loop
  126.         if event.type == QUIT:
  127.             pygame.quit()
  128.             sys.exit()
  129.         if event.type == KEYDOWN:
  130.             if event.key == K_RIGHT:
  131.                 moving_right = True
  132.             if event.key == K_LEFT:
  133.                 moving_left = True
  134.             if event.key == K_UP:
  135.                 if air_timer < 6:
  136.                     vertical_momentum = -5
  137.         if event.type == KEYUP:
  138.             if event.key == K_RIGHT:
  139.                 moving_right = False
  140.             if event.key == K_LEFT:
  141.                 moving_left = False
  142.        
  143.     pygame.display.update()
  144.     clock.tick(60)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top