Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import random
- #Globals
- WIDTH = 800
- HEIGHT = 600
- SEG_SIZE = 20
- IN_GAME = True
- def create_block():
- global BLOCK
- posx=SEG_SIZE*random.randint(1,(WIDTH-SEG_SIZE)/SEG_SIZE)
- posy=SEG_SIZE*random.randint(1,(HEIGHT-SEG_SIZE)/SEG_SIZE)
- BLOCK=c.create_oval(posx,posy, posx+SEG_SIZE,posy+SEG_SIZE, fill='red')
- class Snake(object):
- def __init__(self,segments):
- self.segments=segments
- self.mapping={"Down":(0,1),
- "Right":(1,0),
- "Left":(-1,0),
- "Up":(0,1)}
- self.vector=self.mapping["Right"]
- class Segment(object):
- def __init__(self,x,y):
- self.instance=c.create_rectangle(x,y, x+SEG_SIZE,y+SEG_SIZE,fill="white")
- #Setting up window
- root=Tk()
- root.title("Snake")
- c=Canvas(root,width=WIDTH,height=HEIGHT,bg="#003300")
- c.grid()
- # catch keypressing
- c.focus_set()
- segments=[Segment(SEG_SIZE,SEG_SIZE),
- Segment(SEG_SIZE*2,SEG_SIZE),
- Segment(SEG_SIZE*3,SEG_SIZE)]
- create_block()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement