473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Jory R Ferrell
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.

2 1831
Smygis
126 100+
Because the program is stuck in an infinite loop and never creates the screen or rectangle.
Mar 2 '12 #2
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

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

Similar topics

7
by: Robert Nicholson | last post by:
So I've got one page where I have an image inside a DIV with text-align: center and the image is correctly centered in that block. Making me think that text-align will center the contents of a...
0
by: Rik Brooks | last post by:
I've been working on this all day and finally I surrender. What I'm trying to do should be simple, I think. I'm trying to read an rss feed from cnn and use that to populate an asp.net table...
9
by: Simon | last post by:
I've got a simple and repetitive bit of code for a function in a C implementation of the card game 31s I'm working on. BTW, I am a bit of a novice at C; for the past couple of years I was using...
0
by: DougRenwick | last post by:
I'm an trying to prevent my application from stealing the focus from the application that launched it. Overriding the ShowWithoutActivation property returns a value of true but does not prevent the...
6
by: Toby Inkster | last post by:
Does anyone have any idea why this effect using script.aculo.us is working in Opera, but not Firefox? http://examples.tobyinkster.co.uk/Balloons2/Balloon%20tooltip.html (I've not had the...
8
by: Joshua Moore | last post by:
/* Hi, I was hoping someone could help me with this problem. I did my work and worked my way through the usual compiler messages, but I have run against some problem I can't identify. The compiler...
5
by: Thierry | last post by:
Hello fellow pythonists, I'm a relatively new python developer, and I try to adjust my understanding about "how things works" to python, but I have hit a block, that I cannot understand. I...
7
by: nassausky | last post by:
I acquired a very basic redirect script from: http://www.minisitegallery.com/blog/php-javascript-countdown-script-with-timezone-setting.html which is supposed to count down to a specified date and...
12
by: Hendor | last post by:
Hi all. I've recently set up Apache 2.2 with PHP 5.2 and MySQL 5.1. I played around with SQL a bit, and now I'm trying to access it with PHP. I currently have the code: <?php #...
2
by: Fuzz13 | last post by:
As some of you may have seen (since I have had to practically live on Bytes.com for help) I am working on a program that reads a given .txt or .csv creating an object of an array (lines) per line of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.