944,029 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 840
  • Python RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 10th, 2009
1
Re: fileIO & pygame help
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 ...
Python Syntax (Toggle Plain Text)
  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 ...
Python Syntax (Toggle Plain Text)
  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 ...
Python Syntax (Toggle Plain Text)
  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 ...
Python Syntax (Toggle Plain Text)
  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; Nov 10th, 2009 at 11:05 am. Reason: template code
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
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. 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!
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
Quote ...
... and last not least an event loop at the end
lol
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
fallopiano is offline Offline
68 posts
since Jun 2009
Nov 10th, 2009
0
Re: fileIO & pygame help
ooops fixed thanks lol
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CurtisEClark is offline Offline
12 posts
since Sep 2009

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