I did the Brickbreaker project, it resulted so difficult to me.
I put a score on the top left, but the numbers are overwritten.
"""
File: brickbreaker.py
----------------
YOUR DESCRIPTION HERE
"""
import tkinter
import time
import random
# How big is the playing area?
CANVAS_WIDTH = 605 # Width of drawing canvas in pixels
CANVAS_HEIGHT = 700 # Height of drawing canvas in pixels
# Constants for the bricks
N_ROWS = 8 # How many rows of bricks are there?
N_COLS = 10 # How many columns of bricks are there?
SPACING = 5 # How much space is there between each brick?
BRICK_START_Y = 50 # The y coordinate of the top-most brick
BRICK_HEIGHT = 20 # How many pixels high is each brick
BRICK_WIDTH = (CANVAS_WIDTH - (N_COLS+1) * SPACING ) / N_COLS
# Constants for the ball and paddle
BALL_SIZE = 40
PADDLE_Y = CANVAS_HEIGHT - 40
PADDLE_WIDTH = 80
def main():
canvas = make_canvas(CANVAS_WIDTH, CANVAS_HEIGHT, 'Brick Breaker')
bricks = []
for col in range(N_COLS):
for row in range(N_ROWS):
draw_brick(canvas, col, row)
bricks.append(draw_brick)
score = []
turns = 0
ball = make_ball(canvas)
# we now make a paddle
paddle = canvas.create_rectangle(0, PADDLE_Y, PADDLE_WIDTH, CANVAS_HEIGHT - 20, fill="blue")
dx = 10
dy = 7
# If ball hits the bottom three times, the game is over
while turns < 3:
# Get the mouse location and react to it
mouse_x = canvas.winfo_pointerx()
canvas.moveto(paddle, mouse_x, PADDLE_Y)
canvas.move(ball, dx, dy)
if hit_left_wall(canvas, ball) or hit_right_wall(canvas, ball):
dx *= -1
if hit_bricks(canvas, ball, paddle, score):
dy *= -1
if hit_top_wall(canvas, ball):
dy *= -1
if hit_bottom_wall(canvas, ball):
turns += 1
time.sleep(1)
canvas.delete(ball)
ball = make_ball(canvas)
if turns == 3:
canvas.delete(ball)
canvas.delete(paddle)
score_msg = canvas.create_text(302, 400, text="TOTAL SCORE: " + str(len(score)), font="Arial 20 ", anchor="s")
# redraw canvas
canvas.update()
# pause
time.sleep(1 / 50.)
canvas.mainloop()
def draw_brick(canvas, col, row):
x = col * (SPACING + BRICK_WIDTH) + SPACING
y = row * (SPACING + BRICK_HEIGHT) + BRICK_START_Y
canvas.create_rectangle(x, y, x + (BRICK_WIDTH - 1), y + (BRICK_HEIGHT - 1), fill = 'red')
def make_ball(canvas):
x = (CANVAS_WIDTH - BALL_SIZE) / 2
y = (CANVAS_HEIGHT - BALL_SIZE) / 2
return canvas.create_oval(x, y, x + BALL_SIZE, y + BALL_SIZE, fill='yellow', outline='yellow')
def hit_paddle(canvas, ball, paddle):
paddle_coords = canvas.coords(paddle)
x1 = paddle_coords[0]
y1 = paddle_coords[1]
x2 = paddle_coords[2]
y2 = paddle_coords[3]
results = canvas.find_overlapping(x1, y1, x2, y2)
return len(results) > 1
def hit_bricks(canvas, ball, paddle, score):
ball_coords = canvas.coords(ball)
x_1 = ball_coords[0]
y_1 = ball_coords[1]
x_2 = ball_coords[2]
y_2= ball_coords[3]
results = canvas.find_overlapping(x_1, y_1, x_2, y_2)
for object in results:
if object == paddle or object == ball:
return len(results) > 1
else:
core_msg_up = canvas.create_text(100, 40, text="", font="Arial 12 ",
anchor="s", )
score_msg_up = canvas.create_text(50, 40, text="SCORE: ", font="Arial 12 ",
anchor="s", )
canvas.delete(object)
score.append(canvas.delete(object))
msg = len(score)
canvas.itemconfig(core_msg_up, text = str(msg))
def hit_left_wall(canvas, object):
return get_left_x(canvas, object) <= 0
def hit_top_wall(canvas, object):
return get_top_y(canvas, object) <= 0
def hit_right_wall(canvas, object):
return get_right_x(canvas, object) >= CANVAS_WIDTH
def hit_bottom_wall(canvas, object):
return get_bottom_y(canvas, object) >= CANVAS_HEIGHT
######## These helper methods use "lists" ###########
### Which is a concept you will learn Monday ###########
def get_left_x(canvas, object):
return canvas.coords(object)[0]
def get_top_y(canvas, object):
return canvas.coords(object)[1]
def get_right_x(canvas, object):
return canvas.coords(object)[2]
def get_bottom_y(canvas, object):
return canvas.coords(object)[3]
def make_canvas(width, height, title):
"""
DO NOT MODIFY
Creates and returns a drawing canvas
of the given int size with a blue border,
ready for drawing.
"""
top = tkinter.Tk()
top.minsize(width=width, height=height)
top.title(title)
canvas = tkinter.Canvas(top, width=width + 1, height=height + 1)
canvas.pack()
return canvas
if __name__ == '__main__':
main()
If the ball hits the bottom three times, the game is over.