fileIO & pygame help

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2004
Posts: 4,062
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: 936
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
1
  #11
27 Days Ago
When you have a project like this, it is best to test each shape you want to draw before you combine it all. Let's start out with a pygame template that you can use for each shape ...
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5. w = 640
  6. h = 480
  7. screen = pygame.display.set_mode((w, h))
  8.  
  9. # set (r, g, b) color tuple
  10. color = (random.randrange(255), random.randrange(255),
  11. random.randrange(255))
  12.  
  13.  
  14. # ... your shape code goes here ...
  15.  
  16.  
  17. # update display
  18. pygame.display.flip()
  19.  
  20. # event loop ...
  21. running = True
  22. while running:
  23. for event in pygame.event.get():
  24. # quit when window corner x is clicked
  25. if event.type == pygame.QUIT:
  26. running = False
Now let's test the rectangle shape ...
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5. w = 640
  6. h = 480
  7. screen = pygame.display.set_mode((w, h))
  8.  
  9. # set (r, g, b) color tuple
  10. color = (random.randrange(255), random.randrange(255),
  11. random.randrange(255))
  12.  
  13.  
  14. # set rect corner coordiates to draw a rectangle
  15. # (x1, y1, x2, y2) upper left and lower right corner coordinates
  16. rect = (random.randrange(w), random.randrange(h),
  17. random.randrange(w), random.randrange(h))
  18. # pygame.draw.rect(Surface, color, Rect, width=0)
  19. # if width=0 (or not given) the rectangle is filled with color
  20. pygame.draw.rect(screen, color, rect)
  21.  
  22.  
  23. # update display
  24. pygame.display.flip()
  25.  
  26. # event loop ...
  27. running = True
  28. while running:
  29. for event in pygame.event.get():
  30. # quit when window corner x is clicked
  31. if event.type == pygame.QUIT:
  32. running = False
... next test the line ...
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5. w = 640
  6. h = 480
  7. screen = pygame.display.set_mode((w, h))
  8.  
  9. # set (r, g, b) color tuple
  10. color = (random.randrange(255), random.randrange(255),
  11. random.randrange(255))
  12.  
  13.  
  14. # set start and end position of line
  15. start_pos = random.randrange(w), random.randrange(h)
  16. end_pos = random.randrange(w), random.randrange(h)
  17. # pygame.draw.line(Surface, color, start_pos, end_pos, width=1)
  18. # width is thickness of line (default is 1)
  19. pygame.draw.line(screen, color, start_pos, end_pos)
  20.  
  21.  
  22. # update display
  23. pygame.display.flip()
  24.  
  25. # event loop ...
  26. running = True
  27. while running:
  28. for event in pygame.event.get():
  29. # quit when window corner x is clicked
  30. if event.type == pygame.QUIT:
  31. running = False
... lastly test the circle ...
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5. w = 640
  6. h = 480
  7. screen = pygame.display.set_mode((w, h))
  8.  
  9. # set (r, g, b) color tuple
  10. color = (random.randrange(255), random.randrange(255),
  11. random.randrange(255))
  12.  
  13.  
  14. # set center_pos for circle
  15. center_pos = random.randrange(w), random.randrange(h)
  16. # pygame.draw.circle(Surface, color, center_pos, radius, width=0)
  17. # if width=0 (or not given) the circle is filled with color
  18. radius = 50
  19. pygame.draw.circle(screen, color, center_pos, radius)
  20.  
  21.  
  22. # update display
  23. pygame.display.flip()
  24.  
  25. # event loop ...
  26. running = True
  27. while running:
  28. for event in pygame.event.get():
  29. # quit when window corner x is clicked
  30. if event.type == pygame.QUIT:
  31. running = False
Last edited by vegaseat; 27 Days Ago at 11:05 am. Reason: template code
May 'the Google' be with you!
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
  #12
27 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. lines = len(z)
  12. str(lines)
  13. l = str(('The file shapes.txt has',lines,'lines.'))
  14. output = open('output.txt','w')
  15. output.write(l)
  16. output.close()
  17.  
  18. screen = pygame.display.set_mode((w, h))
  19. if "circle" in z:
  20. pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
  21. elif "line" in z:
  22. pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)),(random.randrange(w), random.randrange(h)), (random.randrange(w), random.randrange(h)))
  23. elif "rect" in z:
  24. pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)),(random.randrange(w), random.randrange(h), random.randrange(w), random.randrange(h)))
  25.  
  26. pygame.display.flip()
  27.  
  28.  
  29. running = True
  30. while running:
  31. for event in pygame.event.get():
  32. if event.type == pygame.QUIT:
  33. exit()

Solved thanks to everyone for the help!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 49
Reputation: fallopiano is an unknown quantity at this point 
Solved Threads: 4
fallopiano's Avatar
fallopiano fallopiano is offline Offline
Light Poster
 
0
  #13
27 Days Ago
... and last not least an event loop at the end
lol
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
  #14
27 Days Ago
ooops fixed thanks lol
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