1+ from PIL import Image , ImageTk
2+ import tkinter as tk
3+ from pygame import mixer
4+ import random
5+
6+ mixer .init ()
7+ window = tk .Tk ()
8+ window .geometry ('1000x600' )
9+ window .title ('Flappy Bird' )
10+
11+ x = 150
12+ y = 300
13+ score = 0
14+ speed = 10
15+ game_over = False
16+
17+ img_bird = Image .open ('images/bird.png' )
18+ img_bird = ImageTk .PhotoImage (img_bird )
19+
20+ img_pipe_down = Image .open ('images/pipe.png' ) # 104x900
21+ img_pipe_top = img_pipe_down .rotate (180 )
22+
23+ img_pipe_down = ImageTk .PhotoImage (img_pipe_down )
24+ img_pipe_top = ImageTk .PhotoImage (img_pipe_top )
25+
26+ img_reset = Image .open ('images/reiniciar.png' )
27+ img_reset = ImageTk .PhotoImage (img_reset )
28+
29+ canvas = tk .Canvas (window , highlightthickness = 0 , bg = '#00bfff' )
30+ canvas .place (relwidth = 1 , relheight = 1 )
31+
32+ text_score = canvas .create_text (50 ,50 , text = '0' , fill = 'white' , font = ('D3 Egoistism outline' , 30 ))
33+
34+ bird = canvas .create_image (x ,y , anchor = 'nw' , image = img_bird )
35+ pipe_top = canvas .create_image (1200 , - 550 , anchor = 'nw' , image = img_pipe_top )
36+ pipe_down = canvas .create_image (1200 , 550 , anchor = 'nw' , image = img_pipe_down )
37+
38+ mixer .music .load ('audio/swoosh.wav' )
39+ mixer .music .play (loops = 0 )
40+
41+ def move_bird_key (event ):
42+ global x ,y
43+ if not game_over :
44+ y -= 30
45+ canvas .coords (bird , x ,y )
46+ mixer .music .load ('audio/wing.wav' )
47+ mixer .music .play (loops = 0 )
48+
49+
50+ window .bind ( "<space>" , move_bird_key )
51+
52+
53+ def move_bird ():
54+ global x ,y
55+ y += 5
56+ canvas .coords (bird , x ,y )
57+ if y < 0 or y > window .winfo_height ():
58+ game_end ()
59+
60+ if not game_over :
61+ window .after (50 , move_bird )
62+
63+ def move_pipe ():
64+ global score , game_over , speed
65+ canvas .move (pipe_top , - speed , 0 )
66+ canvas .move (pipe_down , - speed , 0 )
67+ if canvas .coords (pipe_down )[0 ] < - 100 :
68+ score += 1
69+ speed += 1
70+ canvas .itemconfigure (text_score , text = str (score ))
71+ h = window .winfo_height ()
72+ num = random .choice ([i for i in range (160 ,h , 160 )])
73+ canvas .coords (pipe_down , window .winfo_width (), num + 160 )
74+ canvas .coords (pipe_top , window .winfo_width (), num - 900 )
75+
76+ if 0 < canvas .coords (pipe_down )[0 ]< 160 :
77+ channel = mixer .Channel (1 )
78+ channel .set_volume (1.0 )
79+ sound = mixer .Sound ('audio/point.wav' )
80+ channel .play (sound , loops = 0 )
81+
82+ if canvas .coords (pipe_down ):
83+ if canvas .bbox (bird )[0 ] < canvas .bbox (pipe_down )[2 ] and canvas .bbox (bird )[2 ]> canvas .bbox (pipe_down )[0 ]:
84+ if canvas .bbox (bird )[1 ] < canvas .bbox (pipe_top )[3 ] or canvas .bbox (bird )[3 ]> canvas .bbox (pipe_down )[1 ]:
85+ game_end ()
86+ if not game_over :
87+ window .after (50 , move_pipe )
88+
89+
90+ def reset_game ():
91+ global x ,y ,score , speed , game_over
92+ x = 150
93+ y = 300
94+ score = 0
95+ speed = 10
96+ game_over = False
97+ canvas .coords (bird , x ,y )
98+ canvas .coords (pipe_top , 1200 ,- 550 )
99+ canvas .coords (pipe_down , 1200 , 550 )
100+ canvas .itemconfigure (text_score , text = "0" )
101+ lbl_game_over .place_forget ()
102+ bt_reset .place_forget ()
103+ move_bird ()
104+ move_pipe ()
105+ mixer .music .load ('audio/swoosh.wav' )
106+ mixer .music .play (loops = 0 )
107+
108+ def game_end ():
109+ global game_over
110+ game_over = True
111+ lbl_game_over .place (relx = 0.5 , rely = 0.5 , anchor = 'center' )
112+ bt_reset .place (relx = 0.5 , rely = 0.7 , anchor = 'center' )
113+ mixer .music .load ('audio/hit.wav' )
114+ mixer .music .play (loops = 0 )
115+ while mixer .music .get_busy ():
116+ continue
117+ mixer .music .load ('audio/die.wav' )
118+ mixer .music .play (loops = 0 )
119+
120+
121+ lbl_game_over = tk .Label (window , text = 'Game Over !' , font = ('D3 Egoistism outline' , 30 ), fg = 'white' , bg = '#00bfff' )
122+ bt_reset = tk .Button (window , border = 0 , image = img_reset , activebackground = '#00bfff' , bg = '#00bfff' , command = reset_game )
123+
124+
125+ window .after (50 , move_bird )
126+ window .after (50 , move_pipe )
127+
128+ window .call ('wm' , 'iconphoto' , window ._w , img_bird )
129+ window .mainloop ()
0 commit comments