enrique2334
enrique2334

Reputation: 1059

How do i get "score" to show up in the game window?

So far in my snake game the score is printed in python shell. How can i have it printed in the games window instead? So that the game and the score are in the same window? Thanks Eric

while running:


    screen.fill((0, 0, 0))
    worm.move()
    worm.draw()
    food.draw()

    if worm.crashed:
        exit();
    elif worm.x <= 0 or worm.x >= w -1:
        running = False
    elif worm.y <= 0 or worm.y >= h-1:
        running = False
    elif worm.position() == food.position():
        score += 1
        worm.eat()
        print " score: %d" % score
        food = Food(screen)
    elif food.check(worm.x, worm.y):
        score += 1
        worm.eat()
        print "score: %d" % score
        food = Food(screen)
    elif running == False:
        exit();

    for event in pygame.event.get():
        if event.type == pygame.quit:
            running = False
        elif event.type == pygame.KEYDOWN:
            worm.event(event)

    pygame.display.flip()
    clock.tick(100)

Edit-

while running:


screen.fill((0, 0, 0))
worm.move()
worm.draw()
food.draw()
pygame.font.init()
pygame.font.get_fonts() 

if worm.crashed:
    exit();
elif worm.x <= 0 or worm.x >= w -1:
    running = False
elif worm.y <= 0 or worm.y >= h-1:
    running = False

elif food.check(worm.x, worm.y):
    score += 1
    worm.eat()
    food = Food(screen)
    message = 'score: %d' % score
    font = pygame.font.Font(None, 40)
    text = font.render(message, 1, white)
    screen.blit(text, (50, 50))

elif running == False:
    exit();

for event in pygame.event.get():
    if event.type == pygame.quit:
        running = False
    elif event.type == pygame.KEYDOWN:
        worm.event(event)

Why isnt the Score Apearing, im not getting any errors?

Upvotes: 1

Views: 3875

Answers (2)

beleester
beleester

Reputation: 454

In response to your edit: You're only blitting the text to the screen when you take that particular elif branch (i.e, only when the worm gets the food). On the next frame, you clear the screen, but the food isn't there, so the score doesn't get drawn. You should blit the score to the screen every frame, instead.

Upvotes: 1

Ikke
Ikke

Reputation: 101221

Use the font module to render fonts on screen.

From the userguide:

white = (255, 255, 255)

message = 'your message'
font = pygame.font.Font(None, 40)
text = font.render(message, 1, white)
screen.blit(text, (x_position,y_position))

Upvotes: 1

Related Questions