0

I have trouble adding another piece of snake, In this project, which I have the code for below, each successive part replaces the head, but it depends on self.snake_vel and this is what I have a problem with, because if I set it above 20 then the game is not playable because the snake moves too fast. I changed the FPS and it helped, but I would like the game to be smooth and 60 FPS is necessary for that.

TIA.

import pygame
import random
import sys
import time

pygame.init()
font = pygame.font.SysFont('arial', 25)

RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

class Game:

    def __init__(self, width=600, height=400):

        self.width = width
        self.height = height
        self.screen = pygame.display.set_mode([self.width, self.height])
        self.x = [(width / 2)]
        self.y = [(height / 2)]
        self.snake_vel = 5
        self.x_apple = 300
        self.y_apple = 300
        self.score = 0
        self.length = 1
        self.direction = 2
        pygame.display.set_caption('Snake')

    def move_left(self):
        self.direction = 0

    def move_up(self):
        self.direction = 1

    def move_right(self):
        self.direction = 2

    def move_down(self):
        self.direction = 3


    def draw(self):
        
        self.screen.fill(BLACK)
        for i in range(self.length):
            pygame.draw.rect(self.screen, WHITE,(self.x[i], self.y[i], 30, 30))

            
        if ((self.x[0] > self.x_apple - 20) and (self.x[0] < self.x_apple + 20)) and ((self.y[0] > self.y_apple - 20) and (self.y[0] < self.y_apple + 20)):
            self.x_apple = random.randrange(10, self.width - 30)
            self.y_apple = random.randrange(10, self.height - 30)
            self.score += 1
            self.increase_length()
            
        pygame.draw.rect(self.screen, RED,(self.x_apple, self.y_apple, 30, 30))
                         
    def snake_move(self):

        for i in range(self.length-1,0,-1):
            self.x[i] = self.x[i - 1]
            self.y[i] = self.y[i - 1]

            
        if self.direction == 0:
            self.x[0] -= self.snake_vel
        if self.direction == 2:
            self.x[0] += self.snake_vel
        if self.direction == 3:
            self.y[0] += self.snake_vel
        if self.direction == 1:
            self.y[0] -= self.snake_vel

        if self.x[0] <= 0 or self.x[0] >= self.width:
           self.reset_game()

        if self.y[0] <= 0 or self.y[0] >= self.height:
           self.reset_game()

    def reset_game(self):

        self.x = [(self.width / 2)]
        self.y = [(self.height / 2)]
        self.length = 1
        self.score = 0
    
    def increase_length(self):
        self.length += 1
        self.x.append(self.x[-1])
        self.y.append(self.y[-1])
       

def main():
    clock = pygame.time.Clock()
    player = Game(800,600)
    FPS = 60
    run = True

    def redraw_window():

        pygame.display.update()
        player.draw()

    while run:
        clock.tick(FPS)
        redraw_window()
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            keys = pygame.key.get_pressed()

            if keys[pygame.K_LEFT]:
                player.move_left()
            if keys[pygame.K_RIGHT]:
                player.move_right()
            if keys[pygame.K_DOWN]:
                player.move_down()
            if keys[pygame.K_UP]:
                player.move_up()
            
            print(player.x)
        player.snake_move()
    

    
        
if __name__ == '__main__':
    main()
  
  • if you have list then simply use `insert()` to put new value at the beginning of list, and `pop()` to remove last value - and then you don't have to move values. And when you eat apple then you simply skip `pop` in the next move and it will make it longer. (PL: po prostu użyj `insert()` aby dodać element na początku listy, i `pop()` aby usunąc ostatni element - i wtedy nie musisz przenosić wartości z jednej komórki do następnej). – furas Aug 04 '21 at 21:29
  • I don't know why to use `60 FPS` if snake will have to move every few frames -ie. every 3 frames - (you will have to count frames and skip them) so it will move with speed `~20FPS`. Or you will have to use `clock` to move snake every few seconds. You can also use `timer` to generate event every few seconds and when you get this event in `for event` then you will have to move snake. – furas Aug 04 '21 at 21:36

0 Answers0