python - How do I make this pygame program to change from a drawing state to non drawing state when clicked and vice versa? -
i want simulation of paint work such if mouse button pressed, starts drawing ( button released instantly). again, when button pressed, should stop drawing.
there no error. in present case shared remains pen down (drawing) mode. have tried get_pressed
function succeeded in drawing when key remains pressed. tried making equal (a,0,0)
, vary a
achieve goal unsuccessful.
after few attempts resulted in drawing mode turning on or off instantaneously instead of on click.
here's part controls action:
while keepgoing: clock.tick(30) event in pygame.event.get(): if event.type == pygame.quit: keepgoing = false elif event.type == pygame.mousemotion: lineend = pygame.mouse.get_pos() if pygame.mousebuttonup: pygame.draw.line(background, drawcolor, linestart, lineend, linewidth) linestart = lineend elif event.type == pygame.keydown: mydata = (event, background, drawcolor, linewidth, keepgoing) mydata = checkkeys(mydata) (event, background, drawcolor, linewidth, keepgoing) = mydata screen.blit(background, (0, 0)) mylabel = showstats(drawcolor, linewidth) screen.blit(mylabel, (450, 450)) pygame.display.flip()
complete code link: http://codeshare.io/gbpc0
here's minimal example of want do:
import pygame screen = pygame.display.set_mode((800, 600)) line_start = none while true: mouse_pos = pygame.mouse.get_pos() e in pygame.event.get(): if e.type == pygame.quit: break if e.type == pygame.mousebuttonup: line_start = none if line_start else mouse_pos else: if line_start: pygame.draw.line(screen, pygame.color.color('white'), line_start, mouse_pos, 1) line_start = mouse_pos pygame.display.flip() continue break
it keeps track of state (are in drawing or non-drawing mode?) storing none
or last mouse position in line_start
on mousebuttonup
event.
Comments
Post a Comment