Skip to content

Commit 99fa2cb

Browse files
committed
New Week code
1 parent 346afae commit 99fa2cb

15 files changed

+346
-0
lines changed

code/Week 6/Animation1.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'''Animation 1: χρήση της after() για κίνηση αντικειμένου στον καμβά'''
2+
3+
import tkinter as tk
4+
DEBUG=False
5+
class App():
6+
def __init__(self, root):
7+
self.menu = tk.Frame(root)
8+
self.menu.pack(side= 'top', fill='both', expand=1)
9+
self.f = tk.Frame(root)
10+
self.f.pack(side= 'left', fill='both', expand=1)
11+
self.width = 600
12+
self.height = 600
13+
self.speed = [self.width//100, self.height//100]
14+
self.display_speed = tk.StringVar()
15+
self.display_speed.set("{}".format(self.speed[0]))
16+
self.create_menu()
17+
self.canvas = tk.Canvas(self.f, width=self.width, height=self.height, bg='lightblue')
18+
self.canvas.pack(side='left')
19+
self.r, self.pad = 20, 5
20+
self.x = self.y = self.r+self.pad
21+
self.create_ball(self.x,self.y,self.r)
22+
self.run = False
23+
def create_menu(self):
24+
fnt='Arial 30'
25+
tk.Button(self.menu, text='Go/Stop', font=fnt, command=self.go_stop).pack(side='left', expand=1, fill='both')
26+
tk.Button(self.menu, text='Reset', font=fnt, command=self.reset).pack(side='left', expand=1, fill='both')
27+
tk.Button(self.menu, text=' - ', font=fnt, command=self.slower).pack(side='left', expand=1, fill='both')
28+
tk.Label(self.menu, textvariable=self.display_speed, bg = 'lightyellow',
29+
width=6, relief='sunken', font = fnt).pack(side='left', fill='both',expand=1, padx=5, pady=5)
30+
tk.Button(self.menu, text=' + ', bg='yellow', font=fnt ,command=self.speedup).pack(side= 'left', fill='both', expand=1)
31+
def create_ball(self, x,y,r):
32+
self.canvas.create_oval(self.x - r, self.y - r, self.x + r, self.y + r, tags="thing", fill="red")
33+
def speedup(self):
34+
self.speed = [x+1 for x in self.speed]
35+
self.display_speed.set(str(self.speed[0]))
36+
if DEBUG: print(self.speed)
37+
def slower(self):
38+
print(self.speed)
39+
self.speed = [x-1 for x in self.speed ]
40+
self.display_speed.set(str(self.speed[0]))
41+
if DEBUG: print(self.speed)
42+
def reset(self):
43+
self.canvas.delete('thing')
44+
self.x = self.y = self.r+self.pad
45+
self.create_ball(self.x, self.y, self.r)
46+
def go_stop(self):
47+
self.run = True if not self.run else False
48+
if DEBUG: print("RUN=", self.run)
49+
if self.run: self.move_thing()
50+
def move_thing(self, *args):
51+
if self.run:
52+
if DEBUG: print(*self.speed, self.canvas.bbox("thing"))
53+
self.canvas.move("thing", *self.speed)
54+
self.canvas.update_idletasks()
55+
self.x += self.speed[0] #νέα θέση στον άξονα x
56+
self.y += self.speed[1] #νέα θέση στον άξονα y
57+
if self.r+self.pad < self.x < self.width-self.r-self.pad and \
58+
self.r+self.pad
59+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Animation character 1
2+
# χρησιμοποιεί το φάκελο cartoon
3+
4+
5+
import tkinter as tk
6+
import os
7+
8+
class Animated_character():
9+
def __init__(self, canvas, dir):
10+
self.canvas = canvas
11+
self.canvas_height = int(self.canvas['height'])
12+
self.canvas_width = int(self.canvas['width'])
13+
self.images =[]
14+
try: # φόρτωσε τις εικόνες στη λίστα self.images
15+
for i in range(len(os.listdir(dir))):
16+
fname = str(i+1)+'.gif'
17+
self.images.append(tk.PhotoImage(file=os.path.join(dir,fname)))
18+
except: return
19+
self.image_height = self.find_height(self.images)
20+
self.image_width = self.find_width(self.images)
21+
self.speed = 400
22+
self.starting_point = self.canvas_height - margin - self.image_height
23+
self.current_image = 0
24+
self.art = self.canvas.create_image(self.canvas_width//2-self.image_width//2,
25+
self.starting_point, image=self.images[self.current_image], anchor='nw')
26+
def next_image(self):
27+
self.current_image = self.current_image + 1 if self.current_image+1 < len(self.images) else 0
28+
self.canvas.itemconfig(self.art, image=self.images[self.current_image])
29+
def move(self):
30+
if not self.images: return
31+
self.next_image() # δείξε την επόμενη εικόνα της σειράς
32+
self.canvas.after(self.speed, self.move)
33+
34+
def find_height(self, image_list):
35+
if image_list: return max([x.height() for x in image_list])
36+
else: return 0
37+
def find_width(self, image_list):
38+
if image_list: return max([x.width() for x in image_list])
39+
else: return 0
40+
41+
class App():
42+
def __init__(self, root, **kwargs):
43+
self.canvas = tk.Canvas(root, width=300, height=300, bg='grey95')
44+
self.canvas.pack()
45+
man = Animated_character(self.canvas, 'cartoon' )
46+
man.move()
47+
48+
margin = 5
49+
root = tk.Tk()
50+
app = App(root)
51+
root.mainloop()
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Animation character+background
2+
3+
import tkinter as tk
4+
import os
5+
import time
6+
import random
7+
8+
class Animated_character():
9+
def __init__(self, canvas, dir, size = 0, name =''):
10+
self.canvas = canvas
11+
self.name = name
12+
self.dir = dir
13+
self.run = True
14+
self.canvas_height = int(self.canvas['height'])
15+
self.canvas_width = int(self.canvas['width'])
16+
self.images ={'go':[], 'back':[]} #go : moving right, back : moving left
17+
images = [x for x in os.listdir(self.dir) if x.split('.')[1] == 'gif']
18+
for _i in range(len(images)):
19+
file_name = os.path.join(dir, self.name+str(_i+1)+'.gif')
20+
if os.path.isfile(file_name):
21+
self.images['go'].append(tk.PhotoImage(file=file_name))
22+
file_name = os.path.join(dir, self.name+str(_i+1)+'rev.gif')
23+
if os.path.isfile(file_name):
24+
self.images['back'].append(tk.PhotoImage(file=file_name))
25+
if size:
26+
self.zoom(size)
27+
self.max_height = self.find_max(self.images['go']) # max height of animated character
28+
self.reverse = False
29+
self.speed = 100
30+
self.starting_point = self.canvas_height - margin - self.max_height
31+
self.position_x = self.canvas_width//2-self.find_width(self.images['go'])
32+
self.position_y = self.starting_point
33+
self.current_image = 0
34+
self.art = self.canvas.create_image(self.position_x, self.position_y,
35+
image=self.images['go'][self.current_image], anchor='nw')
36+
37+
def next_image(self):
38+
self.current_image = self.current_image + 1 if self.current_image+1 < len(self.images['go']) else 0
39+
img_list = self.images['back'] if self.reverse else self.images['go']
40+
self.canvas.itemconfig(self.art, image=img_list[self.current_image])
41+
42+
def find_max(self, image_list):
43+
return max([x.height() for x in image_list])
44+
def find_width(self, image_list):
45+
return max([x.width() for x in image_list])
46+
def zoom(self, final):
47+
# the idea from https://stackoverflow.com/questions/6582387/image-resize-under-photoimage
48+
initial = self.find_max(self.images['go'])
49+
for key in self.images:
50+
zoomed_images = []
51+
for im in self.images[key]:
52+
zoomed_images.append(im.zoom(final//10).subsample(initial//10))
53+
self.images[key] = zoomed_images
54+
def move(self):
55+
self.next_image()
56+
if self.position_y < self.starting_point:
57+
self.position_y += 10 # gravity
58+
#self.canvas.coords(self.art, self.position_x, self.position_y)
59+
self.canvas.move(self.art, 0, 10)
60+
self.canvas.after(100, self.move)
61+
def jump(self, event):
62+
self.position_y -= 60 # πήδημα 60 pixels
63+
#self.canvas.coords(self.art, self.position_x, self.position_y)
64+
self.canvas.move(self.art, 0, -60)
65+
66+
class Background():
67+
def __init__(self, canvas, *args, **kwargs):
68+
self.canvas = canvas
69+
self.canvas_width = self.canvas.winfo_width()
70+
self.back_image = tk.PhotoImage(file= 'skyline.gif')
71+
self.back_width = self.back_image.width()
72+
self.back1 = self.canvas.create_image(0,0, image=self.back_image, anchor='nw')
73+
self.back2 = self.canvas.create_image(self.back_width,0, image=self.back_image, anchor='nw')
74+
self.move_background()
75+
76+
def move_background(self):
77+
self.canvas.move(self.back1, -5, 0)
78+
self.canvas.move(self.back2, -5, 0)
79+
coord1 = self.canvas.coords(self.back1)
80+
coord2 = self.canvas.coords(self.back2)
81+
if min(coord1[0], coord2[0]) <= self.canvas_width - self.back_width :
82+
if coord1[0] < coord2[0]: self.canvas.move(self.back1, self.back_width*2, 0)
83+
else: self.canvas.move(self.back2, self.back_width*2, 0)
84+
self.canvas.after(100, self.move_background)
85+
86+
class App():
87+
def __init__(self, root, **kwargs):
88+
self.root = root
89+
self.width=900
90+
self.height=500
91+
self.canvas = tk.Canvas(self.root, width=self.width, height=self.height, bg='grey70')
92+
self.canvas.pack()
93+
b = Background(self.canvas)
94+
man = Animated_character(self.canvas, 'man', name='man' )
95+
man.move()
96+
self.root.bind('', man.jump)
97+
98+
margin = 5
99+
root = tk.Tk()
100+
app = App(root)
101+
root.mainloop()

code/Week 6/Animation_Show_1.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Animation show 1
2+
3+
import tkinter as tk
4+
import time
5+
import random
6+
class ball(object):
7+
def __init__(self, canvas, *args, **kwargs):
8+
self.canvas = canvas
9+
self.id = canvas.create_oval(*args, **kwargs)
10+
self.vx = random.randint(1,5)
11+
self.vy = random.randint(1,5)
12+
13+
def move(self):
14+
x1, y1, x2, y2 = self.canvas.bbox(self.id)
15+
if x2 > app.width: self.vx = -self.vx
16+
if y2 > app.height: self.vy = -self.vy
17+
if x1 < 0: self.vx = -self.vx
18+
if y1 < 0: self.vy = -self.vy
19+
self.canvas.move(self.id, self.vx, self.vy)
20+
21+
class App(object):
22+
def __init__(self, master, **kwargs):
23+
self.master = master
24+
self.width = 400
25+
self.height = 400
26+
self.canvas = tk.Canvas(self.master, width=self.width, height=self.height)
27+
self.canvas.pack()
28+
self.balls = [
29+
#ball(self.canvas, 20, 260, 120, 360, outline='white', fill='blue'),
30+
ball(self.canvas, 2, 2, 40, 40, outline='white', fill='red')]
31+
self.canvas.pack()
32+
self.master.after(0, self.animation)
33+
34+
def animation(self):
35+
for alien in self.balls:
36+
alien.move()
37+
self.master.after(12, self.animation)
38+
39+
root = tk.Tk()
40+
app = App(root)
41+
root.mainloop()

code/Week 6/Animation_Show_2.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Animation show 2
2+
3+
import tkinter as tk
4+
import random
5+
6+
class App():
7+
def __init__(self, root):
8+
self.root = root
9+
self.width = 400
10+
self.height = 400
11+
self.canvas = tk.Canvas(self.root, width=self.width, height=self.height)
12+
self.canvas.pack()
13+
self.balls = [
14+
Ball(self.canvas, 20, 200, 150, 'red'),
15+
Ball(self.canvas, 50, 350, 300, 'blue')
16+
]
17+
self.root.after(0, self.animation)
18+
def animation(self):
19+
for obj in self.balls:
20+
obj.move()
21+
self.root.after(10, self.animation)
22+
23+
class Ball():
24+
def __init__(self, canvas, r, x=0, y=0, color='white'):
25+
self.canvas = canvas
26+
self.id = canvas.create_oval(x-r, y-r, x+r, y+r, fill = color)
27+
self.vx = random.randint(1,5)
28+
self.vy = random.randint(1,5)
29+
def move(self):
30+
pos = self.canvas.bbox(self.id)
31+
if pos[0] <= 0 : self.vx = abs(self.vx)
32+
if pos[1] <= 0 : self.vy = abs(self.vy)
33+
if pos[2] >= app.width : self.vx = -abs(self.vx)
34+
if pos[3] >= app.height : self.vy = -abs(self.vy)
35+
if len(self.canvas.find_overlapping(*pos)) > 1 :
36+
self.vx = - self.vx
37+
self.vy = - self.vy
38+
self.canvas.move(self.id, self.vx, self.vy)
39+
40+
root = tk.Tk()
41+
app = App(root)
42+
root.mainloop()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Animation of background
2+
3+
import tkinter as tk
4+
5+
class Background():
6+
def __init__(self, canvas, *args, **kwargs):
7+
self.canvas = canvas
8+
self.back_image = tk.PhotoImage(file= 'skyline.gif')
9+
self.back_width = self.back_image.width()
10+
self.back1 = self.canvas.create_image(0,0, image=self.back_image, anchor='nw')
11+
self.back2 = self.canvas.create_image(self.back_width,0, image=self.back_image, anchor='nw')
12+
self.canvas_width = int(self.canvas['width'])
13+
self.canvas_height = int(self.canvas['height'])
14+
# print(self.canvas_width, self.canvas_height, self.back_width)
15+
# print('image1', self.canvas.bbox(self.back1))
16+
# print('image2', self.canvas.bbox(self.back2))
17+
self.car_image = tk.PhotoImage(file= 'yellow_car.gif')
18+
self.car = self.canvas.create_image(250,250, image=self.car_image, anchor='nw')
19+
self.run = 'go'
20+
self.speed = -5
21+
self.move_background()
22+
23+
def move_background(self):
24+
self.canvas.move(self.back1, self.speed, 0)
25+
self.canvas.move(self.back2, self.speed, 0)
26+
coord1 = self.canvas.coords(self.back1)
27+
coord2 = self.canvas.coords(self.back2)
28+
if self.run == 'go':
29+
if max(coord1[0], coord2[0]) <= 0 :
30+
if coord1[0] < coord2[0]: self.canvas.move(self.back1, self.back_width*2, 0)
31+
else: self.canvas.move(self.back2, self.back_width*2, 0)
32+
else:
33+
if min(coord1[0], coord2[0]) >= 0 :
34+
if coord1[0] < coord2[0]: self.canvas.move(self.back2, -self.back_width*2, 0)
35+
else: self.canvas.move(self.back1, -self.back_width*2, 0)
36+
self.canvas.after(50, self.move_background)
37+
def toggle(self, e):
38+
self.run = 'go' if self.run == 'back' else 'back'
39+
self.speed *= -1
40+
41+
class App():
42+
def __init__(self, root, **kwargs):
43+
self.root = root
44+
self.width=900
45+
self.height=450
46+
self.canvas = tk.Canvas(self.root, width=self.width, height=self.height)
47+
self.canvas.pack()
48+
b = Background(self.canvas)
49+
self.canvas.bind('<1>', b.toggle)
50+
root = tk.Tk()
51+
app = App(root)
52+
root.mainloop()

code/Week 6/blue_small.gif

18.7 KB
Loading

code/Week 6/car.gif

53.9 KB
Loading

code/Week 6/cartoon/1.gif

4.64 KB
Loading

code/Week 6/cartoon/2.gif

4.23 KB
Loading

0 commit comments

Comments
 (0)