0
\$\begingroup\$
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 . '

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

enter image description here

The "traditional" snake game is grid-based. Basically, maintain a list of body positions. Add a position node from the head when moving, delete a node from the tail, and not delete when the snake grows.

Anyway, to achieve "smooth" movement, there must be a list of body positions:

player=[(580,400)]

To make the growth noticeable, set a minimum moving distance:

step_len = 5
...
isMoved = True
if key_pressed[pygame.K_UP]:
    playery-=step_len
elif key_pressed[pygame.K_DOWN]:
    playery+=step_len
elif key_pressed[pygame.K_LEFT]:
    playerx-=step_len
elif key_pressed[pygame.K_RIGHT]:
    playerx+=step_len
else:
    isMoved = False

And update the snake body when moving:

if isMoved:
    player.insert(0,(playerx,playery))
    #Colliding with the ball 
    if snake.colliderect(ball):
        # roll the ball
    else:
        player.pop(-1)

Full code:

import pygame
from sys import exit 
import random


pygame.init()

#Variables for the snake
step_len = 5
player=[(580,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 = None
    for i in range(len(player)):
        node = pygame.draw.rect(screen,white,(player[i][0],player[i][1],width,height),0)
        if i ==0:
            snake = node
    pygame.display.set_caption("Snake Game")
    
    
    #Keyboard input
    key_pressed=pygame.key.get_pressed()        

    playerx,playery = player[0][0],player[0][1]
    isMoved = True
    if key_pressed[pygame.K_UP]:
        playery-=step_len
    elif key_pressed[pygame.K_DOWN]:
        playery+=step_len
    elif key_pressed[pygame.K_LEFT]:
        playerx-=step_len
    elif key_pressed[pygame.K_RIGHT]:
        playerx+=step_len
    else:
        isMoved = False
    
    if isMoved:
        #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()
        
        player.insert(0,(playerx,playery))
        #Colliding with the ball 
        if snake.colliderect(ball):
            ball_width=random.choice(range(0,1160))
            ball_height=random.choice(range(0,700))
        else:
            player.pop(-1)
    
    pygame.display.update()
    clock.tick(50)
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .