943,933 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 840
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 8th, 2009
0

fileIO & pygame help

Expand Post »
I am trying to make pygame draw a shape from a list of shapes imported with fileIO but i get a pygame import error please help

Python Syntax (Toggle Plain Text)
  1. import random
  2. import pygame
  3. w = 640
  4. h = 480
  5.  
  6. x = open("shapes.txt")
  7. z = x.readlines()[n]
  8. n = random.randrange(6)
  9.  
  10. screen = pygame.display.set_mode((w, h))
  11. pygame.draw.z(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
  12.  
  13. for event in pygame.event.get():
  14. if event.type == QUIT:
  15. exit()
this is my my "shapes.txt"

circle
rect
line
line
rect
circle
Attached Files
File Type: txt shapes.txt (38 Bytes, 10 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CurtisEClark is offline Offline
12 posts
since Sep 2009
Nov 8th, 2009
0
Re: fileIO & pygame help
say if n was circle, this is what python interprets:

python Syntax (Toggle Plain Text)
  1. pygame.draw."circle"(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )

now thats not code; its a string. you could add if statements like so:

python Syntax (Toggle Plain Text)
  1. if z == 'circle':
  2. pygame.draw.circle(arguements_go_here)
  3. elif z == 'line':
  4. # code for drawing line here
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
fallopiano is offline Offline
68 posts
since Jun 2009
Nov 8th, 2009
0
Re: fileIO & pygame help
Python Syntax (Toggle Plain Text)
  1. import random
  2. import pygame
  3. w = 640
  4. h = 480
  5.  
  6. n = random.randrange(6)
  7. x = open("shapes.txt")
  8. z = x.readlines()[n]
  9.  
  10. screen = pygame.display.set_mode((w, h))
  11. if z == "circle":
  12. pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
  13. elif z == "line":
  14. pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
  15. elif z == "rect":
  16. pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
  17.  
  18.  
  19.  
  20. for event in pygame.event.get():
  21. if event.type == QUIT:
  22. exit()

my new code but i get this error now

Message File Name Line Position
Traceback
<module> C:\Users\Curtie\Desktop\HomeWork\CSET\codes\assignment_11\assignment_11.py 2
<module> L:\PortableApps\PortablePython_1.1_py3.0.1\App\lib\site-packages\pygame\__init__.py 95
ImportError: DLL load failed: The specified module could not be found.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CurtisEClark is offline Offline
12 posts
since Sep 2009
Nov 9th, 2009
0
Re: fileIO & pygame help
try

python Syntax (Toggle Plain Text)
  1. import pygame
  2. from pygame.locals import *

if that doesn't work, make sure you have the pygame that matches up with your version of python
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
fallopiano is offline Offline
68 posts
since Jun 2009
Nov 9th, 2009
0
Re: fileIO & pygame help
also, if that fails, try reinstalling your version of pygame
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
fallopiano is offline Offline
68 posts
since Jun 2009
Nov 9th, 2009
0
Re: fileIO & pygame help
Quick hint: Instead of the condition cascade
Python Syntax (Toggle Plain Text)
  1. if z == "circle": ... elif ...
consider a more pythonically succinct approach:
Python Syntax (Toggle Plain Text)
  1. assert n in ('circle', 'line', 'rect')
  2. draw = getattr(pygame.draw, n)
  3. draw(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
Last edited by pythopian; Nov 9th, 2009 at 11:43 am.
Reputation Points: 20
Solved Threads: 25
Junior Poster in Training
pythopian is offline Offline
81 posts
since Nov 2009
Nov 9th, 2009
0
Re: fileIO & pygame help
ive reinstalled python and pygame ive added the
Python Syntax (Toggle Plain Text)
  1. import pygame
  2. from pygame.locals import *
with no results....i am using 3.01. it works in 2.61 but i am required to use 3.01 does anyone else no what might be causing this?
also its in just the import pygame function as i have tested just running that and get the same error code
Last edited by CurtisEClark; Nov 9th, 2009 at 3:39 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CurtisEClark is offline Offline
12 posts
since Sep 2009
Nov 10th, 2009
0
Re: fileIO & pygame help
Python Syntax (Toggle Plain Text)
  1. import random
  2. import pygame
  3.  
  4. w = 640
  5. h = 480
  6.  
  7. n = random.randrange(6)
  8. x = open("shapes.txt")
  9. z = x.readlines()[n]
  10.  
  11.  
  12.  
  13. screen = pygame.display.set_mode((w, h))
  14. if z == "circle":
  15. pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )
  16. elif z == "line":
  17. pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )
  18. elif z == "rect":
  19. pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )
This code works in 2.6.1 but when there are no shapes just a black screen, is there a way to fix this?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CurtisEClark is offline Offline
12 posts
since Sep 2009
Nov 10th, 2009
0
Re: fileIO & pygame help
Python Syntax (Toggle Plain Text)
  1. import random
  2. import pygame
  3.  
  4. w = 640
  5. h = 480
  6.  
  7. n = random.randrange(6)
  8. x = open("shapes.txt")
  9. z = x.readlines()[n]
  10.  
  11.  
  12.  
  13. screen = pygame.display.set_mode((w, h))
  14. if "circle" in z:
  15. pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
  16. elif "line" in z:
  17. pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
  18. elif "rect" in z:
  19. pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )

I've fixed the main problem but now i get

Traceback (most recent call last):
File "<string>", line 244, in run_nodebug
File "C:\Users\Curtie\Desktop\HomeWork\CSET\codes\assignment_11\assignment_112.py", line 17, in <module>
pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
TypeError: Invalid end position argument
>>>
Traceback (most recent call last):
File "<string>", line 244, in run_nodebug
File "C:\Users\Curtie\Desktop\HomeWork\CSET\codes\assignment_11\assignment_112.py", line 19, in <module>
pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
TypeError: Rect argument is invalid
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CurtisEClark is offline Offline
12 posts
since Sep 2009
Nov 10th, 2009
0
Re: fileIO & pygame help
Python Syntax (Toggle Plain Text)
  1. import random
  2. import pygame
  3.  
  4. w = 640
  5. h = 480
  6.  
  7. n = random.randrange(6)
  8. x = open("shapes.txt")
  9. z = x.readlines()[n]
  10.  
  11.  
  12.  
  13. screen = pygame.display.set_mode((w, h))
  14. if z == "circle":
  15. pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )
  16. elif z == "line":
  17. pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )
  18. elif z == "rect":
  19. pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )
This code works in 2.6.1 but when there are no shapes just a black screen, is there a way to fix this?
You need to read the pygame manual!

Drawing a line, circle or rectangle takes different arguments. They all have screen and color in common, but then they diverge. A line takes 2 coordinate points for start and end of line. A circle takes one coordinate point for the center and a radius value. The rectangle takes diagonal corner coordinates.

You also need to call
pygame.display.flip()
for anything to show

... and last not least an event loop at the end.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python MySQL problem
Next Thread in Python Forum Timeline: how do I connect to MySQL server which is firewall protected?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC