| | |
Explanation please!
![]() |
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.
I can understand everything above screen.fill(). So pretty much I wanna know what all this is:
whats width-1 do?
And this
Python Syntax (Toggle Plain Text)
#! /usr/bin/env python import pygame y = 0 dir = 1 running = 1 width = 800 height = 600 screen = pygame.display.set_mode((width, height)) linecolor = 255, 0, 0 bgcolor = 0, 0, 0 while running: event = pygame.event.poll() if event.type == pygame.QUIT: running = 0 screen.fill(bgcolor) pygame.draw.line(screen, linecolor, (0, y), (width-1, y)) y += dir if y == 0 or y == height-1: dir *= -1 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)
pygame.draw.line(screen, linecolor, (0, y), (width-1, y))
And this
Python Syntax (Toggle Plain Text)
y += dir if y == 0 or y == height-1: dir *= -1
There is of course an indent error in your code sample. I added some comments:
width -1 simply draws the line short one notch of hitting the right edge of the display.
python Syntax (Toggle Plain Text)
# move a line down and up the display window import pygame y = 0 dir = 1 running = True width = 800 height = 600 screen = pygame.display.set_mode((width, height)) linecolor = 255, 0, 0 # red #bgcolor = 0, 0, 0 # black bgcolor = 255, 255, 255 # white while running: event = pygame.event.poll() # quit when window corner x is clicked if event.type == pygame.QUIT: running = False screen.fill(bgcolor) pygame.draw.line(screen, linecolor, (0, y), (width-1, y)) y += dir # reverse direction if line hits bottom or top if y == 0 or y == height-1: #dir *= -1 dir = -dir # update display pygame.display.flip()
Last edited by Ene Uran; Mar 22nd, 2008 at 1:53 pm. Reason: spelling
drink her pretty in burbank
![]() |
Similar Threads
- Need explanation for reported negative sum (C++)
- would like an explanation (Site Layout and Usability)
- Narue's Heap sort a further explanation? (C)
- Explanation of Process File (Java)
- Hosts file question about Gorilla's explanation (Viruses, Spyware and other Nasties)
- array declaration explanation needed (C)
Other Threads in the Python Forum
- Previous Thread: Playing videos in python
- Next Thread: A few new math symbols I dont understand.
Views: 485 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Python
application array bash beginner c++ c/c++ change character class client code command convert count create csv ctypes database dictionary django dll error event examples excel exe extensions fdlib file float format framework ftp function graphics gui homework image images import input library line linux list lists logging loop loops microcontroller mouse mysql mysqldb number numbers output parse parsing path port prime processing program programming py2exe pygame pygtk pyqt python random raw_input recursion recursive redirect remote scrolledtext server socket split ssh stdout string strings syntax table terminal text thread threading tkinter transparency tuple tutorial ubuntu unicode variable variables web windows wxpython






