The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Pygame text problem
Discuss Pygame text problem in the Python Programming forum on Dev Shed. Pygame text problem Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 26th, 2012, 01:13 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 21
Time spent in forums: 17 h 33 m 14 sec
Reputation Power: 0
|
|
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.
|

December 26th, 2012, 08:53 PM
|
 |
Contributing User
|
|
|
|
|
How do you choose an operator for blit? You need overwrite instead of and. Maybe.
__________________
[code] Code tags[/code] are essential for python code!
|

January 5th, 2013, 07:11 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 21
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 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|