Python Game Problem

Hello! I’m working in a “Coding Games in Python” book. I’ve entered the code for a game called Big Quiz. It basically asks a question and gives you 4 possible answers. If you answer correct, it will add a point and bring up the next question. I have been over the code a couple times and can’t find the problem. When I run the game in the shell, I get a pile of errors that mostly indicate lines that are not even in my code. This game involves Pygame. I’m not sure if the errors are something to do with pygame libraries? Here is the code:

``````WIDTH = 1280
HEIGHT = 720

main_box = Rect(0, 0, 820, 240)
timer_box = Rect(0, 0, 240, 240)
answer_box1 = Rect(0, 0, 495, 165)
answer_box2 = Rect(0, 0, 495, 165)
answer_box3 = Rect(0, 0, 495, 165)
answer_box4 = Rect(0, 0, 495, 165)

main_box.move_ip(50, 40)
timer_box.move_ip(990, 40)

score = 0
time_left = 10

q1 = ["What is the closest star to Earth after our sun?", "Aldebaran", "Proxima Centauri", "Sirius", 2]

q2 = ["What is the largest planet in our solar system?", "Earth", "Neptune", "Jupiter", "Mercury", 3]

q3 = ["The chemical element uranium was named after which planet?", "Saturn", "Venus", "Earth", "Uranus", 3]

q4 = ["How many planets in our solar system are larger than Earth?", "4", "6", "2", "5", 1]

q5 =["What is the Latin name for the constellation The Winged Horse?", "Horsie", "Pegasus", "Mr. Ed", "Capricorn", 2]

questions = [q1, q2, q3, q4, q5]
question = questions.pop()

def draw():
screen.fill("dim gray")
screen.draw.filled_rect(main_box, "sky blue")
screen.draw.filled_rect(timer_box, "sky blue")

screen.draw.filled_rect(box, "orange")

screen.draw.textbox(str(time_left), timer_box, color=("black"))
screen.draw.textbox(question[0], main_box, color=("black"))

index = 1
screen.draw.textbox(question[index], box, color=("black"))
index = index + 1

def game_over():
global question, time_left
message = "Game over. You got %s questions correct" % str(score)
question = [message, "-", "-", "-", "-", 5]
time_left = 0

global question, score, time_left

score =  score + 1
if questions:
question = questions.pop(0)
time_left = 10
else:
print("End of questions")
game_over()

def on_mouse_down(pos):
index = 1
if box.collidepoint(pos):
print("Clicked on answer " + str(index))
if index == question[5]:
print("You got it correct!")
else:
game_over()
index = index + 1

def update_time_left():
global time_left

if time_left:
time_left = time_left - 1
else:
game_over()

clock.schedule_interval(update_time_left, 1.0)
``````

The error messages are:

Traceback (most recent call last):
File “c:\users\orion\appdata\local\programs\python\python39\lib\runpy.py”, line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File “c:\users\orion\appdata\local\programs\python\python39\lib\runpy.py”, line 87, in run_code
exec(code, run_globals)
File "C:\Users\orion\AppData\Local\Programs\Python\Python39\Scripts\pgzrun.exe_main
.py", line 7, in
File “c:\users\orion\appdata\local\programs\python\python39\lib\site-packages\pgzero\runner.py”, line 93, in main
run_mod(mod)
File “c:\users\orion\appdata\local\programs\python\python39\lib\site-packages\pgzero\runner.py”, line 113, in run_mod
PGZeroGame(mod).run()
File “c:\users\orion\appdata\local\programs\python\python39\lib\site-packages\pgzero\game.py”, line 217, in run
self.mainloop()
File “c:\users\orion\appdata\local\programs\python\python39\lib\site-packages\pgzero\game.py”, line 256, in mainloop
draw()
File “quiz.py”, line 48, in draw
screen.draw.textbox(question[index], box, color=(“black”))
File “c:\users\orion\appdata\local\programs\python\python39\lib\site-packages\pgzero\screen.py”, line 66, in textbox
ptext.drawbox(*args, surf=self._surf, **kwargs)
File “c:\users\orion\appdata\local\programs\python\python39\lib\site-packages\pgzero\ptext.py”, line 476, in drawbox
fontsize = _fitsize(text, fontname, sysfontname, bold, italic, underline,
File “c:\users\orion\appdata\local\programs\python\python39\lib\site-packages\pgzero\ptext.py”, line 162, in _fitsize
if not fits(a):
File “c:\users\orion\appdata\local\programs\python\python39\lib\site-packages\pgzero\ptext.py”, line 153, in fits
texts = wrap(text, fontname, fontsize, sysfontname,
File “c:\users\orion\appdata\local\programs\python\python39\lib\site-packages\pgzero\ptext.py”, line 93, in wrap
texts = text.replace(“\t”, " “).split(”\n")
AttributeError: ‘int’ object has no attribute ‘replace’

They indicate lines well beyond how many lines of code I have. I’m not sure how to fix this. Any help is much appreciated!

I’m not familiar with Pygame, so, perhaps someone else can chime in here.

Error messages are so cryptic sometimes.
Often they’re in the “general vicinity” of what’s wrong…which doesn’t help us and is frustrating for sure.

1 Like

Thanks for the reply! I’m not sure where to begin there. There is no .replace, no .split and no texts anywhere in the code I’ve typed.

I just tried updating the version of pygame installed and it still comes up with all the same errors. I’m stumped here.

1 Like

You’re right.

You’re getting runpy errors. So, I looked that up.
That has to do with locating and executing python modules. I’m thinking that this might be an installation problem with how (did you use `pip`) and where you installed the modules you’re using(?)

See:

There’s also the Pygame docs and I wonder if there’s some info in there that’s helpful and would point to these errors.
https://www.pygame.org/docs/ref/pygame.html

1 Like

Thank you for your help! I’ll look into it. As far as I know, I used pip to install pygame and pygame zero. I did it a while back. I’ve taken a break from coding for a year and I’m just getting back into it.

If I try to update pygame or pygame zero now, it says that requirements are already satisfied. I’m also about 5 games into this book and they all worked fine up to this point. Usually just type pgzrun “file name” in the shell and hit enter and they have all worked until this one. I’ll take a look at the docs and that stack overflow post. If I can or can’t figure it out, I’ll add an update here. Thank you again @lisalisaj !

1 Like

np!

I’d also look into `pgzrun` or `pgzero` errors as well.

1 Like

I followed the advice in the stack overflow and uninstalled pygame and pgzrun. Now it won’t reinstall pgzrun. I even tried uninstalling Python itself and reinstalling.

Now I can’t get any of the previous pygame games to work at all. I’m over my head here

Are you installing it in the same directories?

Maybe there’s something in the Pygame documentation that will help fix this.

I’m thinking that @not-ethan might be able to help you with this as I think he might have some experience in this area.

Errors are frustrating. I recommend taking a break from it and coming back to it with fresh eyes.

1 Like

Thanks @lisalisaj

I’ll take a break. I tried uninstalling and reinstalling Python, Pygame and Pygame Zero multiple times at this point. I just let Python install in whatever default directory it wants and then use pip to install pygame and pygame zero – which I assume also go into the correct directory? (sorry, I don’t have a great understanding of all the options involving the PATH and where all these programs are installing – I’m used to regular programs that just install where they should without adjusting anything haha) I’ll check with @not-ethan as well and see if they can help.

I have no experience with Pygame so I cant help, unfortunately.

1 Like

Thanks anyway @not-ethan. I’ll keep trying things after taking a break for a bit.

2 Likes

I’m at least back to start. I’ve gotten Python, pygame and pygame zero reinstalled (this time all together in a directory I created) and my other games will work.

This one still comes up with a pile of errors. I can’t seem to find any help regarding the book itself. I will go over the code one more time to check for any errors. If that doesn’t work, I’m at a loss.

1 Like