Skip to content

Instantly share code, notes, and snippets.

@dhcgn
Created April 19, 2024 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhcgn/b194fa56b50135efca063881ca00c550 to your computer and use it in GitHub Desktop.
Save dhcgn/b194fa56b50135efca063881ca00c550 to your computer and use it in GitHub Desktop.
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up display
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Snake Game")
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
# Snake initial position and properties
snake_pos = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
snake_direction = "RIGHT"
change_to = snake_direction
# Food position and properties
food_pos = [random.randrange(1, (screen_width//10)) * 10, random.randrange(1, (screen_height//10)) * 10]
food_spawn = True
# Score
score = 0
# Clock to control game speed
clock = pygame.time.Clock()
# Font for displaying score
font = pygame.font.Font(None, 36)
def draw_snake(snake_body):
for pos in snake_body:
pygame.draw.rect(screen, green, (pos[0], pos[1], 10, 10))
def draw_food(food_pos):
pygame.draw.rect(screen, white, (food_pos[0], food_pos[1], 10, 10))
def draw_score(score):
score_text = font.render("Score: " + str(score), True, white)
screen.blit(score_text, (10, 10))
def collision_detection(snake_pos, snake_body):
# Check if the snake collides with itself
for segment in snake_body[1:]:
if snake_pos[0] == segment[0] and snake_pos[1] == segment[1]:
return True
# Check if the snake hits the wall
if snake_pos[0] < 0 or snake_pos[0] >= screen_width or snake_pos[1] < 0 or snake_pos[1] >= screen_height:
return True
return False
def main():
global snake_direction, change_to, food_spawn, score, snake_pos, snake_body, food_pos
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and not (snake_direction == "DOWN"):
change_to = "UP"
elif event.key == pygame.K_DOWN and not (snake_direction == "UP"):
change_to = "DOWN"
elif event.key == pygame.K_LEFT and not (snake_direction == "RIGHT"):
change_to = "LEFT"
elif event.key == pygame.K_RIGHT and not (snake_direction == "LEFT"):
change_to = "RIGHT"
snake_direction = change_to
# Clear the screen
screen.fill(black)
# Move the snake
if snake_direction == "UP":
snake_pos[1] -= 10
elif snake_direction == "DOWN":
snake_pos[1] += 10
elif snake_direction == "LEFT":
snake_pos[0] -= 10
elif snake_direction == "RIGHT":
snake_pos[0] += 10
# Add the new position to the snake body
snake_body.insert(0, list(snake_pos))
# Remove the last segment of the snake body
if not collision_detection(snake_pos, snake_body):
del snake_body[-1]
else:
pygame.quit()
quit()
# Spawn food if it's not already spawned
if food_spawn:
x = random.randrange(1, (screen_width//10)) * 10
y = random.randrange(1, (screen_height//10)) * 10
food_pos = [x, y]
food_spawn = False
# Check if the snake ate the food
if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:
score += 1
food_spawn = True
# Draw the snake, food, and score
draw_snake(snake_body)
draw_food(food_pos)
draw_score(score)
pygame.display.update()
clock.tick(10)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment