|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "ffde2260", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "name": "stderr", |
| 11 | + "output_type": "stream", |
| 12 | + "text": [ |
| 13 | + "Exception in Tkinter callback\n", |
| 14 | + "Traceback (most recent call last):\n", |
| 15 | + " File \"C:\\Users\\DELL\\anaconda3\\lib\\tkinter\\__init__.py\", line 1892, in __call__\n", |
| 16 | + " return self.func(*args)\n", |
| 17 | + " File \"C:\\Users\\DELL\\anaconda3\\lib\\tkinter\\__init__.py\", line 814, in callit\n", |
| 18 | + " func(*args)\n", |
| 19 | + " File \"C:\\Users\\DELL\\AppData\\Local\\Temp\\ipykernel_10004\\2936780779.py\", line 49, in move_eggs\n", |
| 20 | + " (egg_x,egg_y,egg_x2,egg_y2) = c.coords(egg)\n", |
| 21 | + " File \"C:\\Users\\DELL\\anaconda3\\lib\\tkinter\\__init__.py\", line 2766, in coords\n", |
| 22 | + " self.tk.call((self._w, 'coords') + args))]\n", |
| 23 | + "_tkinter.TclError: invalid command name \".!canvas\"\n" |
| 24 | + ] |
| 25 | + } |
| 26 | + ], |
| 27 | + "source": [ |
| 28 | + "from itertools import cycle\n", |
| 29 | + "from random import randrange\n", |
| 30 | + "from tkinter import Tk , Canvas , messagebox , font\n", |
| 31 | + "\n", |
| 32 | + "canvas_width = 800\n", |
| 33 | + "canvas_height = 400\n", |
| 34 | + "\n", |
| 35 | + "win = Tk()\n", |
| 36 | + "c = Canvas(win , width = canvas_width , height = canvas_height , background = 'deep sky blue')\n", |
| 37 | + "c.create_rectangle(-5, canvas_height - 100 , canvas_width + 5 , canvas_height + 5 , fill='sea green', width=0)\n", |
| 38 | + "c.create_oval(-80,-80,120,120,fill='orange' , width=0)\n", |
| 39 | + "c.pack()\n", |
| 40 | + "\n", |
| 41 | + "color_cycle = cycle(['light blue' , 'light pink' , 'light yellow','light green' , 'red', 'blue' , 'green','black'])\n", |
| 42 | + "egg_width = 45\n", |
| 43 | + "egg_height = 55\n", |
| 44 | + "egg_score = 10\n", |
| 45 | + "egg_speed = 500\n", |
| 46 | + "egg_interval = 4000\n", |
| 47 | + "difficulty_factor = 0.95\n", |
| 48 | + "\n", |
| 49 | + "catcher_color = 'blue'\n", |
| 50 | + "catcher_width = 100\n", |
| 51 | + "catcher_height = 100\n", |
| 52 | + "catcher_start_x = canvas_width / 2 - catcher_width / 2\n", |
| 53 | + "catcher_start_y = canvas_height -catcher_height - 20\n", |
| 54 | + "catcher_start_x2 = catcher_start_x + catcher_width\n", |
| 55 | + "catcher_start_y2 = catcher_start_y + catcher_height\n", |
| 56 | + "\n", |
| 57 | + "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)\n", |
| 58 | + "\n", |
| 59 | + "score = 0\n", |
| 60 | + "score_text = c.create_text(10,10,anchor='nw' , font=('Arial',18,'bold'),fill='darkblue',text='Score : ' + str(score))\n", |
| 61 | + "\n", |
| 62 | + "lives_remaning = 3\n", |
| 63 | + "lives_text = c.create_text(canvas_width-10,10,anchor='ne' , font=('Arial',18,'bold'),fill='darkblue',text='Lives : ' + str(lives_remaning))\n", |
| 64 | + "\n", |
| 65 | + "eggs = []\n", |
| 66 | + "\n", |
| 67 | + "def create_eggs():\n", |
| 68 | + " x = randrange(10,740)\n", |
| 69 | + " y = 40\n", |
| 70 | + " new_egg = c.create_oval(x,y,x+egg_width,y+egg_height,fill=next(color_cycle),width=0)\n", |
| 71 | + " eggs.append(new_egg)\n", |
| 72 | + " win.after(egg_interval,create_eggs)\n", |
| 73 | + "\n", |
| 74 | + "def move_eggs():\n", |
| 75 | + " for egg in eggs:\n", |
| 76 | + " (egg_x,egg_y,egg_x2,egg_y2) = c.coords(egg)\n", |
| 77 | + " c.move(egg,0,10)\n", |
| 78 | + " if egg_y2 > canvas_height:\n", |
| 79 | + " egg_dropped(egg)\n", |
| 80 | + " win.after(egg_speed,move_eggs)\n", |
| 81 | + "\n", |
| 82 | + "def egg_dropped(egg):\n", |
| 83 | + " eggs.remove(egg)\n", |
| 84 | + " c.delete(egg)\n", |
| 85 | + " lose_a_life()\n", |
| 86 | + " if lives_remaning == 0:\n", |
| 87 | + " messagebox.showinfo('GAME OVER!' , 'Final Score : ' + str(score))\n", |
| 88 | + " win.destroy()\n", |
| 89 | + "\n", |
| 90 | + "def lose_a_life():\n", |
| 91 | + " global lives_remaning\n", |
| 92 | + " lives_remaning -= 1\n", |
| 93 | + " c.itemconfigure(lives_text , text='Lives : ' + str(lives_remaning))\n", |
| 94 | + "\n", |
| 95 | + "def catch_check():\n", |
| 96 | + " (catcher_x,catcher_y,catcher_x2,catcher_y2) = c.coords(catcher)\n", |
| 97 | + " for egg in eggs:\n", |
| 98 | + " (egg_x,egg_y,egg_x2,egg_y2) = c.coords(egg)\n", |
| 99 | + " if catcher_x < egg_x and egg_x2 < catcher_x2 and catcher_y2 - egg_y2 < 40:\n", |
| 100 | + " eggs.remove(egg)\n", |
| 101 | + " c.delete(egg)\n", |
| 102 | + " increase_score(egg_score)\n", |
| 103 | + " win.after(100,catch_check)\n", |
| 104 | + "\n", |
| 105 | + "def increase_score(points):\n", |
| 106 | + " global score , egg_speed , egg_interval\n", |
| 107 | + " score += points\n", |
| 108 | + " egg_speed = int(egg_speed * difficulty_factor)\n", |
| 109 | + " egg_interval = int(egg_interval * difficulty_factor)\n", |
| 110 | + " c.itemconfigure(score_text , text='Score : ' + str(score))\n", |
| 111 | + "\n", |
| 112 | + "def move_left(event):\n", |
| 113 | + " (x1,y1,x2,y2) = c.coords(catcher)\n", |
| 114 | + " if x1 > 0:\n", |
| 115 | + " c.move(catcher,-20,0)\n", |
| 116 | + "\n", |
| 117 | + "def move_right(event):\n", |
| 118 | + " (x1,y1,x2,y2) = c.coords(catcher)\n", |
| 119 | + " if x2 < canvas_width:\n", |
| 120 | + " c.move(catcher,20,0)\n", |
| 121 | + "\n", |
| 122 | + "c.bind('<Left>' , move_left)\n", |
| 123 | + "c.bind('<Right>' , move_right)\n", |
| 124 | + "c.focus_set()\n", |
| 125 | + "\n", |
| 126 | + "win.after(1000,create_eggs)\n", |
| 127 | + "win.after(1000,move_eggs)\n", |
| 128 | + "win.after(1000,catch_check)\n", |
| 129 | + "\n", |
| 130 | + "win.mainloop()" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": 2, |
| 136 | + "id": "dd897f32", |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [ |
| 139 | + { |
| 140 | + "ename": "ModuleNotFoundError", |
| 141 | + "evalue": "No module named 'node'", |
| 142 | + "output_type": "error", |
| 143 | + "traceback": [ |
| 144 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 145 | + "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", |
| 146 | + "Input \u001b[1;32mIn [2]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnode\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Node\n\u001b[0;32m 3\u001b[0m \u001b[38;5;66;03m# Class of tree\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mclass\u001b[39;00m \u001b[38;5;21;01mTree\u001b[39;00m:\n", |
| 147 | + "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'node'" |
| 148 | + ] |
| 149 | + } |
| 150 | + ], |
| 151 | + "source": [ |
| 152 | + "from node import Node\n", |
| 153 | + "\n", |
| 154 | + "# Class of tree\n", |
| 155 | + "class Tree:\n", |
| 156 | + " def __init__(self):\n", |
| 157 | + " self.root = None\n", |
| 158 | + "\n", |
| 159 | + " # Method to obtain the data\n", |
| 160 | + " def getRoot(self):\n", |
| 161 | + " return self.root\n", |
| 162 | + "\n", |
| 163 | + " # Method to add the data\n", |
| 164 | + " def add(self, data):\n", |
| 165 | + " if self.root is None:\n", |
| 166 | + " self.root = Node(data)\n", |
| 167 | + " else:\n", |
| 168 | + " self._add(data, self.root)\n", |
| 169 | + "\n", |
| 170 | + " def _add(self, data, node):\n", |
| 171 | + " if data < node.data:\n", |
| 172 | + " if node.left is not None:\n", |
| 173 | + " self._add(data, node.left)\n", |
| 174 | + " else:\n", |
| 175 | + " node.left = Node(data)\n", |
| 176 | + " else:\n", |
| 177 | + " if node.right is not None:\n", |
| 178 | + " self._add(data, node.right)\n", |
| 179 | + " else:\n", |
| 180 | + " node.right = Node(data)\n", |
| 181 | + "\n", |
| 182 | + " # Method for find the data\n", |
| 183 | + " def find(self, data):\n", |
| 184 | + " if self.root is not None:\n", |
| 185 | + " return self._find(data, self.root)\n", |
| 186 | + " else:\n", |
| 187 | + " return None\n", |
| 188 | + "\n", |
| 189 | + " def _find(self, data, node):\n", |
| 190 | + " if data == node.data:\n", |
| 191 | + " return node\n", |
| 192 | + " elif (data < node.data and node.left is not None):\n", |
| 193 | + " return self._find(data, node.left)\n", |
| 194 | + " elif (data > node.data and node.right is not None):\n", |
| 195 | + " return self._find(data, node.right)\n", |
| 196 | + "\n", |
| 197 | + " # Method for delete tree\n", |
| 198 | + " def deleteTree(self):\n", |
| 199 | + " self.root = None\n", |
| 200 | + "\n", |
| 201 | + " # Method for print tree in terminal\n", |
| 202 | + " def printTree(self):\n", |
| 203 | + " if self.root is not None:\n", |
| 204 | + " self._printTree(self.root)\n", |
| 205 | + "\n", |
| 206 | + " def _printTree(self, node):\n", |
| 207 | + " if node is not None:\n", |
| 208 | + " self._printTree(node.left)\n", |
| 209 | + " print(str(node.data) + ' ')\n", |
| 210 | + " self._printTree(node.right)" |
| 211 | + ] |
| 212 | + }, |
| 213 | + { |
| 214 | + "cell_type": "code", |
| 215 | + "execution_count": null, |
| 216 | + "id": "999b1e9c", |
| 217 | + "metadata": {}, |
| 218 | + "outputs": [], |
| 219 | + "source": [] |
| 220 | + } |
| 221 | + ], |
| 222 | + "metadata": { |
| 223 | + "kernelspec": { |
| 224 | + "display_name": "Python 3 (ipykernel)", |
| 225 | + "language": "python", |
| 226 | + "name": "python3" |
| 227 | + }, |
| 228 | + "language_info": { |
| 229 | + "codemirror_mode": { |
| 230 | + "name": "ipython", |
| 231 | + "version": 3 |
| 232 | + }, |
| 233 | + "file_extension": ".py", |
| 234 | + "mimetype": "text/x-python", |
| 235 | + "name": "python", |
| 236 | + "nbconvert_exporter": "python", |
| 237 | + "pygments_lexer": "ipython3", |
| 238 | + "version": "3.9.12" |
| 239 | + } |
| 240 | + }, |
| 241 | + "nbformat": 4, |
| 242 | + "nbformat_minor": 5 |
| 243 | +} |
0 commit comments