|
import pygame |
|
import time |
|
import random |
|
|
|
pygame.init() |
|
|
|
|
|
screen_width = 800 |
|
screen_height = 600 |
|
|
|
|
|
white = (255, 255, 255) |
|
red = (213, 50, 80) |
|
green = (0, 255, 0) |
|
black = (0, 0, 0) |
|
|
|
|
|
block_size = 20 |
|
snake_speed = 15 |
|
|
|
|
|
dis = pygame.display.set_mode((screen_width, screen_height)) |
|
pygame.display.set_caption("Snake Game") |
|
|
|
|
|
|
|
def our_snake(block_size, snake_list): |
|
for x in snake_list: |
|
pygame.draw.rect(dis, green, [x[0], x[1], block_size, block_size]) |
|
|
|
|
|
|
|
def game_loop(): |
|
game_over = False |
|
game_close = False |
|
|
|
|
|
snake_x = screen_width / 2 |
|
snake_y = screen_height / 2 |
|
|
|
|
|
snake_x_change = 0 |
|
snake_y_change = 0 |
|
|
|
|
|
snake_list = [] |
|
length_of_snake = 1 |
|
|
|
|
|
food_x = ( |
|
round(random.randrange(0, screen_width - block_size) / block_size) * block_size |
|
) |
|
food_y = ( |
|
round(random.randrange(0, screen_height - block_size) / block_size) * block_size |
|
) |
|
|
|
while not game_over: |
|
while game_close == True: |
|
dis.fill(white) |
|
font_style = pygame.font.SysFont(None, 50) |
|
message = font_style.render( |
|
"You Lost! Press Q-Quit or C-Play Again", True, black |
|
) |
|
dis.blit(message, [screen_width / 6, screen_height / 3]) |
|
|
|
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_loop() |
|
|
|
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_x_change = -block_size |
|
snake_y_change = 0 |
|
elif event.key == pygame.K_RIGHT: |
|
snake_x_change = block_size |
|
snake_y_change = 0 |
|
elif event.key == pygame.K_UP: |
|
snake_y_change = -block_size |
|
snake_x_change = 0 |
|
elif event.key == pygame.K_DOWN: |
|
snake_y_change = block_size |
|
snake_x_change = 0 |
|
|
|
if ( |
|
snake_x >= screen_width |
|
or snake_x < 0 |
|
or snake_y >= screen_height |
|
or snake_y < 0 |
|
): |
|
game_close = True |
|
|
|
snake_x += snake_x_change |
|
snake_y += snake_y_change |
|
dis.fill(white) |
|
|
|
pygame.draw.rect(dis, red, [food_x, food_y, block_size, block_size]) |
|
snake_head = [] |
|
snake_head.append(snake_x) |
|
snake_head.append(snake_y) |
|
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 |
|
|
|
our_snake(block_size, snake_list) |
|
pygame.display.update() |
|
|
|
if snake_x == food_x and snake_y == food_y: |
|
food_x = ( |
|
round(random.randrange(0, screen_width - block_size) / block_size) |
|
* block_size |
|
) |
|
food_y = ( |
|
round(random.randrange(0, screen_height - block_size) / block_size) |
|
* block_size |
|
) |
|
length_of_snake += 1 |
|
|
|
pygame.display.update() |
|
|
|
pygame.time.Clock().tick(snake_speed) |
|
|
|
pygame.quit() |
|
quit() |
|
|
|
|
|
game_loop() |
|
|