|
| 1 | +# Convert block into a snake |
| 2 | +# Draw apple at random locations |
| 3 | + |
| 4 | +import pygame |
| 5 | +from pygame.locals import * |
| 6 | +import time |
| 7 | +import random |
| 8 | + |
| 9 | +SIZE = 40 |
| 10 | + |
| 11 | +class Apple: |
| 12 | + def __init__(self, parent_screen): |
| 13 | + self.parent_screen = parent_screen |
| 14 | + self.image = pygame.image.load("resources/apple.jpg").convert() |
| 15 | + self.x = 120 |
| 16 | + self.y = 120 |
| 17 | + |
| 18 | + def draw(self): |
| 19 | + self.parent_screen.blit(self.image, (self.x, self.y)) |
| 20 | + pygame.display.flip() |
| 21 | + |
| 22 | + def move(self): |
| 23 | + self.x = random.randint(1,25)*SIZE |
| 24 | + self.y = random.randint(1,20)*SIZE |
| 25 | + |
| 26 | +class Snake: |
| 27 | + def __init__(self, parent_screen, length): |
| 28 | + self.parent_screen = parent_screen |
| 29 | + self.image = pygame.image.load("resources/block.jpg").convert() |
| 30 | + self.direction = 'down' |
| 31 | + |
| 32 | + self.length = length |
| 33 | + self.x = [40]*length |
| 34 | + self.y = [40]*length |
| 35 | + |
| 36 | + def move_left(self): |
| 37 | + self.direction = 'left' |
| 38 | + |
| 39 | + def move_right(self): |
| 40 | + self.direction = 'right' |
| 41 | + |
| 42 | + def move_up(self): |
| 43 | + self.direction = 'up' |
| 44 | + |
| 45 | + def move_down(self): |
| 46 | + self.direction = 'down' |
| 47 | + |
| 48 | + def walk(self): |
| 49 | + # update body |
| 50 | + for i in range(self.length-1,0,-1): |
| 51 | + self.x[i] = self.x[i-1] |
| 52 | + self.y[i] = self.y[i-1] |
| 53 | + |
| 54 | + # update head |
| 55 | + if self.direction == 'left': |
| 56 | + self.x[0] -= SIZE |
| 57 | + if self.direction == 'right': |
| 58 | + self.x[0] += SIZE |
| 59 | + if self.direction == 'up': |
| 60 | + self.y[0] -= SIZE |
| 61 | + if self.direction == 'down': |
| 62 | + self.y[0] += SIZE |
| 63 | + |
| 64 | + self.draw() |
| 65 | + |
| 66 | + def draw(self): |
| 67 | + self.parent_screen.fill((110, 110, 5)) |
| 68 | + |
| 69 | + for i in range(self.length): |
| 70 | + self.parent_screen.blit(self.image, (self.x[i], self.y[i])) |
| 71 | + pygame.display.flip() |
| 72 | + |
| 73 | + def increase_length(self): |
| 74 | + self.length += 1 |
| 75 | + self.x.append(-1) |
| 76 | + self.y.append(-1) |
| 77 | + |
| 78 | +class Game: |
| 79 | + def __init__(self): |
| 80 | + pygame.init() |
| 81 | + self.surface = pygame.display.set_mode((1000, 800)) |
| 82 | + self.snake = Snake(self.surface, 5) |
| 83 | + self.snake.draw() |
| 84 | + self.apple = Apple(self.surface) |
| 85 | + self.apple.draw() |
| 86 | + |
| 87 | + |
| 88 | + def play(self): |
| 89 | + self.snake.walk() |
| 90 | + self.apple.draw() |
| 91 | + |
| 92 | + def run(self): |
| 93 | + running = True |
| 94 | + |
| 95 | + while running: |
| 96 | + for event in pygame.event.get(): |
| 97 | + if event.type == KEYDOWN: |
| 98 | + if event.key == K_ESCAPE: |
| 99 | + running = False |
| 100 | + |
| 101 | + if event.key == K_LEFT: |
| 102 | + self.snake.move_left() |
| 103 | + |
| 104 | + if event.key == K_RIGHT: |
| 105 | + self.snake.move_right() |
| 106 | + |
| 107 | + if event.key == K_UP: |
| 108 | + self.snake.move_up() |
| 109 | + |
| 110 | + if event.key == K_DOWN: |
| 111 | + self.snake.move_down() |
| 112 | + |
| 113 | + elif event.type == QUIT: |
| 114 | + running = False |
| 115 | + |
| 116 | + self.play() |
| 117 | + |
| 118 | + time.sleep(.2) |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + game = Game() |
| 122 | + game.run() |
| 123 | + |
| 124 | + |
0 commit comments