Here is a simple implementation of the Snake game using Pygame library and Object Oriented Programming concepts:
main.pyimport pygame
import random
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Set the dimensions of the game window
WINDOW_WIDTH = 500
WINDOW_HEIGHT = 500
# Set the size of each block in the grid (snake body and food)
BLOCK_SIZE = 10
# Set the initial speed of the snake
INITIAL_SPEED = 10
class Snake:
def __init__(self):
self.length = 1
self.positions = [(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2)]
self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
def update(self):
# Update the positions of the snake body blocks
for i in range(len(self.positions) - 1, 0, -1):
self.positions[i] = self.positions[i-1]
# Update the position of the head of the snake based on the current direction
if self.direction == UP:
self.positions[0] = (self.positions[0][0], self.positions[0][1] - BLOCK_SIZE)
elif self.direction == DOWN:
self.positions[0] = (self.positions[0][0], self.positions[0][1] + BLOCK_SIZE)
elif self.direction == LEFT:
self.positions[0] = (self.positions[0][0] - BLOCK_SIZE, self.positions[0][1])
elif self.direction == RIGHT:
self.positions[0] = (self.positions[0][0] + BLOCK_SIZE, self.positions[0][1])
def move_up(self):
if self.direction != DOWN:
self.direction = UP
def move_down(self):
if self.direction != UP:
self.direction = DOWN
def move_left(self):
if self.direction != RIGHT:
self.direction = LEFT
def move_right(self):
if self.direction != LEFT:
self.direction = RIGHT
def grow(self):
self.length += 1
self.positions.append(self.positions[-1])
def get_head_position(self):
return self.positions[0]
def get_body_positions(self):
return self.positions[1:]
class Food:
def __init__(self):
self.position = self.generate_random_position()
def generate_random_position(self):
x = random.randint(0, (WINDOW_WIDTH - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
y = random.randint(0, (WINDOW_HEIGHT - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
return (x, y)
def update(self):
self.position = self.generate_random_position()
# Initialize Pygame
pygame.init()
# Set the dimensions of the game window
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Snake Game')
# Initialize the clock
clock = pygame.time.Clock()
# Set the initial score to 0
score = 0
# Define the directions of movement for the snake
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
# Create the snake and food objects
snake = Snake()
food = Food()
# Start the game loop
running = True
while running:
# Process events (user input, window close, etc)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.move_up()
elif event.key == pygame.K_DOWN:
snake.move_down()
elif event.key == pygame.K_LEFT:
snake.move_left()
elif event.key == pygame.K_RIGHT:
snake.move_right()
# Update the snake and food positions
snake.update()
# Check if the snake ate the food
if snake.get_head_position() == food.position:
snake.grow()
score += 10
food.update()
# Check if the snake collided with itself or the game window boundaries
if snake.get_head_position()[0] < 0 or snake.get_head_position()[0] >= WINDOW_WIDTH or \
snake.get_head_position()[1] < 0 or snake.get_head_position()[1] >= WINDOW_HEIGHT or \
snake.get_head_position() in snake.get_body_positions():
running = False
# Draw the game window
screen.fill(BLACK)
# Draw the snake body blocks
for position in snake.positions:
pygame.draw.rect(screen, GREEN, pygame.Rect(position[0], position[1], BLOCK_SIZE, BLOCK_SIZE))
# Draw the food block
pygame.draw.rect(screen, RED, pygame.Rect(food.position[0], food.position[1], BLOCK_SIZE, BLOCK_SIZE))
# Draw the score on the screen
font = pygame.font.Font(None, 30)
score_text = font.render('Score: ' + str(score), True, WHITE)
screen.blit(score_text, (10, 10))
# Update the display
pygame.display.update()
# Set the clock speed (fps)
clock.tick(INITIAL_SPEED + snake.length*2)
# Quit Pygame
pygame.quit()
4763 chars158 lines
Note: This is a basic implementation of the game, and there are opportunities to improve it further.
gistlibby LogSnag