Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) added pause feature #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Snake Eater
A snake game written in Python using the Pygame library.
# Snake Eater (Remix)

This is a remix of a snake game written in Python by [rajatdiptabiswas](https://github.com/rajatdiptabiswas) using the Pygame library. Two snakes duke it out to see who can get the biggest.


## Installing
Expand All @@ -14,25 +15,10 @@ pip3 install pygame
## Running the application
Download the source code from the repository and run the file just as any other Python script (.py) file.
```
python3 Snake\ Game.py
python3 Snake\ Game.py <difficulty>
```

The `difficulty` variable can be changed with the values provided in the comment to set the difficulty level.

The rest of the code is properly commented and self explanatory. Tweaks can be made to change the play style or visuals of the game.


## Screenshots

![1](https://user-images.githubusercontent.com/32998741/33873439-27f635b2-df45-11e7-8fc1-f7812f17447a.png)
*Written using PyCharm*

![2](https://user-images.githubusercontent.com/32998741/33873437-2780ed2a-df45-11e7-9776-b1f151fa4e02.png)
*Active game screen*

![3](https://user-images.githubusercontent.com/32998741/33873440-28647360-df45-11e7-8291-b82d5646352f.png)
*Game over screen*

`<difficulty>` is a parameter that changes the tick rate of the game. If you have a higher difficulty, the tick rate increases, and consequently the snakes get faster, which makes the game more difficult.

## Prerequisites
* [Python](https://www.python.org)
Expand All @@ -42,8 +28,7 @@ The rest of the code is properly commented and self explanatory. Tweaks can be m
## Authors

* **Rajat Dipta Biswas** - *Initial work*

See also the list of [contributors](https://github.com/rajatdiptabiswas/snake-pygame/graphs/contributors) who participated in this project.
* **Mikhail Dmitrienko** - *Added second snake*

## Acknowledgements
* [Pygame Documentations](https://www.pygame.org/docs/)
Expand Down
207 changes: 189 additions & 18 deletions Snake Game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
Made with PyGame
"""

import pygame, sys, time, random
import pygame, sys, time, random, argparse


parser = argparse.ArgumentParser(description='Arguments for Snake Game')

parser.add_argument('difficulty', type=int, help='Choose difficulty level -- (Easy: 10) (Medium: 25) (Hard: 40) (Harder: 60) (Impossible: 120)')

args = parser.parse_args()

clock = pygame.time.Clock()

# Difficulty settings
# Easy -> 10
# Medium -> 25
# Hard -> 40
# Harder -> 60
# Impossible-> 120
difficulty = 25
difficulty = args.difficulty

# Window size
frame_size_x = 720
Expand Down Expand Up @@ -40,29 +48,44 @@
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
yellow = pygame.Color(255, 255, 0)


# FPS (frames per second) controller
fps_controller = pygame.time.Clock()


# Game variables
snake_pos = [100, 50]
snake_body = [[100, 50], [100-10, 50], [100-(2*10), 50]]
snake_pos = [400, 200]
snake_body = [[0, 0], [0, 0], [0, 0]]

food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]
food_spawn = True

bullet_pos = [-1, -1]
bullet_spawn = False

snake2_pos = [0, 0]
snake2_body = [[0, 0], [0, 0], [0, 0]]

bullet2_pos = [-1, -1]
bullet2_spawn = False

direction = 'RIGHT'
direction2 = 'RIGHT'
bullet_direction = ''
bullet2_direction = ''
change_to = direction
change_to2 = direction2

score = 0
snake_1_score = 0
snake_2_score = 0


# Game Over
def game_over():
my_font = pygame.font.SysFont('times new roman', 90)
game_over_surface = my_font.render('YOU DIED', True, red)
game_over_surface = my_font.render('GAME OVER', True, red)
game_over_rect = game_over_surface.get_rect()
game_over_rect.midtop = (frame_size_x/2, frame_size_y/4)
game_window.fill(black)
Expand All @@ -73,18 +96,45 @@ def game_over():
pygame.quit()
sys.exit()

def pause():
loop = 1
my_font = pygame.font.SysFont('times new roman', 90)
game_over_surface = my_font.render('PAUSED', True, blue)
game_over_rect = game_over_surface.get_rect()
game_over_rect.midtop = (frame_size_x/2, frame_size_y/4)
game_window.fill(black)
game_window.blit(game_over_surface, game_over_rect)

while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
loop = 0
if event.key == pygame.K_SPACE:
game_window.fill((0, 0, 0))
loop = 0
pygame.display.update()
# screen.fill((0, 0, 0))
clock.tick(60)

# Score
def show_score(choice, color, font, size):
score_font = pygame.font.SysFont(font, size)
score_surface = score_font.render('Score : ' + str(score), True, color)
score_surface = score_font.render('Snake 1 Score : ' + str(snake_1_score), True, color)
score2_surface = score_font.render('Snake 2 Score : ' + str(snake_2_score), True, color)
score_rect = score_surface.get_rect()
score2_rect = score2_surface.get_rect()
if choice == 1:
score_rect.midtop = (frame_size_x/10, 15)
score_rect.midtop = (frame_size_x/7, 15)
score2_rect.midtop = (frame_size_x/7, 35)
else:
score_rect.midtop = (frame_size_x/2, frame_size_y/1.25)
score_rect.midtop = (frame_size_x/2, (frame_size_y/1.25) - 20)
score2_rect.midtop = (frame_size_x/2, frame_size_y/1.25)
# game_window.blit(score2_surface, score2_rect)
game_window.blit(score_surface, score_rect)
# pygame.display.flip()
game_window.blit(score2_surface, score2_rect)


# Main logic
Expand All @@ -96,18 +146,67 @@ def show_score(choice, color, font, size):
# Whenever a key is pressed down
elif event.type == pygame.KEYDOWN:
# W -> Up; S -> Down; A -> Left; D -> Right
if event.key == pygame.K_UP or event.key == ord('w'):
if event.key == pygame.K_UP:
change_to = 'UP'
if event.key == pygame.K_DOWN or event.key == ord('s'):
if event.key == pygame.K_DOWN:
change_to = 'DOWN'
if event.key == pygame.K_LEFT or event.key == ord('a'):
if event.key == pygame.K_LEFT:
change_to = 'LEFT'
if event.key == pygame.K_RIGHT or event.key == ord('d'):
if event.key == pygame.K_RIGHT:
change_to = 'RIGHT'
if event.key == ord('w'):
change_to2 = 'UP'
if event.key == ord('s'):
change_to2 = 'DOWN'
if event.key == ord('a'):
change_to2 = 'LEFT'
if event.key == ord('d'):
change_to2 = 'RIGHT'
# Esc -> Create event to quit the game
if event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))

if event.key == pygame.K_p:
pause()

if event.key == ord('/'):
bullet_spawn = True
if direction == 'UP':
bullet_pos[0] = snake_pos[0]
bullet_pos[1] = snake_pos[1] - 20
bullet_direction = 'UP'
if direction == 'DOWN':
bullet_pos[0] = snake_pos[0]
bullet_pos[1] = snake_pos[1] + 20
bullet_direction = 'DOWN'
if direction == 'LEFT':
bullet_pos[0] = snake_pos[0] - 20
bullet_pos[1] = snake_pos[1]
bullet_direction = 'LEFT'
if direction == 'RIGHT':
bullet_pos[0] = snake_pos[0] + 20
bullet_pos[1] = snake_pos[1]
bullet_direction = 'RIGHT'

if event.key == pygame.K_SPACE:
bullet2_spawn = True
if direction == 'UP':
bullet2_pos[0] = snake2_pos[0]
bullet2_pos[1] = snake2_pos[1] - 20
bullet2_direction = 'UP'
if direction == 'DOWN':
bullet2_pos[0] = snake2_pos[0]
bullet2_pos[1] = snake2_pos[1] + 20
bullet2_direction = 'DOWN'
if direction == 'LEFT':
bullet2_pos[0] = snake2_pos[0] - 20
bullet2_pos[1] = snake2_pos[1]
bullet2_direction = 'LEFT'
if direction == 'RIGHT':
bullet2_pos[0] = snake2_pos[0] + 20
bullet2_pos[1] = snake2_pos[1]
bullet2_direction = 'RIGHT'

# Making sure the snake cannot move in the opposite direction instantaneously
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
Expand All @@ -118,7 +217,16 @@ def show_score(choice, color, font, size):
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'

# Moving the snake
if change_to2 == 'UP' and direction2 != 'DOWN':
direction2 = 'UP'
if change_to2 == 'DOWN' and direction2 != 'UP':
direction2 = 'DOWN'
if change_to2 == 'LEFT' and direction2 != 'RIGHT':
direction2 = 'LEFT'
if change_to2 == 'RIGHT' and direction2 != 'LEFT':
direction2 = 'RIGHT'

# Moving the first snake
if direction == 'UP':
snake_pos[1] -= 10
if direction == 'DOWN':
Expand All @@ -128,14 +236,50 @@ def show_score(choice, color, font, size):
if direction == 'RIGHT':
snake_pos[0] += 10

# Moving the second snake
if direction2 == 'UP':
snake2_pos[1] -= 10
if direction2 == 'DOWN':
snake2_pos[1] += 10
if direction2 == 'LEFT':
snake2_pos[0] -= 10
if direction2 == 'RIGHT':
snake2_pos[0] += 10

if bullet_direction == 'UP':
bullet_pos[1] -= 10
if bullet_direction == 'DOWN':
bullet_pos[1] += 10
if bullet_direction == 'LEFT':
bullet_pos[0] -= 10
if bullet_direction == 'RIGHT':
bullet_pos[0] += 10

if bullet2_direction == 'UP':
bullet2_pos[1] -= 10
if bullet2_direction == 'DOWN':
bullet2_pos[1] += 10
if bullet2_direction == 'LEFT':
bullet2_pos[0] -= 10
if bullet2_direction == 'RIGHT':
bullet2_pos[0] += 10

# Snake body growing mechanism
snake_body.insert(0, list(snake_pos))
if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:
score += 1
snake2_body.insert(0, list(snake2_pos))

if (snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]) or (bullet_pos[0] == food_pos[0] and bullet_pos[1] == food_pos[1]):
snake_1_score += 1
food_spawn = False
else:
snake_body.pop()

if (snake2_pos[0] == food_pos[0] and snake2_pos[1] == food_pos[1]) or (bullet2_pos[0] == food_pos[0] and bullet2_pos[1] == food_pos[1]):
snake_2_score += 1
food_spawn = False
else:
snake2_body.pop()

# Spawning food on the screen
if not food_spawn:
food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]
Expand All @@ -149,21 +293,48 @@ def show_score(choice, color, font, size):
# xy-coordinate -> .Rect(x, y, size_x, size_y)
pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10))

for pos in snake2_body:
# Snake body
# .draw.rect(play_surface, color, xy-coordinate)
# xy-coordinate -> .Rect(x, y, size_x, size_y)
pygame.draw.rect(game_window, blue, pygame.Rect(pos[0], pos[1], 10, 10))

# Snake food
pygame.draw.rect(game_window, white, pygame.Rect(food_pos[0], food_pos[1], 10, 10))

# Draw Bullet
if bullet_spawn:
pygame.draw.rect(game_window, red, pygame.Rect(bullet_pos[0], bullet_pos[1], 10, 10))

if bullet2_spawn:
pygame.draw.rect(game_window, yellow, pygame.Rect(bullet2_pos[0], bullet2_pos[1], 10, 10))

# Game Over conditions
# Getting out of bounds
if snake_pos[0] < 0 or snake_pos[0] > frame_size_x-10:
game_over()
if snake_pos[1] < 0 or snake_pos[1] > frame_size_y-10:
game_over()

if snake2_pos[0] < 0 or snake2_pos[0] > frame_size_x-10:
game_over()
if snake2_pos[1] < 0 or snake2_pos[1] > frame_size_y-10:
game_over()

if bullet_pos[0] < 0 or bullet_pos[0] > frame_size_x-10:
bullet_spawn = False
if bullet_pos[1] < 0 or bullet_pos[1] > frame_size_y-10:
bullet_spawn = False

# Touching the snake body
for block in snake_body[1:]:
if snake_pos[0] == block[0] and snake_pos[1] == block[1]:
game_over()

show_score(1, white, 'consolas', 20)
for block in snake2_body[1:]:
if snake2_pos[0] == block[0] and snake2_pos[1] == block[1]:
game_over()
show_score(1, white, 'consolas', 17)
# Refresh game screen
pygame.display.update()
# Refresh rate
Expand Down