Snake game: snake and apples do not appear on the screen












1















I'm trying to make the snake game in python, but sadly my snake and my apples aren't appearing on the screen, all i see is a gray screen with nothing on it, can you help me out? thanks!
P.S - i would like an explanation as well about why my current code isn't working so i would avoid it in the future, thanks again.



import pygame, sys, random, time
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 500
windowHeight = 500
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
red = (255, 0, 0)
green = (0, 255, 0)
color = (100, 100, 100)
snakeHead = [250, 250]
snakePosition = [[250, 250],[240, 250],[230, 250]]
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]

def collisionBoundarias(snakeHead):
if snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0:
return 1
else:
return 0

def collisionSelf(SnakePosition):
snakeHead = snakePosition[0]
if snakeHead in snakePosition[1:]:
return 1
else:
return 0

while True:
windowSurface.fill(color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
snakeHead[0] += 10
if event.type == pygame.K_LEFT or event.key == pygame.K_a:
snakeHead[0] -= 10
if event.type == pygame.K_UP or event.type == pygame.K_w:
snakeHead[1] += 10
if event.type == pygame.K_DOWN or event.type == pygame.K_s:
snakeHead[1] -= 10

def displaySnake(snakePosition):
for position in snakePosition:
pygame.draw.rect(windowSurface ,red,pygame.Rect(position[0],position[1],10,10))

def display_apple(windowSurface, applePosition, apple):
windowSurface.blit(apple ,(applePosition[0], applePosition[1]))

snakePosition.insert(0,list(snakeHead))
snakePosition.pop()


def displayFinalScore(displayText, finalScore):
largeText = pygame.font.Font('freesansbold.ttf',35)
TextSurf = largeText.render(displayText, True, (255, 255, 255))
TextRect = TextSurf.get_rect()
TextRect.center = ((windowWidth/2),(windowHeight/2))
windowSurface.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)

def collisionApple(applePosition, score):
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
score += 1
return applePosition, score

if snakeHead == applePosition:
applePosition, score = collisionApple(applePosition, score)
snakePosition.insert(0,list(snakeHead))


pygame.display.update()
clock = pygame.time.Clock()
clock.tick(20)









share|improve this question























  • That might be because you're never actually drawing your snake? displaySnake is never called upon, in fact, most of your functions and stuff is never used. And you'ved defined them in a while loop. You never said it, but it's obvious that this is one of your first ever projects. And I don't discourage jumping right into game design, game making etc. But you better make sure you have a damn good teacher who can intuitively describe everything to you. Because there's quite a few mistakes here. If you want to fiddle around with a snake game (part 2 inc because this is getting to long)

    – Torxed
    Nov 21 '18 at 21:32











  • I strongly suggest you take a look at the pygame examples where they have a snake game. They've solved it quite differently. You're not far from having a working code yourself. But there's many things that needs to be fixed, essentially your entire code will look different once you have something that will even render stuff. Then you have the performance issues and all the "don't do this" in your code. (for instance, recreating functions, every loop).

    – Torxed
    Nov 21 '18 at 21:33











  • and please make sure that you are indenting code properly.

    – Ken Dekalb
    Nov 21 '18 at 21:34











  • @Torxed TBH the snake example you linked is far from good and does some questionable things...

    – sloth
    Nov 22 '18 at 7:31











  • Since this is most likely the first time you write this kind of code, my suggestion is to start lower. Start by making one single function that draws one single thing. Maybe the apple. Once you get that running correctly, build upon it. Don't rush it and try to write all the code at once.

    – ChatterOne
    Nov 22 '18 at 8:27
















1















I'm trying to make the snake game in python, but sadly my snake and my apples aren't appearing on the screen, all i see is a gray screen with nothing on it, can you help me out? thanks!
P.S - i would like an explanation as well about why my current code isn't working so i would avoid it in the future, thanks again.



import pygame, sys, random, time
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 500
windowHeight = 500
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
red = (255, 0, 0)
green = (0, 255, 0)
color = (100, 100, 100)
snakeHead = [250, 250]
snakePosition = [[250, 250],[240, 250],[230, 250]]
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]

def collisionBoundarias(snakeHead):
if snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0:
return 1
else:
return 0

def collisionSelf(SnakePosition):
snakeHead = snakePosition[0]
if snakeHead in snakePosition[1:]:
return 1
else:
return 0

while True:
windowSurface.fill(color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
snakeHead[0] += 10
if event.type == pygame.K_LEFT or event.key == pygame.K_a:
snakeHead[0] -= 10
if event.type == pygame.K_UP or event.type == pygame.K_w:
snakeHead[1] += 10
if event.type == pygame.K_DOWN or event.type == pygame.K_s:
snakeHead[1] -= 10

def displaySnake(snakePosition):
for position in snakePosition:
pygame.draw.rect(windowSurface ,red,pygame.Rect(position[0],position[1],10,10))

def display_apple(windowSurface, applePosition, apple):
windowSurface.blit(apple ,(applePosition[0], applePosition[1]))

snakePosition.insert(0,list(snakeHead))
snakePosition.pop()


def displayFinalScore(displayText, finalScore):
largeText = pygame.font.Font('freesansbold.ttf',35)
TextSurf = largeText.render(displayText, True, (255, 255, 255))
TextRect = TextSurf.get_rect()
TextRect.center = ((windowWidth/2),(windowHeight/2))
windowSurface.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)

def collisionApple(applePosition, score):
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
score += 1
return applePosition, score

if snakeHead == applePosition:
applePosition, score = collisionApple(applePosition, score)
snakePosition.insert(0,list(snakeHead))


pygame.display.update()
clock = pygame.time.Clock()
clock.tick(20)









share|improve this question























  • That might be because you're never actually drawing your snake? displaySnake is never called upon, in fact, most of your functions and stuff is never used. And you'ved defined them in a while loop. You never said it, but it's obvious that this is one of your first ever projects. And I don't discourage jumping right into game design, game making etc. But you better make sure you have a damn good teacher who can intuitively describe everything to you. Because there's quite a few mistakes here. If you want to fiddle around with a snake game (part 2 inc because this is getting to long)

    – Torxed
    Nov 21 '18 at 21:32











  • I strongly suggest you take a look at the pygame examples where they have a snake game. They've solved it quite differently. You're not far from having a working code yourself. But there's many things that needs to be fixed, essentially your entire code will look different once you have something that will even render stuff. Then you have the performance issues and all the "don't do this" in your code. (for instance, recreating functions, every loop).

    – Torxed
    Nov 21 '18 at 21:33











  • and please make sure that you are indenting code properly.

    – Ken Dekalb
    Nov 21 '18 at 21:34











  • @Torxed TBH the snake example you linked is far from good and does some questionable things...

    – sloth
    Nov 22 '18 at 7:31











  • Since this is most likely the first time you write this kind of code, my suggestion is to start lower. Start by making one single function that draws one single thing. Maybe the apple. Once you get that running correctly, build upon it. Don't rush it and try to write all the code at once.

    – ChatterOne
    Nov 22 '18 at 8:27














1












1








1








I'm trying to make the snake game in python, but sadly my snake and my apples aren't appearing on the screen, all i see is a gray screen with nothing on it, can you help me out? thanks!
P.S - i would like an explanation as well about why my current code isn't working so i would avoid it in the future, thanks again.



import pygame, sys, random, time
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 500
windowHeight = 500
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
red = (255, 0, 0)
green = (0, 255, 0)
color = (100, 100, 100)
snakeHead = [250, 250]
snakePosition = [[250, 250],[240, 250],[230, 250]]
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]

def collisionBoundarias(snakeHead):
if snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0:
return 1
else:
return 0

def collisionSelf(SnakePosition):
snakeHead = snakePosition[0]
if snakeHead in snakePosition[1:]:
return 1
else:
return 0

while True:
windowSurface.fill(color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
snakeHead[0] += 10
if event.type == pygame.K_LEFT or event.key == pygame.K_a:
snakeHead[0] -= 10
if event.type == pygame.K_UP or event.type == pygame.K_w:
snakeHead[1] += 10
if event.type == pygame.K_DOWN or event.type == pygame.K_s:
snakeHead[1] -= 10

def displaySnake(snakePosition):
for position in snakePosition:
pygame.draw.rect(windowSurface ,red,pygame.Rect(position[0],position[1],10,10))

def display_apple(windowSurface, applePosition, apple):
windowSurface.blit(apple ,(applePosition[0], applePosition[1]))

snakePosition.insert(0,list(snakeHead))
snakePosition.pop()


def displayFinalScore(displayText, finalScore):
largeText = pygame.font.Font('freesansbold.ttf',35)
TextSurf = largeText.render(displayText, True, (255, 255, 255))
TextRect = TextSurf.get_rect()
TextRect.center = ((windowWidth/2),(windowHeight/2))
windowSurface.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)

def collisionApple(applePosition, score):
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
score += 1
return applePosition, score

if snakeHead == applePosition:
applePosition, score = collisionApple(applePosition, score)
snakePosition.insert(0,list(snakeHead))


pygame.display.update()
clock = pygame.time.Clock()
clock.tick(20)









share|improve this question














I'm trying to make the snake game in python, but sadly my snake and my apples aren't appearing on the screen, all i see is a gray screen with nothing on it, can you help me out? thanks!
P.S - i would like an explanation as well about why my current code isn't working so i would avoid it in the future, thanks again.



import pygame, sys, random, time
from pygame.locals import *

pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 500
windowHeight = 500
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
red = (255, 0, 0)
green = (0, 255, 0)
color = (100, 100, 100)
snakeHead = [250, 250]
snakePosition = [[250, 250],[240, 250],[230, 250]]
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]

def collisionBoundarias(snakeHead):
if snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0:
return 1
else:
return 0

def collisionSelf(SnakePosition):
snakeHead = snakePosition[0]
if snakeHead in snakePosition[1:]:
return 1
else:
return 0

while True:
windowSurface.fill(color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_RIGHT or event.key == pygame.K_d:
snakeHead[0] += 10
if event.type == pygame.K_LEFT or event.key == pygame.K_a:
snakeHead[0] -= 10
if event.type == pygame.K_UP or event.type == pygame.K_w:
snakeHead[1] += 10
if event.type == pygame.K_DOWN or event.type == pygame.K_s:
snakeHead[1] -= 10

def displaySnake(snakePosition):
for position in snakePosition:
pygame.draw.rect(windowSurface ,red,pygame.Rect(position[0],position[1],10,10))

def display_apple(windowSurface, applePosition, apple):
windowSurface.blit(apple ,(applePosition[0], applePosition[1]))

snakePosition.insert(0,list(snakeHead))
snakePosition.pop()


def displayFinalScore(displayText, finalScore):
largeText = pygame.font.Font('freesansbold.ttf',35)
TextSurf = largeText.render(displayText, True, (255, 255, 255))
TextRect = TextSurf.get_rect()
TextRect.center = ((windowWidth/2),(windowHeight/2))
windowSurface.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)

def collisionApple(applePosition, score):
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
score += 1
return applePosition, score

if snakeHead == applePosition:
applePosition, score = collisionApple(applePosition, score)
snakePosition.insert(0,list(snakeHead))


pygame.display.update()
clock = pygame.time.Clock()
clock.tick(20)






python pygame






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 21:27









xTheMoonLigherx 007xTheMoonLigherx 007

224




224













  • That might be because you're never actually drawing your snake? displaySnake is never called upon, in fact, most of your functions and stuff is never used. And you'ved defined them in a while loop. You never said it, but it's obvious that this is one of your first ever projects. And I don't discourage jumping right into game design, game making etc. But you better make sure you have a damn good teacher who can intuitively describe everything to you. Because there's quite a few mistakes here. If you want to fiddle around with a snake game (part 2 inc because this is getting to long)

    – Torxed
    Nov 21 '18 at 21:32











  • I strongly suggest you take a look at the pygame examples where they have a snake game. They've solved it quite differently. You're not far from having a working code yourself. But there's many things that needs to be fixed, essentially your entire code will look different once you have something that will even render stuff. Then you have the performance issues and all the "don't do this" in your code. (for instance, recreating functions, every loop).

    – Torxed
    Nov 21 '18 at 21:33











  • and please make sure that you are indenting code properly.

    – Ken Dekalb
    Nov 21 '18 at 21:34











  • @Torxed TBH the snake example you linked is far from good and does some questionable things...

    – sloth
    Nov 22 '18 at 7:31











  • Since this is most likely the first time you write this kind of code, my suggestion is to start lower. Start by making one single function that draws one single thing. Maybe the apple. Once you get that running correctly, build upon it. Don't rush it and try to write all the code at once.

    – ChatterOne
    Nov 22 '18 at 8:27



















  • That might be because you're never actually drawing your snake? displaySnake is never called upon, in fact, most of your functions and stuff is never used. And you'ved defined them in a while loop. You never said it, but it's obvious that this is one of your first ever projects. And I don't discourage jumping right into game design, game making etc. But you better make sure you have a damn good teacher who can intuitively describe everything to you. Because there's quite a few mistakes here. If you want to fiddle around with a snake game (part 2 inc because this is getting to long)

    – Torxed
    Nov 21 '18 at 21:32











  • I strongly suggest you take a look at the pygame examples where they have a snake game. They've solved it quite differently. You're not far from having a working code yourself. But there's many things that needs to be fixed, essentially your entire code will look different once you have something that will even render stuff. Then you have the performance issues and all the "don't do this" in your code. (for instance, recreating functions, every loop).

    – Torxed
    Nov 21 '18 at 21:33











  • and please make sure that you are indenting code properly.

    – Ken Dekalb
    Nov 21 '18 at 21:34











  • @Torxed TBH the snake example you linked is far from good and does some questionable things...

    – sloth
    Nov 22 '18 at 7:31











  • Since this is most likely the first time you write this kind of code, my suggestion is to start lower. Start by making one single function that draws one single thing. Maybe the apple. Once you get that running correctly, build upon it. Don't rush it and try to write all the code at once.

    – ChatterOne
    Nov 22 '18 at 8:27

















That might be because you're never actually drawing your snake? displaySnake is never called upon, in fact, most of your functions and stuff is never used. And you'ved defined them in a while loop. You never said it, but it's obvious that this is one of your first ever projects. And I don't discourage jumping right into game design, game making etc. But you better make sure you have a damn good teacher who can intuitively describe everything to you. Because there's quite a few mistakes here. If you want to fiddle around with a snake game (part 2 inc because this is getting to long)

– Torxed
Nov 21 '18 at 21:32





That might be because you're never actually drawing your snake? displaySnake is never called upon, in fact, most of your functions and stuff is never used. And you'ved defined them in a while loop. You never said it, but it's obvious that this is one of your first ever projects. And I don't discourage jumping right into game design, game making etc. But you better make sure you have a damn good teacher who can intuitively describe everything to you. Because there's quite a few mistakes here. If you want to fiddle around with a snake game (part 2 inc because this is getting to long)

– Torxed
Nov 21 '18 at 21:32













I strongly suggest you take a look at the pygame examples where they have a snake game. They've solved it quite differently. You're not far from having a working code yourself. But there's many things that needs to be fixed, essentially your entire code will look different once you have something that will even render stuff. Then you have the performance issues and all the "don't do this" in your code. (for instance, recreating functions, every loop).

– Torxed
Nov 21 '18 at 21:33





I strongly suggest you take a look at the pygame examples where they have a snake game. They've solved it quite differently. You're not far from having a working code yourself. But there's many things that needs to be fixed, essentially your entire code will look different once you have something that will even render stuff. Then you have the performance issues and all the "don't do this" in your code. (for instance, recreating functions, every loop).

– Torxed
Nov 21 '18 at 21:33













and please make sure that you are indenting code properly.

– Ken Dekalb
Nov 21 '18 at 21:34





and please make sure that you are indenting code properly.

– Ken Dekalb
Nov 21 '18 at 21:34













@Torxed TBH the snake example you linked is far from good and does some questionable things...

– sloth
Nov 22 '18 at 7:31





@Torxed TBH the snake example you linked is far from good and does some questionable things...

– sloth
Nov 22 '18 at 7:31













Since this is most likely the first time you write this kind of code, my suggestion is to start lower. Start by making one single function that draws one single thing. Maybe the apple. Once you get that running correctly, build upon it. Don't rush it and try to write all the code at once.

– ChatterOne
Nov 22 '18 at 8:27





Since this is most likely the first time you write this kind of code, my suggestion is to start lower. Start by making one single function that draws one single thing. Maybe the apple. Once you get that running correctly, build upon it. Don't rush it and try to write all the code at once.

– ChatterOne
Nov 22 '18 at 8:27












1 Answer
1






active

oldest

votes


















0














As mentioned in the comments, the objects don't appear because you never draw them by calling the displaySnake and display_apple functions. It also makes no sense to define these functions in the while loop again and again.



Here's a fixed version of the code. I've changed the movement code, so that the snake moves continually each frame. (It could still be improved, but I tried to keep it simple.)



import pygame, sys, random, time
from pygame.locals import *


pygame.init()
mainClock = pygame.time.Clock()
windowWidth = 500
windowHeight = 500
windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
clock = pygame.time.Clock()
red = (255, 0, 0)
green = (0, 255, 0)
color = (100, 100, 100)
snakeHead = [250, 250]
snakePosition = [[250, 250],[240, 250],[230, 250]]
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
apple = pygame.Surface((10, 10))
apple.fill(green)
score = 0
speed = 10
# Define a velocity variable (a list or better a pygame.Vector2).
velocity = pygame.Vector2(speed, 0)


def collisionBoundarias(snakeHead):
return snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0


def collisionApple(applePosition, score):
applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
score += 1
return applePosition, score


def displaySnake(snakePosition):
for position in snakePosition:
pygame.draw.rect(windowSurface, red, pygame.Rect(position[0], position[1], 10, 10))


def display_apple(windowSurface, applePosition, apple):
windowSurface.blit(apple, (applePosition[0], applePosition[1]))


while True:
# Event handling.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
# Replace `type` with `key`.
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
velocity.x = speed
velocity.y = 0
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
velocity.x = -speed
velocity.y = 0
if event.key == pygame.K_UP or event.key == pygame.K_w:
velocity.x = 0
velocity.y = -speed
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
velocity.x = 0
velocity.y = speed

# Game logic.
snakeHead[0] += velocity.x # Update the x-position every frame.
snakeHead[1] += velocity.y # Update the y-position every frame.
snakePosition.insert(0, snakeHead)
snakePosition.pop()

if snakeHead == applePosition:
applePosition, score = collisionApple(applePosition, score)
snakePosition.insert(0, snakeHead)

# Drawing.
windowSurface.fill(color)
displaySnake(snakePosition)
display_apple(windowSurface, applePosition, apple)
pygame.display.update()
clock.tick(20)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420689%2fsnake-game-snake-and-apples-do-not-appear-on-the-screen%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    As mentioned in the comments, the objects don't appear because you never draw them by calling the displaySnake and display_apple functions. It also makes no sense to define these functions in the while loop again and again.



    Here's a fixed version of the code. I've changed the movement code, so that the snake moves continually each frame. (It could still be improved, but I tried to keep it simple.)



    import pygame, sys, random, time
    from pygame.locals import *


    pygame.init()
    mainClock = pygame.time.Clock()
    windowWidth = 500
    windowHeight = 500
    windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
    clock = pygame.time.Clock()
    red = (255, 0, 0)
    green = (0, 255, 0)
    color = (100, 100, 100)
    snakeHead = [250, 250]
    snakePosition = [[250, 250],[240, 250],[230, 250]]
    applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
    apple = pygame.Surface((10, 10))
    apple.fill(green)
    score = 0
    speed = 10
    # Define a velocity variable (a list or better a pygame.Vector2).
    velocity = pygame.Vector2(speed, 0)


    def collisionBoundarias(snakeHead):
    return snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0


    def collisionApple(applePosition, score):
    applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
    score += 1
    return applePosition, score


    def displaySnake(snakePosition):
    for position in snakePosition:
    pygame.draw.rect(windowSurface, red, pygame.Rect(position[0], position[1], 10, 10))


    def display_apple(windowSurface, applePosition, apple):
    windowSurface.blit(apple, (applePosition[0], applePosition[1]))


    while True:
    # Event handling.
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
    sys.exit()
    if event.type == pygame.KEYDOWN:
    # Replace `type` with `key`.
    if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
    velocity.x = speed
    velocity.y = 0
    if event.key == pygame.K_LEFT or event.key == pygame.K_a:
    velocity.x = -speed
    velocity.y = 0
    if event.key == pygame.K_UP or event.key == pygame.K_w:
    velocity.x = 0
    velocity.y = -speed
    if event.key == pygame.K_DOWN or event.key == pygame.K_s:
    velocity.x = 0
    velocity.y = speed

    # Game logic.
    snakeHead[0] += velocity.x # Update the x-position every frame.
    snakeHead[1] += velocity.y # Update the y-position every frame.
    snakePosition.insert(0, snakeHead)
    snakePosition.pop()

    if snakeHead == applePosition:
    applePosition, score = collisionApple(applePosition, score)
    snakePosition.insert(0, snakeHead)

    # Drawing.
    windowSurface.fill(color)
    displaySnake(snakePosition)
    display_apple(windowSurface, applePosition, apple)
    pygame.display.update()
    clock.tick(20)





    share|improve this answer




























      0














      As mentioned in the comments, the objects don't appear because you never draw them by calling the displaySnake and display_apple functions. It also makes no sense to define these functions in the while loop again and again.



      Here's a fixed version of the code. I've changed the movement code, so that the snake moves continually each frame. (It could still be improved, but I tried to keep it simple.)



      import pygame, sys, random, time
      from pygame.locals import *


      pygame.init()
      mainClock = pygame.time.Clock()
      windowWidth = 500
      windowHeight = 500
      windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
      clock = pygame.time.Clock()
      red = (255, 0, 0)
      green = (0, 255, 0)
      color = (100, 100, 100)
      snakeHead = [250, 250]
      snakePosition = [[250, 250],[240, 250],[230, 250]]
      applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
      apple = pygame.Surface((10, 10))
      apple.fill(green)
      score = 0
      speed = 10
      # Define a velocity variable (a list or better a pygame.Vector2).
      velocity = pygame.Vector2(speed, 0)


      def collisionBoundarias(snakeHead):
      return snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0


      def collisionApple(applePosition, score):
      applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
      score += 1
      return applePosition, score


      def displaySnake(snakePosition):
      for position in snakePosition:
      pygame.draw.rect(windowSurface, red, pygame.Rect(position[0], position[1], 10, 10))


      def display_apple(windowSurface, applePosition, apple):
      windowSurface.blit(apple, (applePosition[0], applePosition[1]))


      while True:
      # Event handling.
      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      pygame.quit()
      sys.exit()
      if event.type == pygame.KEYDOWN:
      # Replace `type` with `key`.
      if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
      velocity.x = speed
      velocity.y = 0
      if event.key == pygame.K_LEFT or event.key == pygame.K_a:
      velocity.x = -speed
      velocity.y = 0
      if event.key == pygame.K_UP or event.key == pygame.K_w:
      velocity.x = 0
      velocity.y = -speed
      if event.key == pygame.K_DOWN or event.key == pygame.K_s:
      velocity.x = 0
      velocity.y = speed

      # Game logic.
      snakeHead[0] += velocity.x # Update the x-position every frame.
      snakeHead[1] += velocity.y # Update the y-position every frame.
      snakePosition.insert(0, snakeHead)
      snakePosition.pop()

      if snakeHead == applePosition:
      applePosition, score = collisionApple(applePosition, score)
      snakePosition.insert(0, snakeHead)

      # Drawing.
      windowSurface.fill(color)
      displaySnake(snakePosition)
      display_apple(windowSurface, applePosition, apple)
      pygame.display.update()
      clock.tick(20)





      share|improve this answer


























        0












        0








        0







        As mentioned in the comments, the objects don't appear because you never draw them by calling the displaySnake and display_apple functions. It also makes no sense to define these functions in the while loop again and again.



        Here's a fixed version of the code. I've changed the movement code, so that the snake moves continually each frame. (It could still be improved, but I tried to keep it simple.)



        import pygame, sys, random, time
        from pygame.locals import *


        pygame.init()
        mainClock = pygame.time.Clock()
        windowWidth = 500
        windowHeight = 500
        windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
        clock = pygame.time.Clock()
        red = (255, 0, 0)
        green = (0, 255, 0)
        color = (100, 100, 100)
        snakeHead = [250, 250]
        snakePosition = [[250, 250],[240, 250],[230, 250]]
        applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
        apple = pygame.Surface((10, 10))
        apple.fill(green)
        score = 0
        speed = 10
        # Define a velocity variable (a list or better a pygame.Vector2).
        velocity = pygame.Vector2(speed, 0)


        def collisionBoundarias(snakeHead):
        return snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0


        def collisionApple(applePosition, score):
        applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
        score += 1
        return applePosition, score


        def displaySnake(snakePosition):
        for position in snakePosition:
        pygame.draw.rect(windowSurface, red, pygame.Rect(position[0], position[1], 10, 10))


        def display_apple(windowSurface, applePosition, apple):
        windowSurface.blit(apple, (applePosition[0], applePosition[1]))


        while True:
        # Event handling.
        for event in pygame.event.get():
        if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
        if event.type == pygame.KEYDOWN:
        # Replace `type` with `key`.
        if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
        velocity.x = speed
        velocity.y = 0
        if event.key == pygame.K_LEFT or event.key == pygame.K_a:
        velocity.x = -speed
        velocity.y = 0
        if event.key == pygame.K_UP or event.key == pygame.K_w:
        velocity.x = 0
        velocity.y = -speed
        if event.key == pygame.K_DOWN or event.key == pygame.K_s:
        velocity.x = 0
        velocity.y = speed

        # Game logic.
        snakeHead[0] += velocity.x # Update the x-position every frame.
        snakeHead[1] += velocity.y # Update the y-position every frame.
        snakePosition.insert(0, snakeHead)
        snakePosition.pop()

        if snakeHead == applePosition:
        applePosition, score = collisionApple(applePosition, score)
        snakePosition.insert(0, snakeHead)

        # Drawing.
        windowSurface.fill(color)
        displaySnake(snakePosition)
        display_apple(windowSurface, applePosition, apple)
        pygame.display.update()
        clock.tick(20)





        share|improve this answer













        As mentioned in the comments, the objects don't appear because you never draw them by calling the displaySnake and display_apple functions. It also makes no sense to define these functions in the while loop again and again.



        Here's a fixed version of the code. I've changed the movement code, so that the snake moves continually each frame. (It could still be improved, but I tried to keep it simple.)



        import pygame, sys, random, time
        from pygame.locals import *


        pygame.init()
        mainClock = pygame.time.Clock()
        windowWidth = 500
        windowHeight = 500
        windowSurface = pygame.display.set_mode((windowWidth, windowHeight), 0, 32)
        clock = pygame.time.Clock()
        red = (255, 0, 0)
        green = (0, 255, 0)
        color = (100, 100, 100)
        snakeHead = [250, 250]
        snakePosition = [[250, 250],[240, 250],[230, 250]]
        applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
        apple = pygame.Surface((10, 10))
        apple.fill(green)
        score = 0
        speed = 10
        # Define a velocity variable (a list or better a pygame.Vector2).
        velocity = pygame.Vector2(speed, 0)


        def collisionBoundarias(snakeHead):
        return snakeHead[0] >= 500 or snakeHead[0] < 0 or snakeHead[1] >= 500 or snakeHead[1] < 0


        def collisionApple(applePosition, score):
        applePosition = [random.randrange(1,50)*10,random.randrange(1,50)*10]
        score += 1
        return applePosition, score


        def displaySnake(snakePosition):
        for position in snakePosition:
        pygame.draw.rect(windowSurface, red, pygame.Rect(position[0], position[1], 10, 10))


        def display_apple(windowSurface, applePosition, apple):
        windowSurface.blit(apple, (applePosition[0], applePosition[1]))


        while True:
        # Event handling.
        for event in pygame.event.get():
        if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
        if event.type == pygame.KEYDOWN:
        # Replace `type` with `key`.
        if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
        velocity.x = speed
        velocity.y = 0
        if event.key == pygame.K_LEFT or event.key == pygame.K_a:
        velocity.x = -speed
        velocity.y = 0
        if event.key == pygame.K_UP or event.key == pygame.K_w:
        velocity.x = 0
        velocity.y = -speed
        if event.key == pygame.K_DOWN or event.key == pygame.K_s:
        velocity.x = 0
        velocity.y = speed

        # Game logic.
        snakeHead[0] += velocity.x # Update the x-position every frame.
        snakeHead[1] += velocity.y # Update the y-position every frame.
        snakePosition.insert(0, snakeHead)
        snakePosition.pop()

        if snakeHead == applePosition:
        applePosition, score = collisionApple(applePosition, score)
        snakePosition.insert(0, snakeHead)

        # Drawing.
        windowSurface.fill(color)
        displaySnake(snakePosition)
        display_apple(windowSurface, applePosition, apple)
        pygame.display.update()
        clock.tick(20)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 16:32









        skrxskrx

        15.6k31834




        15.6k31834
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420689%2fsnake-game-snake-and-apples-do-not-appear-on-the-screen%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]