Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.81 KB | None | 0 0
  1. from tkinter import *
  2. import random
  3.  
  4. live = 0
  5. seg = 0
  6.  
  7. # Globals
  8. WIDTH = 800
  9. HEIGHT = 600
  10. SEG_SIZE = 20
  11. IN_GAME = True
  12.  
  13.  
  14. def esc():
  15.     pass
  16.  
  17.  
  18. def restart():
  19.     global IN_GAME
  20.     IN_GAME = False
  21.     s.move()
  22.     head_coords = c.coords(s.segments[-1].instance)
  23.     x1, y1, x2, y2 = head_coords
  24.     # Check for collision with gamefield edges
  25.     if x2 > WIDTH - SEG_SIZE or x1 < 0 + SEG_SIZE or y1 < 0 + SEG_SIZE or y2 > HEIGHT - SEG_SIZE:
  26.         IN_GAME = False
  27.  
  28.         c.create_text((WIDTH / 2) - SEG_SIZE / 8, ((HEIGHT / 2) - (HEIGHT / 4)) + SEG_SIZE * 2,
  29.                       text="Вы наплыли на берег",
  30.                       font="Arial 15",
  31.                       fill="red")
  32.     IN_GAME = True
  33.  
  34.  
  35. # Helper functions
  36. def create_block():
  37.     """ Creates an apple to be eaten """
  38.     global BLOCK
  39.     posx = SEG_SIZE * random.randint(1, (WIDTH-SEG_SIZE-SEG_SIZE) / SEG_SIZE)
  40.     posy = SEG_SIZE * random.randint(1, (HEIGHT-SEG_SIZE - SEG_SIZE) / SEG_SIZE)
  41.     BLOCK = c.create_oval(posx, posy,
  42.                           posx+SEG_SIZE, posy+SEG_SIZE,
  43.                           fill="red", outline = "red")
  44.  
  45.  
  46. def main():
  47.     """ Handles game process """
  48.     global live
  49.     global IN_GAME
  50.     if IN_GAME:
  51.         s.move()
  52.         head_coords = c.coords(s.segments[-1].instance)
  53.         x1, y1, x2, y2 = head_coords
  54.         # Check for collision with gamefield edges
  55.         if x2 > WIDTH - SEG_SIZE or x1 < 0 + SEG_SIZE or y1 < 0 + SEG_SIZE or y2 > HEIGHT - SEG_SIZE:
  56.             IN_GAME = False
  57.  
  58.             c.create_text((WIDTH / 2) - SEG_SIZE / 8, ((HEIGHT / 2) - (HEIGHT / 4)) + SEG_SIZE * 2,
  59.                           text="Вы наплыли на берег",
  60.                           font="Arial 15",
  61.                           fill="red")
  62.  
  63.         # Eating apples
  64.         elif head_coords == c.coords(BLOCK):
  65.             s.add_segment()
  66.             c.delete(BLOCK)
  67.             create_block()
  68.         # Self-eating
  69.         else:
  70.             for index in range(len(s.segments)-1):
  71.                 if head_coords == c.coords(s.segments[index].instance):
  72.                     live = live + 1
  73.  
  74.                     if live == 1:
  75.                         c.delete(live3)
  76.                     elif live == 2:
  77.                         c.delete(live2)
  78.                     elif live == 3:
  79.                         c.delete(live1)
  80.  
  81.                     if live == 3:
  82.                         IN_GAME = False
  83.  
  84.  
  85.  
  86.                         c.create_text((WIDTH / 2) - SEG_SIZE / 8, ((HEIGHT / 2) - (HEIGHT / 4)) + SEG_SIZE * 2,
  87.                                       text="Вы потратили все жизни, съев себя",
  88.                                       font="Arial 15",
  89.                                       fill="red")
  90.  
  91.         root.after(100, main)
  92.     # Not IN_GAME -> stop game and print message
  93.     else:
  94.         c.create_text(WIDTH/2, (HEIGHT/2) - (HEIGHT/4),
  95.                       text="GAME OVER!",
  96.                       font="Arial 20",
  97.                       fill="red")
  98.  
  99.  
  100.  
  101. class Segment(object):
  102.     """ Single snake segment """
  103.     def __init__(self, x, y):
  104.         self.instance = c.create_rectangle(x, y,
  105.                                            x+SEG_SIZE, y+SEG_SIZE,
  106.                                            fill="#000099", outline = '#000099')
  107.  
  108.  
  109. class Snake(object):
  110.     """ Simple Snake class """
  111.     def __init__(self, segments):
  112.         self.segments = segments
  113.         # possible moves
  114.         self.mapping = {"Down": (0, 1), "Right": (1, 0),
  115.                         "Up": (0, -1), "Left": (-1, 0)}
  116.         # initial movement direction
  117.         self.vector = self.mapping["Right"]
  118.  
  119.     def move(self):
  120.         """ Moves the snake with the specified vector"""
  121.         for index in range(len(self.segments)-1):
  122.             segment = self.segments[index].instance
  123.             x1, y1, x2, y2 = c.coords(self.segments[index+1].instance)
  124.             c.coords(segment, x1, y1, x2, y2)
  125.  
  126.         x1, y1, x2, y2 = c.coords(self.segments[-2].instance)
  127.         c.coords(self.segments[-1].instance,
  128.                  x1+self.vector[0]*SEG_SIZE, y1+self.vector[1]*SEG_SIZE,
  129.                  x2+self.vector[0]*SEG_SIZE, y2+self.vector[1]*SEG_SIZE)
  130.  
  131.     def add_segment(self):
  132.         global seg
  133.         """ Adds segment to the snake """
  134.         last_seg = c.coords(self.segments[0].instance)
  135.         x = last_seg[2] - SEG_SIZE
  136.         y = last_seg[3] - SEG_SIZE
  137.         self.segments.insert(0, Segment(x, y))
  138.         seg = seg + 1
  139.  
  140.     def change_direction(self, event):
  141.         """ Changes direction of snake """
  142.         if event.keysym in self.mapping:
  143.             self.vector = self.mapping[event.keysym]
  144.  
  145. # Setting up window
  146. root = Tk()
  147. root.title("Акула")
  148.  
  149.  
  150. c = Canvas(root, width=WIDTH, height=HEIGHT, bg="#00ccff")
  151. c.pack()
  152. # catch keypressing
  153. c.focus_set()
  154. # creating segments and snake
  155. segments = [Segment(SEG_SIZE, SEG_SIZE),
  156.             Segment(SEG_SIZE*2, SEG_SIZE),
  157.             Segment(SEG_SIZE*3, SEG_SIZE)]
  158. s = Snake(segments)
  159. # Reaction on keypress
  160. c.bind("<KeyPress>", s.change_direction)
  161.  
  162. c.create_rectangle(0, 0, SEG_SIZE, HEIGHT, fill = '#ffcc66', outline = '#ffcc66')
  163. c.create_rectangle(0, 0, WIDTH, SEG_SIZE, fill = '#ffcc66', outline = '#ffcc66')
  164. c.create_rectangle(WIDTH, 0, WIDTH - SEG_SIZE, HEIGHT, fill = '#ffcc66', outline = '#ffcc66')
  165. c.create_rectangle(0, HEIGHT, WIDTH, HEIGHT - SEG_SIZE, fill = '#ffcc66', outline = '#ffcc66')
  166.  
  167. live1 = c.create_oval(20, 0, 40, 20, fill = "red", outline = "red")
  168. live2 = c.create_oval(60, 0, 80, 20, fill = "red", outline = "red")
  169. live3 = c.create_oval(100, 0, 120, 20, fill = "red", outline = "red")
  170.  
  171. button = Button(root, width = 10, height = 3, text = "RESTART")
  172. button.config(command = restart)
  173. button.pack()
  174.  
  175. text = Text()
  176.  
  177. create_block()
  178. main()
  179. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement