import pygame
from sys import exit
import random
pygame.init()
#Variables for the snake
playerx=580
playery=400
width=10
height=10
white=(255,255,255)
growing= False
#variabels for the ball
ball_width=110
ball_height=500
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#Screen Details
screen=pygame.display.set_mode((1200,800))
ball=pygame.draw.circle(screen,white,(ball_width,ball_height),3)
pygame.draw.rect(screen,(255,0,0),(10,0,1160,800),1)
snake=pygame.draw.rect(screen,white,(playerx,playery,width,height),0)
pygame.display.set_caption("Snake Game")
#Keyboard input
key_pressed=pygame.key.get_pressed()
if key_pressed[pygame.K_UP]:
playery-=1
if key_pressed[pygame.K_DOWN]:
playery+=1
if key_pressed[pygame.K_LEFT]:
playerx-=1
if key_pressed[pygame.K_RIGHT]:
playerx+=1
#Limiting the snake in the box on x size
if playerx >1160:
pygame.quit()
if playerx < 0:
pygame.quit()
#Limiting the snake in box on y size
if playery >800:
pygame.quit()
if playery<0:
pygame.quit()
#Colliding with the ball
if snake.colliderect(ball):
width+=5
ball_width=random.choice(range(0,1160))
ball_height=random.choice(range(0,700))
pygame.display.update()
clock.tick(90)
'Hello ! I wrote this code to create a snake game. For now it is not finished yet . The biggest problem is that I dont know how to move the snake smoothly and by that I mean to move the snake just by 1 block . Now when I move the snake it moves with the whole body . '