Project 4 - Snake Game in Python
Project 4 - Snake Game in Python
com
import pygame
import random,sys
pygame.init()
clock = pygame.time.Clock()
#SCREEN
screen = pygame.display.set_mode((800,600))
#CAPTION
pygame.display.set_caption("SNAKE GAME")
#SNAKE KI POSITIONS
snake_pos = [[400,300],[420,300],[440,300]]
#APPLE KI POSITION
#apple_pos = [100,100]
#SCORE
1 7048972696 [email protected]
S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.com
score = 0
#DIRECTIONS
step = 20
up = (0, -step)
left = (-step,0)
right = (step,0)
down = (0,step)
direction = left
#FONT
timer = 0
running = True
while running:
screen.fill((20,150,20))
#EVENT FETCHER
2 7048972696 [email protected]
S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.com
#QUIT
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit(0)
#KEYDOWN
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
print("UP")
direction = up
if event.key == pygame.K_DOWN:
print("DOWN")
direction = down
if event.key == pygame.K_LEFT:
print("LEFT")
direction = left
if event.key == pygame.K_RIGHT:
print("RIGHT")
direction = right
3 7048972696 [email protected]
S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.com
timer += 1
if timer == 6:
timer = 0
if snake_pos[0] == apple_pos:
snake_pos.append(snake_pos[-1])
score += 1
#SNAKE DEATH
if snake_pos[0] == snake_pos[i]:
print("DEAD!")
running = False
pygame.quit()
sys.exit(0)
#VERTICAL BOUNDARY
4 7048972696 [email protected]
S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.com
print("DEAD!")
running = False
pygame.quit()
sys.exit(0)
#HORIZONTAL BOUNDARY
print("DEAD!")
running = False
pygame.quit()
sys.exit(0)
#SNAKE PRINT
#APPLE PRINT
5 7048972696 [email protected]
S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.com
#SCORE PRINT
screen.blit(text, (0,0))
clock.tick(30)
pygame.display.update()
6 7048972696 [email protected]