943,878 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 514
  • Python RSS
Mar 22nd, 2008
0

Explanation please!

Expand Post »
During my quest to learn pygame I came across a tutorial that was really helping me through. However I came across a piece of code that has confused me. Being that this tutorial has a very vague explanation on each of the snippets, I need someone to explain this to me.

Python Syntax (Toggle Plain Text)
  1. #! /usr/bin/env python
  2.  
  3. import pygame
  4.  
  5. y = 0
  6. dir = 1
  7. running = 1
  8. width = 800
  9. height = 600
  10. screen = pygame.display.set_mode((width, height))
  11. linecolor = 255, 0, 0
  12. bgcolor = 0, 0, 0
  13.  
  14. while running:
  15. event = pygame.event.poll()
  16. if event.type == pygame.QUIT:
  17. running = 0
  18.  
  19. screen.fill(bgcolor)
  20. pygame.draw.line(screen, linecolor, (0, y), (width-1, y))
  21.  
  22. y += dir
  23. if y == 0 or y == height-1: dir *= -1
  24.  
  25. pygame.display.flip()

I can understand everything above screen.fill(). So pretty much I wanna know what all this is:
Python Syntax (Toggle Plain Text)
  1. pygame.draw.line(screen, linecolor, (0, y), (width-1, y))
whats width-1 do?
And this
Python Syntax (Toggle Plain Text)
  1. y += dir
  2. if y == 0 or y == height-1: dir *= -1
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
rysin is offline Offline
41 posts
since Jan 2008
Mar 22nd, 2008
0

Re: Explanation please!

There is of course an indent error in your code sample. I added some comments:
python Syntax (Toggle Plain Text)
  1. # move a line down and up the display window
  2.  
  3. import pygame
  4.  
  5. y = 0
  6. dir = 1
  7. running = True
  8. width = 800
  9. height = 600
  10. screen = pygame.display.set_mode((width, height))
  11. linecolor = 255, 0, 0 # red
  12. #bgcolor = 0, 0, 0 # black
  13. bgcolor = 255, 255, 255 # white
  14.  
  15. while running:
  16. event = pygame.event.poll()
  17. # quit when window corner x is clicked
  18. if event.type == pygame.QUIT:
  19. running = False
  20.  
  21. screen.fill(bgcolor)
  22. pygame.draw.line(screen, linecolor, (0, y), (width-1, y))
  23.  
  24. y += dir
  25. # reverse direction if line hits bottom or top
  26. if y == 0 or y == height-1:
  27. #dir *= -1
  28. dir = -dir
  29. # update display
  30. pygame.display.flip()
width -1 simply draws the line short one notch of hitting the right edge of the display.
Last edited by Ene Uran; Mar 22nd, 2008 at 2:53 pm. Reason: spelling
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005

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: Playing videos in python
Next Thread in Python Forum Timeline: A few new math symbols I dont understand.





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


Follow us on Twitter


© 2011 DaniWeb® LLC