0

I have two multiple threads, and my aim is to print "mouse1" and "mouse2" seperately using the two multiple threads. But the program isn't working. It prints nothing, and it can't be closed properly.

import threading
import pygame

screen = pygame.display.set_mode((800, 800))
def mouse1():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                print('mouse1')
            else:
                pass

def mouse2():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                print('mouse2')
            else:
                pass

t2 = threading.Thread(target=mouse1)
t1 = threading.Thread(target=mouse2)

t1.start()
t2.start()

t1.join()
t2.join()

I expect when the mouse button is clicked, the output to be many "mouse1" and "mouse2".

liushi
  • 83
  • 7

1 Answers1

-1

Here you go. I copied the example from https://www.pygame.org/docs/tut/PygameIntro.html and followed https://stackoverflow.com/a/34288442/326242 in order to distinguish the buttons.

import sys, pygame
pygame.init()

LEFT_MOUSE_BUTTON = 1
MIDDLE_MOUSE_BUTTON = 2
RIGHT_MOUSE_BUTTON = 3

def handleMouseEvents(event):
    if event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == LEFT_MOUSE_BUTTON:
            print("Left mouse button clicked!")
        elif event.button == MIDDLE_MOUSE_BUTTON:
            print("Middle mouse button clicked!")
        elif event.button == RIGHT_MOUSE_BUTTON:
            print("Right mouse button clicked!")

        sys.stdout.flush()
    else:
        pass

size = width, height = 320, 240
speed = [2, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load("intro_ball.gif")
ballrect = ball.get_rect()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        else:
            handleMouseEvents(event)

    ballrect = ballrect.move(speed)
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(black)
    screen.blit(ball, ballrect)
    pygame.display.flip()

The main things I fixed were:

  • Added a call to sys.stdout.flush() after printing to the output. Otherwise you don't see the output until the program ends and flushes the output automatically.
  • Added the example code that actually sets up a display area and shows things on it.
  • Checked event.button to see which button is being clicked.
  • Got rid of the threading stuff. Pygame has its own threading system, follow it unless you really know what you're doing.

You'll need to add intro_ball.gif to your source code, alongside the file that this code is in. I got it from https://www.pygame.org/docs/_images/intro_ball.gif

Andrew Koster
  • 1,550
  • 1
  • 21
  • 31