liquid.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python
  2. """This examples demonstrates a simplish water effect of an
  3. image. It attempts to create a hardware display surface that
  4. can use pageflipping for faster updates. Note that the colormap
  5. from the loaded GIF image is copied to the colormap for the
  6. display surface.
  7. This is based on the demo named F2KWarp by Brad Graham of Freedom2000
  8. done in BlitzBasic. I was just translating the BlitzBasic code to
  9. pygame to compare the results. I didn't bother porting the text and
  10. sound stuff, that's an easy enough challenge for the reader :]"""
  11. import pygame, os
  12. from pygame.locals import *
  13. from math import sin
  14. import time
  15. main_dir = os.path.split(os.path.abspath(__file__))[0]
  16. def main():
  17. #initialize and setup screen
  18. pygame.init()
  19. screen = pygame.display.set_mode((640, 480), HWSURFACE|DOUBLEBUF)
  20. #load image and quadruple
  21. imagename = os.path.join(main_dir, 'data', 'liquid.bmp')
  22. bitmap = pygame.image.load(imagename)
  23. bitmap = pygame.transform.scale2x(bitmap)
  24. bitmap = pygame.transform.scale2x(bitmap)
  25. #get the image and screen in the same format
  26. if screen.get_bitsize() == 8:
  27. screen.set_palette(bitmap.get_palette())
  28. else:
  29. bitmap = bitmap.convert()
  30. #prep some variables
  31. anim = 0.0
  32. #mainloop
  33. xblocks = range(0, 640, 20)
  34. yblocks = range(0, 480, 20)
  35. stopevents = QUIT, KEYDOWN, MOUSEBUTTONDOWN
  36. while 1:
  37. for e in pygame.event.get():
  38. if e.type in stopevents:
  39. return
  40. anim = anim + 0.02
  41. for x in xblocks:
  42. xpos = (x + (sin(anim + x * .01) * 15)) + 20
  43. for y in yblocks:
  44. ypos = (y + (sin(anim + y * .01) * 15)) + 20
  45. screen.blit(bitmap, (x, y), (xpos, ypos, 20, 20))
  46. pygame.display.flip()
  47. time.sleep(0.01)
  48. if __name__ == '__main__': main()
  49. """BTW, here is the code from the BlitzBasic example this was derived
  50. from. i've snipped the sound and text stuff out.
  51. -----------------------------------------------------------------
  52. ; Brad@freedom2000.com
  53. ; Load a bmp pic (800x600) and slice it into 1600 squares
  54. Graphics 640,480
  55. SetBuffer BackBuffer()
  56. bitmap$="f2kwarp.bmp"
  57. pic=LoadAnimImage(bitmap$,20,15,0,1600)
  58. ; use SIN to move all 1600 squares around to give liquid effect
  59. Repeat
  60. f=0:w=w+10:If w=360 Then w=0
  61. For y=0 To 599 Step 15
  62. For x = 0 To 799 Step 20
  63. f=f+1:If f=1600 Then f=0
  64. DrawBlock pic,(x+(Sin(w+x)*40))/1.7+80,(y+(Sin(w+y)*40))/1.7+60,f
  65. Next:Next:Flip:Cls
  66. Until KeyDown(1)
  67. """