0

I am making a simple game in pgzero, but the screen. function never seems to work correctly.

when I try to display text at the start like so:

screen.draw.text("Hello", (50, 30), color="orange")

it comes out saying screen is not defined

yet when I use it here:

def draw():
    screen.clear()
    bg.draw() 
    bread.draw()

it works perfectly fine

how do I fix this?

CodeWizard777
  • 71
  • 1
  • 10
  • Where is screen declared? This might be a scoping issue. Perhaps you can provide a bit more of your code? – Katharine Osborne Jan 30 '22 at 18:52
  • this is basically all there is in terms of the code... `screen` is never declared, yet it still seems to work in `screen.clear()` Im not sure how I would define screen – CodeWizard777 Jan 31 '22 at 00:14

1 Answers1

1

I have no familiarity with pgzero, but I installed it and played around, and when I put line inside the draw() function it runs fine:

def draw():
    screen.draw.text("Hello", (50, 30), color="orange")

So it looks like draw() needs to be there to show things on screen (and the screen object is already part of draw(). Let me know if you want an explanation of how this can happen (short answer: object inheritance).

Let me know if this solves your problem.

Looking at the documentation there's a few built-in functions (event hooks) like this that have special use. I think this is meant to get users unfamiliar with programming up and running quickly, but it's annoying that there seems to be no basic explanation in the documentation about this. And this is likely to confuse learners when they switch to learning a different language. However these things are always a work in progress so hopefully it improves.

More information: https://pygame-zero.readthedocs.io/en/stable/hooks.html

Katharine Osborne
  • 6,571
  • 6
  • 32
  • 61
  • better, but it doesnt show up. Maybe a problem with the layers? – CodeWizard777 Jan 31 '22 at 21:39
  • Can you add your full program? When I include the line inside draw() it draws the text as expected. If you have other lines doing other things along with it, that may be causing issues, so it would be helpful to see the full context – Katharine Osborne Jan 31 '22 at 22:59