•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 402,787 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,792 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 229 | Replies: 2
![]() |
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation:
Rep Power: 9
Solved Threads: 173
This is modified code from one of the Python snippets on DaniWeb ...
python Syntax (Toggle Plain Text)
# move an image rectangle to follow the mouse click position import pygame as pg # initialize pygame pg.init() # use an image you have (.bmp .jpg .png .gif) image_file = "ball_r.gif" # RGB color tuple for screen background black = (0,0,0) # screen width and height sw = 640 sh = 480 # create a screen screen = pg.display.set_mode((sw, sh)) # give the screen a title pg.display.set_caption('image follows mouse click position') # load an image # convert() unifies the pixel format for faster blit image = pg.image.load(image_file).convert() # get the rectangle the image occupies # rec(x, y, w, h) start_rect = image.get_rect() image_rect = start_rect running = True while running: event = pg.event.poll() keyinput = pg.key.get_pressed() # exit on corner 'x' click or escape key press if keyinput[pg.K_ESCAPE]: raise SystemExit elif event.type == pg.QUIT: running = False elif event.type == pg.MOUSEBUTTONDOWN: image_rect = start_rect.move(event.pos) # this erases the old sreen with black screen.fill(black) # put the image on the screen screen.blit(image, image_rect) # update screen pg.display.flip()
Last edited by vegaseat : 30 Days Ago at 4:05 pm. Reason: simplify
May 'the Google' be with you!
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: help with strange error
- Next Thread: Python for Laptop Robot Speech recognition and TTS.



Linear Mode