[pygame] Text not displaying.

Started by
2 comments, last by Captain P 13 years, 2 months ago
Hello I am trying to get the text to show up when the program is first started and then will be taken away from the new screen button but it isn't working.
One other thing if you could tell me how to make it save multiple images instead of just saving one it would be nice. Thanks for your time.

import pygame
from pygame.locals import *

pygame.init()
fs = False
firsrun = True

def main():
firstrun = True
fs = False
isloaded = False
size = (640,480)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Drawing Application 0.1a")

bg = pygame.Surface(screen.get_size())
bg = bg.convert()
bg.fill((255,255,255))

text = pygame.font.SysFont("Arial", 20)
label = text.render("Welcome. 1-9 will change line thickness.",1,(0,0,255))
label2 = text.render("Pressing N will wipe the screen clean.",1,(0,0,255))
label3 = text.render("F - Fullscreen S - Save - Load",1,(0,0,255))

image = ""

clock = pygame.time.Clock()
ok = True

while ok:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
ok = False
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
ok = False
if event.key == K_f:
if not fs:
screen = pygame.display.set_mode(size, FULLSCREEN)
fs = True
else:
screen = pygame.display.set_mode(size)
fs = False
if event.key == K_l:
image = pygame.image.load("DA1.bmp")
isloaded = True
if event.key == K_s:
pygame.image.save(screen,"DA1.bmp")
if event.key == K_n:
isloaded = False
bg.fill((255,255,255))
if event.type == MOUSEBUTTONDOWN:
linestart = pygame.mouse.get_pos()
if event.type == MOUSEBUTTONUP:
lineend = pygame.mouse.get_pos()
pygame.draw.line(bg, (0,0,0), linestart, lineend, 3)

screen.blit(bg,(0,0))
if firstrun:
bg.fill((255,255,255))
screen.blit(label,(50,100))
screen.blit(label2,(50,130))
screen.blit(label3,(60,160))
firstrun = False
if isloaded:
bg.fill((255,255,255))
screen.blit(image,(0,0))

pygame.display.flip()

if __name__ == "__main__":
main()
Advertisement
The code you've shown doesn't run due to indentation problems... with Python, always make sure that indentation is preserved correctly! :)

Anyway, you're only drawing your labels the first run, but you're clearing the screen every run (by blitting bg to the screen). Why not draw those labels every run instead?

As for saving multiple images, you could check all files in the current working directory by using os.walk(os.getcwd()). The os.path module also contains useful functions, be sure to have a look at them.
Create-ivity - a game development blog Mouseover for more information.
Well i got the little intro labels showing now, and I believe I have found a way to save multiple images but for some reason it isn't working. I am using this as my save
now different from the code in my original post.

import pygame
import datetime
from pygame.locals import *

pygame.init()
fs = False
firsrun = True

def main():
firstrun = True
fs = False
isloaded = False
time = datetime.datetime.now()
size = (640,480)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Drawing Application 0.1a")

bg = pygame.Surface(screen.get_size())
bg = bg.convert()
bg.fill((255,255,255))

text = pygame.font.SysFont("Arial", 30)
label = text.render("Welcome. 1-9 will change line thickness.",1,(0,0,255))
label2 = text.render("Pressing N will wipe the screen clean.",1,(0,0,255))
label3 = text.render("F - Fullscreen S - Save L - Load",1,(0,0,255))
texw = screen.get_width() / 6
image = ""

thick = 3
clock = pygame.time.Clock()
ok = True

while ok:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
ok = False
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
ok = False
if event.key == K_f:
if not fs:
screen = pygame.display.set_mode(size, FULLSCREEN)
fs = True
else:
screen = pygame.display.set_mode(size)
fs = False
if event.key == K_l:
image = pygame.image.load("DA1.bmp")
isloaded = True
if event.key == K_s:
pygame.image.save(screen,("DrawApp" + time.strftime("%Y-%m-%d_%H:%M") + ".bmp"))
if event.key == K_n:
isloaded = False
firstrun = False
bg.fill((255,255,255))
if event.key == K_1:
thick = 1
if event.key == K_2:
thick = 2
if event.key == K_3:
thick = 3
if event.key == K_4:
thick = 4
if event.key == K_5:
thick = 5
if event.key == K_6:
thick = 6
if event.key == K_7:
thick = 7
if event.key == K_8:
thick = 8
if event.key == K_9:
thick = 9
if event.type == MOUSEBUTTONDOWN:
linestart = pygame.mouse.get_pos()
if event.type == MOUSEBUTTONUP:
lineend = pygame.mouse.get_pos()
pygame.draw.line(bg, (0,0,0), linestart, lineend, thick)

screen.blit(bg,(0,0))
if firstrun:
bg.fill((255,255,255))
screen.blit(label,(texw,100))
screen.blit(label2,(texw,130))
screen.blit(label3,(texw,160))
if isloaded:
bg.fill((255,255,255))
screen.blit(image,(0,0))

pygame.display.flip()

if __name__ == "__main__":
main()


im getting pygame.error Couldn't open DrawApp2011-01-19_Current Time Here.bmp but I do not understand what is wrong.
Your code doesn't work again due to indentation problems, so we can't run it for ourselves. Also, when do you get that error? When you try to save an image? When you try to load it? "Current Time Here" doesn't appear anywhere in your code, so is this the actual code you were using when you got that error, or did you accidentally use older code somehow?
Create-ivity - a game development blog Mouseover for more information.

This topic is closed to new replies.

Advertisement