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

Re: [pygame] Drawing a box with the mouse





On 7/28/07, Ian Mallett <geometrian@xxxxxxxxx> wrote:
I also assumed that yellow_block is a single yellow block of color.  I think it would be faster to draw a rectangle of a single color than to load a image of one.

...but in case you are using an image:

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

pygame.init()

surface = pygame.display.set_mode((800,600))
yellow_block = pygame.image.load("yellow_box.gif")

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()
                point_drawing = None
        else:
            can_click = 1
        if point_drawing == None:
            draw_yellow_block = pygame.transform.scale(yellow_block, (mouse2x-mouse1x,mouse2y-mouse1y))
            surface.blit(draw_yellow_block, (mouse1x, mouse1y))
        pygame.display.flip()
main()