Jul-27-2020, 04:05 PM
(This post was last modified: Jul-27-2020, 04:05 PM by blacklight.)
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.
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()