Advertisement
Bad_Programist

Untitled

Feb 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.88 KB | None | 0 0
  1. import pygame
  2. from random import randint
  3. pygame.init()
  4. pygame.font.init()
  5. myfont = pygame.font.SysFont('Helvetica', 90)
  6. smallfont = pygame.font.SysFont('Helvatica', 70)
  7. win = pygame.display.set_mode((1000, 1000))
  8. blocks = [[(50 * i, 50 * j, 50, 50) for i in range(20)] for j in range(20)]
  9. for j in range(20):
  10.     for i in range(20):
  11.         pygame.draw.rect(win, (255, 255, 255), (50 * i, 50 * j, 50, 50), 1)
  12.  
  13.  
  14. class Snake():
  15.     def __init__(self):
  16.         self.body = [[10, 10, 'R'], [10, 9, 'R'], [10, 8, 'R']]
  17.         self.run = True
  18.         self.score = 0
  19.  
  20.     def checklost(self):
  21.         if self.body[0][0] >= 20 or self.body[0][0] < 0 or self.body[0][1] >= 20 or self.body[0][1] < 0:
  22.             self.run = False
  23.         for i in Snake.body[1:]:
  24.             if i[:2] == self.body[0][:2]:
  25.                 self.run = False
  26.  
  27.     def checkdir(self, i):
  28.         if self.body[i][2] == 'R' and self.body[i - 1][1] != self.body[i][1] + 1:
  29.             return False
  30.         if self.body[i][2] == 'L' and self.body[i - 1][1] != self.body[i][1] - 1:
  31.             return False
  32.         if self.body[i][2] == 'U' and self.body[i - 1][0] != self.body[i][0] - 1:
  33.             return False
  34.         if self.body[i][2] == 'D' and self.body[i - 1][0] != self.body[i][0] + 1:
  35.             return False
  36.         return True
  37.  
  38.     def changedir(self, keys):
  39.         if keys[pygame.K_RIGHT] and self.body[0][2] != 'L' and self.body[0][2] != 'R':
  40.             self.body[0][2] = 'R'
  41.         elif keys[pygame.K_LEFT] and self.body[0][2] != 'R' and self.body[0][2] != 'L':
  42.             self.body[0][2] = 'L'
  43.         elif keys[pygame.K_UP] and self.body[0][2] != 'D' and self.body[0][2] != 'U':
  44.             self.body[0][2] = 'U'
  45.         elif keys[pygame.K_DOWN] and self.body[0][2] != 'U' and self.body[0][2] != 'D':
  46.             self.body[0][2] = 'D'
  47.  
  48.     def bodycontrol(self):
  49.         for i in range(len(self.body) - 1, 0, -1):
  50.             if not self.checkdir(i):
  51.                 self.body[i][2] = self.body[i - 1][2]
  52.  
  53.     def move(self):
  54.         for i in self.body:
  55.             if i[2] == 'R':
  56.                 i[1] += 1
  57.             if i[2] == 'L':
  58.                 i[1] -= 1
  59.             if i[2] == 'U':
  60.                 i[0] -= 1
  61.             if i[2] == 'D':
  62.                 i[0] += 1
  63.  
  64.     def eatfood(self):
  65.         if self.body[0][:2] == Food.foodpos:
  66.             self.growth()
  67.             Food.changefood()
  68.             self.score += 1
  69.  
  70.     def growth(self):
  71.         if self.body[-1][2] == 'R':
  72.             if self.body[-1][1] != 0:
  73.                 self.body.append([self.body[-1][0], self.body[-1][1] - 1, self.body[-1][2]])
  74.             elif self.body[-1][0] == 0:
  75.                 self.body.append([self.body[-1][0] + 1, self.body[-1][1], 'U'])
  76.             else:
  77.                 self.body.append([self.body[-1][0] - 1, self.body[-1][1], 'D'])
  78.         if self.body[-1][2] == 'L':
  79.             if self.body[-1][1] != 19:
  80.                 self.body.append([self.body[-1][0], self.body[-1][1] + 1, self.body[-1][2]])
  81.             elif self.body[-1][0] == 0:
  82.                 self.body.append([self.body[-1][0] + 1, self.body[-1][1], 'U'])
  83.             else:
  84.                 self.body.append([self.body[-1][0] - 1, self.body[-1][1], 'D'])
  85.         if self.body[-1][2] == 'U':
  86.             if self.body[-1][0] != 19:
  87.                 self.body.append([self.body[-1][0] + 1, self.body[-1][1], self.body[-1][2]])
  88.             elif self.body[-1][1] == 0:
  89.                 self.body.append([self.body[-1][0], self.body[-1][1] + 1, 'L'])
  90.             else:
  91.                 self.body.append([self.body[-1][0], self.body[-1][1] - 1, 'R'])
  92.         if self.body[-1][2] == 'D':
  93.             if self.body[-1][0] != 0:
  94.                 self.body.append([self.body[-1][0] - 1, self.body[-1][1], self.body[-1][2]])
  95.             elif self.body[-1][1] == 0:
  96.                 self.body.append([self.body[-1][0], self.body[-1][1] + 1, 'L'])
  97.             else:
  98.                 self.body.append([self.body[-1][0], self.body[-1][1] - 1, 'R'])
  99.  
  100.  
  101. class Food():
  102.     def __init__(self):
  103.         self.foodpos = [randint(0, 19), randint(0, 19)]
  104.  
  105.     def changefood(self):
  106.         self.foodpos = [randint(0, 19), randint(0, 19)]
  107.         for i in Snake.body:
  108.             if self.foodpos == i[:2]:
  109.                 self.changefood()
  110.  
  111.  
  112. Snake = Snake()
  113. Food = Food()
  114. start = False
  115. pygame.display.set_caption('Snake Game')
  116. c = 1
  117. while not start:
  118.     pygame.time.delay(50)
  119.     if pygame.key.get_pressed()[pygame.K_SPACE]:
  120.         start = True
  121.     if pygame.key.get_pressed()[pygame.K_RIGHT] and c != 3:
  122.         c += 1
  123.     if pygame.key.get_pressed()[pygame.K_LEFT] and c != 1:
  124.         c -= 1
  125.     for event in pygame.event.get():
  126.         if event.type == pygame.QUIT:
  127.             Snake.run = False
  128.             start = True
  129.     Text = myfont.render('Select your speed', False, (255, 255, 100))
  130.     smalltext1 = myfont.render('Fast', False, (0, 255, 0))
  131.     smalltext2 = myfont.render('Med', False, (0, 255, 0))
  132.     smalltext3 = myfont.render('Slow', False, (0, 255, 0))
  133.     starttext = smallfont.render('Press SPACE to start', False, (255, 255, 100))
  134.     win.blit(Text, (200, 400))
  135.     win.blit(smalltext1, (125, 12 * 50 - 5))
  136.     win.blit(smalltext2, (430, 12 * 50 - 5))
  137.     win.blit(smalltext3, (725, 12 * 50 - 5))
  138.     win.blit(starttext, (260, 16 * 50))
  139.  
  140.     if c == 2:
  141.         pygame.draw.rect(win, (255, 0, 0), (400, 12 * 50, 200, 100), 10)
  142.     else:
  143.         pygame.draw.rect(win, (255, 255, 255), (400, 12 * 50, 200, 100), 10)
  144.     if c == 1:
  145.         pygame.draw.rect(win, (255, 0, 0), (100, 12 * 50, 200, 100), 10)
  146.     else:
  147.         pygame.draw.rect(win, (255, 255, 255), (100, 12 * 50, 200, 100), 10)
  148.     if c == 3:
  149.         pygame.draw.rect(win, (255, 0, 0), (700, 12 * 50, 200, 100), 10)
  150.     else:
  151.         pygame.draw.rect(win, (255, 255, 255), (700, 12 * 50, 200, 100), 10)
  152.     pygame.display.update()
  153.  
  154. n = 0
  155. if c == 1:
  156.     n = 75
  157. if c == 2:
  158.     n = 100
  159. if c == 3:
  160.     n = 125
  161.  
  162. while Snake.run:
  163.     pygame.display.set_caption('Snake Game   Your Score is ' + str(Snake.score))
  164.     pygame.time.delay(n)
  165.  
  166.     k = pygame.key.get_pressed()
  167.     Snake.changedir(k)
  168.     Snake.move()
  169.     Snake.eatfood()
  170.     Snake.bodycontrol()
  171.     Snake.checklost()
  172.     for event in pygame.event.get():
  173.         if event.type == pygame.QUIT:
  174.             Snake.run = False
  175.     win.fill((0, 0, 0))
  176.     for j in range(20):
  177.         for i in range(20):
  178.             pygame.draw.rect(win, (255, 255, 255), (50 * i, 50 * j, 50, 50), 1)
  179.     if Snake.run:
  180.         rect = blocks[Snake.body[0][0]][Snake.body[0][1]]
  181.         pygame.draw.rect(win, (0, 150, 0), rect)
  182.         for i in Snake.body:
  183.             Rect = blocks[i[0]][i[1]]
  184.             pygame.draw.rect(win, (0, 255, 0), Rect, 10)
  185.         r = blocks[Food.foodpos[0]][Food.foodpos[1]]
  186.         pygame.draw.rect(win, (255, 0, 0), r, 10)
  187.         pygame.display.update()
  188. print('Your score was ' + str(Snake.score))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement