Advertisement
Mussab_Blue

SnakeGame

Feb 15th, 2021 (edited)
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.84 KB | None | 0 0
  1.  
  2. from kivy.config import Config
  3. Config.set('graphics', 'fullscreen', 'auto')
  4. from kivy.app import App
  5. from kivy.uix.gridlayout import GridLayout
  6. from kivy.uix.widget import Widget
  7. from kivy.properties import StringProperty, ListProperty, NumericProperty
  8. from kivy.core.window import Window
  9. from kivy.clock import Clock
  10. from kivy.lang import Builder
  11. import random
  12. from random import choice
  13.  
  14. width = Window.width
  15. height = Window.height
  16.  
  17. def rand_pos():
  18.   x = choice(range(10, width-100))
  19.   y = choice(range(100, height-200))
  20.   return (x, y)
  21.  
  22.  
  23. def calc_dist(touch, side):
  24.     x = touch[0]
  25.     y = touch[1]
  26.    
  27.     dx = 100 * x// width
  28.     dy = 100 * y// height
  29.    
  30.     if side == "bottomLeft":
  31.         if dx < dy:
  32.             return True
  33.    
  34.     if side == "bottomRight":
  35.         br = 100 - dx
  36.         if br < dy:
  37.             return True
  38.    
  39.     if side == "topLeft":
  40.         tl = 100 - dy
  41.         if dx < tl:
  42.             return True
  43.    
  44.     if side == "topRight":
  45.         if dx > dy:
  46.             return True
  47.     return False
  48.    
  49. class Part(Widget):
  50.     color = ListProperty([1,1,1,1])
  51.     def __init__(self, **kw):
  52.         super(Part, self).__init__(**kw)
  53.         self.size = (40,40)
  54.         self.change_color()
  55.        
  56.     def change_color(self):
  57.         self.color = [round(random.random(), 1) for _ in range(3)]
  58.        
  59.  
  60. class Board(GridLayout):
  61.     move_x = 0
  62.     move_y = 0
  63.     body = list()  
  64.     step = 40
  65.    
  66.     def __init__(self, **kw):
  67.         super(Board, self).__init__(**kw)
  68.         self.new_game()
  69.    
  70.     def new_game(self):                  
  71.         self.head = Part(pos= (500, 800))
  72.         self.add_widget(self.head)
  73.        
  74.         self.food = Part()
  75.         self.food.pos = rand_pos()
  76.         self.add_widget(self.food)
  77.      
  78.         self.body = [self.head]
  79.         Clock.schedule_interval(self.looper, .3)
  80.  
  81.     def clear(self):
  82.         self.move_x = 0
  83.         self.move_y = 0
  84.         try:
  85.             for part in self.body:
  86.                 self.remove_widget(part)
  87.             self.remove_widget(self.food)
  88.         except:
  89.             pass
  90.         del self.body[:]
  91.        
  92.        
  93.            
  94.     def on_touch_down(self, touch):
  95.         self.move_x = 0
  96.         self.move_y = 0
  97.         spot = (touch.x, touch.y)
  98.         head = (self.head.x, self.head.y)
  99.        
  100.         if spot[0] < head[0] and spot[1] < head[1]:
  101.             if calc_dist(spot, "bottomLeft"):
  102.                 self.move_x = -self.step
  103.             else:
  104.                 self.move_y = -self.step
  105.        
  106.         if spot[0] > head[0] and spot[1] > head[1]:
  107.             if calc_dist(spot, "topRight"):
  108.                 self.move_x = self.step
  109.             else:
  110.                 self.move_y = self.step
  111.                
  112.         if spot[0] < head[0] and spot[1] > head[1]:
  113.             if calc_dist(spot, "topLeft"):
  114.                 self.move_x = -self.step
  115.             else:
  116.                 self.move_y = self.step
  117.  
  118.         if spot[0] > head[0] and spot[1] < head[1]:
  119.             if calc_dist(spot, "bottomRight"):
  120.                 self.move_x = self.step
  121.             else:
  122.                 self.move_y = -self.step
  123.  
  124.     def create_part(self, poss, color):
  125.         part = Part(pos = poss)
  126.         part.color = color
  127.         self.add_widget(part)
  128.         self.body.append(part)
  129.  
  130.  
  131.     before = None    
  132.     def looper(self, dt):
  133.         current = (self.head.x, self.head.y)  
  134.         if self.head.x > self.x and self.head.x < self.width and self.head.y > self.y and self.head.y < self.height:                   
  135.             self.head.x += self.move_x
  136.             self.head.y += self.move_y
  137.            
  138.             for part in self.body[1:]:
  139.                 self.before = (part.x, part.y)
  140.                 part.pos = current
  141.                 current = self.before
  142.                                  
  143.             if self.head.collide_widget(self.food):
  144.                 color = self.food.color
  145.                 if not self.before:
  146.                     self.before = current
  147.                
  148.                 self.food.change_color()                
  149.                 self.food.pos = rand_pos()
  150.                
  151.                 self.create_part(self.before, color)
  152.         else:
  153.             self.clear()
  154.             Clock.unschedule(self.looper)
  155.             self.new_game()                                    
  156.  
  157. class SnakeGameApp(App):
  158.     def build(self):
  159.         return Board(size = Window.size)
  160.      
  161. Builder.load_string("""
  162. <Part>:
  163.    size_hint:(None, None)
  164.    canvas:
  165.        Color:
  166.            rgb: root.color
  167.        Rectangle:
  168.            size: self.size # 30, 30
  169.            pos: self.pos
  170.              
  171. <Board>:
  172.    canvas:
  173.        Color:
  174.            rgba: .4, .4, 1, 1
  175.        Rectangle:
  176.            size: self.size
  177.            pos: self.pos                        
  178. """)
  179.  
  180. if __name__ == "__main__":
  181.     snakegame = SnakeGameApp()
  182.     snakegame.run()
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement