User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jul 2008
Posts: 5
Reputation: ratperson is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ratperson ratperson is offline Offline
Newbie Poster

pygame: click and picture appears

  #1  
Jul 10th, 2008
I am here making my video game wondering how to make an image appear when someone clicks in a certain area. If I need an image to pop up when some one clicks anywhere within a large rectangle of space how would I go about doing that? Thanks!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Posts: 36
Reputation: leegeorg07 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Light Poster

Re: pygame: click and picture appears

  #2  
31 Days Ago
at the moment what i can think of is lookin at the pygame examples you get when you download pygame.
i can help you on the matter of the area to use. that would be that you need to specify the coordinates so to speak and thats all i can think of
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,425
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: pygame: click and picture appears

  #3  
30 Days Ago
This is modified code from one of the Python snippets on DaniWeb ...
  1. # move an image rectangle to follow the mouse click position
  2.  
  3. import pygame as pg
  4.  
  5. # initialize pygame
  6. pg.init()
  7.  
  8. # use an image you have (.bmp .jpg .png .gif)
  9. image_file = "ball_r.gif"
  10.  
  11. # RGB color tuple for screen background
  12. black = (0,0,0)
  13.  
  14. # screen width and height
  15. sw = 640
  16. sh = 480
  17. # create a screen
  18. screen = pg.display.set_mode((sw, sh))
  19. # give the screen a title
  20. pg.display.set_caption('image follows mouse click position')
  21.  
  22. # load an image
  23. # convert() unifies the pixel format for faster blit
  24. image = pg.image.load(image_file).convert()
  25. # get the rectangle the image occupies
  26. # rec(x, y, w, h)
  27. start_rect = image.get_rect()
  28. image_rect = start_rect
  29.  
  30. running = True
  31. while running:
  32. event = pg.event.poll()
  33. keyinput = pg.key.get_pressed()
  34. # exit on corner 'x' click or escape key press
  35. if keyinput[pg.K_ESCAPE]:
  36. raise SystemExit
  37. elif event.type == pg.QUIT:
  38. running = False
  39. elif event.type == pg.MOUSEBUTTONDOWN:
  40. image_rect = start_rect.move(event.pos)
  41.  
  42. # this erases the old sreen with black
  43. screen.fill(black)
  44. # put the image on the screen
  45. screen.blit(image, image_rect)
  46. # update screen
  47. pg.display.flip()
Last edited by vegaseat : 30 Days Ago at 4:05 pm. Reason: simplify
Attached Images
File Type: gif ball_r.gif (1.9 KB, 8 views)
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Python Forum

All times are GMT -4. The time now is 10:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC