Explanation please!

Reply

Join Date: Jan 2008
Posts: 41
Reputation: rysin is an unknown quantity at this point 
Solved Threads: 0
rysin's Avatar
rysin rysin is offline Offline
Light Poster

Explanation please!

 
0
  #1
Mar 22nd, 2008
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.

  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:
  1. pygame.draw.line(screen, linecolor, (0, y), (width-1, y))
whats width-1 do?
And this
  1. y += dir
  2. if y == 0 or y == height-1: dir *= -1
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,593
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 187
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Explanation please!

 
0
  #2
Mar 22nd, 2008
There is of course an indent error in your code sample. I added some comments:
  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 1:53 pm. Reason: spelling
drink her pretty in burbank
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 485 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC