This usually includes initializing the api in this

This preview shows page 12 - 22 out of 50 pages.

This usually includes Initializing the API (in this case, Pygame) Setting some display mode With Pygame this will be done via: pygame.init() surface = pygame.display.set_mode((width, height)) Where width and height are the integer dimensions of the window. Pass a a tuple Width and height are given in pixels What is returned is the “surface” that we’ll draw on In many graphics applications we refer to this as the frame buffer 12 4/22/2019
Game Loop Most graphics APIs are event-driven That is, they have an infinite loop running, and as events occur, they are added to an event queue to be served. Common events include: Redraw Mouse click Mouse move Keyboard click Timer 13 4/22/2019
Game Loop So at the very least we need to create an infinite loop. And at each iteration of the loop, process all the events in the event queue, and then ask Pygame to update the display window while(True): for event in pygame.event.get(): pass pygame.display.update() But as is, our program will never quit! To quit the program, we’ll allow the user to either Attempt to close the window by clicking on the close-window button Type the ‘q’ key 14 4/22/2019
Events Every Event object has a type which tells us what kind of event the object represents. In the image below, the left column lists the different types, all of which are constants accessible through pygame.<type> For instance, we can see if an event is a QUIT type by: if event.type == pygame.QUIT : 15 4/22/2019
Events The right column in the image lists attributes of a particular type of event that are accessible through the __dict__ dictionary attribute. For instance, if I want to see if an event is a KEYDOWN event and the key that is pressed is the ‘q’ key we can do: if (event.type == pygame.KEYDOWN) and (event.__dict__[‘key’] == pygame.K_q): 16 4/22/2019
Game Loop with events Ok so now we can at least quit our application while(True): for event in pygame.event.get(): if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.__dict__['key'] == pygame.K_q): pygame.quit() exit() pygame.display.update() 17 4/22/2019
Keyboard Events From the previous example, it should be relatively clear how to check the key’s code using the Pygame constants like K_q For keyboard events, these codes each start with K_ followed by the key Here is a reference of the various keys: 18 4/22/2019
Let’s see our full program import pygame #initialization pygame.init() surface = pygame.display.set_mode((400, 300)) pygame.display.set_caption('Hello World!’) #give window a title #main game loop while True: for event in pygame.event.get(): if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.__dict__['key'] == pygame.K_q): pygame.quit() exit() pygame.display.update() 19 4/22/2019
Let’s see our full program To exit we need to either press q or close the window 20 4/22/2019
Mouse Events The other important events we may want to respond to are mouse events.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture