Open source small and fun games writen by AI in Python
Posted: Fri Feb 07, 2025 2:10 pm
Here is small and fun game i called is colorfull snake!

First install python3-pygame.
For ubuntu just run:
Save the code in file snake.py and then run the game with python3 snake.py.
youtu.be/Ylr8QgQ2xp8

First install python3-pygame.
For ubuntu just run:
Code: Select all
sudo pip install pygame
Code: Select all
import pygame
import random
import sys
# Initialize pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
CELL_SIZE = 20
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
ORANGE = (255, 165, 0)
PURPLE = (128, 0, 128)
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Colorful Snake Game")
# Clock to control the frame rate
clock = pygame.time.Clock()
# Font for displaying score
font = pygame.font.SysFont(None, 35)
# Snake class
class Snake:
def __init__(self):
self.positions = [(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)]
self.direction = (0, 0)
self.length = 1 # Initialize length attribute
self.color = random.choice([GREEN, BLUE, YELLOW, CYAN, MAGENTA, ORANGE, PURPLE])
def get_head_position(self):
return self.positions[0]
def turn(self, point):
if self.length > 1 and (point[0] * -1, point[1] * -1) == self.direction:
return
else:
self.direction = point
def move(self):
cur = self.get_head_position()
x, y = self.direction
new = (((cur[0] + (x * CELL_SIZE)) % SCREEN_WIDTH), (cur[1] + (y * CELL_SIZE)) % SCREEN_HEIGHT)
if len(self.positions) > 2 and new in self.positions[2:]:
self.reset()
else:
self.positions.insert(0, new)
if len(self.positions) > self.length:
self.positions.pop()
def reset(self):
self.length = 1
self.positions = [(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)]
self.direction = random.choice([(1, 0), (0, 1), (-1, 0), (0, -1)])
self.color = random.choice([GREEN, BLUE, YELLOW, CYAN, MAGENTA, ORANGE, PURPLE])
def draw(self, surface):
for p in self.positions:
r = pygame.Rect((p[0], p[1]), (CELL_SIZE, CELL_SIZE))
pygame.draw.rect(surface, self.color, r)
pygame.draw.rect(surface, WHITE, r, 1)
def handle_keys(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.turn((0, -1))
elif event.key == pygame.K_DOWN:
self.turn((0, 1))
elif event.key == pygame.K_LEFT:
self.turn((-1, 0))
elif event.key == pygame.K_RIGHT:
self.turn((1, 0))
# Food class
class Food:
def __init__(self):
self.position = (0, 0)
self.color = RED
self.randomize_position()
def randomize_position(self):
self.position = (random.randint(0, (SCREEN_WIDTH // CELL_SIZE) - 1) * CELL_SIZE,
random.randint(0, (SCREEN_HEIGHT // CELL_SIZE) - 1) * CELL_SIZE)
def draw(self, surface):
r = pygame.Rect((self.position[0], self.position[1]), (CELL_SIZE, CELL_SIZE))
pygame.draw.rect(surface, self.color, r)
pygame.draw.rect(surface, WHITE, r, 1)
def drawGrid(surface):
for y in range(0, int(SCREEN_HEIGHT), int(CELL_SIZE)):
for x in range(0, int(SCREEN_WIDTH), int(CELL_SIZE)):
r = pygame.Rect((x, y), (CELL_SIZE, CELL_SIZE))
pygame.draw.rect(surface, BLACK, r, 1)
def main():
snake = Snake()
food = Food()
while True:
snake.handle_keys()
drawGrid(screen)
snake.move()
if snake.get_head_position() == food.position:
snake.length += 1
food.randomize_position()
snake.draw(screen)
food.draw(screen)
text = font.render(f"Score: {snake.length - 1}", True, WHITE)
screen.blit(text, (5, 5))
pygame.display.update()
clock.tick(10)
if __name__ == "__main__":
main()
youtu.be/Ylr8QgQ2xp8