Advertisement
SmallBlue

Snake on python

Jan 28th, 2021
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. import pygame, sys
  2. import random
  3.  
  4. pygame.init()
  5.  
  6. x = 200
  7. y = 200
  8.  
  9. fx = 0
  10. fy = 0
  11.  
  12. followers = 0
  13.  
  14. screen = pygame.display.set_mode((500, 500))
  15. pygame.display.set_caption('Snake but from memory')
  16.  
  17. player = pygame.draw.rect(screen,(255,255,255),(1000,1000,50,50))
  18. body = []
  19.  
  20. fruit = pygame.draw.rect(screen,(255,0,0),(1000,1000,25,25))
  21.  
  22. lastkey = [(50,0)]
  23.  
  24. pastkeys = []
  25.  
  26.  
  27.  
  28.  
  29. def reset():
  30.  
  31.     global x
  32.     global y
  33.     global followers
  34.     global lastkey
  35.     global pastkeys
  36.     x = 200
  37.     y = 200
  38.     followers = 0
  39.  
  40.     lastkey = [(50,0)]
  41.  
  42.     pastkeys = []
  43.  
  44.     body = []
  45.  
  46. def generate_fruit():
  47.     global fx
  48.     global fy
  49.     fx = random.randint(0,9) * 50 #+ 13
  50.     fy = random.randint(0,9) * 50 #+ 13
  51.     if fx == x and fy == y:
  52.         print('Cant spawn there, snake head is occupying the space!')
  53.         generate_fruit()
  54.     else:
  55.         for rect in body:
  56.             if rect.x == fx and rect.y == fy:
  57.                 print('Cant spawn there, snake body is occupying the space!')
  58.                 generate_fruit()
  59.                 break
  60.         fx = fx + 2
  61.         fy = fy + 2
  62.     #print(fx, ' ', fy)
  63. reset()
  64. generate_fruit()
  65.  
  66. while True:
  67.     for event in pygame.event.get():
  68.         if event.type == pygame.QUIT: sys.exit()
  69.  
  70.         if event.type==pygame.KEYDOWN:
  71.             if event.key==pygame.K_RIGHT:
  72.                 lastkey.append((50,0))
  73.             elif event.key==pygame.K_LEFT:
  74.                 lastkey.append((-50,0))
  75.             elif event.key==pygame.K_UP:
  76.                 lastkey.append((0,-50))
  77.             elif event.key==pygame.K_DOWN:
  78.                 lastkey.append((0,50))
  79.             elif event.key==pygame.K_r:
  80.                 generate_fruit()
  81.             #print(lastkey)
  82.     x = x + lastkey[0][0]
  83.     y = y + lastkey[0][1]
  84.     #print(pastkeys)
  85.  
  86.     if len(lastkey) > 1:
  87.         lastkey.remove(lastkey[0])
  88.  
  89.     if x >= 500:
  90.         x = 0
  91.     elif x < 0:
  92.         x = 450
  93.  
  94.     if y >= 500:
  95.         y = 0
  96.     elif y < 0:
  97.         y = 450
  98.  
  99.     pastkeys.insert(0,(x, y))
  100.     #if len(pastkeys) >= 101:
  101.         #pastkeys.remove(pastkeys[100])
  102.  
  103.  
  104.     fruit = pygame.draw.rect(screen,(255,0,0),(fx,fy,25,25))
  105.  
  106.     player = pygame.draw.rect(screen,(100,255,100),(x,y,30,30))
  107.  
  108.     for _ in range(followers):
  109.         #print(_)
  110.         if _ == 0:
  111.             continue
  112.         body.append(pygame.draw.rect(screen,(150,255,150),(pastkeys[_][0],pastkeys[_][1],30,30)))
  113.         while len(body) > followers - 1:
  114.             body.remove(body[0])
  115.         #print(body)
  116.  
  117.  
  118.     if pygame.Rect.colliderect(player,fruit):
  119.         generate_fruit()
  120.         if followers == 0:
  121.             followers = followers + 2
  122.         else:
  123.             followers = followers + 1
  124.  
  125.     if pygame.Rect.collidelist(player,body) > -1:
  126.         pygame.time.wait(4000)
  127.         reset()
  128.  
  129.     pygame.display.update()
  130.     #print(x, ' ', y)
  131.     screen.fill((0,0,0))
  132.     pygame.time.delay(250)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement