[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Drawing a box with the mouse



import pygame
from pygame.locals import *
import os, sys

pygame.init()

surface = pygame.display.set_mode((800,600))

mouse1x = 0
mouse1y = 0
mouse2x = 0
mouse2y = 0

point_drawing = 1

can_click = 1

def main():
    global mouse1x, mouse1y, mouse2x, mouse2y
    global point_drawing
    global can_click
    while True:
        surface.fill((255,255,255))
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit(); sys.exit()
        mpress = pygame.mouse.get_pressed()
        if mpress[0] and can_click == 1:
            can_click = 0
            if point_drawing == 1:
                mouse1x, mouse1y = pygame.mouse.get_pos()
                point_drawing = 2
            else:
                mouse2x, mouse2y = pygame.mouse.get_pos()
        else:
            can_click = 1  
        pygame.draw.rect(surface,(255,255,0),(mouse1x,mouse1y,mouse2x-mouse1x,mouse2y-mouse1y),0)
        pygame.display.flip()
main()