-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics_lab_1.py
81 lines (59 loc) · 1.78 KB
/
graphics_lab_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import sys
import random
from PIL import Image, ImageTk
from tkinter import Tk, Frame, Canvas, ALL, NW
class Constants:
BOARD_WIDTH = 300
BOARD_HEIGHT = 300
DELAY = 100
DOT_SIZE = 10
MAX_RAND_POS = 27
class Board(Canvas):
def __init__(self):
super().__init__(width=Constants.BOARD_WIDTH, height=Constants.BOARD_HEIGHT,
background="white", highlightthickness=0)
self.initGame()
self.pack()
def initGame(self):
self.x=50
self.y=50
self.fx=10
self.fy=10
self.doDrawing()
self.bind_all("<Key>", self.onKeyPressed)
self.after(Constants.DELAY, self.onTimer)
def doDrawing(self):
'''self.create_text(30, 10, text="Score: {0}".format(self.score),
tag="score", fill="white")'''
self.create_rectangle(80,80,self.x,self.y, fill = "red")
def onKeyPressed(self, e):
'''controls direction variables with cursor keys'''
key = e.keysym
LEFT_CURSOR_KEY = "Left"
if key == LEFT_CURSOR_KEY:
self.x -= self.fx
RIGHT_CURSOR_KEY = "Right"
if key == RIGHT_CURSOR_KEY:
self.x += self.fx
UP_CURSOR_KEY = "Up"
if key == UP_CURSOR_KEY:
self.y -= self.fy
DOWN_CURSOR_KEY = "Down"
if key == DOWN_CURSOR_KEY:
self.y += self.fy
def onTimer(self):
'''creates a game cycle each timer event '''
self.doDrawing()
self.after(Constants.DELAY, self.onTimer)
class Ball(Frame):
def __init__(self):
super().__init__()
self.master.title('Snake')
self.board = Board()
self.pack()
def main():
root = Tk()
nib = Ball()
root.mainloop()
if __name__ == '__main__':
main()