|
| 1 | +""" |
| 2 | + Simple snake |
| 3 | + made a python game |
| 4 | + hope you like it |
| 5 | + |
| 6 | +""" |
| 7 | + |
| 8 | +import pygame |
| 9 | + |
| 10 | +# --- Globals --- |
| 11 | +# Colors |
| 12 | +BLACK = (0, 0, 0) |
| 13 | +WHITE = (255, 255, 255) |
| 14 | + |
| 15 | +# Set the width and height of each snake segment |
| 16 | +segment_width = 15 |
| 17 | +segment_height = 15 |
| 18 | +# Margin between each segment |
| 19 | +segment_margin = 3 |
| 20 | + |
| 21 | +# Set initial speed |
| 22 | +x_change = segment_width + segment_margin |
| 23 | +y_change = 0 |
| 24 | + |
| 25 | + |
| 26 | +class Segment(pygame.sprite.Sprite): |
| 27 | + """ Class to represent one segment of the snake. """ |
| 28 | + # -- Methods |
| 29 | + # Constructor function |
| 30 | + def __init__(self, x, y): |
| 31 | + # Call the parent's constructor |
| 32 | + super().__init__() |
| 33 | + |
| 34 | + # Set height, width |
| 35 | + self.image = pygame.Surface([segment_width, segment_height]) |
| 36 | + self.image.fill(WHITE) |
| 37 | + |
| 38 | + # Make our top-left corner the passed-in location. |
| 39 | + self.rect = self.image.get_rect() |
| 40 | + self.rect.x = x |
| 41 | + self.rect.y = y |
| 42 | + |
| 43 | +# Call this function so the Pygame library can initialize itself |
| 44 | +pygame.init() |
| 45 | + |
| 46 | +# Create an 800x600 sized screen |
| 47 | +screen = pygame.display.set_mode([800, 600]) |
| 48 | + |
| 49 | +# Set the title of the window |
| 50 | +pygame.display.set_caption('Snake Example') |
| 51 | + |
| 52 | +allspriteslist = pygame.sprite.Group() |
| 53 | + |
| 54 | +# Create an initial snake |
| 55 | +snake_segments = [] |
| 56 | +for i in range(15): |
| 57 | + x = 250 - (segment_width + segment_margin) * i |
| 58 | + y = 30 |
| 59 | + segment = Segment(x, y) |
| 60 | + snake_segments.append(segment) |
| 61 | + allspriteslist.add(segment) |
| 62 | + |
| 63 | + |
| 64 | +clock = pygame.time.Clock() |
| 65 | +done = False |
| 66 | + |
| 67 | +while not done: |
| 68 | + |
| 69 | + for event in pygame.event.get(): |
| 70 | + if event.type == pygame.QUIT: |
| 71 | + done = True |
| 72 | + |
| 73 | + # Set the speed based on the key pressed |
| 74 | + # We want the speed to be enough that we move a full |
| 75 | + # segment, plus the margin. |
| 76 | + if event.type == pygame.KEYDOWN: |
| 77 | + if event.key == pygame.K_LEFT: |
| 78 | + x_change = (segment_width + segment_margin) * -1 |
| 79 | + y_change = 0 |
| 80 | + if event.key == pygame.K_RIGHT: |
| 81 | + x_change = (segment_width + segment_margin) |
| 82 | + y_change = 0 |
| 83 | + if event.key == pygame.K_UP: |
| 84 | + x_change = 0 |
| 85 | + y_change = (segment_height + segment_margin) * -1 |
| 86 | + if event.key == pygame.K_DOWN: |
| 87 | + x_change = 0 |
| 88 | + y_change = (segment_height + segment_margin) |
| 89 | + |
| 90 | + # Get rid of last segment of the snake |
| 91 | + # .pop() command removes last item in list |
| 92 | + old_segment = snake_segments.pop() |
| 93 | + allspriteslist.remove(old_segment) |
| 94 | + |
| 95 | + # Figure out where new segment will be |
| 96 | + x = snake_segments[0].rect.x + x_change |
| 97 | + y = snake_segments[0].rect.y + y_change |
| 98 | + segment = Segment(x, y) |
| 99 | + |
| 100 | + # Insert new segment into the list |
| 101 | + snake_segments.insert(0, segment) |
| 102 | + allspriteslist.add(segment) |
| 103 | + |
| 104 | + # -- Draw everything |
| 105 | + # Clear screen |
| 106 | + screen.fill(BLACK) |
| 107 | + |
| 108 | + allspriteslist.draw(screen) |
| 109 | + |
| 110 | + # Flip screen |
| 111 | + pygame.display.flip() |
| 112 | + |
| 113 | + # Pause |
| 114 | + clock.tick(5) |
| 115 | + |
| 116 | +pygame.quit() |
0 commit comments