Advertisement
Guest User

pygame blitting/camera test

a guest
Nov 22nd, 2013
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. from pygame import *
  2. import random
  3.  
  4. init()
  5. screen = display.set_mode((0,0), FULLSCREEN)
  6. sx, sy = screen.get_size()
  7.  
  8. wx, wy = 4000, 4000
  9. worldmap = Surface((wx, wy))
  10.  
  11. worldmap.fill((40, 40, 40))
  12. for _ in range(1000):
  13. c = random.randint(20, 60)
  14. x = random.randint(0, wx)
  15. y = random.randint(0, wy)
  16. r = random.randint(wx // 20, wx // 10)
  17. draw.circle(worldmap, (c, c, c), (x, y), r)
  18.  
  19. playing = True
  20. px, py = wx // 2, wy // 2
  21. clock = time.Clock()
  22. f = font.Font(None, 64)
  23. while not any(e.type == KEYDOWN and e.key == K_ESCAPE for e in event.get()):
  24. dt = 0.001 * clock.tick()
  25. k = key.get_pressed()
  26.  
  27. # Player position in world coordinates
  28. px += (k[K_RIGHT] - k[K_LEFT]) * 1000 * dt
  29. py += (k[K_DOWN] - k[K_UP]) * 1000 * dt
  30. px = min(max(px, 0), wx)
  31. py = min(max(py, 0), wy)
  32.  
  33. # Camera position (ie center of the screen in world coordinates)
  34. cx = min(max(px, sx//2), wx-sx//2)
  35. cy = min(max(py, sy//2), wy-sy//2)
  36.  
  37. # draw world map
  38. screen.blit(worldmap, (sx//2 - cx, sy//2 - cy))
  39. # draw player
  40. draw.circle(screen, (0, 0, 100), (int(sx//2 + px - cx), int(sy//2 + py - cy)), 20)
  41. # fps counter
  42. screen.blit(f.render("%.1ffps" % clock.get_fps(), True, (255, 255, 255)), (0, 0))
  43.  
  44. display.flip()
  45.  
  46. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement