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

added snake game #1081

Merged
merged 2 commits into from
Nov 5, 2024
Merged
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
112 changes: 112 additions & 0 deletions Game_Development/SnakeGame/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import pygame
import random
import sys

# Initialize pygame
pygame.init()

# Screen dimensions and setup
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Snake Game")

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# Game variables
snake_size = 20
snake_speed = 15
snake_pos = [100, 50]
snake_body = [[100, 50], [80, 50], [60, 50]]
direction = 'RIGHT'
change_to = direction
score = 0

# Food
food_pos = [random.randrange(1, (SCREEN_WIDTH // snake_size)) * snake_size,
random.randrange(1, (SCREEN_HEIGHT // snake_size)) * snake_size]
food_spawn = True

# Fonts
font = pygame.font.SysFont('Arial', 24)

# Game over function
def game_over():
screen.fill(BLACK)
game_over_text = font.render(f"Game Over! Your score: {score}", True, WHITE)
screen.blit(game_over_text, [SCREEN_WIDTH // 2 - 100, SCREEN_HEIGHT // 2])
pygame.display.flip()
pygame.time.sleep(2)
pygame.quit()
sys.exit()

# Main game loop
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != 'DOWN':
change_to = 'UP'
elif event.key == pygame.K_DOWN and direction != 'UP':
change_to = 'DOWN'
elif event.key == pygame.K_LEFT and direction != 'RIGHT':
change_to = 'LEFT'
elif event.key == pygame.K_RIGHT and direction != 'LEFT':
change_to = 'RIGHT'

# Update direction
direction = change_to

# Move the snake
if direction == 'UP':
snake_pos[1] -= snake_size
elif direction == 'DOWN':
snake_pos[1] += snake_size
elif direction == 'LEFT':
snake_pos[0] -= snake_size
elif direction == 'RIGHT':
snake_pos[0] += snake_size

# Snake body growing mechanism
snake_body.insert(0, list(snake_pos))
if snake_pos == food_pos:
score += 10
food_spawn = False
else:
snake_body.pop()

if not food_spawn:
food_pos = [random.randrange(1, (SCREEN_WIDTH // snake_size)) * snake_size,
random.randrange(1, (SCREEN_HEIGHT // snake_size)) * snake_size]
food_spawn = True

# Game Over conditions
if (snake_pos[0] < 0 or snake_pos[0] > SCREEN_WIDTH - snake_size or
snake_pos[1] < 0 or snake_pos[1] > SCREEN_HEIGHT - snake_size):
game_over()

for block in snake_body[1:]:
if snake_pos == block:
game_over()

# Fill screen and draw snake and food
screen.fill(BLACK)
for pos in snake_body:
pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], snake_size, snake_size))

pygame.draw.rect(screen, RED, pygame.Rect(food_pos[0], food_pos[1], snake_size, snake_size))

# Display score
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, [0, 0])

# Refresh game screen and set FPS
pygame.display.update()
clock.tick(snake_speed)
2 changes: 2 additions & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,8 @@
* [Main](Game_Development/PingPong/main.py)
* Rock-Paper-Scissors
* [Rps](Game_Development/Rock-Paper-Scissors/RPS.py)
* Snakegame
* [Main](Game_Development/SnakeGame/main.py)
* Snake Game
* [Snake](Game_Development/Snake_Game/Snake.py)
* Space Quiz.Py
Expand Down
Loading