Grade Level: High School / College Introductory Programming
Duration: 90 minutes
pip install pygame).pip install pygame.snake_game.py).Present and explain the following Python code and missing parts:
import pygame
import sys
import random
# Initialize pygame
pygame.init()
# Define colors
WHITE = (255, 255, 255)
YELLOW =
BLACK =
RED =
GREEN =
BLUE =
# Set up display dimensions
dis_width = 600
dis_height =
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('Snake Game')
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
# Set up fonts
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def our_snake(snake_block, snake_list):
# Draws the snake on the screen.
for x in snake_list:
pygame.draw.rect(dis, BLACK, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
# Renders a message on the screen.
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width / 6, dis_height / 3])
def gameLoop():
game_over = False
game_close = False
# Starting position for the snake's head
x1 = dis_width / 2
y1 = dis_height / 2
x1_change = 0
y1_change = 0
snake_list = []
length_of_snake = 1
# Generate initial food coordinates
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close:
dis.fill(BLUE)
message("You lost! Press Q-Quit or C-Play Again", RED)
pygame.display.update()
**************** missing code ************************
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
********************* missing Code **********************
# Check for collisions with walls
****************** missing Code ************************
# Check collision with self
for segment in snake_list[:-1]:
if segment == snake_head:
game_close = True
****************** missing code ***************
clock.tick(snake_speed)
pygame.quit()
sys.exit()
if __name__ == "__main__":
gameLoop()
Discussion Points:
our_snake (drawing the snake) and message (displaying messages).
Begin GameLoop:
Initialize game variables:
Set game_over to False
Set game_close to False
Set snake starting position at center (x, y)
Set movement change variables (x_change, y_change) to 0
Initialize snake_list and length_of_snake = 1
Generate initial food position (food_x, food_y)
While game_over is False:
While game_close is True (game over state):
Fill screen with background color
Display "Game Over" message with options (Q to Quit, C to Restart)
For each event:
If key pressed is 'Q':
Set game_over True and exit inner loop
Else if key pressed is 'C':
Restart gameLoop
For each event in event queue:
If event is QUIT:
Set game_over True
If event is KEYDOWN:
Update direction (x_change, y_change) based on arrow keys
Update snake head position:
x = x + x_change
y = y + y_change
If snake collides with wall:
Set game_close to True
Create new snake head from updated (x, y)
Append new head to snake_list
If snake_list length > length_of_snake:
Remove tail segment from snake_list
For each segment in snake_list except head:
If segment equals snake head (collision with itself):
Set game_close to True
Fill screen with background color
Draw food at (food_x, food_y)
Draw snake using snake_list
If snake head position equals food position:
Generate new food position
Increment length_of_snake
Update display
Control frame rate with clock.tick(snake_speed)
End GameLoop:
Quit pygame and exit program