Advertisement
cookertron

Snake Concept Pygame & PyDroid

Jan 20th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 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 = 30
  15.  
  16. W = DS.get_width()
  17. H = DS.get_height()
  18. HW, HH = int(W / 2), int(H / 2)
  19.  
  20. segmentSize = 20
  21. hx = 0
  22. hy = -segmentSize
  23. sx = HW
  24. sy = HH
  25.  
  26. tail = [[sx, sy + index * segmentSize] for index in range(10)]
  27.  
  28. for segment in tail:
  29.     pygame.draw.rect(DS, WHITE, (segment[0], segment[1], segmentSize, segmentSize), 0)
  30.  
  31. pressed = False
  32.  
  33. while True:
  34.     e = pygame.event.get()
  35.    
  36.     pygame.draw.rect(DS, WHITE, (sx, sy, segmentSize, segmentSize), 0)
  37.    
  38.     sx += hx
  39.     sy += hy
  40.    
  41.     tail.insert(0, [sx, sy])
  42.    
  43.     pygame.draw.rect(DS, BLACK, tail.pop() + [segmentSize, segmentSize], 0)
  44.    
  45.     mb = pygame.mouse.get_pressed()[0]
  46.    
  47.     if mb and not pressed:
  48.         mx1, my1 = pygame.mouse.get_pos()
  49.         pressed = True
  50.     elif not mb and pressed:
  51.         pressed = False
  52.         mx2, my2 = pygame.mouse.get_pos()
  53.         dx = mx2 - mx1
  54.         dy = my2 - my1
  55.         if dx > 200 and not hx:
  56.             hx = segmentSize
  57.             hy = 0
  58.         elif dx < -200 and not hx:
  59.             hx = -segmentSize
  60.             hy = 0
  61.         elif dy > 200 and not hy:
  62.             hx = 0
  63.             hy = segmentSize
  64.         elif dy < -200 and not hy:
  65.             hx = 0
  66.             hy = -segmentSize
  67.     if pressed:
  68.         pygame.draw.line(DS, WHITE, (mx1, my1), pygame.mouse.get_pos(), 1)
  69.            
  70.     pygame.display.update()
  71.     #DS.fill(BLACK)
  72.     CLOCK.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement