|
| 1 | +# import modules |
| 2 | +from tkinter import Tk, Button, Label |
| 3 | +from tkinter import Canvas |
| 4 | +from random import randint |
| 5 | + |
| 6 | +# defining Tk from Tkinter |
| 7 | +tk = Tk() |
| 8 | +# Set window title and icon |
| 9 | +tk.title("Catch-The-Ball") |
| 10 | +tk.iconbitmap("logo-ico.ico") |
| 11 | +tk.resizable(False, False) |
| 12 | + |
| 13 | +# For defining the canvas |
| 14 | +canvas = Canvas(tk, width=600, height=600, bg='#ffff99') |
| 15 | +canvas.pack() |
| 16 | + |
| 17 | +# Variable for the vertical distance travelled by ball |
| 18 | +limit = 0 |
| 19 | + |
| 20 | +# Variable for horizontal distance of bar from x-axis |
| 21 | +dist = 5 |
| 22 | + |
| 23 | +# Variable for score |
| 24 | +score = 0 |
| 25 | + |
| 26 | +# Class for the Creating and moving ball |
| 27 | +class Ball: |
| 28 | + # for creation of ball on the canvas |
| 29 | + def __init__(self, canvas, x1, y1, x2, y2): |
| 30 | + self.x1 = x1 |
| 31 | + self.y1 = y1 |
| 32 | + self.x2 = x2 |
| 33 | + self.y2 = y2 |
| 34 | + self.canvas = canvas |
| 35 | + # for creation of ball object |
| 36 | + self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2, |
| 37 | + fill="red", tags='dot1') |
| 38 | + |
| 39 | + # for moving the ball |
| 40 | + def move_ball(self): |
| 41 | + |
| 42 | + # defining offset |
| 43 | + offset = 10 |
| 44 | + global limit |
| 45 | + |
| 46 | + # checking if ball lands ground or bar |
| 47 | + if limit >= 510: |
| 48 | + global dist, score, next |
| 49 | + |
| 50 | + # checking that ball falls on the bar |
| 51 | + if (dist - offset <= self.x1 and |
| 52 | + dist + 40 + offset >= self.x2): |
| 53 | + |
| 54 | + # incrementing the score |
| 55 | + score += 10 |
| 56 | + |
| 57 | + # dissappear the ball |
| 58 | + canvas.delete('dot1') |
| 59 | + |
| 60 | + # calling the function for again |
| 61 | + # creation of ball object |
| 62 | + ball_set() |
| 63 | + |
| 64 | + else: |
| 65 | + # dissappear the ball |
| 66 | + canvas.delete('dot1') |
| 67 | + bar.delete_bar(self) |
| 68 | + |
| 69 | + # display the score |
| 70 | + score_board() |
| 71 | + return |
| 72 | + |
| 73 | + # incrementing the vertical distance |
| 74 | + # travelled by ball by deltay |
| 75 | + limit += 1 |
| 76 | + |
| 77 | + # moving the ball in vertical direction |
| 78 | + # by taking x=0 and y=deltay |
| 79 | + self.canvas.move(self.ball, 0, 1) |
| 80 | + |
| 81 | + # for continuous moving of ball again call move_ball |
| 82 | + self.canvas.after(10, self.move_ball) |
| 83 | + |
| 84 | + # class for creating and moving bar |
| 85 | + |
| 86 | +class bar: |
| 87 | + # method for creating bar |
| 88 | + def __init__(self, canvas, x1, y1, x2, y2): |
| 89 | + self.x1 = x1 |
| 90 | + self.y1 = y1 |
| 91 | + self.x2 = x2 |
| 92 | + self.y2 = y2 |
| 93 | + self.canvas = canvas |
| 94 | + |
| 95 | + # for creating bar using create_rectangle |
| 96 | + self.rod = canvas.create_rectangle(self.x1, self.y1, self.x2, self.y2, |
| 97 | + fill="#0000ff", tags='dot2') |
| 98 | + |
| 99 | + # method for moving the bar |
| 100 | + def move_bar(self, num): |
| 101 | + global dist |
| 102 | + # checking the forward or backward button |
| 103 | + if (num == 1): |
| 104 | + |
| 105 | + # moving the bar in forward direction by |
| 106 | + # taking x-axis positive distance and |
| 107 | + # taking vertical distance y=0 |
| 108 | + self.canvas.move(self.rod, 20, 0) |
| 109 | + |
| 110 | + # incrementing the distance of bar from x-axis |
| 111 | + dist += 20 |
| 112 | + else: |
| 113 | + |
| 114 | + # moving the bar in backward direction by taking x-axis |
| 115 | + # negative distance and taking vertical distance y=0 |
| 116 | + self.canvas.move(self.rod, -20, 0) |
| 117 | + |
| 118 | + # decrementing the distance of bar from x-axis |
| 119 | + dist -= 20 |
| 120 | + |
| 121 | + def delete_bar(self): |
| 122 | + canvas.delete('dot2') |
| 123 | + |
| 124 | + # Function to define the dimensions of the ball |
| 125 | + |
| 126 | + |
| 127 | +def ball_set(): |
| 128 | + global limit |
| 129 | + limit = 0 |
| 130 | + |
| 131 | + # for random x-axis distance from |
| 132 | + # where the ball starts to fall |
| 133 | + value = randint(0, 570) |
| 134 | + |
| 135 | + # define the dimensions of the ball |
| 136 | + ball1 = Ball(canvas, value, 20, value + 30, 50) |
| 137 | + |
| 138 | + # call function for moving of the ball |
| 139 | + ball1.move_ball() |
| 140 | + |
| 141 | + |
| 142 | +# Function for displaying the score |
| 143 | +# after getting over of the game |
| 144 | +def score_board(): |
| 145 | + root2 = Tk() |
| 146 | + root2.title("Catch the ball Game") |
| 147 | + root2.resizable(False, False) |
| 148 | + canvas2 = Canvas(root2, width=300, height=300) |
| 149 | + canvas2.pack() |
| 150 | + |
| 151 | + w = Label(canvas2, text="\nOOPS...GAME IS OVER\n\nYOUR SCORE = " |
| 152 | + + str(score) + "\n\n") |
| 153 | + w.pack() |
| 154 | + |
| 155 | + button3 = Button(canvas2, text="PLAY AGAIN", bg="#ffb31a", |
| 156 | + command=lambda: play_again(root2)) |
| 157 | + button3.pack() |
| 158 | + |
| 159 | + button4 = Button(canvas2, text="EXIT", bg="#ffb31a", |
| 160 | + command=lambda: exit_handler(root2)) |
| 161 | + button4.pack() |
| 162 | + |
| 163 | + |
| 164 | +# Function for handling the play again request |
| 165 | +def play_again(root2): |
| 166 | + root2.destroy() |
| 167 | + main() |
| 168 | + |
| 169 | + |
| 170 | +# Function for handling exit request |
| 171 | +def exit_handler(root2): |
| 172 | + root2.destroy() |
| 173 | + tk.destroy() |
| 174 | + |
| 175 | + |
| 176 | +# Main function |
| 177 | +def main(): |
| 178 | + global score, dist |
| 179 | + score = 0 |
| 180 | + dist = 0 |
| 181 | + |
| 182 | + # defining the dimensions of bar |
| 183 | + bar1 = bar(canvas, 5, 560, 45, 575) |
| 184 | + |
| 185 | + # defining the text,colour of buttons and |
| 186 | + # also define the action after click on |
| 187 | + # the button by calling suitable methods |
| 188 | + button = Button(canvas, text="==>", bg="#00ff00", |
| 189 | + command=lambda: bar1.move_bar(1)) |
| 190 | + |
| 191 | + # placing the buttons at suitable location on the canvas |
| 192 | + button.place(x=300, y=580) |
| 193 | + |
| 194 | + button2 = Button(canvas, text="<==", bg="#00ff00", |
| 195 | + command=lambda: bar1.move_bar(0)) |
| 196 | + button2.place(x=260, y=580) |
| 197 | + |
| 198 | + # calling the function for defining |
| 199 | + # the dimensions of ball |
| 200 | + ball_set() |
| 201 | + tk.mainloop() |
| 202 | + |
| 203 | + |
| 204 | +# Main code |
| 205 | +if (__name__ == "__main__"): |
| 206 | + main() |
0 commit comments