Snake game using openCV (hand tracking)

The snake game using openCV, cvzone libraries.
Here I will explain the steps for the code. If you need a final code, please check the bottom of the blog.

1. Video

2. Setup the libraries and camera
After installing the libraries, you can check if it works using the below code.
 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
import cvzone
from cvzone.HandTrackingModule import HandDetector
import cv2

cap = cv2.VideoCapture(0) 
cap.set(3, 1280) 
cap.set(4, 720)
detector = HandDetector(detectionCon = 0.8, maxHands = 1) 

cap = cv2.VideoCapture(0) # connect camera
cap.set(3, 1280) # set the width and height of the image
cap.set(4, 720)
detector = HandDetector(detectionCon=0.8, maxHands=1) # set the threshold and the number of hands to detect


while True:
    success, img = cap.read()
    img = cv2.flip(img, 1)
    hands, img = detector.findHands(img, flipType=False)
    
    cv2.imshow("Image", img)
    
    key = cv2.waitKey(1)
    if key == 27: # key 'esc'
        cv2.destroyAllWindows()
        cap.release() # disconnect and turn off the camera
        print('break')
        break

Error occurred when I imported cvzone. Then I could find that my python version was 3.9, and cvzone works only for python 3.6 or 3.8. So if you get the same error, you need to downgrade python to 3.8.

If the code works well, you can get an image like below.


3. decline class, variables, methods
We need more detailed functions for playing the game. So I declined the class, variables, and methods(functions). For just working, I declined empty functions.
  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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
import math
import random
import cvzone
import cv2
import numpy as np
from cvzone.HandTrackingModule import HandDetector
import time

class SnakeGameClass:
    def __init__(self):
        self.backSize = (1280, 720)
        self.numTile = 25 # tile number of the game: ex) 25 means 25 x 25 tiles game
        
        self.lastFinger = [None, None] # the last location of the index finger
        self.points = [(self.numTile//2, self.numTile//2)] # all the locations of the snake
                                                            # the initial location of the snake is center
        self.length = 1 # The length of the snake
        self.foodPoint = (0,0) # The location of the food
        self.score = 0
        self.gameOver = False
        self.gameStart = False
        
        self.direction = '' # left, right, up, down = 'l', 'r', 'u', 'd'
        
        self.snakePeriod = 0.25 # (sec) the period of the snake moving
    
    def start(self):
        cap = cv2.VideoCapture(0)
        cap.set(3, 1280)
        cap.set(4, 720)
        detector = HandDetector(detectionCon=0.8, maxHands=1)
        
        # run this loop consistently and get capture every loop
        while True:
            success, img = cap.read()
            img = cv2.flip(img, 1)
            hands, img = detector.findHands(img, flipType=False)
            
            if hands:
                lmList = hands[0]['lmList']
                self.lastFinger = lmList[8][0:2]
                self.updateDirection()
                self.gameStart = True
            
            if self.isTimeToMoveSnake():
                self.update() # update the class variables for moving of snake
            
            img = self.displayGUI(img) # update the GUI
            cv2.imshow("Image", img)
            
            key = cv2.waitKey(1)
            if key == ord('r'): # key r to reset the game
                self.resetGame()
                print('reset')
            elif key == 27: # key 'esc' to break the game
                cv2.destroyAllWindows()
                cap.release() # disconnect and turn off the camera
                print('break')
                break
                
    def update(self):
        # if it is time to move the snake
        # change the class variables about moving
        return 0

    def randomFood(self):
        # return the new location of food
        # (tuple) (i, j)
        # i,j : 0 ~ (numTile -1)
        return (0,0)

    def updateDirection(self):
        # using the last node of self.points which means the head of the snake
        # and using the lastFinger position
        # change self.direction to one of these : 'l', 'r', 'u', 'd' (left, right, up, down)
        return 0

    def indexToPixel(self, index):
        # we need to change tile index to pixel index for decision the direction
        # parameter index : the location of the last node (head of the snake)
        # ex) (0, 0) ~ (24, 24)
        # return : the location represented with pixel
        # ex) (600, 600)
        x_new, y_new = (0, 0)
        return (x_new, y_new)

    def isTimeToMoveSnake(self):
        # if it is time to move of the snake : return 1
        # or return 0
        return True

    def resetGame(self):
        # if you press 'r' button, this method will be executed
        # reset the game
        return 0

    def displayGUI(self, imgMain):
        # display the GUI
        # change the img to include GUI and return img

        return imgMain

    def drawSquare(self, imgMain, position, color, fill=False):
        # position = not the position of the pixel, the position of the tile
        # left, top is (0, 0), right top is (i, 0), right bottom is (i, j)
        # color = (b, g, r), 0 ~ 255
        # fill = determine if want to fill or not
        return imgMain
    
game = SnakeGameClass()
game.start()

The result after executing the code is same with 2.

4. Final code
Then fill the functions and complete the code.
  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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import math
import random
import cvzone
import cv2
import numpy as np
from cvzone.HandTrackingModule import HandDetector
import time

class SnakeGameClass:
    def __init__(self):
        self.backSize = (1280, 720)
        self.gameSize = (500, 500)
        self.numTile = 25
        self.margin = 30
        
        self.lastFinger = [None, None] # the last location of the index finger
        self.points = [(self.numTile//2, self.numTile//2)] # all the locations of the snake
        self.length = 1 # The length of the snake
        self.foodPoint = self.randomFood()
        self.score = 0
        self.gameOver = False
        self.gameStart = False
        
        self.direction = '' # left, right, up, down = 'l', 'r', 'u', 'd'
        
        self.previousTime = time.time()
        self.currentTime = time.time()
        self.snakePeriod = 0.25 # (sec) the period of the snake moving

    def start(self):
        cap = cv2.VideoCapture(0)
        cap.set(3, 1280)
        cap.set(4, 720)
        detector = HandDetector(detectionCon=0.8, maxHands=1)
        
        # run this loop consistently and get capture every loop
        while True:
            success, img = cap.read()
            img = cv2.flip(img, 1)
            hands, img = detector.findHands(img, flipType=False)
            
            if hands:
                lmList = hands[0]['lmList']
                self.lastFinger = lmList[8][0:2]
                self.updateDirection()
                self.gameStart = True
            
            if self.isTimeToMoveSnake():
                self.update() # update the class variables for moving of snake
                
            img = self.displayGUI(img) # update the GUI
            cv2.imshow("Image", img)
            
            key = cv2.waitKey(1)
            if key == ord('r'): # key r to reset the game
                self.resetGame()
                print('reset')
            elif key == 27: # key 'esc' to break the game
                cv2.destroyAllWindows()
                cap.release() # disconnect and turn off the camera
                print('break')
                break
                
    def update(self):
        # if it is time to move the snake
        # change the class variables about moving
        if (self.gameOver == True) or (self.gameStart == False):
            return 0
        
        hx, hy = self.points[-1] # position of the head
        if self.direction == 'l': #left
            if hx - 1 < 0: # when it collided to the wall
                self.gameOver = True
                return 0
            elif (hx - 1, hy) in self.points:
                self.gameOver = True
                return 0
            elif (hx - 1, hy) == self.foodPoint: # when head ate food
                self.whenAteFood()
                return 0
            else:
                self.points.append((hx-1,hy))
                del self.points[0]
                
        elif self.direction == 'r': #right
            if hx + 1 >= self.numTile: # when it collided to the wall
                self.gameOver = True
                return 0
            elif (hx + 1, hy) in self.points:
                self.gameOver = True
                return 0
            elif (hx + 1, hy) == self.foodPoint: # when head ate food
                self.whenAteFood()
                return 0
            else:
                self.points.append((hx+1,hy))
                del self.points[0]
                
        elif self.direction == 'u': #left
            if hy - 1 < 0: # when it collided to the wall
                self.gameOver = True
                return 0
            elif (hx, hy-1) in self.points:
                self.gameOver = True
                return 0
            elif (hx, hy-1) == self.foodPoint: # when head ate food
                self.whenAteFood()
                return 0
            else:
                self.points.append((hx,hy-1))
                del self.points[0]
                
        else:
            if hy + 1 >= self.numTile: # when it collided to the wall
                self.gameOver = True
                return 0
            elif (hx, hy + 1) in self.points:
                self.gameOver = True
                return 0
            elif (hx, hy + 1) == self.foodPoint: # when head ate food
                self.whenAteFood()
                return 0
            else:
                self.points.append((hx,hy + 1))
                del self.points[0]
                
        return 0    
    
    def whenAteFood(self):
        self.points.append(self.foodPoint)
        self.length += 1
        self.foodPoint = self.randomFood()
        self.score += 1
        return 0
    
    def randomFood(self):
        # return the new location of food
        # (tuple) (i, j)
        # i,j : 0 ~ (numTile -1)
        foodSpace = []
        for i in range(self.numTile):
            for j in range(self.numTile):
                foodSpace.append((i,j))
        
        for item in self.points:
            foodSpace.remove(item)
        
        index = random.randrange(len(foodSpace))
        return foodSpace[index]
        
    def updateDirection(self):
        # using the last node of self.points and the lastFinger position
        # determine the direction 
        # return one of these : 'l', 'r', 'u', d'
        x_finger, y_finger = self.lastFinger
        x_head, y_head = self.indexToPixel(self.points[-1])
        
        dx = x_finger - x_head
        dy = y_finger - y_head
        
        if abs(dx) >= abs(dy):
            if dx >= 0:
                self.direction = 'r'
            else:
                self.direction = 'l'
        else:
            if dy >= 0:
                self.direction = 'd'
            else:
                self.direction = 'u'
        return 0
    
    def indexToPixel(self, index):
        # we need to change tile index to pixel index for decision the direction
        # parameter index : the location of the last node (head of the snake)
        # ex) (0, 0) ~ (24, 24)
        # return : the location represented with pixel
        # ex) (600, 600)
        i, j = index
        a, _ = self.backSize
        c, _ = self.gameSize
        e = self.margin
        f = c // self.numTile
        x_new = a/2 - c/2 + f*(i+1/2) + 1/2
        y_new = 2*e + f*(j+1/2) + 1/2
        
        return (x_new, y_new)
        
    def isTimeToMoveSnake(self):
        # if it is time to move of the snake : return 1
        # or return 0
        self.currentTime = time.time()
        if self.currentTime > self.previousTime + self.snakePeriod:
            self.previousTime += self.snakePeriod
            return True
        return False
    
    def resetGame(self):
        # if you press 'r' button, this method will be executed
        # reset the game
        self.lastFinger = [None, None] # the last location of the index finger
        self.points = [(self.numTile//2, self.numTile//2)] # all the locations of the snake
        self.length = 1 # The length of the snake
        self.foodPoint = self.randomFood()
        self.score = 0
        self.gameOver = False
        self.gameStart = False
        
        return 0

    def displayGUI(self, imgMain):
        # display the GUI
        # change the img to include GUI and return img
        
        marginColor = (0, 255, 255)
        marginThick = 3 # px
        
        textColor = (0, 0, 0)
        backColor = (0, 255, 255)
        fontScale = 2
        fontThick = 2
        
        snakeColor = (255, 0, 0)
        headColor = (255, 255, 0)
        
        foodColor = (0, 255, 255)
        
        imgMain = cv2.rectangle(imgMain,
                                (self.backSize[0]//2-self.gameSize[0]//2-self.margin+1, 1*self.margin+1),
                                (self.backSize[0]//2+self.gameSize[0]//2+self.margin, 3*self.margin+self.gameSize[1]),
                                marginColor, marginThick)
        
        if self.gameOver:
            cvzone.putTextRect(imgMain, "Game Over", [300, 400],
                               scale=5, thickness=3, offset=20)
            cvzone.putTextRect(imgMain, f'Your Score:{self.score}', [300, 500],
                               scale=5, thickness=3, offset=20)
            cvzone.putTextRect(imgMain, "Press 'r' to restart", [300, 600],
                               scale=5, thickness=3, offset=20)
        elif self.gameStart == False:
            imgMain,_ = cvzone.putTextRect(imgMain, "Show your hand for start",
                              (self.backSize[0]//2-self.gameSize[0]//2+1, self.margin+self.gameSize[1]),
                              fontScale, fontThick, textColor, backColor)
            return imgMain
        
        for i, point in enumerate(self.points):
            if i != len(self.points)-1: # if is not the head
                imgMain = self.drawSquare(imgMain, point, snakeColor)
            else:
                imgMain = self.drawSquare(imgMain, point, headColor, True)
                
        imgMain = self.drawSquare(imgMain, self.foodPoint, foodColor, True)
        
        
        
        return imgMain
    
    def drawSquare(self, imgMain, position, color, fill=False):
        # position = not the position of the pixel, the position of the array
        # left, top is (0, 0), right top is (i, 0)
        # color = (b, g, r)
        # fill = determine if want to fill or not
        
        if fill == True:
            thickness = -1 # fill the rectangle
        else:
            thickness = 1 # 1px
        
        i, j = position
        n = self.gameSize[0] // self.numTile # number of pixels of each rectangle
    
        imgMain = cv2.rectangle(imgMain, (self.backSize[0]//2-self.gameSize[0]//2+n*i+1, 2*self.margin+n*j+1),
                                (self.backSize[0]//2-self.gameSize[0]//2+n*(i+1), 2*self.margin+n*(j+1)),
                                color, thickness)
        return imgMain
    
game = SnakeGameClass()
game.start()

I need to explain about line 184 which changes the tile index to pixel index. The function updateDirection() decides the direction using the index finger point location and the head location. However, the units of two are not the same. Unit of the finger location is 'pixel' and the unit of the snake head is 'tile index'. So we need process to change tile index to pixel index.

You can check how it is calculated with the images below.
You can check each tile in the game screen in the camera image.
(a, b) = backSize
c = gameSize
f = c / numTile
e = margin
If you need any help or is there any problem, please comment or email me.
Thank you.

댓글