#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep Power
    0

    Question Pygame text problem


    Hello there, I am working on an Asteroids type game currently in pygame, and am having a bit of trouble with text.
    I am not really sure if you wanna read the whole code, so here is just the code that is related to the problem:
    Code:
    import pygame
    import random
    from math import *
    from pygame.locals import *
    
    win_flags = 0
    screen_rect = Rect(0, 0, 800, 600)
    n_stars = 200
    n_asteroids = 10
    
    # rgb colours
    black = (0, 0, 0)
    white = (255, 255, 255)
    yellow = (255, 255, 0)
    transparent = (1, 2, 3)
    
    ...
    
    def main():
        # initialize pygame
        pygame.init()
    
        #initialize game font
        game_font = pygame.font.Font(None, 30)
    
        # create a new window
        screen = pygame.display.set_mode(screen_rect.size, win_flags)
        pygame.display.set_caption('Asteroids by Luca') # title of the game
        # create star map on the background
        background = pygame.Surface(screen_rect.size)
        background.fill(black)
        for i in range(0, n_stars):
            x = random.randint(0, screen_rect.width-1)
            y = random.randint(0, screen_rect.height-1)
            pygame.draw.circle(background, white, (x, y), 0)
    
    
        # copy the background onto the screen
        screen.blit(background, (0, 0))
        pygame.display.update()
    
        # initialize starting sprites
        player = Player()
    
        # initialize sprite groups
        render = pygame.sprite.Group()
        render.add(player)
    
        clock = pygame.time.Clock()
    
        # create asteroids
        asteroids = pygame.sprite.Group()
        for i in range(0, n_asteroids):
            # new asteroids are stored in the asteroids group
            asteroid = Asteroid()
            render.add(asteroid)
            asteroids.add(asteroid)
    
        lives = 3
        # lives text
        lives_text = game_font.render('%s' % lives, 2, white)
        lives_rect = lives_text.get_rect(center= (30, 30))
        screen.blit(lives_text, lives_rect)
    
        while True:
            ...
    
            # detect collisions
            for asteroid in pygame.sprite.spritecollide(player, asteroids, False):
                asteroid.kill()
                lives -= 1
                lives_text = game_font.render('%s' % lives, 2, white)
                lives_rect = lives_text.get_rect(center= (30, 30))
                screen.blit(lives_text, lives_rect)
                if lives == 0:
                    player.kill()
                    for asteroid in asteroids:
                            asteroid.kill()
                    text = game_font.render('GAME OVER', False, white)
                    text_rect = text.get_rect(center=screen_rect.center)
                    screen.blit(text, text_rect)
    
    
            clock.tick(30)
            # update the sprites
            render.update()
            # draw the scene
            render.clear(screen, background)
            render.draw(screen)
            pygame.display.update()
    
    # run main()
    if __name__ == "__main__": main()
    Now the problem that I am having has to do with showing up lives text on the screen - it shows up at the start of the game, but when I lose a life, it overwrites itself rather than rewriting.
    I have seen several tutorials about it, both Youtube videos and some other stuff on the Internet, but no one seems to be having that same problem.
    If all else fails, I will try and redraw the whole thing when the collision happens, but for now, if anyone can help me, that would be great.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    How do you choose an operator for blit? You need overwrite instead of and. Maybe.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    25
    Rep Power
    0
    Sorry, I just saw this post, but, I didn't quite understand you... However, I decided to redraw the screen since I had to use it somewhere else too, so that's solved. Thanks anyhow

IMN logo majestic logo threadwatch logo seochat tools logo