我正在尝试在pygame中为学校项目制作绘图程序。在这个模块中,我打算让用户按下鼠标并在表面上画一条线。如果一个人按下一个矩形,那个人选择的颜色就是绘制线条的颜色。由于某种原因,变量可以改变,但即使我按下鼠标,也没有画线。 这是代码:
def painting_module():
running = True
while running:
#clock for timingn processes
Clock.tick(FPS)
# Process input (event)
for event in pygame.event.get():
Mouse_location = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
displacement_val = pygame.mouse.get_rel()
color_to_paint_with = (0,0,0)
if event.type == pygame.QUIT:
running = False
if click[0] == 1:
# Intended to select colors if pressed, draw a line if the mouse is not on the rect
if red_rect.collidepoint(Mouse_location):
color_to_paint_with = RED
print "Red"
print color_to_paint_with
if green_rect.collidepoint(Mouse_location):
color_to_paint_with = GREEN
print "red"
print color_to_paint_with
if blue_rect.collidepoint(Mouse_location):
color_to_paint_with = BLUE
print "red"
print color_to_paint_with
if gray_rect.collidepoint(Mouse_location):
color_to_paint_with = GRAY
print "red"
print color_to_paint_with
if eraser_ivory_rect.collidepoint(Mouse_location):
color_to_paint_with = IVORY
print "red"
print color_to_paint_with
#draws the line
pygame.draw.line(Screen, color_to_paint_with, Mouse_location, (Mouse_location[0] + displacement_val[0], Mouse_location[1] + displacement_val[1]))
答案 0 :(得分:0)
我会做的是
painting_module()
def painting_module():
running = True
while running:
#clock for timingn processes
Clock.tick(FPS)
# Process input (event)
for event in pygame.event.get():
Mouse_location = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
displacement_val = pygame.mouse.get_rel()
color_to_paint_with = (0,0,0)
if event.type == pygame.QUIT:
running = False
if click[0] == 1:
# Intended to select colors if pressed, draw a line if the mouse is not on the rect
if red_rect.collidepoint(Mouse_location):
color_to_paint_with = RED
print "Red"
print color_to_paint_with
// Call drawloop here
if green_rect.collidepoint(Mouse_location):
color_to_paint_with = GREEN
print "red"
print color_to_paint_with
if blue_rect.collidepoint(Mouse_location):
color_to_paint_with = BLUE
print "red"
print color_to_paint_with
if gray_rect.collidepoint(Mouse_location):
color_to_paint_with = GRAY
print "red"
print color_to_paint_with
if eraser_ivory_rect.collidepoint(Mouse_location):
color_to_paint_with = IVORY
print "red"
print color_to_paint_with
def drawline(color_to_paint_with, Mouse_location, displacement_val):
for event in pygame.event.get():
while pygame.mouse.get_pressed()[0] == 1:
pygame.draw.line(Screen, color_to_paint_with, Mouse_location, (Mouse_location[0] + displacement_val[0], Mouse_location[1] + displacement_val[1]))
pygame.display.flip()
我会用这个替换“if pygame.mouse.get_pressed():”代码块。