fileIO & pygame help

Thread Solved

Join Date: Sep 2009
Posts: 11
Reputation: CurtisEClark is an unknown quantity at this point 
Solved Threads: 0
CurtisEClark CurtisEClark is offline Offline
Newbie Poster

fileIO & pygame help

 
0
  #1
22 Days Ago
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

  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, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 45
Reputation: fallopiano is an unknown quantity at this point 
Solved Threads: 4
fallopiano's Avatar
fallopiano fallopiano is offline Offline
Light Poster
 
0
  #2
22 Days Ago
say if n was circle, this is what python interprets:

  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:

  1. if z == 'circle':
  2. pygame.draw.circle(arguements_go_here)
  3. elif z == 'line':
  4. # code for drawing line here
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: CurtisEClark is an unknown quantity at this point 
Solved Threads: 0
CurtisEClark CurtisEClark is offline Offline
Newbie Poster
 
0
  #3
22 Days Ago
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 45
Reputation: fallopiano is an unknown quantity at this point 
Solved Threads: 4
fallopiano's Avatar
fallopiano fallopiano is offline Offline
Light Poster
 
0
  #4
21 Days Ago
try

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 45
Reputation: fallopiano is an unknown quantity at this point 
Solved Threads: 4
fallopiano's Avatar
fallopiano fallopiano is offline Offline
Light Poster
 
0
  #5
21 Days Ago
also, if that fails, try reinstalling your version of pygame
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 79
Reputation: pythopian is an unknown quantity at this point 
Solved Threads: 21
pythopian pythopian is offline Offline
Junior Poster in Training
 
0
  #6
21 Days Ago
Quick hint: Instead of the condition cascade
  1. if z == "circle": ... elif ...
consider a more pythonically succinct approach:
  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; 21 Days Ago at 11:43 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: CurtisEClark is an unknown quantity at this point 
Solved Threads: 0
CurtisEClark CurtisEClark is offline Offline
Newbie Poster
 
0
  #7
21 Days Ago
ive reinstalled python and pygame ive added the
  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; 21 Days Ago at 3:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: CurtisEClark is an unknown quantity at this point 
Solved Threads: 0
CurtisEClark CurtisEClark is offline Offline
Newbie Poster
 
0
  #8
20 Days Ago
  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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: CurtisEClark is an unknown quantity at this point 
Solved Threads: 0
CurtisEClark CurtisEClark is offline Offline
Newbie Poster
 
0
  #9
20 Days Ago
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,019
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #10
20 Days Ago
Originally Posted by CurtisEClark View Post
  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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC