Skip to content
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
Binary file modified .DS_Store
Binary file not shown.
Binary file added Snake/.DS_Store
Binary file not shown.
Binary file added Snake/Assets/BoldPixels.otf
Binary file not shown.
Binary file added Snake/Assets/bg_music.mp3
Binary file not shown.
Binary file added Snake/Assets/lose.wav
Binary file not shown.
Binary file added Snake/Assets/powerup.wav
Binary file not shown.
Empty file added Snake/Classes/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions Snake/Classes/apple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pygame
import json
import random

data = json.load(open("data.json"))
rows = data["rows"]
cols = data["cols"]


class Apple(pygame.sprite.Sprite):
def __init__(self, window, size, color):
super().__init__()
self.window = window
self.size = size
self.color = color
self.position = random.randint(0, rows - 1), random.randint(0, cols - 1)

def generate_new_position(self):
return random.randint(0, rows - 1), random.randint(0, cols - 1)

def draw(self):
pygame.draw.rect(
self.window,
self.color,
pygame.Rect(
(self.position[0] * 30, self.position[1] * 30), (self.size, self.size)
),
width=7,
border_radius=3,
)
67 changes: 67 additions & 0 deletions Snake/Classes/snake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pygame


class Snake(pygame.sprite.Sprite):
def __init__(self, window, size):
self.snake_positions = [(0, 0), (1, 0), (2, 0), (3, 0)]
self.size = size
self.window = window
self.moving_direction = "right"
self.increase = False

def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
if self.moving_direction != "right":
self.moving_direction = "left"
elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:
if self.moving_direction != "left":
self.moving_direction = "right"
elif keys[pygame.K_UP] or keys[pygame.K_w]:
if self.moving_direction != "down":
self.moving_direction = "up"
elif keys[pygame.K_DOWN] or keys[pygame.K_s]:
if self.moving_direction != "up":
self.moving_direction = "down"

if not self.increase:
self.snake_positions.pop(0)

last_position = self.snake_positions[-1]

if self.moving_direction == "left":
self.snake_positions.append((last_position[0] - 1, last_position[1]))
elif self.moving_direction == "right":
self.snake_positions.append((last_position[0] + 1, last_position[1]))
elif self.moving_direction == "up":
self.snake_positions.append((last_position[0], last_position[1] - 1))
elif self.moving_direction == "down":
self.snake_positions.append((last_position[0], last_position[1] + 1))

def draw(self):
i = 0
color = (0, 209, 0)
for snake_position in self.snake_positions:
i += 1
if i % 2 == 0:
color = (0, 178, 0)
else:
color = (0, 209, 0)
pygame.draw.rect(
self.window,
color,
pygame.Rect(
(snake_position[0] * self.size, snake_position[1] * self.size),
(self.size, self.size),
),
)

def reset(self):
self.moving_direction = "right"
self.snake_positions = [
(0, 0),
(1, 0),
(2, 0),
(3, 0),
]
self.increase = False
25 changes: 25 additions & 0 deletions Snake/Classes/text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pygame


class Text:
def __init__(self, window, text, font_size, color, alpha):
self.window = window
self.text = text
self.font_size = font_size
self.color = color
self.font = pygame.font.Font("Assets/BoldPixels.otf", self.font_size)
self.text_surface = self.font.render(
self.text, True, self.color
).convert_alpha()
self.text_surface.set_alpha(alpha)
self.alpha = alpha

def draw(self, position):
self.window.blit(self.text_surface, position)

def update_text(self, new_text):
self.text = new_text
self.text_surface = self.font.render(
self.text, True, self.color
).convert_alpha()
self.text_surface.set_alpha(self.alpha)
23 changes: 23 additions & 0 deletions Snake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Snake is a game where you need to control a snake to eat apples while avoiding ramming into yourself or the wall.

Controls - WSAD or arrow keys

I made this when I was learning pygame.

## How to play

1. Download this repo
2. Install pygame
3. Run main.py

Credits -
Lose Sound Effect by <a href="https://pixabay.com/users/r0t0r-34451638/?utm_source=link-attribution&utm_medium=referral&utm_campaign=music&utm_content=151672">R0T0R</a> from <a href="https://pixabay.com/sound-effects//?utm_source=link-attribution&utm_medium=referral&utm_campaign=music&utm_content=151672">Pixabay</a>

Power-Up Sound Effect by <a href="https://pixabay.com/users/freesound_community-46691455/?utm_source=link-attribution&utm_medium=referral&utm_campaign=music&utm_content=6768">freesound_community</a> from <a href="https://pixabay.com/sound-effects//?utm_source=link-attribution&utm_medium=referral&utm_campaign=music&utm_content=6768">Pixabay</a>

Font is BoldPixels by Yuki Pixels from <a href="https://www.1001fonts.com/boldpixels-font.html"> 1001 Fonts.</a>

Background Music from #Uppbeat (free for Creators!):
https://uppbeat.io/t/pecan-pie/boogie

Find the main repository <a href="https://github.com/Mathdallas-code/Python-snake">here</a>.
4 changes: 4 additions & 0 deletions Snake/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"rows": 20,
"cols": 10
}
138 changes: 138 additions & 0 deletions Snake/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import pygame
import math
import json

from Classes.snake import Snake
from Classes.apple import Apple
from Classes.text import Text

pygame.init()
pygame.font.init()
pygame.mixer.init()

data = json.load(open("data.json"))
rows = data["rows"]
cols = data["cols"]

WIDTH, HEIGHT = 30 * rows, 30 * cols
FPS = 10

window = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("Snake 🐍")

white = pygame.Color(255, 255, 255)
green = pygame.Color(0, 255, 0)
red = pygame.Color(255, 0, 0)

snake = Snake(window, 30)
apple = Apple(window, 30, red)

lose_text = Text(window, "You lost! :(", 75, red, 255)
win_text = Text(window, "You won! :D", 75, green, 255)

lose_sound = pygame.mixer.Sound("Assets/lose.wav")
powerup_sound = pygame.mixer.Sound("Assets/powerup.wav")
pygame.mixer.music.load("Assets/bg_music.mp3")
pygame.mixer.music.set_volume(0.3)


bg_color = (1, 5, 36)


def game():
clock = pygame.time.Clock()
run = True

hover_value = 0.0
pygame.mixer.music.play(-1)

score = len(snake.snake_positions)
score_text = Text(window, str(score), 75, (255, 0, 0), 128)
lose_text_under = Text(window, "Your score is " + str(score), 75, white, 255)
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

hover_value += 0.1
window.fill((bg_color))

score_text.update_text(str(score))
lose_text_under.update_text("Your score is " + str(score))

# Handling apple spawning
if apple.position in snake.snake_positions:
snake.increase = True
apple.position = apple.generate_new_position()
score += 1

else:
snake.increase = False

apple.draw()

if snake.increase:
snake.increase = True
pygame.mixer.Sound.play(powerup_sound)

snake.move()
snake.draw()
score_text.draw(
(
WIDTH // 2 - score_text.text_surface.get_width() // 2,
(HEIGHT // 2 - score_text.text_surface.get_height() // 2)
- math.sin(hover_value * 2) * 20,
)
)

if (
snake.snake_positions[-1] in snake.snake_positions[:-1]
or snake.snake_positions[-1][0] < 0
or snake.snake_positions[-1][0] > rows - 1
or snake.snake_positions[-1][1] < 0
or snake.snake_positions[-1][1] > cols - 1
):
run = False
pygame.mixer.Sound.play(lose_sound)
pygame.mixer.music.stop()
window.fill((bg_color))
lose_text.draw(
(
WIDTH // 2 - lose_text.text_surface.get_width() // 2,
50,
)
)
lose_text_under.draw(
(
WIDTH // 2 - lose_text_under.text_surface.get_width() // 2,
125,
)
)
if len(snake.snake_positions) == rows * cols:
run = False
window.fill((bg_color))

win_text.draw(
(
WIDTH // 2 - win_text.text_surface.get_width() // 2,
50,
)
)

pygame.display.flip()
score_text.text_surface.fill((0, 0, 0, 0))


def main(window=window):
while True:
game()
snake.reset()
pygame.time.delay(1500)
pygame.display.flip()


if __name__ == "__main__":
main(window)
1 change: 1 addition & 0 deletions Snake/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pygame