2020GREEN = (0 , 255 , 0 )
2121WHITE = (255 , 255 , 255 )
2222
23- apple = pygame .image .load ('apple.png' )
24-
2523def drawGrid ():
2624 for row in range (ROWS ):
2725 pygame .draw .line (win , WHITE , (0 , row * CELLSIZE ), (WIDTH , row * CELLSIZE ), 1 )
2826 for col in range (COLS ):
2927 pygame .draw .line (win , WHITE , (col * CELLSIZE , 0 ), (col * CELLSIZE , HEIGHT ))
3028
31- def randomApple ():
32- return random .randint (0 ,ROWS - 1 ), random .randint (0 ,COLS - 1 )
33-
34- dx = dy = 0
3529
3630class Snake :
3731 def __init__ (self ):
38- self .length = 0
39- self .direction = UP
40- self .body = 20
41- self .x = WIDTH // 2
42- self .y = HEIGHT // 2
32+ self .length = 1
33+ self .direction = None
34+ self .x = COLS // 2
35+ self .y = ROWS // 2
36+ self .head = (COLS // 2 * CELLSIZE , ROWS // 2 * CELLSIZE )
37+ self .body = [self .head ]
4338
4439 def update (self ):
45- self .x += dx
46- self .y += dy
40+ head = self .body [- 1 ]
41+ if self .direction == 'up' :
42+ head = (head [0 ], head [1 ] - CELLSIZE )
43+ elif self .direction == 'down' :
44+ head = (head [0 ], head [1 ] + CELLSIZE )
45+ elif self .direction == 'left' :
46+ head = (head [0 ] - CELLSIZE , head [1 ])
47+ elif self .direction == 'right' :
48+ head = (head [0 ] + CELLSIZE , head [1 ])
49+
50+ self .head = head
51+ self .body .append (self .head )
52+ if self .length < len (self .body ):
53+ self .body .pop (0 )
54+
55+ def eatFood (self ):
56+ self .length += 1
57+
58+ def checkFood (self , food ):
59+ if self .head [0 ] == food .x and self .head [1 ] == food .y :
60+ self .eatFood ()
61+ food .respawn ()
62+
63+ def draw (self ):
64+ for block in self .body :
65+ x , y = block
66+ pygame .draw .rect (win , GREEN , (x , y , CELLSIZE , CELLSIZE ))
67+
68+ class Food :
69+ def __init__ (self ):
70+ self .image = pygame .image .load ('apple.png' )
71+ self .respawn ()
72+
73+ def respawn (self ):
74+ self .x = random .randint (0 ,COLS - 1 ) * CELLSIZE
75+ self .y = random .randint (0 ,ROWS - 1 ) * CELLSIZE
76+ print (self .x , self .y )
4777
4878 def draw (self ):
49- pygame . draw . rect ( win , GREEN , self .x , self .y , self .body , self . body )
79+ win . blit ( self .image , ( self .x , self .y ) )
5080
51- ay , ax = randomApple ()
5281snake = Snake ()
82+ food = Food ()
5383
5484running = True
5585while running :
@@ -62,28 +92,23 @@ def draw(self):
6292 if event .key == pygame .K_ESCAPE :
6393 running = False
6494
65- if event .key == pygame .K_RIGHT :
66- dx = CELLSIZE
67- dy = 0
95+ if event .key == pygame .K_RIGHT and snake .direction != 'left' :
96+ snake .direction = 'right'
6897
69- if event .key == pygame .K_LEFT :
70- dx = - CELLSIZE
71- dy = 0
98+ if event .key == pygame .K_LEFT and snake .direction != 'right' :
99+ snake .direction = 'left'
72100
73- if event .key == pygame .K_UP :
74- dx = 0
75- dy = - CELLSIZE
101+ if event .key == pygame .K_UP and snake .direction != 'down' :
102+ snake .direction = 'up'
76103
77- if event .key == pygame .K_DOWN :
78- dx = 0
79- dy = CELLSIZE
104+ if event .key == pygame .K_DOWN and snake .direction != 'up' :
105+ snake .direction = 'down'
80106
81- # drawGrid()
82- x += dx
83- y += dy
84- win .blit (apple , (ax * CELLSIZE , ay * CELLSIZE ))
107+ drawGrid ()
85108 snake .update ()
109+ snake .checkFood (food )
86110 snake .draw ()
111+ food .draw ()
87112
88113 clock .tick (FPS )
89114 pygame .display .update ()
0 commit comments