├── images ├── flower.jpg ├── flowers.png └── dark-forest.jpg ├── hands on (gstreamer) ├── 921166201 │ └── exercise.py ├── 922120811 │ └── exercise.py ├── 922122800 │ └── exercise.py ├── 922164112 │ └── exercise.py ├── 931104510 │ └── exercise.py ├── 931108009 │ └── exercise.py ├── 931112307 │ └── exercise.py ├── 931118010 │ └── exercise.py ├── 931122712 │ └── exercise.py ├── 931129504 │ └── exercise.py ├── 931146402 │ └── exercise.py ├── 932068002 │ └── exercise.py ├── 941149002 │ └── exercise.py └── 941155403 │ └── exercise.py ├── hands-on exercise ├── 921166201 │ └── exercise.py ├── 922120811 │ └── exercise.py ├── 922122800 │ └── exercise.py ├── 922164112 │ └── exercise.py ├── 931104510 │ └── exercise.py ├── 931108009 │ └── exercise.py ├── 931112307 │ └── exercise.py ├── 931118010 │ └── exercise.py ├── 931122712 │ └── exercise.py ├── 931129504 │ └── exercise.py ├── 931146402 │ └── exercise.py ├── 932068002 │ └── exercise.py ├── 941149002 │ └── exercise.py └── 941155403 │ └── exercise.py └── README.md /images/flower.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sarbabi/multimedia_course/HEAD/images/flower.jpg -------------------------------------------------------------------------------- /images/flowers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sarbabi/multimedia_course/HEAD/images/flowers.png -------------------------------------------------------------------------------- /images/dark-forest.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sarbabi/multimedia_course/HEAD/images/dark-forest.jpg -------------------------------------------------------------------------------- /hands on (gstreamer)/931104510/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931104510 2 | #exercise: Time for action – recording (page 157) 3 | -------------------------------------------------------------------------------- /hands on (gstreamer)/931108009/exercise.py: -------------------------------------------------------------------------------- 1 | 2 | #exercise: Time for action – audio file format converter (page 142) 3 | -------------------------------------------------------------------------------- /hands on (gstreamer)/931118010/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931118010 2 | #exercise: Time for action – recording (page 133) 3 | -------------------------------------------------------------------------------- /hands on (gstreamer)/931129504/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931129504 2 | #exercise: Time for action – recording (page 157) 3 | -------------------------------------------------------------------------------- /hands on (gstreamer)/931146402/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931146402 2 | #exercise: Time for action – MP3 cutter! (page 152) 3 | -------------------------------------------------------------------------------- /hands on (gstreamer)/921166201/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 921166201 2 | #exercise: Time for action – recording (page 157) 3 | 4 | -------------------------------------------------------------------------------- /hands on (gstreamer)/922122800/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 922122800 2 | #exercise: Time for action – playing an audio: method 1 (page 133) 3 | -------------------------------------------------------------------------------- /hands on (gstreamer)/922164112/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 922164112 2 | #exercise: Time for action – recording (page 157) 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /hands on (gstreamer)/931112307/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931112307 2 | #exercise: Time for action – playing an audio: method 2 (page 133) 3 | -------------------------------------------------------------------------------- /hands on (gstreamer)/931122712/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931122712 2 | #exercise: Time for action – audio file format converter (page 142) 3 | -------------------------------------------------------------------------------- /hands on (gstreamer)/932068002/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 932068002 2 | #exercise: Time for action – playing an audio: method 2 (page 138) 3 | -------------------------------------------------------------------------------- /hands on (gstreamer)/941149002/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 941149002 2 | #exercise: Time for action – playing an audio: method 1 (page 133) 3 | -------------------------------------------------------------------------------- /hands on (gstreamer)/941155403/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 941155403 2 | #exercise: Time for action – playing an audio: method 1 (page 133) 3 | -------------------------------------------------------------------------------- /hands-on exercise/922120811/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 922120811 2 | #exercise: Time for action – bouncing ball animation (page 102) 3 | 4 | -------------------------------------------------------------------------------- /hands on (gstreamer)/922120811/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 922120811 2 | #exercise: Time for action – playing an audio: method 1 (page 133) 3 | 4 | -------------------------------------------------------------------------------- /hands-on exercise/922164112/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 922164112 2 | #exercise: Time for action – bouncing ball animation (page 102) 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # multimedia_course 2 | 3 | In this repository learning material for the "Multimeida in Python" course will be updated in a rolling manner. 4 | -------------------------------------------------------------------------------- /hands-on exercise/931129504/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931129504 2 | #exercise: Time for action – viewing an existing animation (page 98) 3 | 4 | import pyglet 5 | animation = pyglet.image.load_animation("C:\\Images\\SimpleAnimation.gif") 6 | animSprite = pyglet.sprite.Sprite(animation) 7 | w = animSprite.width 8 | h = animSprite.height 9 | win = pyglet.window.Window(width=w, height=h) 10 | r, g, b, alpha = 0.5, 0.5, 0.8, 1. 11 | pyglet.gl.glClearColor(r, g, b, alpha) 12 | 13 | @win.event 14 | def on_draw(): 15 | win.clear() 16 | animSprite.draw() 17 | 18 | 19 | pyglet.app.run() 20 | -------------------------------------------------------------------------------- /hands-on exercise/931104510/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931104510 2 | #exercise: Time for action – viewing an existing animation (page 98) 3 | 4 | import pyglet 5 | animation = pyglet.image.load_animation("C:\\Images\\SimpleAnimation.gif") 6 | animSprite = pyglet.sprite.Sprite(animation) 7 | w = animSprite.width 8 | h = animSprite.height 9 | win = pyglet.window.Window(width=w, height=h) 10 | r, g, b, alpha = 0.5, 0.5, 0.8, 1. 11 | pyglet.gl.glClearColor(r, g, b, alpha) 12 | 13 | @win.event 14 | def on_draw(): 15 | win.clear() 16 | animSprite.draw() 17 | 18 | 19 | pyglet.app.run() 20 | 21 | -------------------------------------------------------------------------------- /hands-on exercise/922122800/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 922122800 2 | #exercise: Time for action – animation using a sequence of images (page 100) 3 | 4 | import pyglet 5 | 6 | #image ha ro adressdahe karde va dar yek moteghayer rekhte ba in sorat ke dar system man dar draives c ye poshe data sakhte shodae va dar poshe data poshe image gozashtam va image ha ro tosh gozashtam.shoma adrass on directore ke axha ro gozashtin badid. 7 | 8 | image_frames = ("C:\\data\\Images\\clock1.png", 9 | "C:\\data\\Images\\clock2.png", 10 | "C:\\data\\Images\\clock3.png") 11 | 12 | #axha mian lod mishan ya be abarate liste az tasavir ro mesaze 13 | images = map(lambda img: pyglet.image.load(img), image_frames) 14 | 15 | #in khat meyad animation ro mesaze be in sorart ke tasavir ke address dahe kardem dar faseleh 0.33 jabeja meshan va donbaley az tasaveyr be sorat animation sakhtee meshe. 16 | 17 | animation = pyglet.image.Animation.from_image_sequence(images, 0.33) 18 | 19 | #in khat yek nemoneh az animation ro sakhte 20 | animSprite = pyglet.sprite.Sprite(animation) 21 | 22 | #tanzimat tool & arze 23 | w = animSprite.width 24 | h = animSprite.height 25 | win = pyglet.window.Window(width=w, height=h) 26 | 27 | # tanzimat range paszamineh be range safid 28 | pyglet.gl.glClearColor(1, 1, 1, 1) 29 | 30 | 31 | #arsebare karde ke yak dayrektor ast va be tagher IPA komk mikonad 32 | @win.event 33 | 34 | #metode kashedan ro sada zadeh 35 | def on_draw(): 36 | win.clear() 37 | #animation ro namaish mideh 38 | animSprite.draw() 39 | 40 | 41 | -------------------------------------------------------------------------------- /hands-on exercise/931118010/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931118010 2 | #exercise: Time for action – animation using a sequence of images (page 100) 3 | 4 | import pyglet 5 | 6 | #image ha ro adressdahe karde va dar yek moteghayer rekhte ba in sorat ke dar system man dar draives c ye poshe data sakhte shodae va dar poshe data poshe image gozashtam va image ha ro tosh gozashtam.shoma adrass on directore ke axha ro gozashtin badid. 7 | 8 | image_frames = ("C:\\data\\Images\\clock1.png", 9 | "C:\\data\\Images\\clock2.png", 10 | "C:\\data\\Images\\clock3.png") 11 | 12 | #axha mian lod mishan ya be abarate liste az tasavir ro mesaze 13 | images = map(lambda img: pyglet.image.load(img), image_frames) 14 | 15 | #in khat meyad animation ro mesaze be in sorart ke tasavir ke address dahe kardem dar faseleh 0.33 jabeja meshan va donbaley az tasaveyr be sorat animation sakhtee meshe. 16 | 17 | animation = pyglet.image.Animation.from_image_sequence(images, 0.33) 18 | 19 | #in khat yek nemoneh az animation ro sakhte 20 | animSprite = pyglet.sprite.Sprite(animation) 21 | 22 | #tanzimat tool & arze 23 | w = animSprite.width 24 | h = animSprite.height 25 | win = pyglet.window.Window(width=w, height=h) 26 | 27 | # tanzimat range paszamineh be range safid 28 | pyglet.gl.glClearColor(1, 1, 1, 1) 29 | 30 | 31 | #arsebare karde ke yak dayrektor ast va be tagher IPA komk mikonad 32 | @win.event 33 | 34 | #metode kashedan ro sada zadeh 35 | def on_draw(): 36 | win.clear() 37 | #animation ro namaish mideh 38 | animSprite.draw() 39 | 40 | 41 | -------------------------------------------------------------------------------- /hands-on exercise/931146402/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931146402 2 | #exercise: Time for action – bouncing ball animation (page 102) 3 | 4 | import pyglet 5 | import time 6 | 7 | class SingleImageAnimation(pyglet.window.Window): 8 | def __init__(self, width=600, height=600): 9 | pyglet.window.Window.__init__(self, 10 | width=width, 11 | height=height, 12 | resizable = True) 13 | self.drawableObjects = [] 14 | self.rising = False 15 | self.ballSprite = None 16 | self.createDrawableObjects() 17 | self.adjustWindowSize() 18 | def createDrawableObjects(self): 19 | """ 20 | Create sprite objects that will be drawn within the 21 | window. 22 | """ 23 | ball_img= pyglet.image.load('images/ball.png') 24 | ball_img.anchor_x = ball_img.width / 2 25 | ball_img.anchor_y = ball_img.height / 2 26 | self.ballSprite = pyglet.sprite.Sprite(ball_img) 27 | self.ballSprite.position = ( 28 | self.ballSprite.width + 100, 29 | self.ballSprite.height*2 - 50) 30 | self.drawableObjects.append(self.ballSprite) 31 | def adjustWindowSize(self): 32 | w = self.ballSprite.width * 3 33 | h = self.ballSprite.height * 3 34 | self.width = w 35 | self.height = h 36 | def moveObjects(self, t): 37 | if self.ballSprite.y - 100 < 0: 38 | self.rising = True 39 | elif self.ballSprite.y > self.ballSprite.height*2 - 50: 40 | self.rising = False 41 | 42 | if not self.rising: 43 | self.ballSprite.y -= 5 44 | self.ballSprite.rotation -= 6 45 | else: 46 | self.ballSprite.y += 5 47 | self.ballSprite.rotation += 5 48 | def on_draw(self): 49 | self.clear() 50 | for d in self.drawableObjects: 51 | d.draw() 52 | win = SingleImageAnimation() 53 | # Set window background color to gray. 54 | pyglet.gl.glClearColor(0.5, 0.5, 0.5, 1) 55 | 56 | pyglet.clock.schedule_interval(win.moveObjects, 1.0/20) 57 | 58 | pyglet.app.run() 59 | 60 | -------------------------------------------------------------------------------- /hands-on exercise/932068002/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 932068002 2 | #exercise: Time for action – raindrops animation (page 114) 3 | 4 | import pyglet 5 | import time 6 | 7 | # kelas RainDropsAnimation sazande kelas method pyglet.window.Window ra farakhani mikonad. 8 | class RainDropsAnimation(pyglet.window.Window): 9 | def __init__(self, width = None, height = None): 10 | pyglet.window.Window.__init__(self, 11 | width = width, 12 | height = height) 13 | self.drawableObjects = [] 14 | self.createDrawableObjects() 15 | 16 | # ashya ya spriteha ra braye rasm ghatarat baran dar darone panjere pyglet ejad mikonad. 17 | def createDrawableObjects(self): 18 | num_rows = 4 19 | num_columns = 1 20 | droplet = 'C:\\Users\\amir\\image\\droplet.png' 21 | # manategh daron file tasvir ra baraye ejad framehaye animition shakhsi estefade mikonad. 22 | animation = self.setup_animation(droplet, 23 | num_rows, 24 | num_columns) 25 | # sprite baraye sheye moteharek ghatarat baran ejad shode ast. 26 | self.dropletSprite = pyglet.sprite.Sprite(animation) 27 | self.dropletSprite.position = (0,0) 28 | 29 | # Add these sprites to the list of drawables 30 | self.drawableObjects.append(self.dropletSprite) 31 | 32 | # setup_animation sheye animition ra be vasileye manategh mokhtalef daron tasvir ejad mikonad. 33 | def setup_animation(self, img, num_rows, num_columns): 34 | # avalin nemone az tasvir dar khat zir ejad shode ast. 35 | base_image = pyglet.image.load(img) 36 | 37 | # yek nemone az ImageGrid ejad shode ast. 38 | animation_grid = pyglet.image.ImageGrid(base_image, 39 | num_rows, 40 | num_columns) 41 | image_frames = [] 42 | 43 | for i in range(num_rows*num_columns, 0, -1): 44 | # mantaghe khas tasvir ra migirad va sepas anra ejad mikonad. 45 | frame = animation_grid[i-1] 46 | animation_frame = pyglet.image.AnimationFrame(frame, 0.8) 47 | # ghalebhaye animation mothark dar list image_frames zakhira shoda ast. 48 | image_frames.append(animation_frame) 49 | # nemoney pyglet.image.Animation ba astafad az in list ejad meshavad. 50 | animation = pyglet.image.Animation(image_frames) 51 | return animation 52 | # The overridden API method on_draw which is called when the Window. needs to be re-drawn. 53 | def on_draw(self): 54 | self.clear() 55 | for d in self.drawableObjects: 56 | d.draw() 57 | 58 | # yek nemone panjere az panjere pyglet baraye namayesh animation ejad mikonad. 59 | win = RainDropsAnimation() 60 | 61 | # rang background panjere ra ba rang firozeye set mikonad. 62 | r, g, b, alpha = 0, 1, 1, 1 63 | pyglet.gl.glClearColor(r, g, b, alpha) 64 | 65 | pyglet.app.run() 66 | -------------------------------------------------------------------------------- /hands-on exercise/931112307/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931112307 2 | #exercise: Time for action – raindrops animation (page 114) 3 | import pyglet 4 | import time 5 | 6 | # kelas RainDropsAnimation sazande kelas method pyglet.window.Window ra farakhani mikonad. 7 | class RainDropsAnimation(pyglet.window.Window): 8 | def __init__(self, width = None, height = None): 9 | pyglet.window.Window.__init__(self, 10 | width = width, 11 | height = height) 12 | self.drawableObjects = [] 13 | self.createDrawableObjects() 14 | 15 | # ashya ya spriteha ra braye rasm ghatarat baran dar darone panjere pyglet ejad mikonad. 16 | def createDrawableObjects(self): 17 | num_rows = 4 18 | num_columns = 1 19 | droplet = 'C:\\Users\\amir\\image\\droplet.png' 20 | # manategh daron file tasvir ra baraye ejad framehaye animition shakhsi estefade mikonad. 21 | animation = self.setup_animation(droplet, 22 | num_rows, 23 | num_columns) 24 | # sprite baraye sheye moteharek ghatarat baran ejad shode ast. 25 | self.dropletSprite = pyglet.sprite.Sprite(animation) 26 | self.dropletSprite.position = (0,0) 27 | 28 | # Add these sprites to the list of drawables 29 | self.drawableObjects.append(self.dropletSprite) 30 | 31 | # setup_animation sheye animition ra be vasileye manategh mokhtalef daron tasvir ejad mikonad. 32 | def setup_animation(self, img, num_rows, num_columns): 33 | # avalin nemone az tasvir dar khat zir ejad shode ast. 34 | base_image = pyglet.image.load(img) 35 | 36 | # yek nemone az ImageGrid ejad shode ast. 37 | animation_grid = pyglet.image.ImageGrid(base_image, 38 | num_rows, 39 | num_columns) 40 | image_frames = [] 41 | 42 | for i in range(num_rows*num_columns, 0, -1): 43 | # mantaghe khas tasvir ra migirad va sepas anra ejad mikonad. 44 | frame = animation_grid[i-1] 45 | animation_frame = pyglet.image.AnimationFrame(frame, 0.8) 46 | # ghalebhaye animation mothark dar list image_frames zakhira shoda ast. 47 | image_frames.append(animation_frame) 48 | # nemoney pyglet.image.Animation ba astafad az in list ejad meshavad. 49 | animation = pyglet.image.Animation(image_frames) 50 | return animation 51 | # The overridden API method on_draw which is called when the Window. needs to be re-drawn. 52 | def on_draw(self): 53 | self.clear() 54 | for d in self.drawableObjects: 55 | d.draw() 56 | 57 | # yek nemone panjere az panjere pyglet baraye namayesh animation ejad mikonad. 58 | win = RainDropsAnimation() 59 | 60 | # rang background panjere ra ba rang firozeye set mikonad. 61 | r, g, b, alpha = 0, 1, 1, 1 62 | pyglet.gl.glClearColor(r, g, b, alpha) 63 | 64 | pyglet.app.run() 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /hands-on exercise/921166201/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 921166201 2 | #exercise: Time for action – bouncing ball animation (page 102) 3 | 4 | 5 | import pyglet 6 | import time 7 | from pyglet.image.codecs.png import PNGImageDecoder 8 | 9 | class SingleImageAnimation(pyglet.window.Window): 10 | def __init__(self, width = None, height = None): 11 | pyglet.window.Window.__init__(self, 12 | width = width, 13 | height = height, 14 | resizable = True) 15 | self.drawableObjects = [] 16 | self.rising = False 17 | self.ballSprite = None 18 | self.paused = False 19 | self.createDrawableObjects() 20 | self.adjustWindowSize() 21 | 22 | def createDrawableObjects(self): 23 | """ 24 | Create sprite objects that will be drawn within the 25 | window. 26 | """ 27 | ball_img= pyglet.image.load('images/ball.png', decoder=PNGImageDecoder()) 28 | ball_img.anchor_x = ball_img.width / 2 29 | ball_img.anchor_y = ball_img.height / 2 30 | 31 | self.ballSprite = pyglet.sprite.Sprite(ball_img) 32 | self.ballSprite.position = (self.ballSprite.width + 100, 33 | self.ballSprite.height*2 - 50) 34 | 35 | self.drawableObjects.append(self.ballSprite) 36 | 37 | def adjustWindowSize(self): 38 | """ 39 | Resizes the pyglet window. 40 | """ 41 | w = self.ballSprite.width*3 42 | h = self.ballSprite.height*3 43 | self.width = w 44 | self.height = h 45 | 46 | def on_draw(self): 47 | """ 48 | Overrides pyglet.window.Window.on_draw to draw the sprite. 49 | """ 50 | self.clear() 51 | for d in self.drawableObjects: 52 | d.draw() 53 | 54 | def on_key_press(self, key, modifiers): 55 | """ 56 | Overrides pyglet.window.Window.on_key_press to handle 57 | key presee event 58 | """ 59 | if key == pyglet.window.key.P and not self.paused: 60 | pyglet.clock.unschedule(self.moveObjects) 61 | self.paused = True 62 | elif key == pyglet.window.key.R and self.paused: 63 | pyglet.clock.schedule_interval(win.moveObjects, 1.0/20) 64 | self.paused = False 65 | 66 | def moveObjects(self, t): 67 | """ 68 | Move the image sprites / play the sound . 69 | This method is scheduled to be called every 1/N seconds using 70 | pyglet.clock.schedule_interval. 71 | """ 72 | if self.ballSprite.y - 100 < 0: 73 | self.rising = True 74 | elif self.ballSprite.y > self.ballSprite.height*2 - 50: 75 | self.rising = False 76 | 77 | if not self.rising: 78 | self.ballSprite.y -= 5 79 | self.ballSprite.rotation -= 6 80 | else: 81 | self.ballSprite.y += 5 82 | self.ballSprite.rotation += 5 83 | 84 | win = SingleImageAnimation() 85 | 86 | # Set window background color to white. 87 | r, g, b, alpha = 1, 1, 1, 1 88 | pyglet.gl.glClearColor(r, g, b, alpha) 89 | 90 | # Schedule the method win.moveObjects to be called every 91 | # 0.05 seconds. 92 | pyglet.clock.schedule_interval(win.moveObjects, 1.0/20) 93 | 94 | 95 | pyglet.app.run() 96 | 97 | 98 | -------------------------------------------------------------------------------- /hands-on exercise/941149002/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 941149002 2 | #exercise: Project: a simple bowling animation (page 108) 3 | 4 | 5 | from pyglet.gl import * 6 | import time 7 | 8 | 9 | class SingleImageAnimation(pyglet.window.Window): 10 | 11 | def __init__(self, width=None, height=None): 12 | """the init method 13 | define the variables and objects 14 | """ 15 | super().__init__(width=width, height=height, resizable=True) 16 | self.drawableObjects = [] 17 | self.rising = False 18 | self.ballSprite = None 19 | self.pinSprite = None 20 | self.createDrawableObjects() 21 | self.adjustWindowSize() 22 | self.paused = False 23 | self.pinHorizontal = False 24 | 25 | def createDrawableObjects(self): 26 | """ 27 | method for create the objects 28 | """ 29 | ball_img = pyglet.image.load('data/ball.png') 30 | ball_img.anchor_x = ball_img.width // 2 31 | ball_img.anchor_y = ball_img.height // 2 32 | pin_img = pyglet.image.load('data/pin.png') 33 | pin_img.anchor_x = (pin_img.width // 4) * 3 34 | pin_img.anchor_y = 0 35 | self.ballSprite = pyglet.sprite.Sprite(ball_img) 36 | self.ballSprite.position = (0 + 100, self.ballSprite.height) 37 | self.pinSprite = pyglet.sprite.Sprite(pin_img) 38 | self.pinSprite.position = (self.ballSprite.width * 2 + 200, self.ballSprite.height // 2) 39 | # Add these sprites to the list of drawables 40 | self.drawableObjects.append(self.pinSprite) 41 | self.drawableObjects.append(self.ballSprite) 42 | 43 | def adjustWindowSize(self): 44 | """ 45 | method for define the size of window 46 | """ 47 | w = self.ballSprite.width * 100 48 | h = self.ballSprite.height * 10 49 | self.width = w 50 | self.height = h 51 | 52 | def moveObjects(self, t): 53 | """ 54 | method for moving objects 55 | """ 56 | 57 | if self.pinHorizontal: 58 | self.ballSprite.x = 100 59 | 60 | if self.ballSprite.x + self.ballSprite.width / 2 < self.pinSprite.x - self.pinSprite.width / 2: 61 | if self.ballSprite.x == 100: 62 | time.sleep(1) 63 | self.pinSprite.rotation = 0 64 | self.pinHorizontal = False 65 | 66 | self.ballSprite.x += 5 67 | self.ballSprite.rotation += 5 68 | 69 | if self.ballSprite.x + self.ballSprite.width / 2 >= self.pinSprite.x - self.pinSprite.width / 2: 70 | self.pinSprite.rotation += 5 71 | if self.pinSprite.rotation == 90: 72 | self.pinHorizontal = True 73 | 74 | def on_key_press(self, key, modifiers): 75 | """ 76 | method for setting the keys 77 | """ 78 | if key == pyglet.window.key.P and not self.paused: 79 | pyglet.clock.unschedule(self.moveObjects) 80 | self.paused = True 81 | elif key == pyglet.window.key.R and self.paused: 82 | pyglet.clock.schedule_interval(self.moveObjects, 1.0 / 30) 83 | self.paused = False 84 | 85 | def on_draw(self): 86 | self.clear() 87 | for d in self.drawableObjects: 88 | d.draw() 89 | 90 | 91 | win = SingleImageAnimation() 92 | # Set window background color. 93 | pyglet.gl.glClearColor(0.5, 0.5, 0.9, 1) 94 | # Set the time to reply moveObjects method 95 | pyglet.clock.schedule_interval(win.moveObjects, 1.0 / 30) 96 | 97 | pyglet.app.run() 98 | -------------------------------------------------------------------------------- /hands-on exercise/941155403/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 941155403 2 | #exercise: Project: a simple bowling animation (page 108) 3 | 4 | 5 | from pyglet.gl import * 6 | import time 7 | 8 | 9 | class SingleImageAnimation(pyglet.window.Window): 10 | 11 | def __init__(self, width=None, height=None): 12 | """the init method 13 | define the variables and objects 14 | """ 15 | super().__init__(width=width, height=height, resizable=True) 16 | self.drawableObjects = [] 17 | self.rising = False 18 | self.ballSprite = None 19 | self.pinSprite = None 20 | self.createDrawableObjects() 21 | self.adjustWindowSize() 22 | self.paused = False 23 | self.pinHorizontal = False 24 | 25 | def createDrawableObjects(self): 26 | """ 27 | method for create the objects 28 | """ 29 | ball_img = pyglet.image.load('data/ball.png') 30 | ball_img.anchor_x = ball_img.width // 2 31 | ball_img.anchor_y = ball_img.height // 2 32 | pin_img = pyglet.image.load('data/pin.png') 33 | pin_img.anchor_x = (pin_img.width // 4) * 3 34 | pin_img.anchor_y = 0 35 | self.ballSprite = pyglet.sprite.Sprite(ball_img) 36 | self.ballSprite.position = (0 + 100, self.ballSprite.height) 37 | self.pinSprite = pyglet.sprite.Sprite(pin_img) 38 | self.pinSprite.position = (self.ballSprite.width * 2 + 200, self.ballSprite.height // 2) 39 | # Add these sprites to the list of drawables 40 | self.drawableObjects.append(self.pinSprite) 41 | self.drawableObjects.append(self.ballSprite) 42 | 43 | def adjustWindowSize(self): 44 | """ 45 | method for define the size of window 46 | """ 47 | w = self.ballSprite.width * 100 48 | h = self.ballSprite.height * 10 49 | self.width = w 50 | self.height = h 51 | 52 | def moveObjects(self, t): 53 | """ 54 | method for moving objects 55 | """ 56 | 57 | if self.pinHorizontal: 58 | self.ballSprite.x = 100 59 | 60 | if self.ballSprite.x + self.ballSprite.width / 2 < self.pinSprite.x - self.pinSprite.width / 2: 61 | if self.ballSprite.x == 100: 62 | time.sleep(1) 63 | self.pinSprite.rotation = 0 64 | self.pinHorizontal = False 65 | 66 | self.ballSprite.x += 5 67 | self.ballSprite.rotation += 5 68 | 69 | if self.ballSprite.x + self.ballSprite.width / 2 >= self.pinSprite.x - self.pinSprite.width / 2: 70 | self.pinSprite.rotation += 5 71 | if self.pinSprite.rotation == 90: 72 | self.pinHorizontal = True 73 | 74 | def on_key_press(self, key, modifiers): 75 | """ 76 | method for setting the keys 77 | """ 78 | if key == pyglet.window.key.P and not self.paused: 79 | pyglet.clock.unschedule(self.moveObjects) 80 | self.paused = True 81 | elif key == pyglet.window.key.R and self.paused: 82 | pyglet.clock.schedule_interval(self.moveObjects, 1.0 / 30) 83 | self.paused = False 84 | 85 | def on_draw(self): 86 | self.clear() 87 | for d in self.drawableObjects: 88 | d.draw() 89 | 90 | 91 | win = SingleImageAnimation() 92 | # Set window background color. 93 | pyglet.gl.glClearColor(0.5, 0.5, 0.9, 1) 94 | # Set the time to reply moveObjects method 95 | pyglet.clock.schedule_interval(win.moveObjects, 1.0 / 30) 96 | 97 | pyglet.app.run() 98 | -------------------------------------------------------------------------------- /hands-on exercise/931108009/exercise.py: -------------------------------------------------------------------------------- 1 | import pyglet 2 | import time 3 | 4 | class RainyDayAnimation(pyglet.window.Window): 5 | def __init__(self, width = None, height = None): 6 | pyglet.window.Window.__init__(self, 7 | width = width, 8 | height = height, 9 | resizable = True) 10 | self.drawableObjects = [] 11 | self.paused = False 12 | 13 | self.createDrawableObjects() 14 | self.adjustWindowSize() 15 | # Make sure to replace the following media path to 16 | # with an appropriate path on your computer. 17 | self.horn_sound =pyglet.media.load('data/horn.wav', streaming=False) 18 | 19 | def createDrawableObjects(self): 20 | """ 21 | Create the objects (sprites) for drawing within the 22 | pyglet Window. 23 | """ 24 | num_rows = 4 25 | num_columns = 1 26 | droplet = 'data/droplet.png' 27 | animation = self.setup_animation(droplet, 28 | num_rows, 29 | num_columns) 30 | 31 | self.dropletSprite = pyglet.sprite.Sprite(animation) 32 | self.dropletSprite.position = (0,200) 33 | 34 | cloud = pyglet.image.load('data/cloud.png') 35 | self.cloudSprite = pyglet.sprite.Sprite(cloud) 36 | self.cloudSprite.y = 100 37 | 38 | lightening = pyglet.image.load('data/lightening.png') 39 | self.lSprite = pyglet.sprite.Sprite(lightening) 40 | self.lSprite.y = 200 41 | 42 | car = pyglet.image.load('data/car.png') 43 | self.carSprite = pyglet.sprite.Sprite(car, -500, 0) 44 | 45 | 46 | # Add these sprites to the list of drawables 47 | self.drawableObjects.append(self.cloudSprite) 48 | self.drawableObjects.append(self.lSprite) 49 | self.drawableObjects.append(self.dropletSprite) 50 | self.drawableObjects.append(self.carSprite) 51 | 52 | def setup_animation(self, img, num_rows, num_columns): 53 | """ 54 | Create animation object using different regions of 55 | a single image. 56 | @param img: The image file path 57 | @type img: string 58 | @param num_rows: Number of rows in the image grid 59 | @type num_rows: int 60 | @param num_columns: Number of columns in the image grid 61 | @type num_columns: int 62 | """ 63 | base_image = pyglet.image.load(img) 64 | animation_grid = pyglet.image.ImageGrid(base_image, 65 | num_rows, 66 | num_columns) 67 | image_frames = [] 68 | 69 | for i in range(num_rows*num_columns, 0, -1): 70 | frame = animation_grid[i-1] 71 | animation_frame = pyglet.image.AnimationFrame(frame, 0.2) 72 | image_frames.append(animation_frame) 73 | 74 | animation = pyglet.image.Animation(image_frames) 75 | return animation 76 | 77 | def adjustWindowSize(self): 78 | """ 79 | Adjust the width and height of the Pyglet Window. 80 | """ 81 | w = 600 82 | h = 900 83 | self.width = w 84 | self.height = h 85 | 86 | def on_draw(self): 87 | """ 88 | The overridden API method on_draw which is called when the Window 89 | needs to be re-drawn. 90 | """ 91 | self.clear() 92 | for d in self.drawableObjects: 93 | d.draw() 94 | 95 | def on_key_press(self, key, modifiers): 96 | """ 97 | Captures key press events. 98 | Overrides pyglet.window.Window.on_key_press() 99 | """ 100 | if key == pyglet.window.key.P and not self.paused: 101 | pyglet.clock.unschedule(self.moveObjects) 102 | pyglet.clock.unschedule(self.show_lightening) 103 | self.paused = True 104 | elif key == pyglet.window.key.R and self.paused: 105 | pyglet.clock.schedule_interval(self.moveObjects, 1.0/20) 106 | pyglet.clock.schedule_interval(self.show_lightening, 1.0) 107 | self.paused = False 108 | 109 | def moveObjects(self, t): 110 | """ 111 | Move the image sprites / play the sound . 112 | This method is scheduled to be called every 1/N seconds using 113 | pyglet.clock.schedule_interval. 114 | 115 | """ 116 | if self.carSprite.x <= self.cloudSprite.width: 117 | self.carSprite.x += 10 118 | else: 119 | self.carSprite.x = -500 120 | self.horn_sound.play() 121 | 122 | def show_lightening(self, t): 123 | """ 124 | toggle the property value Sprite.visible , also move 125 | the sprite representing the lightening image 126 | This method is scheduled to be called every second using 127 | pyglet.clock.schedule_interval. 128 | """ 129 | if self.lSprite.visible: 130 | self.lSprite.visible = False 131 | else: 132 | if(self.lSprite.x == 100): 133 | self.lSprite.x += 200 134 | else: 135 | self.lSprite.x = 100 136 | 137 | self.lSprite.visible = True 138 | 139 | # Create a Pyglet Window instance to show the animation. 140 | win = RainyDayAnimation() 141 | 142 | # Set window background color to white. 143 | r, g, b, alpha = 0, 1, 1, 1 144 | pyglet.gl.glClearColor(r, g, b, alpha) 145 | 146 | # Schedule the method RainyDayAnimation.moveObjects to be called every 147 | # 0.05 seconds. 148 | pyglet.clock.schedule_interval(win.moveObjects, 1.0/20) 149 | 150 | # Show the lightening every 1 second 151 | pyglet.clock.schedule_interval(win.show_lightening, 1.0) 152 | 153 | pyglet.app.run()#author: 931108009 154 | #exercise: Project: drive on a rainy day!(page 117) 155 | 156 | -------------------------------------------------------------------------------- /hands-on exercise/931122712/exercise.py: -------------------------------------------------------------------------------- 1 | #author: 931122712 2 | #exercise: Project: drive on a rainy day!(page 117) 3 | 4 | #------------------------------------------------------------------- 5 | 6 | # First Import Required libraries 7 | # Notice : For Install pyglet library we must Use a VPN and run Below code in CMD 8 | # conda install -c conda-forge pyglet 9 | import pyglet 10 | import time 11 | 12 | class RainyDayAnimation(pyglet.window.Window): 13 | def __init__(self, width = None, height = None): 14 | pyglet.window.Window.__init__(self, 15 | width = width, 16 | height = height, 17 | resizable = True) 18 | self.drawableObjects = [] 19 | self.paused = False 20 | 21 | self.createDrawableObjects() 22 | self.adjustWindowSize() 23 | # Make sure to replace the following media path to 24 | # with an appropriate path on your computer. 25 | self.horn_sound =pyglet.media.load('data\Sound\HORN.wav', 26 | streaming=False) 27 | 28 | def createDrawableObjects(self): 29 | """ 30 | Create the objects (sprites) for drawing within the 31 | pyglet Window. 32 | """ 33 | num_rows = 4 34 | num_columns = 1 35 | droplet = 'data/Images/droplet.png' 36 | animation = self.setup_animation(droplet, 37 | num_rows, 38 | num_columns) 39 | 40 | self.dropletSprite = pyglet.sprite.Sprite(animation) 41 | self.dropletSprite.position = (0,200) 42 | 43 | cloud = pyglet.image.load('data/Images/cloud.png') 44 | self.cloudSprite = pyglet.sprite.Sprite(cloud) 45 | self.cloudSprite.y = 100 46 | 47 | lightening = pyglet.image.load('data/Images/lightening.png') 48 | self.lSprite = pyglet.sprite.Sprite(lightening) 49 | self.lSprite.y = 200 50 | 51 | car = pyglet.image.load('data/Images/car.png') 52 | self.carSprite = pyglet.sprite.Sprite(car, -500, 0) 53 | 54 | 55 | # Add these sprites to the list of drawables 56 | self.drawableObjects.append(self.cloudSprite) 57 | self.drawableObjects.append(self.lSprite) 58 | self.drawableObjects.append(self.dropletSprite) 59 | self.drawableObjects.append(self.carSprite) 60 | 61 | def setup_animation(self, img, num_rows, num_columns): 62 | """ 63 | Create animation object using different regions of 64 | a single image. 65 | @param img: The image file path 66 | @type img: string 67 | @param num_rows: Number of rows in the image grid 68 | @type num_rows: int 69 | @param num_columns: Number of columns in the image grid 70 | @type num_columns: int 71 | """ 72 | base_image = pyglet.image.load(img) 73 | animation_grid = pyglet.image.ImageGrid(base_image, 74 | num_rows, 75 | num_columns) 76 | image_frames = [] 77 | 78 | for i in range(num_rows*num_columns, 0, -1): 79 | frame = animation_grid[i-1] 80 | animation_frame = pyglet.image.AnimationFrame(frame, 0.2) 81 | image_frames.append(animation_frame) 82 | 83 | animation = pyglet.image.Animation(image_frames) 84 | return animation 85 | 86 | def adjustWindowSize(self): 87 | """ 88 | Adjust the width and height of the Pyglet Window. 89 | """ 90 | w = 600 91 | h = 900 92 | self.width = w 93 | self.height = h 94 | 95 | def on_draw(self): 96 | """ 97 | The overridden API method on_draw which is called when the Window 98 | needs to be re-drawn. 99 | """ 100 | self.clear() 101 | for d in self.drawableObjects: 102 | d.draw() 103 | 104 | def on_key_press(self, key, modifiers): 105 | """ 106 | Captures key press events. 107 | Overrides pyglet.window.Window.on_key_press() 108 | """ 109 | if key == pyglet.window.key.P and not self.paused: 110 | pyglet.clock.unschedule(self.moveObjects) 111 | pyglet.clock.unschedule(self.show_lightening) 112 | self.paused = True 113 | elif key == pyglet.window.key.R and self.paused: 114 | pyglet.clock.schedule_interval(self.moveObjects, 1.0/20) 115 | pyglet.clock.schedule_interval(self.show_lightening, 1.0) 116 | self.paused = False 117 | 118 | def moveObjects(self, t): 119 | """ 120 | Move the image sprites / play the sound . 121 | This method is scheduled to be called every 1/N seconds using 122 | pyglet.clock.schedule_interval. 123 | 124 | """ 125 | if self.carSprite.x <= self.cloudSprite.width: 126 | self.carSprite.x += 10 127 | else: 128 | self.carSprite.x = -500 129 | self.horn_sound.play() 130 | 131 | def show_lightening(self, t): 132 | """ 133 | toggle the property value Sprite.visible , also move 134 | the sprite representing the lightening image 135 | This method is scheduled to be called every second using 136 | pyglet.clock.schedule_interval. 137 | """ 138 | if self.lSprite.visible: 139 | self.lSprite.visible = False 140 | else: 141 | if(self.lSprite.x == 100): 142 | self.lSprite.x += 200 143 | else: 144 | self.lSprite.x = 100 145 | 146 | self.lSprite.visible = True 147 | 148 | # Create a Pyglet Window instance to show the animation. 149 | win = RainyDayAnimation() 150 | 151 | # Set window background color to white. 152 | r, g, b, alpha = 1, 1, 1, 1 153 | pyglet.gl.glClearColor(r, g, b, alpha) 154 | 155 | # Schedule the method RainyDayAnimation.moveObjects to be called every 156 | # 0.05 seconds. 157 | pyglet.clock.schedule_interval(win.moveObjects, 1.0/20) 158 | 159 | # Show the lightening every 1 second 160 | pyglet.clock.schedule_interval(win.show_lightening, 1.0) 161 | 162 | pyglet.app.run() 163 | 164 | #End 165 | --------------------------------------------------------------------------------