Commit 4e7dab23d41f9c16d33c4024ae4c22dd56b1af8b

Authored by John Markus Bjørndalen
1 parent a115f5f7

lecture notes and source code for today's lecture

lectures/event-driven-code/Chapter03/README.md 0 → 100644
  1 +Images from the PyGame book (see course resources).
  2 +
... ...
lectures/event-driven-code/Chapter03/fugu.png 0 → 100644

30.4 KB

lectures/event-driven-code/Chapter03/sushiplate.jpg 0 → 100644

47 KB

lectures/event-driven-code/ball.png 0 → 100644

857 Bytes

lectures/event-driven-code/dipatcher.py 0 → 100644
  1 +#!/usr/bin/env python
  2 +
  3 +"""Dispatcher example.
  4 +
  5 +* Demonstrates how to make a simple event dispatcher and how to use it.
  6 +
  7 +* Shows some alternatives for creating event handlers that can be registered with the
  8 + dispatcher.
  9 +
  10 +* Shows how the dispatcher can be used with PyGame events and how to add new event types
  11 + to PyGame.
  12 +
  13 +"""
  14 +
  15 +
  16 +import pygame
  17 +import time
  18 +import math
  19 +from pygame.locals import *
  20 +import random
  21 +
  22 +
  23 +pygame.init()
  24 +
  25 +screen_res_x = 640
  26 +screen_res_y = 480
  27 +
  28 +screen = pygame.display.set_mode((screen_res_x, screen_res_y), 0, 32)
  29 +
  30 +# ------------------------------------------------------------
  31 +#
  32 +# Some objects to display
  33 +#
  34 +# ------------------------------------------------------------
  35 +
  36 +bgimage = pygame.image.load("Chapter03/sushiplate.jpg")
  37 +background = bgimage.convert()
  38 +fish = pygame.image.load("Chapter03/fugu.png").convert_alpha()
  39 +
  40 +class Ball:
  41 + def __init__(self):
  42 + self.img = pygame.image.load("ball.png").convert_alpha()
  43 + self.x = random.random() * 640
  44 + self.y = random.random() * 480
  45 +
  46 + self.dir_x = 1
  47 + self.dir_y = 1
  48 + self.speed = 120 # hvor mange pixels per sekund
  49 +
  50 + def move(self, time_passed_seconds):
  51 + dist = time_passed_seconds * self.speed
  52 +
  53 + self.x = self.x + dist * self.dir_x
  54 + self.y = self.y + dist * self.dir_y
  55 +
  56 + if self.x < 0:
  57 + self.dir_x = 1
  58 + if self.y < 0:
  59 + self.dir_y = 1
  60 + if self.x > screen.get_width():
  61 + self.dir_x = -1
  62 + if self.y > screen.get_height():
  63 + self.dir_y = -1
  64 +
  65 + def draw(self):
  66 + screen.blit(self.img, (self.x, self.y))
  67 +
  68 +class SuperBall(Ball):
  69 + def __init__(self):
  70 + Ball.__init__(self)
  71 + self.speed = 50 + random.random() * 120
  72 + self.img = pygame.image.load("spider.png").convert_alpha()
  73 +
  74 + def change_speed(self, new_speed):
  75 + self.speed = new_speed
  76 + self.speed = min(50, self.speed)
  77 + self.speed = max(500, self.speed)
  78 +
  79 +
  80 + def move(self, time_passed_seconds):
  81 + #self.change_speed(random.random() * 500)
  82 + self.change_speed(math.sin(time.time()) * 500)
  83 + Ball.move(self, time_passed_seconds)
  84 +
  85 +
  86 +# ------------------------------------------------------------
  87 +#
  88 +# Dispatch/event handler code
  89 +#
  90 +# ------------------------------------------------------------
  91 +
  92 +class Dispatcher:
  93 + def __init__(self):
  94 + self.__handlers = {}
  95 +
  96 + def register_handler(self, etype, handler):
  97 + """Register a callable object (function, method or object with __call__ method) that
  98 + will be called for 'etype' events.
  99 + The event handler will receive the event as a parameter.
  100 + NB: This simple code assumes only a single handler
  101 + for each type.
  102 + """
  103 + self.__handlers[etype] = handler
  104 + def dispatch(self, event):
  105 + if event.type in self.__handlers:
  106 + self.__handlers[event.type](event)
  107 + else:
  108 + print("Unknown event type", event.type, pygame.event.event_name(event.type), event)
  109 +
  110 +def mouse_motion_handler(event):
  111 + print("Mouse move:", event.pos, event.rel, event)
  112 + pass
  113 +
  114 +def timer_event_handler(event):
  115 + print("Timer ping", event)
  116 +
  117 +def quit_handler(event):
  118 + global finished
  119 + print("Finished")
  120 + finished = True
  121 +
  122 +FOOEVENT = USEREVENT+1
  123 +dispatcher = Dispatcher()
  124 +dispatcher.register_handler(MOUSEMOTION, mouse_motion_handler)
  125 +dispatcher.register_handler(FOOEVENT, timer_event_handler)
  126 +dispatcher.register_handler(QUIT, quit_handler)
  127 +
  128 +# every other second, we let a timer fire a FOOEVENT
  129 +pygame.time.set_timer(FOOEVENT, 2000)
  130 +
  131 +# Make the program quit after 10 seconds by having a timer post a QUIT event
  132 +# after 10 seconds.
  133 +# pygame.time.set_timer(QUIT, 10000)
  134 +
  135 +
  136 +class FooHandler:
  137 + """
  138 + Example handler object that can hold state.
  139 + There are two ways of calling this handler from the dispatcher:
  140 + 1) pass the object itself and the dispatcher will invoke the
  141 + __call__ method when trying to use the object as a function.
  142 + 2) pass the handler method from the object to the dispatcher.
  143 + Python will keep the binding between the method and the object(self),
  144 + so the dispatcher can call the method as if it was a function.
  145 + """
  146 + def __init__(self, var = 42):
  147 + self.var = var
  148 +
  149 + def handler(self, event):
  150 + """Event handler option 1.
  151 + Pass this method to the dispatcher when registering the event type."""
  152 + print("foohandler - var =", self.var, "event=", event)
  153 +
  154 + def __call__(self, event):
  155 + """Event handler option 2.
  156 + Pass the object itself to the dispatcher when registering the event type.
  157 + """
  158 + print("foohandler - var =", self.var, "event=", event)
  159 +
  160 +foohandler = FooHandler()
  161 +#dispatcher.register_handler(FOOEVENT, foohandler.handler)
  162 +dispatcher.register_handler(FOOEVENT, foohandler)
  163 +
  164 +
  165 +
  166 +# ------------------------------------------------------------
  167 +#
  168 +# Main loop
  169 +#
  170 +# ------------------------------------------------------------
  171 +
  172 +clock = pygame.time.Clock()
  173 +
  174 +objects = [
  175 + Ball(),
  176 + Ball(),
  177 + SuperBall()
  178 + ]
  179 +
  180 +finished = False
  181 +while not finished:
  182 + foohandler.var += 1
  183 + for event in pygame.event.get():
  184 + dispatcher.dispatch(event)
  185 +
  186 + time_passed = clock.tick(1000)
  187 + time_passed_seconds = time_passed / 1000.0
  188 +
  189 + for obj in objects:
  190 + obj.move(time_passed_seconds)
  191 +
  192 + screen.blit(background, (0,0))
  193 +
  194 + for obj in objects:
  195 + obj.draw()
  196 +
  197 + # draw "cursor" object
  198 + x,y = pygame.mouse.get_pos()
  199 + fish_x = x - fish.get_width() / 2
  200 + fish_y = y - fish.get_height() / 2
  201 + screen.blit(fish, (fish_x, fish_y))
  202 +
  203 + pygame.display.update()
  204 +
  205 +pygame.quit()
... ...
lectures/event-driven-code/spider.png 0 → 100644

7.63 KB

lectures/event-driven-programming.pdf 0 → 100644
No preview for this file type