├── LICENSE ├── README.md ├── data.dat ├── flappy_bird.py └── images ├── bird.gif └── game.gif /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Dimitar Dimitrov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FlappyRocket 2 | This is a basic Python version of Flappy Bird, it's also my first tkinter project. 3 | 4 | ### Support Python 3 5 | ### Doesn't support Python 2 6 | 7 | # Instructions: 8 | 13 | 14 | 15 | ![game](https://user-images.githubusercontent.com/112943652/201140801-5aea6918-a80c-49b4-a1e2-75675258a7d8.gif) 16 | -------------------------------------------------------------------------------- /data.dat: -------------------------------------------------------------------------------- 1 | 10 -------------------------------------------------------------------------------- /flappy_bird.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import random 3 | import os 4 | 5 | FRAMERATE = 20 6 | SCORE = -1 7 | 8 | 9 | def center(toplevel): 10 | toplevel.update_idletasks() 11 | w = toplevel.winfo_screenwidth() 12 | h = toplevel.winfo_screenheight() 13 | size = tuple(int(top) for top in toplevel.geometry().split('+')[0].split('x')) 14 | x = w / 2 - size[0] / 2 15 | y = h / 2 - size[1] / 2 - 35 16 | toplevel.geometry("%dx%d+%d+%d" % (size + (x, y))) 17 | 18 | 19 | main = Tk() 20 | main.resizable(width=False, height=False) 21 | main.title("Flappy Rocket") 22 | main.geometry('550x700') 23 | 24 | center(main) 25 | 26 | BIRD_Y = 200 27 | PIPE_X = 550 28 | PIPE_HOLE = 0 29 | NOW_PAUSE = False 30 | 31 | BEST_SCORE = 0 32 | 33 | if os.path.isfile("data.dat"): 34 | scoreFile = open('data.dat') 35 | BEST_SCORE = int(scoreFile.read()) 36 | scoreFile.close() 37 | else: 38 | scoreFile = open('data.dat', 'w') 39 | scoreFile.write(str(BEST_SCORE)) 40 | scoreFile.close() 41 | 42 | w = Canvas(main, width=550, height=700, background="#8A00E4") 43 | w.pack() 44 | 45 | birdImg = PhotoImage(file="images/bird.gif") 46 | bird = w.create_image(100, BIRD_Y, image=birdImg) 47 | 48 | up_count = 0 49 | endRectangle = endBest = endScore = None 50 | 51 | pipeUp = w.create_rectangle(PIPE_X, 0, PIPE_X + 100, PIPE_HOLE, fill="#74BF2E", outline="#74BF2E") 52 | pipeDown = w.create_rectangle(PIPE_X, PIPE_HOLE + 200, PIPE_X + 100, 700, fill="#74BF2E", outline="#74BF2E") 53 | score_w = w.create_text(15, 45, text="0", font='Impact 60', fill='#ffffff', anchor=W) 54 | 55 | 56 | def generatePipeHole(): 57 | global PIPE_HOLE 58 | global SCORE 59 | global FRAMERATE 60 | SCORE += 1 61 | w.itemconfig(score_w, text=str(SCORE)) 62 | PIPE_HOLE = random.randint(50, 500) 63 | if SCORE + 1 % 7 == 0 and SCORE != 0: 64 | FRAMERATE -= 1 65 | 66 | 67 | generatePipeHole() 68 | 69 | 70 | def birdUp(event=None): 71 | global BIRD_Y 72 | global up_count 73 | global NOW_PAUSE 74 | 75 | if NOW_PAUSE == False: 76 | BIRD_Y -= 20 77 | if BIRD_Y <= 0: BIRD_Y = 0 78 | w.coords(bird, 100, BIRD_Y) 79 | if up_count < 5: 80 | up_count += 1 81 | main.after(FRAMERATE, birdUp) 82 | 83 | else: 84 | up_count = 0 85 | else: 86 | restartGame() 87 | 88 | 89 | def birdDown(): 90 | global BIRD_Y 91 | global NOW_PAUSE 92 | 93 | BIRD_Y += 8 94 | if BIRD_Y >= 700: BIRD_Y = 700 95 | w.coords(bird, 100, BIRD_Y) 96 | if NOW_PAUSE == False: main.after(FRAMERATE, birdDown) 97 | 98 | 99 | def pipesMotion(): 100 | global PIPE_X 101 | global PIPE_HOLE 102 | global NOW_PAUSE 103 | 104 | PIPE_X -= 5 105 | w.coords(pipeUp, PIPE_X, 0, PIPE_X + 100, PIPE_HOLE) 106 | w.coords(pipeDown, PIPE_X, PIPE_HOLE + 200, PIPE_X + 100, 700) 107 | 108 | if PIPE_X < -100: 109 | PIPE_X = 550 110 | generatePipeHole() 111 | 112 | if NOW_PAUSE == False: main.after(FRAMERATE, pipesMotion) 113 | 114 | 115 | def engGameScreen(): 116 | global endRectangle 117 | global endScore 118 | global endBest 119 | endRectangle = w.create_rectangle(0, 0, 550, 700, fill='#4EC0CA') 120 | endScore = w.create_text(15, 200, text="Your score: " + str(SCORE), font='Impact 50', fill='#ffffff', anchor=W) 121 | endBest = w.create_text(15, 280, text="Best score: " + str(BEST_SCORE), font='Impact 50', fill='#ffffff', anchor=W) 122 | 123 | 124 | def detectCollision(): 125 | global NOW_PAUSE 126 | global BEST_SCORE 127 | 128 | if (PIPE_X < 150 and PIPE_X + 100 >= 55) and (BIRD_Y < PIPE_HOLE + 45 or BIRD_Y > PIPE_HOLE + 175): 129 | NOW_PAUSE = True 130 | if SCORE > BEST_SCORE: 131 | BEST_SCORE = SCORE 132 | scoreFile = open('data.dat', 'w') 133 | scoreFile.write(str(BEST_SCORE)) 134 | scoreFile.close() 135 | engGameScreen() 136 | if NOW_PAUSE == False: main.after(FRAMERATE, detectCollision) 137 | 138 | 139 | def restartGame(): 140 | global PIPE_X 141 | global BIRD_Y 142 | global SCORE 143 | global NOW_PAUSE 144 | global FRAMERATE 145 | 146 | BIRD_Y = 200 147 | PIPE_X = 550 148 | SCORE = -1 149 | FRAMERATE = 20 150 | NOW_PAUSE = False 151 | w.delete(endScore) 152 | w.delete(endRectangle) 153 | w.delete(endBest) 154 | generatePipeHole() 155 | main.after(FRAMERATE, birdDown) 156 | main.after(FRAMERATE, pipesMotion) 157 | main.after(FRAMERATE, detectCollision) 158 | 159 | 160 | main.after(FRAMERATE, birdDown) 161 | main.after(FRAMERATE, pipesMotion) 162 | main.after(FRAMERATE, detectCollision) 163 | main.bind("", birdUp) 164 | main.mainloop() -------------------------------------------------------------------------------- /images/bird.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MitkoVtori/FlappyRocket/890c60e0c2270f70468ffe9ef46f430b38ee7804/images/bird.gif -------------------------------------------------------------------------------- /images/game.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MitkoVtori/FlappyRocket/890c60e0c2270f70468ffe9ef46f430b38ee7804/images/game.gif --------------------------------------------------------------------------------