Computer Project
Computer Project
For the ones who wish to make a career in this field, this small project
can be regarded as their first step. “A small step for man is a giant
leap for mankind.” These words by Neil Armstrong can be used
as a motivation for young programmers and game developers to
create a different sort of thing which is useful as well as entertaining
to the masses.
3
What is python?
4
Why python?
5
ABOUT THE GAME AND HOW IT WORKS
In the game of Snake, the player uses the arrow keys to move
a "snake" around the board. As the snake finds food, it eats the food,
and thereby grows larger. The game ends when the snake either
moves off the screen or moves into itself. The goal is to make the
snake as large as possible before that happens.
6
MODULES/FUNCTIONS USED IN CODING
1. PYGAME
2. TIME
3. RANDOM
1. FOR/WHILE LOOP
2. IF/ELIF/ELSE STATEMENT
7
Source code for the Snake Game
import pygame
import time
import random
pygame.init()
# Define colors
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
# Snake settings
snake_block = 10
8
snake_speed = 15
9
# Main game loop
def gameLoop():
game_over = False
game_close = False
# Change in position
x1_change = 0
y1_change = 0
# Snake body
snake_list = []
length_of_snake = 1
# Food position
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
10
while not game_over:
while game_close:
dis.fill(blue)
message("You Lost! Press C-Play Again or Q-Quit", red)
Your_score(length_of_snake - 1)
pygame.display.update()
11
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
for x in snake_list[:-1]:
if x == snake_head:
game_close = True
12
our_snake(snake_block, snake_list)
Your_score(length_of_snake - 1)
pygame.display.update()
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
13
OUTPUT
14
Conclusion
The good thing about this game and our solution is that it is very
simple. The approach is pretty simple and easy to understand
even for beginners. This game could run on many platforms.
There can be many more features which can be added to this game to
make it more interactive and interesting.
15
BIBLIOGRAPHY
Books used:
1. Sumita Arora Computer text book for class XI
2. Sumita Arora Computer text book for class XII
Websites:
1. https://www.codementor.io/arsya/build-snake-game-using-curses-
du107zpmw
2. https://pythonspot.com/snake-with-pygame/
3. http://programarcadegames.com/python_examples/f.php?
file=snake.py
16