Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 26th, 2012, 01:13 PM
Lucantrop Lucantrop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 21 Lucantrop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 33 m 14 sec
Reputation 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.

Reply With Quote
  #2  
Old December 26th, 2012, 08:53 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
How do you choose an operator for blit? You need overwrite instead of and. Maybe.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old January 5th, 2013, 07:11 AM
Lucantrop Lucantrop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 21 Lucantrop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 33 m 14 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Pygame text problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap