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)
answer_box1.move_ip(50, 358)
answer_box2.move_ip(735, 358)
answer_box3.move_ip(50, 538)
answer_box4.move_ip(735, 538)
answer_boxes = [answer_box1, answer_box2, answer_box3, answer_box4]
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")
for box in answer_boxes:
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
for box in answer_boxes:
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
def correct_answer():
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
for box in answer_boxes:
if box.collidepoint(pos):
print("Clicked on answer " + str(index))
if index == question[5]:
print("You got it correct!")
correct_answer()
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!