Snakes
This is the simplest Snakes Game I have ever known.
----Please Follow me this is only request to the the viewer.
import pygame
import random
pygame.init()
# Colors
black = (0, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
red = (255, 0, 0)
white = (255, 255, 255)
screen_width = 900
screen_height = 600
# Creating Window
gameWindow = pygame.display.set_mode((screen_width, screen_height))
# Game Title
pygame.display.set_caption("Snake Game")
pygame.display.update()
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 60)
def text_screen(text, color, x, y):
screen_text = font.render(text, True, color)
gameWindow.blit(screen_text, [x, y])
def plot_snake(gameWindow, color, snk_list, snake_size):
for x,y in snk_list:
pygame.draw.rect(gameWindow, color, [x, y, snake_size, snake_size])
# Game Loop
def gameloop():
# Game Specific Variables
exit_game = False
game_over = False
snake_x = 45
snake_y = 55
velocity_x = 0
velocity_y = 0
snk_list = []
snk_length = 1
food_x = random.randint(20, screen_width/ 2)
food_y = random.randint(20, screen_height/ 2)
score = 0
snake_size = 24
fps = 20
while not exit_game:
if game_over:
gameWindow.fill(white)
text_screen("Game Over! Press Enter To Continue", red, 100, 250)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
gameloop()
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_game = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
velocity_x = 10
velocity_y = 0
if event.key == pygame.K_LEFT:
velocity_x = -10
velocity_y = 0
if event.key == pygame.K_UP:
velocity_y = -10
velocity_x = 0
if event.key == pygame.K_DOWN:
velocity_y = 10
velocity_x = 0
snake_x = snake_x + velocity_x
snake_y = snake_y + velocity_y
if abs(snake_x - food_x)<20 and abs(snake_y - food_y)<20:
score +=1
food_x = random.randint(20, screen_width/ 2)
food_y = random.randint(20, screen_height/ 2)
snk_length +=5
gameWindow.fill(red)
text_screen("Score: " + str(score * 10), white, 5, 5)
pygame.draw.rect(gameWindow, blue, [food_x, food_y, snake_size, snake_size])
head = []
head.append(snake_x)
head.append(snake_y)
snk_list.append(head)
if len(snk_list)>snk_length:
del snk_list[0]
if head in snk_list[:-1]:
game_over = True
if snake_x<0 or snake_x>screen_width or snake_y<0 or snake_y>screen_height:
game_over = True
plot_snake(gameWindow, black, snk_list, snake_size)
pygame.display.update()
clock.tick(fps)
pygame.quit()
quit()
gameloop()
----Type pip install PyGame in integrated terminal or in Windows PowerShell(if you use Windows OS).

Sir I like this and I tried to insert cheat codes but I was not able to insert. Please tell me how to insert.
ReplyDeleteYou are a beginner so you may not insert cheat codes. But regarding Email through AI I'm also working on that.
ReplyDelete