how to add segments to the snake body - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: how to add segments to the snake body (/thread-28637.html) |
how to add segments to the snake body - blacklight - Jul-27-2020 Hi so I don't really understand how to add a segment to the correct position after the snake has eaten the fruit. Can someone assist me with this? note: look in the conditional for if the snake has made contact with the fruit. I already have a concept for loop there. import pygame import time import random pygame.font.init() # global variables game = True width, height = 500, 500 x, y = 20, 30 x2, y2 = 240, 250 lives = 3 answer = "yes" segments = [] # setting up the font object font = pygame.font.SysFont("comicsans", 40) lose = font.render("You lost!", 1, (255,255,255), (250,250)) lose_width = lose.get_width() # setting up screen WIN = pygame.display.set_mode((500,500)) pygame.display.set_caption("Snake by Sam") # load the background BG = pygame.image.load("snake.png") BG_resize = pygame.transform.scale(BG, (width, height)) WIN.blit(BG_resize,(0,0)) WIN.blit(font.render(f"Lives: {str(lives)}", 0 , (255,255,255)), (10,5)) # draw square on screen snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20)) key = None # draw fruit(circle), setup fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) def rand(): global x2, y2 x2 = random.randint(0, 490) y2 = random.randint(0, 490) def move(): global key global snake global x global y global fruit if answer == "yes" or "y" or "Yes": WIN.blit(font.render(f"Lives: {str(lives)}", 0 , (255,255,255)), (10,5)) snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20)) fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) if key == "d": x = snake.left time.sleep(0.02) x = x + 10 WIN.blit(BG_resize,(0,0)) WIN.blit(font.render(f"Lives: {str(lives)}", 0 , (255,255,255)), (10,5)) snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20)) fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) if key == "s": y = snake.top time.sleep(0.02) y = y + 10 WIN.blit(BG_resize,(0,0)) WIN.blit(font.render(f"Lives: {str(lives)}", 0 , (255,255,255)), (10,5)) snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20)) fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) if key == "w": y = snake.top time.sleep(0.02) y = y - 10 WIN.blit(BG_resize,(0,0)) WIN.blit(font.render(f"Lives: {str(lives)}", 0 , (255,255,255)), (10,5)) snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20)) fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) if key == "a": x = snake.left time.sleep(0.02) x = x - 10 WIN.blit(BG_resize,(0,0)) WIN.blit(font.render(f"Lives: {str(lives)}", 0 , (255,255,255)), (10,5)) snake = pygame.draw.rect(WIN, (255,0,0), (x,y,20,20)) fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) # mainloop while game: for event in pygame.event.get(): if event.type == pygame.QUIT: game = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_d: WIN.blit(BG_resize,(0,0)) key = "d" if event.key == pygame.K_w: WIN.blit(BG_resize,(0,0)) key = "w" if event.key == pygame.K_s: WIN.blit(BG_resize,(0,0)) key = "s" if event.key == pygame.K_a: WIN.blit(BG_resize,(0,0)) key = "a" move() # border control if snake.left > 490: WIN.blit(BG_resize,(0,0)) snake = pygame.draw.rect(WIN, (255,0,0), (250,250,20,20)) fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) x,y = 250, 250 key = None lives -= 1 if snake.left == 0: WIN.blit(BG_resize,(0,0)) snake = pygame.draw.rect(WIN, (255,0,0), (250,250,20,20)) fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) x,y = 250, 250 key = None lives -= 1 if snake.top > 490: WIN.blit(BG_resize,(0,0)) snake = pygame.draw.rect(WIN, (255,0,0), (250,250,20,20)) fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) x,y = 250, 250 key = None lives -= 1 if snake.top == 0: WIN.blit(BG_resize,(0,0)) snake = pygame.draw.rect(WIN, (255,0,0), (250,250,20,20)) fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) x,y = 250, 250 key = None lives -= 1 # check if the snake has made contact with the fruit. if snake.left < fruit.left + 20 and snake.left > fruit.left - 20 and snake.top < fruit.top + 25 and snake.top > fruit.top - 25: WIN.blit(BG_resize,(0,0)) WIN.blit(font.render(f"Lives: {str(lives)}", 0 , (255,255,255)), (10,5)) rand() fruit = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) # add a segment new_segment = pygame.draw.rect(WIN, (0,0,255), (x2,y2,20,20)) segments.append(new_segment) # move the following segment to the next position for index in range(len(segments) -1, 0, -1): pass if lives == 0: WIN.blit(BG_resize,(0,0)) WIN.blit(font.render("You lost!", 0, (255,255,255)), (250 - (lose_width/2),250)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYDOWN: WIN.blit(BG_resize,(0,0)) lives = 3 rand() pygame.display.update() RE: how to add segments to the snake body - angelabarrios - Sep-13-2023 Hello, To add segments to the snake's body after it has eaten the fruit, you can modify the following parts of your code: 1. Create a new segment object for each segment you want to add to the snake's body. You can use the pygame.Rect class to represent each segment. In your code, you can add the following line after generating a new fruit position: pizza tower python new_segment = pygame.Rect(x, y, 20, 20) segments.append(new_segment)2. This creates a new segment with the same position as the fruit and adds it to the segments list. Move the existing segments of the snake's body towards the head. This ensures that each segment follows the previous segment. You can do this by iterating over the segments list in reverse order and updating the position of each segment to match the position of the segment in front of it. Add the following code after adding the new segment: python for index in range(len(segments) - 1, 0, -1): segments[index].x = segments[index - 1].x segments[index].y = segments[index - 1].yThis loop starts from the last segment and moves each segment towards the position of the segment in front of it. 3. Finally, update the position of the first segment (the one closest to the snake's head) to match the current position of the snake's head. This ensures that the snake's body moves along with the head. Add the following code after the previous loop: python segments[0].x = x segments[0].y = yThis updates the position of the first segment to match the current position of the snake's head. |