100 Lines of Code: Creating a Fun Python Game

Python, with its concise syntax and powerful libraries, is a great language for creating small yet engaging games. In this article, we’ll explore how to build a simple yet addictive snake game using just 100 lines of Python code.

The snake game is a classic arcade game where the player controls a snake that grows longer by eating food items. The challenge is to navigate the snake without colliding with the walls or its own body.

Here’s the code for the snake game in 100 lines or less:

pythonimport pygame
import sys

# Initialize pygame
pygame.init()

# Set up the screen
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("Snake Game")

# Define colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# Define the snake
snake = [(50, 50), (40, 50), (30, 50)]
food = (150, 150)

# Define the direction
direction = 'RIGHT'

# Game loop
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and direction != 'RIGHT':
direction = 'LEFT'
if event.key == pygame.K_RIGHT and direction != 'LEFT':
direction = 'RIGHT'
if event.key == pygame.K_UP and direction != 'DOWN':
direction = 'UP'
if event.key == pygame.K_DOWN and direction != 'UP':
direction = 'DOWN'

# Move the snake
new_head = list(snake[0])
if direction == 'RIGHT':
new_head[0] += 20
if direction == 'LEFT':
new_head[0] -= 20
if direction == 'UP':
new_head[1] -= 20
if direction == 'DOWN':
new_head[1] += 20

snake.insert(0, new_head)

# Check for collision with walls or body
if new_head[0] < 0 or new_head[0] >= 400 or new_head[1] < 0 or new_head[1] >= 400:
running = False
for segment in snake[1:]:
if new_head == segment:
running = False

# Check for food
if new_head == food:
food = None
while not food or food in snake:
food = (random.randint(0, 19) * 20, random.randint(0, 19) * 20)
else:
snake.pop()

# Draw the snake and food
screen.fill(WHITE)
for pos in snake:
pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], 20, 20))
if food:
pygame.draw.rect(screen, RED, pygame.Rect(food[0], food[1], 20, 20))

pygame.display.flip()
clock.tick(10)

pygame.quit()
sys.exit()

In this code, we use the pygame library for handling graphics and events. We set up a window with a white background and define the colors for the snake and food. The snake is represented as a list of tuples, where each tuple represents a segment of the snake with its x and y coordinates.

The game loop continuously checks for events like keyboard input and quits the game when the player presses the close button. Inside the loop, we update the snake’s position based on the direction and check for collisions with the walls or the snake’s body. If the snake eats the food, we generate a new food position randomly.

Finally, we draw the snake and food on the screen using pygame’s drawing functions. The game runs at a constant framerate of 10 frames

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *