snake game python+ sql code
snake game python+ sql code
import pygame
import time
import random
import mysql.connector
# Connect to MySQL
def connect_db():
try:
db_config = {
'user': 'snake_game_user',
'password': 'your_password',
'host': 'localhost',
'database': 'snake_game_db'
}
return mysql.connector.connect(**db_config)
except mysql.connector.Error as err:
print(f"Database connection error: {err}")
raise
# Set display
window_x = 720
window_y = 480
clock = pygame.time.Clock()
fps= 5 # fps controller
# initial direction
direction = 'RIGHT'
change_to = direction
snake_body.insert(0, list(snake_pos))
if snake_pos == food_pos:
score += 10
food_spawn = False
else:
snake_body.pop() #snake grow from 'food'
if not food_spawn:
food_pos = [random.randrange(1, (window_x // 10)) * 10, random.randrange(1,
(window_y // 10)) * 10]
food_spawn = True
game_window.fill(b)
sql part
USE snake_game_db;