Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import random
- live = 0
- seg = 0
- # Globals
- WIDTH = 800
- HEIGHT = 600
- SEG_SIZE = 20
- IN_GAME = True
- def esc():
- pass
- def restart():
- global IN_GAME
- IN_GAME = False
- s.move()
- head_coords = c.coords(s.segments[-1].instance)
- x1, y1, x2, y2 = head_coords
- # Check for collision with gamefield edges
- if x2 > WIDTH - SEG_SIZE or x1 < 0 + SEG_SIZE or y1 < 0 + SEG_SIZE or y2 > HEIGHT - SEG_SIZE:
- IN_GAME = False
- c.create_text((WIDTH / 2) - SEG_SIZE / 8, ((HEIGHT / 2) - (HEIGHT / 4)) + SEG_SIZE * 2,
- text="Вы наплыли на берег",
- font="Arial 15",
- fill="red")
- IN_GAME = True
- # Helper functions
- def create_block():
- """ Creates an apple to be eaten """
- global BLOCK
- posx = SEG_SIZE * random.randint(1, (WIDTH-SEG_SIZE-SEG_SIZE) / SEG_SIZE)
- posy = SEG_SIZE * random.randint(1, (HEIGHT-SEG_SIZE - SEG_SIZE) / SEG_SIZE)
- BLOCK = c.create_oval(posx, posy,
- posx+SEG_SIZE, posy+SEG_SIZE,
- fill="red", outline = "red")
- def main():
- """ Handles game process """
- global live
- global IN_GAME
- if IN_GAME:
- s.move()
- head_coords = c.coords(s.segments[-1].instance)
- x1, y1, x2, y2 = head_coords
- # Check for collision with gamefield edges
- if x2 > WIDTH - SEG_SIZE or x1 < 0 + SEG_SIZE or y1 < 0 + SEG_SIZE or y2 > HEIGHT - SEG_SIZE:
- IN_GAME = False
- c.create_text((WIDTH / 2) - SEG_SIZE / 8, ((HEIGHT / 2) - (HEIGHT / 4)) + SEG_SIZE * 2,
- text="Вы наплыли на берег",
- font="Arial 15",
- fill="red")
- # Eating apples
- elif head_coords == c.coords(BLOCK):
- s.add_segment()
- c.delete(BLOCK)
- create_block()
- # Self-eating
- else:
- for index in range(len(s.segments)-1):
- if head_coords == c.coords(s.segments[index].instance):
- live = live + 1
- if live == 1:
- c.delete(live3)
- elif live == 2:
- c.delete(live2)
- elif live == 3:
- c.delete(live1)
- if live == 3:
- IN_GAME = False
- c.create_text((WIDTH / 2) - SEG_SIZE / 8, ((HEIGHT / 2) - (HEIGHT / 4)) + SEG_SIZE * 2,
- text="Вы потратили все жизни, съев себя",
- font="Arial 15",
- fill="red")
- root.after(100, main)
- # Not IN_GAME -> stop game and print message
- else:
- c.create_text(WIDTH/2, (HEIGHT/2) - (HEIGHT/4),
- text="GAME OVER!",
- font="Arial 20",
- fill="red")
- class Segment(object):
- """ Single snake segment """
- def __init__(self, x, y):
- self.instance = c.create_rectangle(x, y,
- x+SEG_SIZE, y+SEG_SIZE,
- fill="#000099", outline = '#000099')
- class Snake(object):
- """ Simple Snake class """
- def __init__(self, segments):
- self.segments = segments
- # possible moves
- self.mapping = {"Down": (0, 1), "Right": (1, 0),
- "Up": (0, -1), "Left": (-1, 0)}
- # initial movement direction
- self.vector = self.mapping["Right"]
- def move(self):
- """ Moves the snake with the specified vector"""
- for index in range(len(self.segments)-1):
- segment = self.segments[index].instance
- x1, y1, x2, y2 = c.coords(self.segments[index+1].instance)
- c.coords(segment, x1, y1, x2, y2)
- x1, y1, x2, y2 = c.coords(self.segments[-2].instance)
- c.coords(self.segments[-1].instance,
- x1+self.vector[0]*SEG_SIZE, y1+self.vector[1]*SEG_SIZE,
- x2+self.vector[0]*SEG_SIZE, y2+self.vector[1]*SEG_SIZE)
- def add_segment(self):
- global seg
- """ Adds segment to the snake """
- last_seg = c.coords(self.segments[0].instance)
- x = last_seg[2] - SEG_SIZE
- y = last_seg[3] - SEG_SIZE
- self.segments.insert(0, Segment(x, y))
- seg = seg + 1
- def change_direction(self, event):
- """ Changes direction of snake """
- if event.keysym in self.mapping:
- self.vector = self.mapping[event.keysym]
- # Setting up window
- root = Tk()
- root.title("Акула")
- c = Canvas(root, width=WIDTH, height=HEIGHT, bg="#00ccff")
- c.pack()
- # catch keypressing
- c.focus_set()
- # creating segments and snake
- segments = [Segment(SEG_SIZE, SEG_SIZE),
- Segment(SEG_SIZE*2, SEG_SIZE),
- Segment(SEG_SIZE*3, SEG_SIZE)]
- s = Snake(segments)
- # Reaction on keypress
- c.bind("<KeyPress>", s.change_direction)
- c.create_rectangle(0, 0, SEG_SIZE, HEIGHT, fill = '#ffcc66', outline = '#ffcc66')
- c.create_rectangle(0, 0, WIDTH, SEG_SIZE, fill = '#ffcc66', outline = '#ffcc66')
- c.create_rectangle(WIDTH, 0, WIDTH - SEG_SIZE, HEIGHT, fill = '#ffcc66', outline = '#ffcc66')
- c.create_rectangle(0, HEIGHT, WIDTH, HEIGHT - SEG_SIZE, fill = '#ffcc66', outline = '#ffcc66')
- live1 = c.create_oval(20, 0, 40, 20, fill = "red", outline = "red")
- live2 = c.create_oval(60, 0, 80, 20, fill = "red", outline = "red")
- live3 = c.create_oval(100, 0, 120, 20, fill = "red", outline = "red")
- button = Button(root, width = 10, height = 3, text = "RESTART")
- button.config(command = restart)
- button.pack()
- text = Text()
- create_block()
- main()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement