## Everything following a '#' is a 'comment'. Comments are ## explanations for the programmer or other human beings looking at ## the code. The Python interpreter ignores them. All lines that do ## not start with a '#', are instructions for the Python interpreter. ## Load the pygame library, so that we can use the functionality it ## provides. import pygame ## Define a function called run_game. ## This function defines our whole game, which is a very simple ## game. All it does is display a picture. def run_game(): ## Initialize the pygame submodules. pygame.init() ## Open the pygame window with the specified width and height. We ## call our pygame window 'my_win'. width = 640 height = 480 my_win = pygame.display.set_mode((width,height)) ## This is the "game loop". We will talk much more about the game ## loop next week. For now: the game loop keeps the pygame window ## on the screen until you click the 'X' in the upper right hand ## corner. keepGoing = True while (keepGoing): ## Here we check whether somebody clicked the 'X' in the upper ## right hand corner of the pygame window. for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False ###### START: This is the beginning of the part that you need to ###### look at for this first lab exercise. ## Make the background black. ## ## We specify colors using the expression ## 'pygame.color.Color()', where '' ## needs to be replaced by an actual color name in ## quotes. (Note 1: The quotes are important. Note 2: I will ## always use angular brackets for places where you need to ## substitute in an expression.) For more supported color ## names, look at the table of X11 colors on this page: ## http://en.wikipedia.org/wiki/Web_colors. (Note: for pygame ## all letters need to be lower case.) my_win.fill(pygame.color.Color("black")) ## Draw two rectangles. ## ## We specify where we want to draw it by giving the name of ## our pygame window - 'my_win'. We specify a color (as ## described above). We specify where in the window to put ## the rectangles. This is done by giving a tuple of four ## numbers. The third number specifies the width of the ## rectangle and the fourth number specifies the height. The ## first two numbers specify the x-coordinate and the ## y-coordinate of the upper left hand corner of the ## rectangle. The origin of the coordinate system is in the ## upper left hand corner of the pygame window. x values ## increase going from the left to right; y values increase ## going from top to bottom. pygame.draw.rect(my_win, pygame.color.Color('white'), (100,100,50,50)) pygame.draw.rect(my_win, pygame.color.Color('darkmagenta'), (140,140,70,40)) ## Draw a circle. ## ## We specify the window, the color, the position (x and y ## coordinates) of the center, and the radius. pygame.draw.circle(my_win, pygame.color.Color('navy'), (500,250),75) ## Draw a line. ## ## We specify the window, the color, the starting point and ## the ending point of the line. pygame.draw.line(my_win,pygame.color.Color('deeppink'),(50,320),(550,200)) ## Draw a polygon. ## ## We specify the window, the color, and a list of points - ## each points represents one corner. pygame.draw.polygon(my_win,pygame.color.Color('darkgreen'), [(150,300),(150+80,300),(150+40,300+120)]) ###### END: This is the end of the part that you need to look at ###### for this first lab exercise. ## Show the pygame window. pygame.display.update() ## The game loop ends here. ## This closes your pygame window after we have left the game ## loop, i.e., after somebody closed the window. pygame.quit() ## Call the function run_game. run_game()