0% found this document useful (0 votes)
5 views3 pages

Import Pygame

This document is a Python script that implements a simple Snake game using the Pygame library. It defines the game window, controls for the snake, food generation, and scoring system. The game runs in a loop until the player either quits or the snake collides with itself or the boundaries.

Uploaded by

basheerdars
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Import Pygame

This document is a Python script that implements a simple Snake game using the Pygame library. It defines the game window, controls for the snake, food generation, and scoring system. The game runs in a loop until the player either quits or the snake collides with itself or the boundaries.

Uploaded by

basheerdars
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

import random
pygame.init()

# Colors
white = (255, 255, 255)
red = (255, 0, 0)
black = (0, 0, 0)

# creating window
screen_width = 900
screen_height = 600
gamewindow = pygame.display.set_mode((screen_width, screen_height))

# Game Caption
pygame.display.set_caption("Snake by Zohaib Jarwar")
pygame.display.update()

clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 55)
def text_screen(text, color, x, y):
screen_text = font.render(text, True, color)
gamewindow.blit(screen_text, [x,y])

def plot_snake(gamewindow, color, snk_list, snake_size):


for x,y in snk_list:
pygame.draw.rect(gamewindow, color, [x, y, snake_size, snake_size])

# game loop
def gameloop():
# Game Specific Variables
exit_game = False
game_over = False
snake_x = 45
snake_y = 55
snake_size = 25
fps = 60
velocity_x = 0
velocity_y = 0
food_x = random.randint(20, screen_width / 2)
food_y = random.randint(20, screen_height / 2)
score = 0
init_velocity = 5
snk_list = []
snk_length = 1
while not exit_game:
if game_over:
gamewindow.fill(white)
text_screen("Game Over! Press Enter to Continue", red, 100, 250)

for event in pygame.event.get():


if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
gameloop()
else:

for event in pygame.event.get():


if event.type == pygame.QUIT:
exit_game = True

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
velocity_x = init_velocity
velocity_y = 0
if event.key == pygame.K_LEFT:
velocity_x = - init_velocity
velocity_y = 0
if event.key == pygame.K_UP:
velocity_y = - init_velocity
velocity_x = 0
if event.key == pygame.K_DOWN:
velocity_y = init_velocity
velocity_x = 0

snake_x = snake_x + velocity_x


snake_y = snake_y + velocity_y

if abs(snake_x - food_x)<12 and abs(snake_y - food_y)<12:


score +=1

food_x = random.randint(20, screen_width / 2)


food_y = random.randint(20, screen_height / 2)
snk_length +=5

gamewindow.fill(white)
text_screen("score: " + str(score * 10), red, 5, 5)
pygame.draw.rect(gamewindow, red, [food_x, food_y, snake_size,
snake_size])

head = []
head.append(snake_x)
head.append(snake_y)
snk_list.append(head)

if len(snk_list)>snk_length:
del snk_list[0]

if head in snk_list[:-1]:
game_over = True

if snake_x<0 or snake_x>screen_width or snake_y<0 or


snake_y>screen_height:
game_over = True

plot_snake(gamewindow, black, snk_list, snake_size)


pygame.display.update()
clock.tick(fps)
pygame.quit()

quit()
gameloop()

You might also like