Python Snake Game Basic Codes
import pygame
import time
import random
pygame.init()
# Game Window
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Slither')
# Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
# Snake Size
block_size = 10
# FPS
fps = 15
# Snake Start Position
snake_startx = display_width/2
snake_starty = display_height/2
# Change of Snake Direction
snake_speed = 0
# Snake List
snake_list = []
snake_length = 1
# Random Apple Spawn
randAppleX = round(random.randrange(0, display_width - block_size)/10.0)*10.0
randAppleY = round(random.randrange(0, display_height - block_size)/10.0)*10.0
# Game Loop
game_over = False
game_close = False
# Clock
clock = pygame.time.Clock()
# Message Function
def message_to_screen(msg,color):
mesg = font_style.render(msg, True, color)
gameDisplay.blit(mesg, [display_width/2-len(msg)*11/2, display_height/2])
# Game Loop
while not game_over:
while game_close == True:
gameDisplay.fill(white)
message_to_screen("You Lost! Press C-Play Again or Q-Quit", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
game_close = False
snake_list = []
snake_length = 1
randAppleX = round(random.randrange(0, display_width - block_size)/10.0)*10.0
randAppleY = round(random.randrange(0, display_height - block_size)/10.0)*10.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snake_speed = -10
if event.key == pygame.K_RIGHT:
snake_speed = 10
if event.key == pygame.K_UP:
snake_speed = -10
if event.key == pygame.K_DOWN:
snake_speed = 10
if snake_speed == 0:
snake_speed = 10
x1_change = snake_speed
y1_change = 0
snake_Head = []
snake_Head.append(x1_change)
snake_Head.append(y1_change)
snake_list.append(snake_Head)
if len(snake_list) > snake_length:
del snake_list[0]
for x in snake_list[:-1]:
if x == snake_Head:
game_close = True
x1 += x1_change
y1 += y1_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, red,[randAppleX, randAppleY, block_size, block_size])
snake_skin = blue
for x in snake_list:
pygame.draw.rect(gameDisplay, snake_skin, [x[0],x[1], block_size, block_size])
pygame.display.update()
if x1 >= display_width or x1 < 0 or y1 >= display_height or y1 < 0:
game_close = True
clock.tick(fps)
pygame.quit()
quit()
Explanation:
* Import necessary modules:
* pygame: For game development functionalities.
* time: For controlling game speed.
* random: For generating random apple positions.
* Initialize pygame:
* pygame.init(): Initializes all pygame modules.
* Game Window setup:
* display_width, display_height: Define the game window dimensions.
* gameDisplay: Creates the game window surface.
* pygame.display.set_caption(): Sets the title of the game window.
* Color definitions:
* black, white, red, blue: Define colors used in the game.
* Game parameters:
* block_size: Size of each snake block.
* fps: Frames per second, controlling game speed.
* Snake initializations:
* snake_startx, snake_starty: Starting position of the snake.
* snake_speed: Initial speed of the snake.
* snake_list: A list to store the coordinates of each snake block.
* snake_length: Initial length of the snake.
* Apple initialization:
* randAppleX, randAppleY: Randomly generate the initial x and y coordinates of the apple.
* Game loop variables:
* game_over: Flag to indicate the end of the game.
* game_close: Flag to indicate whether the game is paused (after a collision).
* Clock for controlling game speed:
* clock = pygame.time.Clock()
* Message function:
* message_to_screen(): A helper function to display messages on the screen.
* Main game loop:
* Event handling:
* pygame.event.get(): Handles user input events (e.g., keyboard presses, window closing).
* Controls snake movement based on arrow key presses.
* Snake movement:
* Updates snake coordinates based on current speed.
* Checks for collisions with itself.
* Apple handling:
* Draws the apple on the screen.
* Checks if the snake has eaten the apple.
* Snake drawing:
* Draws the snake on the screen.
* Boundary checks:
* Checks if the snake has gone off the screen.
* Game over/close handling:
* Displays game over/close messages.
* Handles user input for restarting or quitting.
* Update the display:
* pygame.display.update(): Updates the game window with the latest changes.
* Control game speed:
* clock.tick(fps): Limits the game to run at the specified FPS.
* Quit pygame:
* pygame.quit()
* quit(): Exits the program.
This code provides a basic framework for the classic Snake game in Python using pygame. You can extend it further by:
* Adding sound effects.
* Implementing different game levels.
* Increasing game difficulty over time.
* Creating a more visually appealing interface.
* Adding multiplayer functionality.
* And much more!
Comments
Post a Comment