
Snake Python
Presentation
•
Computers
•
KG
•
Practice Problem
•
Hard
Romenion Kz
Used 1+ times
FREE Resource
15 Slides • 0 Questions
1
Terminal
pip install pygame
2
import pygame
from random import randrange
3
RES = 800
SIZE = 50
x, y = randrange(SIZE, RES - SIZE, SIZE), randrange(SIZE, RES - SIZE, SIZE)
apple = randrange(SIZE, RES - SIZE, SIZE), randrange(SIZE, RES - SIZE, SIZE)
4
RES = 800
SIZE = 50
x, y = randrange(SIZE, RES - SIZE, SIZE), randrange(SIZE, RES - SIZE, SIZE)
apple = randrange(SIZE, RES - SIZE, SIZE), randrange(SIZE, RES - SIZE, SIZE)
5
length = 1
snake = [(x, y)]
dx, dy = 0, 0
fps = 60
6
dirs = {'W': True, 'S': True, 'A': True, 'D': True, }
score = 0
speed_count, snake_speed = 0, 10
7
pygame.init()
surface = pygame.display.set_mode([RES, RES])
clock = pygame.time.Clock()
font_score = pygame.font.SysFont('Arial', 26, bold=True)
font_end = pygame.font.SysFont('Arial', 66, bold=True)
img = pygame.image.load('1.jpg').convert()
8
def close_game():
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
9
while True:
surface.blit(img, (0, 0))
# drawing snake, apple
[pygame.draw.rect(surface, pygame.Color('green'), (i, j, SIZE - 1, SIZE - 1)) for i, j in snake]
pygame.draw.rect(surface, pygame.Color('red'), (*apple, SIZE, SIZE))
10
# show score
render_score = font_score.render(f'SCORE: {score}', 1, pygame.Color('orange'))
surface.blit(render_score, (5, 5))
# snake movement
speed_count += 1
11
# snake movement
speed_count += 1
if not speed_count % snake_speed:
x += dx * SIZE
y += dy * SIZE
snake.append((x, y))
snake = snake[-length:]
12
# eating food
if snake[-1] == apple:
apple = randrange(SIZE, RES - SIZE, SIZE),randrange(SIZE, RES - SIZE, SIZE)
length += 1
score += 1
snake_speed -= 1
snake_speed = max(snake_speed, 4)
13
# game over
if x < 0 or x > RES - SIZE or y < 0 or y > RES - SIZE or len(snake) != len(set(snake)):
while True:
render_end = font_end.render('GAME OVER', 1, pygame.Color('orange'))
surface.blit(render_end, (RES // 2 - 200, RES // 3))
pygame.display.flip()
close_game()
14
# game over
if x < 0 or x > RES - SIZE or y < 0 or y > RES - SIZE or len(snake) != len(set(snake)):
while True:
render_end = font_end.render('GAME OVER', 1, pygame.Color('orange'))
surface.blit(render_end, (RES // 2 - 200, RES // 3))
pygame.display.flip()
close_game()
pygame.display.flip()
clock.tick(fps)
close_game()
15
# controls
key = pygame.key.get_pressed()
if key[pygame.K_w]:
if dirs['W']:
dx, dy = 0, -1
dirs = {'W': True, 'S': False, 'A': True, 'D': True, }
elif key[pygame.K_s]:
if dirs['S']:
dx, dy = 0, 1
dirs = {'W': False, 'S': True, 'A': True, 'D': True, }
elif key[pygame.K_a]:
if dirs['A']:
dx, dy = -1, 0
dirs = {'W': True, 'S': True, 'A': True, 'D': False, }
elif key[pygame.K_d]:
if dirs['D']:
dx, dy = 1, 0
dirs = {'W': True, 'S': True, 'A': False, 'D': True, }
Terminal
pip install pygame
Show answer
Auto Play
Slide 1 / 15
SLIDE
Similar Resources on Wayground
9 questions
Homophones and Homographs
Presentation
•
5th Grade
10 questions
Mississippian Land and Farming
Presentation
•
5th Grade
14 questions
Google Docs Toolbar (Lesson)
Presentation
•
2nd Grade - Professio...
10 questions
Computer Fundamentals
Presentation
•
4th - 10th Grade
10 questions
Spot the Difference
Presentation
•
6th - 8th Grade
10 questions
Avoiding Double Negatives
Presentation
•
6th - 8th Grade
11 questions
Symbolism
Presentation
•
6th - 8th Grade
11 questions
Fun Facts
Presentation
•
4th Grade
Popular Resources on Wayground
20 questions
Math Review
Quiz
•
3rd Grade
15 questions
Fast food
Quiz
•
7th Grade
20 questions
Context Clues
Quiz
•
6th Grade
20 questions
Inferences
Quiz
•
4th Grade
19 questions
Classifying Quadrilaterals
Quiz
•
3rd Grade
20 questions
Figurative Language Review
Quiz
•
6th Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
10 questions
Identify Fractions, Mixed Numbers & Improper Fractions
Quiz
•
3rd - 4th Grade
Discover more resources for Computers
20 questions
Cartoon Characters!
Quiz
•
KG - 5th Grade
10 questions
Movie Trivia
Quiz
•
KG - 2nd Grade
15 questions
Memorial Day Trivia
Quiz
•
KG - 12th Grade
12 questions
Name that Candy
Quiz
•
KG - 12th Grade
20 questions
Guess The App
Quiz
•
KG - Professional Dev...
20 questions
Guess that Disney
Quiz
•
KG - 5th Grade
16 questions
cartoons
Quiz
•
KG - 6th Grade
16 questions
Fun Fun Fun Fun!!!!!!!!!!!!!!
Quiz
•
KG - 5th Grade