Advertisement
cookertron

Android Swipe Move

Jan 20th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. import pygame_sdl2
  2. pygame_sdl2.import_as_pygame()
  3. import pygame
  4. import sys
  5.  
  6. # define some colors
  7. BLACK = (0, 0, 0)
  8. WHITE = (255, 255, 255)
  9.  
  10. pygame.init()
  11. DS = pygame.display.set_mode((640, 480))
  12. CLOCK = pygame.time.Clock()
  13.  
  14. FPS = 120
  15.  
  16. W = DS.get_width()
  17. H = DS.get_height()
  18. HW, HH = int(W / 2), int(H / 2)
  19.  
  20. hx = 0
  21. hy = 1
  22. sx = HW
  23. sy = HH
  24. pressed = False
  25.  
  26. while True:
  27.     e = pygame.event.get()
  28.    
  29.     pygame.draw.rect(DS, WHITE, (sx, sy, 20, 20), 0)
  30.    
  31.     sx += hx
  32.     sy += hy
  33.    
  34.     mb = pygame.mouse.get_pressed()[0]
  35.    
  36.     if mb and not pressed:
  37.         mx1, my1 = pygame.mouse.get_pos()
  38.         pressed = True
  39.     elif not mb and pressed:
  40.         pressed = False
  41.         mx2, my2 = pygame.mouse.get_pos()
  42.         dx = mx2 - mx1
  43.         dy = my2 - my1
  44.         if dx > 200:
  45.             hx = 1
  46.             hy = 0
  47.         elif dx < -200:
  48.             hx = -1
  49.             hy = 0
  50.         elif dy > 200:
  51.             hx = 0
  52.             hy = 1
  53.         elif dy < -200:
  54.             hx = 0
  55.             hy = -1
  56.     if pressed:
  57.         pygame.draw.line(DS, WHITE, (mx1, my1), pygame.mouse.get_pos(), 1)
  58.            
  59.     pygame.display.update()
  60.     DS.fill(BLACK)
  61.     CLOCK.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement