I currently have a problem with the segment logic in which the segments overlap making the snake look basically like a square than a snake. And I know that using speed = size and delay the loop solves this problem but it makes the movement feel janky and i hate that. I’m thinking about using a for loop for the other segments except the head but I don’t know how.
import pygame import sys # Initialize Pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 1920,1080 # Create the screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Snake Game") #Coordinates snake: font = pygame.font.Font(None, 32) def draw_grid(): """Draw a grid on the screen.""" for x in range(0, WIDTH, size): pygame.draw.line(screen, (50, 50, 50), (x, 0), (x, HEIGHT)) for y in range(0, HEIGHT, size): pygame.draw.line(screen, (50, 50, 50), (0, y), (WIDTH, y)) # Game loop size = 30 snake = [(300, 300), (240, 300), (180, 300)] length = 3 speed = 5 direction = 'RIGHT' next_direction = direction # Clock for controlling frame rate clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Key presses for direction pressed_key = pygame.key.get_pressed() if pressed_key[pygame.K_UP] and direction != 'DOWN': next_direction = "UP" elif pressed_key[pygame.K_DOWN] and direction != 'UP': next_direction = "DOWN" elif pressed_key[pygame.K_LEFT] and direction != 'RIGHT': next_direction = "LEFT" elif pressed_key[pygame.K_RIGHT] and direction != 'LEFT': next_direction = "RIGHT" # Get the current head position head_x, head_y = snake[0] # Change direction only when the head is fully aligned in the grid if not head_x % size and not head_y % size: direction = next_direction #This will make the snake teleport when reaching the borders of the screen to the other side head_x %= WIDTH head_y %= HEIGHT # the snake will move indefinitely according to the direction if direction == 'UP': head_y -= speed elif direction == 'DOWN': head_y += speed elif direction == 'LEFT': head_x -= speed elif direction == 'RIGHT': head_x += speed # text = font.render(f'{x:.1f},{y:.1f}', True, (0, 255, 0)) # textRect = text.get_rect() # textRect.center = (screen.get_width() // 2, screen.get_height() // 2) #Creates a new head with the new coordinates 'popping' the previous elements snake.insert(0, (head_x, head_y)) snake.pop() screen.fill((6, 64, 43)) draw_grid() #screen.blit(text, textRect) #For displaying coordinates for segment in snake: pygame.draw.rect(screen, (0,0,255), (*segment, size, size)) # Updates the screen pygame.display.update() clock.tick(60)
submitted by /u/lord8bits
[link] [comments]
r/learnpython I currently have a problem with the segment logic in which the segments overlap making the snake look basically like a square than a snake. And I know that using speed = size and delay the loop solves this problem but it makes the movement feel janky and i hate that. I’m thinking about using a for loop for the other segments except the head but I don’t know how. import pygame import sys # Initialize Pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 1920,1080 # Create the screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption(“Snake Game”) #Coordinates snake: font = pygame.font.Font(None, 32) def draw_grid(): “””Draw a grid on the screen.””” for x in range(0, WIDTH, size): pygame.draw.line(screen, (50, 50, 50), (x, 0), (x, HEIGHT)) for y in range(0, HEIGHT, size): pygame.draw.line(screen, (50, 50, 50), (0, y), (WIDTH, y)) # Game loop size = 30 snake = [(300, 300), (240, 300), (180, 300)] length = 3 speed = 5 direction = ‘RIGHT’ next_direction = direction # Clock for controlling frame rate clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Key presses for direction pressed_key = pygame.key.get_pressed() if pressed_key[pygame.K_UP] and direction != ‘DOWN’: next_direction = “UP” elif pressed_key[pygame.K_DOWN] and direction != ‘UP’: next_direction = “DOWN” elif pressed_key[pygame.K_LEFT] and direction != ‘RIGHT’: next_direction = “LEFT” elif pressed_key[pygame.K_RIGHT] and direction != ‘LEFT’: next_direction = “RIGHT” # Get the current head position head_x, head_y = snake[0] # Change direction only when the head is fully aligned in the grid if not head_x % size and not head_y % size: direction = next_direction #This will make the snake teleport when reaching the borders of the screen to the other side head_x %= WIDTH head_y %= HEIGHT # the snake will move indefinitely according to the direction if direction == ‘UP’: head_y -= speed elif direction == ‘DOWN’: head_y += speed elif direction == ‘LEFT’: head_x -= speed elif direction == ‘RIGHT’: head_x += speed # text = font.render(f'{x:.1f},{y:.1f}’, True, (0, 255, 0)) # textRect = text.get_rect() # textRect.center = (screen.get_width() // 2, screen.get_height() // 2) #Creates a new head with the new coordinates ‘popping’ the previous elements snake.insert(0, (head_x, head_y)) snake.pop() screen.fill((6, 64, 43)) draw_grid() #screen.blit(text, textRect) #For displaying coordinates for segment in snake: pygame.draw.rect(screen, (0,0,255), (*segment, size, size)) # Updates the screen pygame.display.update() clock.tick(60) submitted by /u/lord8bits [link] [comments]
I currently have a problem with the segment logic in which the segments overlap making the snake look basically like a square than a snake. And I know that using speed = size and delay the loop solves this problem but it makes the movement feel janky and i hate that. I’m thinking about using a for loop for the other segments except the head but I don’t know how.
import pygame import sys # Initialize Pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 1920,1080 # Create the screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Snake Game") #Coordinates snake: font = pygame.font.Font(None, 32) def draw_grid(): """Draw a grid on the screen.""" for x in range(0, WIDTH, size): pygame.draw.line(screen, (50, 50, 50), (x, 0), (x, HEIGHT)) for y in range(0, HEIGHT, size): pygame.draw.line(screen, (50, 50, 50), (0, y), (WIDTH, y)) # Game loop size = 30 snake = [(300, 300), (240, 300), (180, 300)] length = 3 speed = 5 direction = 'RIGHT' next_direction = direction # Clock for controlling frame rate clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # Key presses for direction pressed_key = pygame.key.get_pressed() if pressed_key[pygame.K_UP] and direction != 'DOWN': next_direction = "UP" elif pressed_key[pygame.K_DOWN] and direction != 'UP': next_direction = "DOWN" elif pressed_key[pygame.K_LEFT] and direction != 'RIGHT': next_direction = "LEFT" elif pressed_key[pygame.K_RIGHT] and direction != 'LEFT': next_direction = "RIGHT" # Get the current head position head_x, head_y = snake[0] # Change direction only when the head is fully aligned in the grid if not head_x % size and not head_y % size: direction = next_direction #This will make the snake teleport when reaching the borders of the screen to the other side head_x %= WIDTH head_y %= HEIGHT # the snake will move indefinitely according to the direction if direction == 'UP': head_y -= speed elif direction == 'DOWN': head_y += speed elif direction == 'LEFT': head_x -= speed elif direction == 'RIGHT': head_x += speed # text = font.render(f'{x:.1f},{y:.1f}', True, (0, 255, 0)) # textRect = text.get_rect() # textRect.center = (screen.get_width() // 2, screen.get_height() // 2) #Creates a new head with the new coordinates 'popping' the previous elements snake.insert(0, (head_x, head_y)) snake.pop() screen.fill((6, 64, 43)) draw_grid() #screen.blit(text, textRect) #For displaying coordinates for segment in snake: pygame.draw.rect(screen, (0,0,255), (*segment, size, size)) # Updates the screen pygame.display.update() clock.tick(60)
submitted by /u/lord8bits
[link] [comments]