我的问答游戏中的 draw() 函数无法正常工作
The draw() function in my quiz game is not working properly
好的,我正在使用 python 制作问答游戏。我拥有它,以便当您的鼠标悬停在其中一个选项上时,它会改变颜色。但是,当我尝试这样做时出现了两个问题。
- 当我将鼠标悬停在它上面时,只有 answer_box4 实际上会稍微改变颜色。
- 当我将鼠标悬停在 answer_box4 上时,它会更改所有框的颜色。
代码如下:
try:
logname = 'c:/temp/pgzrun.log'
fontname = 'arial.ttf'
import faulthandler
faulthandler.enable()
import os, sys
script_dir = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
os.chdir(script_dir)
import pgzrun
import playsound
import random
from random import randint
WIDTH = 1280
HEIGHT = 720
def draw():
screen.fill("purple")
screen.draw.filled_rect(main_box, "sky blue")
screen.draw.filled_rect(timer_box, "sky blue")
screen.draw.text("Score: " + str(score), color="black", topleft=(10, 10), fontname=fontname)
for boxc in answer_boxes:
screen.draw.filled_rect(box, color)
screen.draw.textbox(str(time_left), timer_box, color=('black'), fontname=fontname)
screen.draw.textbox(question[0], main_box, color=('black'), fontname=fontname)
index = 1
for box in answer_boxes:
screen.draw.textbox(question[index], box, color=('black'), fontname=fontname)
index = index + 1
def game_over():
global question, time_left, scoredecreaserps
scoredecreaserps = 0
message = 'Game Over. You got %s questions correct' %str(numques)
question = [message, '-', '-', '-', '-', 5]
time_left = 0
def correct_answer():
global question, score, numques, time_left
numques = numques + 1
score = score + 1000
if questions:
question = questions.pop()
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()
def score_lower():
global score
if score:
score = score - scoredecreaserps
def on_mouse_move(pos):
global color
for box in answer_boxes:
if box.collidepoint(pos):
color = "medium blue"
print(color)
else:
color = "green yellow"
print(color)
main_box = Rect(50, 40, 820, 240)
timer_box = Rect(990, 40, 240, 240)
answer_box1 = Rect(50, 358, 495, 165)
answer_box2 = Rect(735, 358, 495, 165)
answer_box3 = Rect(50, 538, 495, 165)
answer_box4 = Rect(735, 538, 495, 165)
answer_boxes = [answer_box1, answer_box2, answer_box3, answer_box4]
scoredecreaserps = 80
color = "green yellow"
numques = 0
score = 0
time_left = 10
q1 = ["Who was the third president of the Philippines?",
'Rodrigo Duterte', 'Ramon Magsaysay', 'Jose P. Laurel',
'Fidel V. Ramos', 3]
q2 = ['When was the Philippines granted independece from the US?'
, '1977', '1946', '1935', '1907', 2]
q3 = ['When was the Philippines colonized by Spain', '1898', '1523', '1654',
'1521', 4]
questions = [q1, q2, q3]
random.shuffle(questions)
question = questions.pop(0)
clock.schedule_interval(update_time_left, 1.0)
clock.schedule_interval(score_lower, 1.0)
pgzrun.go()
except:
import traceback
with open(logname, 'a', encoding = 'utf-8') as f:
f.write(''.join(traceback.format_exc()) + '\n')
**
每次鼠标经过方框时,您都会迭代所有方框
def on_mouse_move(pos):
global color
for box in answer_boxes:
if box.collidepoint(pos):
color = "medium blue"
print(color)
else:
color = "green yellow"
print(color)
最后一个盒子是 box4 answer_boxes = [answer_box1, answer_box2, answer_box3, answer_box4]
。最后,如果将 box1 悬停,您会将其颜色更改为蓝色,然后在循环中移动到 box2,进入“else”块并将 box1 的颜色更改为绿色。
为什么?我怀疑是 global color
的声明:每个框指的是相同的颜色。这与 box4 不同并且单击时会更改所有框的颜色这一事实是一致的:作为 for 循环中的最后一个,悬停后会将颜色覆盖为所需的颜色,如果单击会更新所有框的颜色。
您应该将每个框实现为具有自己颜色的对象,然后更改特定框的颜色。
好的,我正在使用 python 制作问答游戏。我拥有它,以便当您的鼠标悬停在其中一个选项上时,它会改变颜色。但是,当我尝试这样做时出现了两个问题。
- 当我将鼠标悬停在它上面时,只有 answer_box4 实际上会稍微改变颜色。
- 当我将鼠标悬停在 answer_box4 上时,它会更改所有框的颜色。
代码如下:
try:
logname = 'c:/temp/pgzrun.log'
fontname = 'arial.ttf'
import faulthandler
faulthandler.enable()
import os, sys
script_dir = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
os.chdir(script_dir)
import pgzrun
import playsound
import random
from random import randint
WIDTH = 1280
HEIGHT = 720
def draw():
screen.fill("purple")
screen.draw.filled_rect(main_box, "sky blue")
screen.draw.filled_rect(timer_box, "sky blue")
screen.draw.text("Score: " + str(score), color="black", topleft=(10, 10), fontname=fontname)
for boxc in answer_boxes:
screen.draw.filled_rect(box, color)
screen.draw.textbox(str(time_left), timer_box, color=('black'), fontname=fontname)
screen.draw.textbox(question[0], main_box, color=('black'), fontname=fontname)
index = 1
for box in answer_boxes:
screen.draw.textbox(question[index], box, color=('black'), fontname=fontname)
index = index + 1
def game_over():
global question, time_left, scoredecreaserps
scoredecreaserps = 0
message = 'Game Over. You got %s questions correct' %str(numques)
question = [message, '-', '-', '-', '-', 5]
time_left = 0
def correct_answer():
global question, score, numques, time_left
numques = numques + 1
score = score + 1000
if questions:
question = questions.pop()
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()
def score_lower():
global score
if score:
score = score - scoredecreaserps
def on_mouse_move(pos):
global color
for box in answer_boxes:
if box.collidepoint(pos):
color = "medium blue"
print(color)
else:
color = "green yellow"
print(color)
main_box = Rect(50, 40, 820, 240)
timer_box = Rect(990, 40, 240, 240)
answer_box1 = Rect(50, 358, 495, 165)
answer_box2 = Rect(735, 358, 495, 165)
answer_box3 = Rect(50, 538, 495, 165)
answer_box4 = Rect(735, 538, 495, 165)
answer_boxes = [answer_box1, answer_box2, answer_box3, answer_box4]
scoredecreaserps = 80
color = "green yellow"
numques = 0
score = 0
time_left = 10
q1 = ["Who was the third president of the Philippines?",
'Rodrigo Duterte', 'Ramon Magsaysay', 'Jose P. Laurel',
'Fidel V. Ramos', 3]
q2 = ['When was the Philippines granted independece from the US?'
, '1977', '1946', '1935', '1907', 2]
q3 = ['When was the Philippines colonized by Spain', '1898', '1523', '1654',
'1521', 4]
questions = [q1, q2, q3]
random.shuffle(questions)
question = questions.pop(0)
clock.schedule_interval(update_time_left, 1.0)
clock.schedule_interval(score_lower, 1.0)
pgzrun.go()
except:
import traceback
with open(logname, 'a', encoding = 'utf-8') as f:
f.write(''.join(traceback.format_exc()) + '\n')
**
每次鼠标经过方框时,您都会迭代所有方框
def on_mouse_move(pos):
global color
for box in answer_boxes:
if box.collidepoint(pos):
color = "medium blue"
print(color)
else:
color = "green yellow"
print(color)
最后一个盒子是 box4 answer_boxes = [answer_box1, answer_box2, answer_box3, answer_box4]
。最后,如果将 box1 悬停,您会将其颜色更改为蓝色,然后在循环中移动到 box2,进入“else”块并将 box1 的颜色更改为绿色。
为什么?我怀疑是 global color
的声明:每个框指的是相同的颜色。这与 box4 不同并且单击时会更改所有框的颜色这一事实是一致的:作为 for 循环中的最后一个,悬停后会将颜色覆盖为所需的颜色,如果单击会更新所有框的颜色。
您应该将每个框实现为具有自己颜色的对象,然后更改特定框的颜色。