Sign in to follow this  
Wayward Son

Troubles with Pygame

Recommended Posts

I'm a beginner and I'm having some trouble setting up a defined function in pygame. Here's the code:
 

 

import random, pygame, sys, time
from pygame.locals import *
pygame.init()
WINDOWWIDTH=600
WINDOWHEIGHT=600
TEXTCOLOR=255,255,255
BACKGROUNDCOLOR=0,0,0
windowSurface=pygame.display.set_mode((int(WINDOWWIDTH/2), int(WINDOWHEIGHT/2)),0,32)
Stren=0
End=0
Dev=0
Awar=0
Int=0
Wil=0
Agi=0
Luk=0
att=[Stren, End, Dev, Awar, Int, Wil, Agi, Luk]
def displayText(text, font, surface, x, y):
    textobj=pygame.font.render(text, font, TEXTCOLOR)
    textrect=textobj.get_rect()
    textrect.topleft=(x, y)
    surface.blit(textobj, textrect)
displayText('''From where dost thou hail?
1)Caresh
2)Tenin
3)Heartlands
4)Swamplands
5)Sub-Tenin Desert''', None, windowSurface, (WINDOWWIDTH/2), (WINDOWHEIGHT/2))
   
   And here's the resultant error:

Traceback (most recent call last):
  File "C:/Python Prog/CharRolling.py", line 31, in <module>
    5)Sub-Tenin Desert''', None, windowSurface, (WINDOWWIDTH/2), (WINDOWHEIGHT/2))
  File "C:/Python Prog/CharRolling.py", line 21, in displayText
    textobj=pygame.font.render(text, font, TEXTCOLOR)
AttributeError: 'module' object has no attribute 'render'

 

I've got no idea why this won't work. I was following some instructions from a python programming lesson book (as far as the def statement is concerned). Please help me.

 

Share this post


Link to post
Share on other sites

You might want to reformat your code in the post - the editor has a thing to do pre-formatted code blocks.  Particularly since this is Python, and whitespace matters.

 

Might be an old book that doesn't match up with the current Pygame font package.  render is a method that you call on a Font object that's been created, not a module-level function.

Share this post


Link to post
Share on other sites

This seems to do something, it still needs work. I also cleaned it up a bit.

Notice how some extra spaces and empty lines make it more readable.

import random, pygame, sys, time
from pygame.locals import *

pygame.init()

WINDOWWIDTH =600
WINDOWHEIGHT =600
TEXTCOLOR = (255, 255, 255)
BACKGROUNDCOLOR = (0, 0, 0)

windowSurface = pygame.display.set_mode((int(WINDOWWIDTH/2), int(WINDOWHEIGHT/2)), 0, 32)

Stren = 0
End   = 0
Dev   = 0
Awar  = 0
Int   = 0
Wil   = 0
Agi   = 0
Luk   = 0
att = [Stren, End, Dev, Awar, Int, Wil, Agi, Luk]

def displayText(text, fontname, surface, x, y):
    textfont = pygame.font.Font(fontname, 15)   # this line was missing, I also renamed a few variables
    textobj  = textfont.render(text, True, TEXTCOLOR, BACKGROUNDCOLOR)
    # textrect = textobj.get_rect()  you don't need this
    surface.blit(textobj, (x, y))

# Moved your ugly multi-line string a bit out of the way
# It fails to render properly, as pygame cannot move to the next line.
# Instead, draw each line separately
lines = '''From where dost thou hail?
1)Caresh
2)Tenin
3)Heartlands
4)Swamplands
5)Sub-Tenin Desert'''

# You need to catch events, and loop, or your screen is about 0.01 seconds visible.
while True:
    windowSurface.fill(BACKGROUNDCOLOR)

    displayText(lines, None, windowSurface, 0, 0)
    #(WINDOWWIDTH/2), (WINDOWHEIGHT/2)  fails, as the window is also that size

    pygame.display.flip()

    event = pygame.event.wait()
    if event.type == pygame.QUIT:
        break
Edited by Alberth

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this