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

[pygame] Drawing a box with the mouse



I've been working at this for some time now and I can't figure out what the problem is. What i'm trying to do is when the mouse is clicked, it saves the mouse x,y then when the mouse clicks again at a different location it saves the new mouse x,y then transforms.scale a image of a box with those coordinates. For some reason I can't get the second mouse x,y to show up. Thanks in advance!

import pygame
from pygame.locals import *
import sys


pygame.init ()
screen = pygame.display.set_mode((500,350)).convert()
background = "">#background = "">background.fill((250, 250, 250))
yellow_block = pygame.image.load("yellow_box.gif")
screen.blit(background, (0, 0))
pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouseX1, mouseY1 = pygame.mouse.get_pos ()
            print "Mouse 1", mouseX1, mouseY1
            clear = pygame.event.clear()
            while not pygame.MOUSEBUTTONDOWN:
                mouseX2, mouseY2 = pygame.mouse.get_pos()
                print "Mouse 2", mouseX2, mouseY2
                box_resize = ((mouseX2 - mouseX1) + 1), ((mouseY2 - mouseY1) + 1)
                yellow_block = pygame.transform.scale(yellow_block, box_resize)
                background.blit(yellow_block, (mouseX1, mouseY1))
                screen.blit(background, (0, 0))
                pygame.display.update(yellow_block)
               
            #box_size = 100, 100
            #yellow_block = pygame.transform.scale(yellow_block, box_size)
            #background.blit(yellow_block, (mouseX1, mouseY1))
           
            #screen.blit(background, (0, 0))
            #pygame.display.flip()
        elif event.type == pygame.QUIT:
            sys.exit()


Eric Hunter
hunter.eric1@xxxxxxxxx