Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import sdl2.ext
  5.  
  6. if len(sys.argv) == 1:
  7. print ("Usage: imv <filename>")
  8. sys.exit(0)
  9.  
  10. sdl2.ext.init()
  11.  
  12.  
  13. factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE)
  14. sprite = factory.from_image(sys.argv[1])
  15.  
  16. rw,rh = sprite.size
  17. w = rw
  18. h = rh
  19. if w > 640:
  20. w = 640
  21. if h > 480:
  22. h = 480
  23.  
  24. window = sdl2.ext.Window(sys.argv[1], size=(w, h))
  25. window.show()
  26.  
  27. spriterenderer = factory.create_sprite_render_system(window)
  28. spriterenderer.render(sprite)
  29.  
  30. movspeed = 30
  31. lastx = 0
  32. lasty = 0
  33. def move(x,y):
  34. global lastx
  35. global lasty
  36. lastx = lastx + x * movspeed
  37. lasty = lasty + y * movspeed
  38.  
  39. if lastx > 0:
  40. lastx = 0
  41. if lasty > 0:
  42. lasty = 0
  43. if rh + lasty - h < 0:
  44. lasty = h - rh
  45. if rw + lastx - w < 0:
  46. lastx = w - rw
  47.  
  48. sprite.x = lastx
  49. sprite.y = lasty
  50. spriterenderer.render(sprite)
  51.  
  52. running = True
  53. while running:
  54. events = sdl2.ext.get_events()
  55. for event in events:
  56. if event.type == sdl2.SDL_QUIT:
  57. running = False
  58. break
  59. if event.type == sdl2.SDL_KEYDOWN:
  60. if event.key.keysym.sym == sdl2.SDLK_UP:
  61. move(0, 1)
  62. elif event.key.keysym.sym == sdl2.SDLK_DOWN:
  63. move(0, -1)
  64. elif event.key.keysym.sym == sdl2.SDLK_LEFT:
  65. move(1, 0)
  66. elif event.key.keysym.sym == sdl2.SDLK_RIGHT:
  67. move(-1, 0)
  68. sdl2.SDL_Delay(10)
  69.  
  70.  
  71. sdl2.ext.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement