|
| 1 | +'''Modifications are : |
| 2 | + 1. The player will always win without even playing! |
| 3 | +''' |
| 4 | +from tkinter import * |
| 5 | +import time |
| 6 | +import random |
| 7 | + |
| 8 | +root = Tk() |
| 9 | +root.title("Brick") |
| 10 | +root.geometry("500x570") |
| 11 | +root.resizable(0, 0) |
| 12 | +root.wm_attributes("-topmost", 1) |
| 13 | +canvas = Canvas(root, width=500, height=500, bd=0, highlightthickness=0, highlightbackground="Red", bg="Black") |
| 14 | +canvas.pack(padx=10, pady=10) |
| 15 | +score = Label(height=50, width=80, text="Score: 00", font="Consolas 14 bold") |
| 16 | +score.pack(side="left") |
| 17 | +root.update() |
| 18 | + |
| 19 | + |
| 20 | +class Ball: |
| 21 | + def __init__(self, canvas, color, paddle, bricks, score): |
| 22 | + self.bricks = bricks |
| 23 | + self.canvas = canvas |
| 24 | + self.paddle = paddle |
| 25 | + self.score = score |
| 26 | + self.bottom_hit = False |
| 27 | + self.hit = 0 |
| 28 | + self.id = canvas.create_oval(10, 10, 25, 25, fill=color, width=1) |
| 29 | + self.canvas.move(self.id, 230, 461) |
| 30 | + start = [4, 3.8, 3.6, 3.4, 3.2, 3, 2.8, 2.6] |
| 31 | + random.shuffle(start) |
| 32 | + #print(start) |
| 33 | + self.x = start[0] |
| 34 | + self.y = -start[0] |
| 35 | + self.canvas.move(self.id, self.x, self.y) |
| 36 | + self.canvas_height = canvas.winfo_height() |
| 37 | + self.canvas_width = canvas.winfo_width() |
| 38 | + |
| 39 | + def brick_hit(self, pos): |
| 40 | + for brick_line in self.bricks: |
| 41 | + for brick in brick_line: |
| 42 | + brick_pos = self.canvas.coords(brick.id) |
| 43 | + #print(brick_pos) |
| 44 | + try: |
| 45 | + if pos[2] >= brick_pos[0] and pos[0] <= brick_pos[2]: |
| 46 | + if pos[3] >= brick_pos[1] and pos[1] <= brick_pos[3]: |
| 47 | + canvas.bell() |
| 48 | + self.hit += 1 |
| 49 | + self.score.configure(text="Score: " + str(self.hit)) |
| 50 | + self.canvas.delete(brick.id) |
| 51 | + return True |
| 52 | + except: |
| 53 | + continue |
| 54 | + return False |
| 55 | + |
| 56 | + |
| 57 | + def paddle_hit(self, pos): |
| 58 | + paddle_pos = self.canvas.coords(self.paddle.id) |
| 59 | + if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]: |
| 60 | + if pos[3] >= paddle_pos[1] and pos[1] <= paddle_pos[3]: |
| 61 | + #print("paddle hit") |
| 62 | + return True |
| 63 | + return False |
| 64 | + |
| 65 | + def draw(self): |
| 66 | + self.canvas.move(self.id, self.x, self.y) |
| 67 | + pos = self.canvas.coords(self.id) |
| 68 | + #print(pos) |
| 69 | + start = [4, 3.8, 3.6, 3.4, 3.2, 3, 2.8, 2.6] |
| 70 | + random.shuffle(start) |
| 71 | + if self.brick_hit(pos): |
| 72 | + self.y = start[0] |
| 73 | + if pos[1] <= 0: |
| 74 | + self.y = start[0] |
| 75 | + if pos[3] >= self.canvas_height: |
| 76 | + self.bottom_hit = True |
| 77 | + if pos[0] <= 0: |
| 78 | + self.x = start[0] |
| 79 | + if pos[2] >= self.canvas_width: |
| 80 | + self.x = -start[0] |
| 81 | + if self.paddle_hit(pos): |
| 82 | + self.y = -start[0] |
| 83 | + |
| 84 | + |
| 85 | +class Paddle: |
| 86 | + def __init__(self, canvas, color): |
| 87 | + self.canvas = canvas |
| 88 | + self.id = canvas.create_rectangle(-1000, 0, 1000, 10, fill=color) |
| 89 | + self.canvas.move(self.id, 200, 485) |
| 90 | + self.x = 0 |
| 91 | + self.pausec=0 |
| 92 | + self.canvas_width = canvas.winfo_width() |
| 93 | + self.canvas.bind_all("<Left>", self.turn_left) |
| 94 | + self.canvas.bind_all("<Right>", self.turn_right) |
| 95 | + self.canvas.bind_all("<space>", self.pauser) |
| 96 | + |
| 97 | + |
| 98 | + def draw(self): |
| 99 | + pos = self.canvas.coords(self.id) |
| 100 | + #print(pos) |
| 101 | + if pos[0] + self.x <= 0: |
| 102 | + self.x = 0 |
| 103 | + if pos[2] + self.x >= self.canvas_width: |
| 104 | + self.x = 0 |
| 105 | + self.canvas.move(self.id, self.x, 0) |
| 106 | + |
| 107 | + def turn_left(self, event): |
| 108 | + self.x = -3.5 |
| 109 | + |
| 110 | + def turn_right(self, event): |
| 111 | + self.x = 3.5 |
| 112 | + |
| 113 | + def pauser(self,event): |
| 114 | + self.pausec+=1 |
| 115 | + if self.pausec==2: |
| 116 | + self.pausec=0 |
| 117 | + |
| 118 | + |
| 119 | +class Bricks: |
| 120 | + def __init__(self, canvas, color): |
| 121 | + self.canvas = canvas |
| 122 | + self.id = canvas.create_oval(5, 5, 25, 25, fill=color, width=2) |
| 123 | + |
| 124 | + |
| 125 | +playing = False |
| 126 | + |
| 127 | + |
| 128 | +def start_game(event): |
| 129 | + global playing |
| 130 | + if playing is False: |
| 131 | + playing = True |
| 132 | + score.configure(text="Score: 00") |
| 133 | + canvas.delete("all") |
| 134 | + BALL_COLOR = ["red", "yellow", "white"] |
| 135 | + BRICK_COLOR = ["PeachPuff3", "dark slate gray", "rosy brown", "light goldenrod yellow", "turquoise3", "salmon", |
| 136 | + "light steel blue", "dark khaki", "pale violet red", "orchid", "tan", "MistyRose2", |
| 137 | + "DodgerBlue4", "wheat2", "RosyBrown2", "bisque3", "DarkSeaGreen1"] |
| 138 | + random.shuffle(BALL_COLOR) |
| 139 | + paddle = Paddle(canvas, "blue") |
| 140 | + bricks = [] |
| 141 | + for i in range(0, 5): |
| 142 | + b = [] |
| 143 | + for j in range(0, 19): |
| 144 | + random.shuffle(BRICK_COLOR) |
| 145 | + tmp = Bricks(canvas, BRICK_COLOR[0]) |
| 146 | + b.append(tmp) |
| 147 | + bricks.append(b) |
| 148 | + |
| 149 | + for i in range(0, 5): |
| 150 | + for j in range(0, 19): |
| 151 | + canvas.move(bricks[i][j].id, 25 * j, 25 * i) |
| 152 | + |
| 153 | + ball = Ball(canvas, BALL_COLOR[0], paddle, bricks, score) |
| 154 | + root.update_idletasks() |
| 155 | + root.update() |
| 156 | + |
| 157 | + time.sleep(1) |
| 158 | + while 1: |
| 159 | + if paddle.pausec !=1: |
| 160 | + try: |
| 161 | + canvas.delete(m) |
| 162 | + del m |
| 163 | + except: |
| 164 | + pass |
| 165 | + if not ball.bottom_hit: |
| 166 | + ball.draw() |
| 167 | + paddle.draw() |
| 168 | + root.update_idletasks() |
| 169 | + root.update() |
| 170 | + time.sleep(0.01) |
| 171 | + if ball.hit==95: |
| 172 | + canvas.create_text(250, 250, text="YOU WON !!", fill="yellow", font="Consolas 24 ") |
| 173 | + root.update_idletasks() |
| 174 | + root.update() |
| 175 | + playing = False |
| 176 | + break |
| 177 | + else: |
| 178 | + canvas.create_text(250, 250, text="GAME OVER!!", fill="red", font="Consolas 24 ") |
| 179 | + root.update_idletasks() |
| 180 | + root.update() |
| 181 | + playing = False |
| 182 | + break |
| 183 | + else: |
| 184 | + try: |
| 185 | + if m==None:pass |
| 186 | + except: |
| 187 | + m=canvas.create_text(250, 250, text="PAUSE!!", fill="green", font="Consolas 24 ") |
| 188 | + root.update_idletasks() |
| 189 | + root.update() |
| 190 | + |
| 191 | + |
| 192 | +root.bind_all("<Return>", start_game) |
| 193 | +canvas.create_text(250, 250, text="Press Enter to start Game!!", fill="red", font="Consolas 18") |
| 194 | +j=canvas.find_all() |
| 195 | +root.mainloop() |
0 commit comments