This course introduces students to computer programming concepts and practices. Students will write and test computer programs, using various problem-solving strategies. They will learn the fundamentals of program design and apply a software development life-cycle model to a software development project. Students will also learn about computer environments and systems, and explore environmental issues related to computers, safe computing practices, emerging technologies, and post secondary opportunities in computer-related fields.
import pygame
import random
width = 640
height = 400
snake_length = 200
snake_x = width // 2
snake_y = height // 2
snake_dir_x = 0
snake_dir_y = -1
snake_body = []
crashed = False
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game")
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and snake_dir_y != 1:
snake_dir_x = 0
snake_dir_y = -1
elif event.key == pygame.K_DOWN and snake_dir_y != -1:
snake_dir_x = 0
snake_dir_y = 1
elif event.key == pygame.K_LEFT and snake_dir_x != 1:
snake_dir_x = -1
snake_dir_y = 0
elif event.key == pygame.K_RIGHT and snake_dir_x != -1:
snake_dir_x = 1
snake_dir_y = 0
snake_x += snake_dir_x
snake_y += snake_dir_y
if snake_x < 0 or snake_x >= width or snake_y < 0 or snake_y >= height:
crashed = True
if not crashed and (snake_x, snake_y) in snake_body:
crashed = True
if crashed:
running = False
snake_body.insert(0, (snake_x, snake_y))
if len(snake_body) > snake_length:
snake_body.pop()
screen.fill((0, 0, 0))
for seg_x, seg_y in snake_body:
screen.set_at((int(seg_x), int(seg_y)), (255, 255, 255))
pygame.display.flip()
clock.tick(30)
if crashed:
print("Crash!")
pygame.quit()
Icons: 📄 Document - ✳️ Presentation - 📺 Video - 🔗 Website link