Python Forum
help with python mouse clicks and Pygame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with python mouse clicks and Pygame
#1
I want to be able to draw lines between my mouse clicks, for example, click 1(put a dot, draws a line from 0,0 to point 1) click 2( puts dot, draws a line from point 1 to point 2) and so on,
look at DEF SCENE here's some of my code...

from pygame import *
init()
size = width, height = 800, 600
screen = display.set_mode(size)
button = 0
BLACK = (0, 0, 0)
RED = (255, 0, 0)
screen.fill(BLACK)
def getVal(tup):
    """ getVal returns the (position+1) of the first 1 within a tuple.
        This is used because MOUSEBUTTONDOWN and MOUSEMOTION deal with
        mouse events differently
    """
    for i in range(3):
        if tup[i]==1:
            return i+1
    return 0
    
def drawScene(screen, button):
    # Draw circle if the left mouse button is down.
    if button==1:
        draw.circle(screen,RED,(mx,my), 5)
        x,y = mouse.get_pos()
        draw.line(screen,RED,(0,0),(x,y),1) 
    display.flip()
    if button>1:  
        screen.fill(BLACK)
     
     
display.flip()
    
    
running = True
myClock = time.Clock()
 
while running:
    for e in event.get():
        keydown= pygame.Key.get_pressed()
        if KeyDown[pygame.K_ESCAPE]:
            quit()
        if e.type== QUIT:
            running=False
        if e.type== MOUSEBUTTONDOWN:
            mx,my = e.pos
            button= e.button
        if e.type== MOUSEMOTION:
            mx,my= e.pos
            button= getVal(e.buttons)
    drawScene(screen,button)
    myClock.tick(60)
quit()
Reply
#2
Any help please?
Reply
#3
The approach I would take is to save the location of each mouse click to a list, then loop over the list to draw lines from each point to the next.

points = []

if e.type== MOUSEBUTTONDOWN:
    points.append(mouse.get_pos())

if len(points) > 1:
    for i in range((len(points) - 1)):
        pg.draw.line(screen, (255, 255, 255), points[i], points[i +1], 2)
Should be pretty close.
Reply
#4
Never get key pressed in event loop. When in event loop. Only use event items.
Example.
import pygame

def main():
    pygame.init()
    pygame.display.set_caption("Mouse Draw")
    surface = pygame.display.set_mode((800, 600))
    clock = pygame.time.Clock()
    rect = surface.get_rect()
    fps = 60

    line_surface = pygame.Surface(rect.size, pygame.SRCALPHA)
    line_surface.fill((0, 0, 0, 0))

    mouse_position = None
    display_line = None
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEMOTION:
                if mouse_position:
                    display_line = mouse_position, event.pos
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if mouse_position:
                        pygame.draw.line(line_surface, pygame.Color("red"), mouse_position, event.pos)
                        mouse_position = None
                        display_line = None
                    else:
                        mouse_position = event.pos


        surface.fill(pygame.Color('black'))
        surface.blit(line_surface, (0, 0))

        if display_line:
            pygame.draw.line(surface, pygame.Color('lawngreen'), *display_line)

        pygame.display.update()
        clock.tick(fps)

main()

If you want it to be continues lines. Change line 28.
mouse_position = event.pos
99 percent of computer problems exists between chair and keyboard.
Reply
#5
Thanks so much, just had one more question... is there any way I can make the first line disappear when the second line gets drawn? (I'm making a game where it would be useful.) Thanks in advance!
Reply
#6
This is programming. You can do what ever you put you mind too. So yes.

You need to put boolean variable around surface.blit(line_surface, (0, 0). Create show_lines = False before main loop. Then an if block around surface.blit(line_surface, (0, 0).
You need a new variable. draw_line = None under display_line before main loop.
Then in event mouse button down. draw_line = mouse_position, event.pos
Then you draw it like display_line.
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Accurate Clicks Per Second Hamti 5 3,935 Sep-06-2022, 07:14 AM
Last Post: zackwang
  [split] Accurate Clicks Per Second kathyalex 4 3,141 Feb-16-2021, 04:27 PM
Last Post: SheeppOSU
  Accurate Clicks Per Second JudyLarose 8 9,311 Feb-10-2021, 03:00 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020