Guest User

Pygame Physics Explanation Script

a guest
Oct 17th, 2018
6,471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. #!/usr/bin/python3.4
  2. # setup #
  3. import pygame, sys
  4. # setup pygame/window #
  5. mainClock = pygame.time.Clock()
  6. from pygame.locals import *
  7. pygame.init()
  8. pygame.display.set_caption('Physics Explanation')
  9. screen = pygame.display.set_mode((500,500),0,32)
  10.  
  11. player = pygame.Rect(100,100,40,80)
  12.  
  13. tiles = [pygame.Rect(200,350,50,50),pygame.Rect(260,320,50,50)]
  14.  
  15. def collision_test(rect,tiles):
  16.     collisions = []
  17.     for tile in tiles:
  18.         if rect.colliderect(tile):
  19.             collisions.append(tile)
  20.     return collisions
  21.  
  22. def move(rect,movement,tiles): # movement = [5,2]
  23.     rect.x += movement[0]
  24.     collisions = collision_test(rect,tiles)
  25.     for tile in collisions:
  26.         if movement[0] > 0:
  27.             rect.right = tile.left
  28.         if movement[0] < 0:
  29.             rect.left = tile.right
  30.     rect.y += movement[1]
  31.     collisions = collision_test(rect,tiles)
  32.     for tile in collisions:
  33.         if movement[1] > 0:
  34.             rect.bottom = tile.top
  35.         if movement[1] < 0:
  36.             rect.top = tile.bottom
  37.     return rect
  38.  
  39. right = False
  40. left = False
  41. up = False
  42. down = False
  43.    
  44.  
  45. # loop #
  46. while True:
  47.    
  48.     # clear display #
  49.     screen.fill((0,0,0))
  50.  
  51.     movement = [0,0]
  52.     if right == True:
  53.         movement[0] += 5
  54.     if left == True:
  55.         movement[0] -= 5
  56.     if up == True:
  57.         movement[1] -= 5
  58.     if down == True:
  59.         movement[1] += 5
  60.  
  61.     player = move(player,movement,tiles)
  62.  
  63.     pygame.draw.rect(screen,(255,255,255),player)
  64.  
  65.     for tile in tiles:
  66.         pygame.draw.rect(screen,(255,0,0),tile)
  67.    
  68.     # event handling #
  69.     for event in pygame.event.get():
  70.         if event.type == QUIT:
  71.             pygame.quit()
  72.             sys.exit()
  73.         if event.type == KEYDOWN:
  74.             if event.key == K_RIGHT:
  75.                 right = True
  76.             if event.key == K_LEFT:
  77.                 left = True
  78.             if event.key == K_DOWN:
  79.                 down = True
  80.             if event.key == K_UP:
  81.                 up = True
  82.         if event.type == KEYUP:
  83.             if event.key == K_RIGHT:
  84.                 right = False
  85.             if event.key == K_LEFT:
  86.                 left = False
  87.             if event.key == K_DOWN:
  88.                 down = False
  89.             if event.key == K_UP:
  90.                 up = False
  91.    
  92.     # update display #
  93.     pygame.display.update()
  94.     mainClock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment