Untitled

 avatar
unknown
plain_text
2 years ago
2.9 kB
4
Indexable
import pygame
import time
import random
 
pygame.init()
 
# Set up the window display
display_width = 600
display_height = 400
game_display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Snake Game')
 
# Set up the colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
 
# Set up the clock
clock = pygame.time.Clock()
 
# Set up the snake and its properties
block_size = 10
snake_speed = 15
 
font_style = pygame.font.SysFont(None, 30)
 
def message(msg,color):
    text = font_style.render(msg, True, color)
    game_display.blit(text, [display_width/6, display_height/3])
 
def snake(block_size, snake_list):
    for x in snake_list:
        pygame.draw.rect(game_display, green, [x[0],x[1],block_size,block_size])
 
# Main game loop
def gameLoop():
    game_over = False
    game_close = False
 
    x1 = display_width / 2
    y1 = display_height / 2
 
    x1_change = 0
    y1_change = 0
 
    snake_List = []
    Length_of_snake = 1
 
    foodx = round(random.randrange(0, display_width - block_size) / 10.0) * 10.0
    foody = round(random.randrange(0, display_height - block_size) / 10.0) * 10.0
 
    while not game_over:
 
        while game_close == True:
            game_display.fill(white)
            message("You Lost! Press Q-Quit or C-Play Again",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:
                        gameLoop()
 
        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:
                    x1_change = -block_size
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = block_size
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -block_size
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = block_size
                    x1_change = 0
 
        if x1 >= display_width or x1 < 0 or y1 >= display_height or y1 < 0:
            game_close = True
 
        x1 += x1_change
        y1 += y1_change
 
        game_display.fill(white)
 
        pygame.draw.rect(game_display, red, [foodx, foody, block_size, block_size])
 
        snake_Head = []
        snake_Head.append(x1)
        snake_Head.append(y1)
        snake_List.append(snake_Head)
 
        if len(snake_List) > Length_of_snake:
            del snake_List[0]
 
        for x in snake_List[:-1]:
            if x == snake_Head:
                game_close = True
Editor is loading...