import pygame
import random
# Initialize Pygame
pygame.init()
# Set screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Snake Game")
# Define colors
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# Snake and food classes
class Snake:
def __init__(self):
self.length = 5
self.body = [[400, 300], [400, 290], [400, 280], [400, 270], [400, 260]]
self.direction = "RIGHT"
def move(self):
new_head = [self.body[0][0], self.body[0][1]]
if self.direction == "RIGHT":
new_head[0] += 10
elif self.direction == "LEFT":
new_head[0] -= 10
elif self.direction == "UP":
new_head[1] -= 10
elif self.direction == "DOWN":
new_head[1] += 10
self.body.insert(0, new_head)
if len(self.body) > self.length:
self.body.pop()
def change_direction(self, new_direction):
if new_direction != "UP" and self.body[0][1] < self.body[-1][1]:
self.direction = new_direction
if new_direction != "DOWN" and self.body[0][1] > self.body[-1][1]:
self.direction = new_direction
if new_direction != "LEFT" and self.body[0][0] > self.body[-1][0]:
self.direction = new_direction
if new_direction != "RIGHT" and self.body[0][0] < self.body[-1][0]:
self.direction = new_direction
class Food:
def __init__(self):
self.x = random.randint(10, SCREEN_WIDTH - 10) // 10 * 10
self.y = random.randint(10, SCREEN_HEIGHT - 10) // 10 * 10
def generate(self):
self.x = random.randint(10, SCREEN_WIDTH - 10) // 10 * 10
self.y = random.randint(10, SCREEN_HEIGHT - 10) // 10 * 10
# Game loop
def game_loop(snake, food):
game_over = False
clock = pygame.time.Clock()
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.change_direction("UP")
elif event.key == pygame.K_DOWN:
snake.change_direction("DOWN")
elif event.key == pygame.K_LEFT:
snake.change_direction("LEFT")
elif event.key == pygame.K_RIGHT:
snake.change_direction("RIGHT")
snake.move()
# Check for collision with food
if snake.body[0] == [food.x, food.y]:
food.generate()
snake.length += 1
# Check for collision with walls or self
if snake.body[0][0] < 0 or snake.body[0][0] >= SCREEN_WIDTH or snake.body[0][1] < 0 or snake.body[0][1] >= SCREEN_HEIGHT:
game_over = True
for segment in snake.body[1:]:
if snake.body[0] == segment:
game_over = True
screen.fill(WHITE)
for pos in snake.body:
pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], 10, 10))
pygame.draw.rect(screen, RED, pygame.Rect(food.x, food.y, 10, 10))
pygame.display.update()
clock.tick(15)
pygame.quit()
# Main function
def main():
snake = Snake()
food = Food()
game_loop(snake, food)
if __name__ == "__main__":
main()
import pygame
import random
# Initialize Pygame
pygame.init()
# Set screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Snake Game")
# Define colors
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Snake and food classes
class Snake:
def __init__(self):
self.length = 5
self.body = [[400, 300], [400, 290], [400, 280], [400, 270], [400, 260]]
self.direction = "RIGHT"
def move(self):
new_head = [self.body[0][0], self.body[0][1]]
if self.direction == "RIGHT":
new_head[0] += 10
elif self.direction == "LEFT":
new_head[0] -= 10
elif self.direction == "UP":
new_head[1] -= 10
elif self.direction == "DOWN":
new_head[1] += 10
self.body.insert(0, new_head)
if len(self.body) > self.length:
self.body.pop()
def change_direction(self, new_direction):
if new_direction != "UP" and self.body[0][1] < self.body[-1][1]:
self.direction = new_direction
if new_direction != "DOWN" and self.body[0][1] > self.body[-1][1]:
self.direction = new_direction
if new_direction != "LEFT" and self.body[0][0] > self.body[-1][0]:
self.direction = new_direction
if new_direction != "RIGHT" and self.body[0][0] < self.body[-1][0]:
self.direction = new_direction
class Food:
def __init__(self):
self.x = random.randint(10, SCREEN_WIDTH - 10) // 10 * 10
self.y = random.randint(10, SCREEN_HEIGHT - 10) // 10 * 10
def generate(self):
self.x = random.randint(10, SCREEN_WIDTH - 10) // 10 * 10
self.y = random.randint(10, SCREEN_HEIGHT - 10) // 10 * 10
# Game loop
def game_loop(snake, food, score):
game_over = False
clock = pygame.time.Clock()
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.change_direction("UP")
elif event.key == pygame.K_DOWN:
snake.change_direction("DOWN")
elif event.key == pygame.K_LEFT:
snake.change_direction("LEFT")
elif event.key == pygame.K_RIGHT:
snake.change_direction("RIGHT")
snake.move()
# Check for collision with food
if snake.body[0] == [food.x, food.y]:
food.generate()
snake.length += 1
score += 10
# Check for collision with walls or self
if snake.body[0][0] < 0 or snake.body[0][0] >= SCREEN_WIDTH or snake.body[0][1] < 0 or snake.body[0][1] >= SCREEN_HEIGHT:
game_over = True
for segment in snake.body[1:]:
if snake.body[0] == segment:
game_over = True
screen.fill(WHITE)
for pos in snake.body:
pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], 10, 10))
pygame.draw.rect(screen, RED, pygame.Rect(food.x, food.y, 10, 10))
pygame.display