1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Filename: A01_game_loop_and_events.py
# Author: James D. Miller; Gustavus Adolphus College.

import sys, os
import pygame

# PyGame Constants
from pygame.locals import *
from pygame.color import THECOLORS

# Initialize the Pygame environment.
pygame.init()

# Create a display surface to write to.
display_surface = pygame.display.set_mode((600,400))

# Instantiate a clock to help control the framerate.
myclock = pygame.time.Clock()

# Set the intial color of the whole pygame window.
display_surface.fill(THECOLORS["white"])

# Initialize some variables.
framerate_limit = 120
time_s = 0.0
key_e = "U"
key_f = "U"
user_done = False
mouse_button_UD = "U"

while not user_done:

    dt_s = float(myclock.tick( framerate_limit) * 1e-3)
    #myclock.tick(framerate_limit)
    
    #=====================================================
    # Get user input
    #=====================================================
    
    # loop through the list of events in the event queue.
    for event in pygame.event.get():
        
        # This main "if" structure checks the event type of each event.
        # Depending on the event type (QUIT, KEYDOWN, KEYUP, MOUSEBUTTONDOWN, or
        # MOUSEBUTTONUP), addition checks are made to identify the characteristics of the
        # event.
        if (event.type == pygame.QUIT): 
            user_done = True
        
        elif (event.type == pygame.KEYDOWN):
            if (event.key == K_ESCAPE):
                user_done = True
            elif (event.key==K_e):
                key_e = 'D'
            elif (event.key==K_f):
                key_f = 'D'
                
        elif (event.type == pygame.KEYUP):
            if (event.key==K_e):
                key_e = 'U'
            elif (event.key==K_f):
                key_f = 'U'
        
        elif (event.type == pygame.MOUSEBUTTONDOWN):
            mouse_button_UD = 'D'
            
            # The get_pressed method returns T/F values in a tuple.
            (button1, button2, button3) = pygame.mouse.get_pressed()
            
            if button1:
                mouse_button = 1
            elif button2:
                mouse_button = 2
            elif button3:
                mouse_button = 3
            else:
                mouse_button = 0
                    
        elif (event.type == pygame.MOUSEBUTTONUP):
            mouse_button_UD = 'U'

    # Get the cursor position: x,y. Return this as a tuple.
    mouse_xy = pygame.mouse.get_pos()
    
    #=====================================================
    # End of user input collection
    #=====================================================
    
    # Erase the screen if the "d" key is pressed. Do this by filling the entire
    # screen with grey color.
    if (key_e == 'D'):
        display_surface.fill(THECOLORS["grey"])
    
    # Determine the color for the circle based on if a mouse button is up (U) or down (D).
    if ((mouse_button_UD == 'D') and (mouse_button == 1)):
        circle_color = THECOLORS["yellow"]
    elif ((mouse_button_UD == 'D') and (mouse_button == 3)):
        circle_color = THECOLORS["red"]
    else:
        circle_color = THECOLORS["blue"]
    
    # Draw the circle
    pygame.draw.circle(display_surface, circle_color, mouse_xy, 10, 0)
    
    # Add the incremental time to our time variable.
    time_s += dt_s
    
    # Print to the command window.
    print time_s, dt_s, myclock.get_fps()
    
    # If the "f" key is up (not Down), update the entire display window. 
    if (key_f != 'D'):
        pygame.display.flip()