By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,667 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,667 IT Pros & Developers. It's quick & easy.

Don't understand why my simple pygame script (draw rectangle) isn't working...

Jory R Ferrell
P: 62
The code works if I don't define the event loops, but when I DO define events, the screen isn't even generated.
I initialized the pygame module before the loop.
Expand|Select|Wrap|Line Numbers
  1.  
  2. #-------------------------------------------------------------------------------
  3. # Name:        module3
  4. # Purpose:
  5. #
  6. # Author:      office
  7. #
  8. # Created:     02/03/2012
  9. # Copyright:   (c) office 2012
  10. # Licence:     <your licence>
  11. #-------------------------------------------------------------------------------
  12. #!/usr/bin/env python
  13.  
  14. import pygame
  15. from pygame.locals import *
  16. from sys import exit         #load pygame module
  17.  
  18.  
  19. pygame.init()
  20.  
  21. w = 640                 #set width of screen
  22. h = 480                 #set height
  23. x = 50
  24. y = 50
  25. rectW = 100
  26. rectH = 100
  27.  
  28. while 1:
  29.     for event in pygame.event.get():
  30.             if event.type == QUIT:
  31.                 pygame.quit()
  32.  
  33.  
  34.             pressed_mouse = pygame.mouse.get_pressed()
  35.             if pressed_mouse[0]:
  36.                 newX, newY = pygame.mouse.get_pos()
  37.                 x = newX
  38.                 y = newY
  39.  
  40.                 display.update()
  41.  
  42.  
  43.  
  44.  
  45. screen = pygame.display.set_mode((w, h)) #make screen
  46. pygame.draw.rect(screen, (0, 255, 0), (x, y, rectW, rectH), 1)
  47.  
  48.  
  49.  
  50. pygame.display.flip()
  51.  
Mar 2 '12 #1

✓ answered by Smygis

Because the program is stuck in an infinite loop and never creates the screen or rectangle.

Share this Question
Share on Google+
2 Replies

Smygis
100+
P: 126
Because the program is stuck in an infinite loop and never creates the screen or rectangle.
Mar 2 '12 #2

Jory R Ferrell
P: 62
Ok...I fixed it. Obviously I needed to place the draw.rect event inside the event loop and the screen draw event before it. :P
Mar 3 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.