-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.py
77 lines (61 loc) · 2.39 KB
/
snake.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
import pygame
from pygame.sprite import Sprite
class Head(Sprite):
"""A class for head of the snake"""
def __init__(self, sg_game):
"""Initialize the snake and set its starting position"""
super().__init__()
self.screen = sg_game.screen
self.settings = sg_game.settings
# load the head image and set its retc attribute
self.image = pygame.image.load('images/head.png')
self.rect = self.image.get_rect()
# create snake at the starting position
self.rect.x = self.settings.starting_x
self.rect.y = self.settings.starting_y
self.x = float(self.rect.x)
self.y = float(self.rect.y)
def update(self):
"""Move the head in 4 directions"""
if self.settings.snake_dir == 0:
self.y -= self.settings.snake_speed
self.rect.y = self.y
elif self.settings.snake_dir == 1:
self.x -= self.settings.snake_speed
self.rect.x = self.x
elif self.settings.snake_dir == 2:
self.y += self.settings.snake_speed
self.rect.y = self.y
elif self.settings.snake_dir == 3:
self.x += self.settings.snake_speed
self.rect.x = self.x
def set_up(self):
"""Set up snake at the starting position"""
self.rect.x = self.settings.starting_x
self.rect.y = self.settings.starting_y
self.x = float(self.rect.x)
self.y = float(self.rect.y)
def blitme(self):
"""Draw the head at its current location"""
self.screen.blit(self.image, self.rect)
class Tail(Sprite):
"""A class for tail of the snake"""
def __init__(self, sg_game):
"""Initialize the tail and sets it starting position"""
super().__init__()
self.screen = sg_game.screen
self.head = sg_game.head
self.settings = sg_game.settings
self.snake_tail = sg_game.snake_tail
# load the image and set its rect attribute
self.image = pygame.image.load('images/tail.png')
self.rect = self.image.get_rect()
# starts tail at the back of the snake
self.rect.x = self.head.rect.x
self.rect.y = self.head.rect.y
self.x = float(self.rect.x)
self.y = float(self.rect.y)
def update(self):
"""update the tail of the snake"""
self.rect.x = self.head.rect.x
self.rect.y = self.head.rect.y