Skip to content

Commit e7b9d32

Browse files
committed
Adding Egg Catcher Game
1 parent d75397f commit e7b9d32

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

EggCatcherGame.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from itertools import cycle
2+
from random import randrange
3+
from tkinter import Tk , Canvas , messagebox , font
4+
5+
canvas_width = 800
6+
canvas_height = 400
7+
8+
win = Tk()
9+
c = Canvas(win , width = canvas_width , height = canvas_height , background = 'deep sky blue')
10+
c.create_rectangle(-5, canvas_height - 100 , canvas_width + 5 , canvas_height + 5 , fill='sea green', width=0)
11+
c.create_oval(-80,-80,120,120,fill='orange' , width=0)
12+
c.pack()
13+
14+
color_cycle = cycle(['light blue' , 'light pink' , 'light yellow','light green' , 'red', 'blue' , 'green','black'])
15+
egg_width = 45
16+
egg_height = 55
17+
egg_score = 10
18+
egg_speed = 500
19+
egg_interval = 4000
20+
difficulty_factor = 0.95
21+
22+
catcher_color = 'blue'
23+
catcher_width = 100
24+
catcher_height = 100
25+
catcher_start_x = canvas_width / 2 - catcher_width / 2
26+
catcher_start_y = canvas_height -catcher_height - 20
27+
catcher_start_x2 = catcher_start_x + catcher_width
28+
catcher_start_y2 = catcher_start_y + catcher_height
29+
30+
catcher = c.create_arc(catcher_start_x ,catcher_start_y ,catcher_start_x2,catcher_start_y2 , start=200 , extent = 140 , style='arc' , outline=catcher_color , width=3)
31+
32+
score = 0
33+
score_text = c.create_text(10,10,anchor='nw' , font=('Arial',18,'bold'),fill='darkblue',text='Score : ' + str(score))
34+
35+
lives_remaning = 3
36+
lives_text = c.create_text(canvas_width-10,10,anchor='ne' , font=('Arial',18,'bold'),fill='darkblue',text='Lives : ' + str(lives_remaning))
37+
38+
eggs = []
39+
40+
def create_eggs():
41+
x = randrange(10,740)
42+
y = 40
43+
new_egg = c.create_oval(x,y,x+egg_width,y+egg_height,fill=next(color_cycle),width=0)
44+
eggs.append(new_egg)
45+
win.after(egg_interval,create_eggs)
46+
47+
def move_eggs():
48+
for egg in eggs:
49+
(egg_x,egg_y,egg_x2,egg_y2) = c.coords(egg)
50+
c.move(egg,0,10)
51+
if egg_y2 > canvas_height:
52+
egg_dropped(egg)
53+
win.after(egg_speed,move_eggs)
54+
55+
def egg_dropped(egg):
56+
eggs.remove(egg)
57+
c.delete(egg)
58+
lose_a_life()
59+
if lives_remaning == 0:
60+
messagebox.showinfo('GAME OVER!' , 'Final Score : ' + str(score))
61+
win.destroy()
62+
63+
def lose_a_life():
64+
global lives_remaning
65+
lives_remaning -= 1
66+
c.itemconfigure(lives_text , text='Lives : ' + str(lives_remaning))
67+
68+
def catch_check():
69+
(catcher_x,catcher_y,catcher_x2,catcher_y2) = c.coords(catcher)
70+
for egg in eggs:
71+
(egg_x,egg_y,egg_x2,egg_y2) = c.coords(egg)
72+
if catcher_x < egg_x and egg_x2 < catcher_x2 and catcher_y2 - egg_y2 < 40:
73+
eggs.remove(egg)
74+
c.delete(egg)
75+
increase_score(egg_score)
76+
win.after(100,catch_check)
77+
78+
def increase_score(points):
79+
global score , egg_speed , egg_interval
80+
score += points
81+
egg_speed = int(egg_speed * difficulty_factor)
82+
egg_interval = int(egg_interval * difficulty_factor)
83+
c.itemconfigure(score_text , text='Score : ' + str(score))
84+
85+
def move_left(event):
86+
(x1,y1,x2,y2) = c.coords(catcher)
87+
if x1 > 0:
88+
c.move(catcher,-20,0)
89+
90+
def move_right(event):
91+
(x1,y1,x2,y2) = c.coords(catcher)
92+
if x2 < canvas_width:
93+
c.move(catcher,20,0)
94+
95+
c.bind('<Left>' , move_left)
96+
c.bind('<Right>' , move_right)
97+
c.focus_set()
98+
99+
win.after(1000,create_eggs)
100+
win.after(1000,move_eggs)
101+
win.after(1000,catch_check)
102+
103+
win.mainloop()

0 commit comments

Comments
 (0)