-1

Hi guys I started learning pygames, i started playing snake and came to an obstacle. I don't know how to make a function for my snake to grow when it eats an apple, I've looked at a lot of snake codes and I'm still not sure how to do it. I dont have idea how to do that, I realy hope you can give mi some advice to improve my game.

your help would do me good, thanks


import pygame
import random
import math
# Init
pygame.init()
# Screen
screen = pygame.display.set_mode((800, 600))
# Caption and Icon
pygame.display.set_caption("Snake Game")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
# Background
background = pygame.image.load('background.jpg')
# Snake
snakeImg = pygame.image.load('snake.png')
snakeX = 300
snakeY = 300
snakeX_change = 0
snakeY_change = 0
# Apple
appleImg = pygame.image.load('apple.png')
appleX = random.randint(32, 768)
appleY = random.randint(32, 568)
def snake(x, y):
    screen.blit(snakeImg, (x, y))
def apple(x, y):
    screen.blit(appleImg, (x, y))
# Collision
def isCollision(appleX, appleY, snaketX, snakeY):
    distance = distance = math.sqrt(math.pow(appleX - snakeX, 2) + (math.pow(appleY - snakeY, 2)))
    if distance < 27:
        return True
    else:
        return False
# Game Loop
score = 0
running = True
while running:
    # Background Image
    screen.blit(background, (0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
        # Snake Movment
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                snakeX_change = -0.1
                snakeY_change = 0
            if event.key == pygame.K_UP:
                snakeY_change = -0.1
                snakeX_change = 0
            if event.key == pygame.K_RIGHT:
                snakeX_change = 0.1
                snakeY_change = 0
            if event.key == pygame.K_DOWN:
                snakeY_change = 0.1
                snakeX_change = 0
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                snakeX_change = -0.1
            if event.key == pygame.K_RIGHT:
                snakeX_change = 0.1
            if event.key == pygame.K_DOWN:
                snakeY_change = 0.1
            if event.key == pygame.K_UP:
                snakeY_change = -0.1
    snakeX += snakeX_change
    snakeY += snakeY_change
    if snakeX <= 0:
        snakeX = 0
    elif snakeX >= 770:
        snakeX = 770
    if snakeY <= 0:
        snakeY = 0
    elif snakeY >= 570:
        snakeY = 570
    # Collision
    collision = isCollision(appleX, appleY, snakeX, snakeY)
    if collision:
        score += 1
        print(score)
        appleX = random.randint(32, 768)
        appleY = random.randint(32, 568)
    snake(snakeX, snakeY)
    apple(appleX, appleY)
    pygame.display.flip()

dariober
  • 8,240
  • 3
  • 30
  • 47
  • I suggest looking at examples of snake game implementations and see how they do it, and then try implementing it in your own way, and coming back here only if you get stuck. You haven't indicated that you've done any research or attempted to do this at all, so if you have actually done so then you should make that clear. – Random Davis Apr 09 '21 at 15:19
  • 2
    Its great that you are learning python and tackling a challenge like this. However I think you may need to get a little further with tutorials, like how to use functions or classes and methods, before trying to get help from stackoverflow--which is more about people that have learned the basics already having specific questions that go beyond basics. – Andrew Allaire Apr 09 '21 at 15:20

1 Answers1

0

When snake's head collide with the apple, add 1 element inside snake's body, you can interprete snake's body as a list of elements, containing tuples perhaps.