Advertisement
Guest User

snake

a guest
Apr 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. import pygame, sys
  2. from math import *
  3. heroPos = 209
  4. clock = pygame.time.Clock()
  5. screen = pygame.display.set_mode([720,540])
  6. import random
  7. cells = []
  8. hey = bool
  9. pos = []
  10. applePos = random.randint(1,401)
  11. for x in range(100):
  12.     pos.append(0)
  13. lenght = 0
  14. clock = pygame.time.Clock()
  15.  
  16. dir = 0
  17. for column in range(20):
  18.     for line in range(20):
  19.         cells.append([51 + 22 * column, 51 + 22 * line, 21, 21])
  20.  
  21. while True:
  22.     if heroPos == applePos:
  23.         lenght += 1
  24.         applePos = random.randint(0, 401)
  25.  
  26.  
  27.     screen.fill((0,0,0))
  28.     pygame.draw.rect(screen, [0, 255, 0], cells[applePos])
  29.     for event in pygame.event.get():
  30.         if event.type == pygame.QUIT:
  31.             sys.exit(0)
  32.         if event.type == pygame.KEYDOWN and event.key == pygame.K_d:
  33.             lenght += 1
  34.         if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  35.             hey = True
  36.         if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT and not dir == 4:
  37.             dir = 2
  38.         if event.type == pygame.KEYDOWN and event.key == pygame.K_UP and dir != 3:
  39.             dir = 1
  40.         if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN and dir != 1:
  41.             dir = 3
  42.         if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT and dir != 2:
  43.             dir = 4
  44.  
  45.     pygame.draw.rect(screen, [255 , 255, 255], [50, 50, 440, 440], 3)
  46.     for x in range(20):
  47.         pygame.draw.line(screen,[255,255,255], [50 + 22 * x, 50], [50 + 22 * x, 490], 1)
  48.     for x in range(20):
  49.         pygame.draw.line(screen,[255,255,255], [50, 50 + 22 * x], [490, 50 + 22 * x], 1)
  50.  
  51.     if hey == True:
  52.         if True:
  53.             clock.tick(8)
  54.             if dir == 1:
  55.                 if heroPos % 20 != 0:
  56.                     heroPos -= 1
  57.                 else:
  58.                     print("stop")
  59.                     dir = 0
  60.                     hey = False
  61.                     heroPos = 209
  62.                     lenght = 0
  63.             if dir == 2:
  64.                 if heroPos < 380:
  65.                     heroPos += 20
  66.                 else:
  67.                     print("stop")
  68.                     dir = 0
  69.                     hey = False
  70.                     heroPos = 209
  71.                     lenght = 0
  72.             if dir == 3:
  73.                 if heroPos % 20 != 19:
  74.                     heroPos += 1
  75.                 else:
  76.                     print("stop")
  77.                     dir = 0
  78.                     hey = False
  79.                     heroPos = 209
  80.                     lenght = 0
  81.             if dir == 4:
  82.                 if heroPos > 20:
  83.                     heroPos -= 20
  84.                 else:
  85.                     print("stop")
  86.                     dir = 0
  87.                     hey = False
  88.                     heroPos = 209
  89.                     lenght = 0
  90.             if dir != 0:
  91.                 pos.insert(0, heroPos)
  92.                 pos.pop()
  93.     for x in range(lenght):
  94.         pygame.draw.rect(screen, [255, 0, 0], cells[pos[x + 1]])
  95.         if pos[x + 1] == heroPos:
  96.             print("stop")
  97.             dir = 0
  98.             hey = False
  99.             heroPos = 209
  100.             lenght = 0
  101.  
  102.  
  103.  
  104.  
  105.     pygame.draw.rect(screen, [255, 0, 0], cells[heroPos])
  106.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement