paddle game final

by Aneta X

Description

I submit my paddle game where the goal is to hit as many bricks as possible. The player gets one point for hitting each orange bricks and zero points for hitting the red brick. Each time he hits the red brick a message 'BOOM' appears. When the score reaches 100 and 200 the ball increases its speed. https://youtu.be/mMfgQTMAZGk """ File: paddle_game.py ---------------- """ import tkinter import random import time CANVAS_WIDTH = 600 # Width of drawing canvas in pixels CANVAS_HEIGHT = 600 N_ROWS = 15 N_COLS = 17 SPACING = 3 PADDLE_Y = CANVAS_HEIGHT - 40 PADDLE_WIDTH = 100 BRICK_WIDTH = (CANVAS_WIDTH - (N_COLS + 1) * SPACING) / N_COLS BRICK_HEIGHT = 20 BALL_SIZE = 18 def main(): canvas = make_canvas(CANVAS_WIDTH, CANVAS_HEIGHT, 'Paddle Game') start_yy = CANVAS_HEIGHT / 2 - BALL_SIZE / 2 end_y = start_yy + BALL_SIZE object = [] start_x = SPACING start_y = 1 # TODO: 1. we now make a paddle and a brick wall canvas.update() for y in range(N_ROWS): for x in range(N_COLS): upper_corner_x = start_x + (BRICK_WIDTH + SPACING) * x upper_corner_y = start_y + (BRICK_HEIGHT + SPACING) * y bricks = canvas.create_rectangle(upper_corner_x, upper_corner_y, upper_corner_x + BRICK_WIDTH, upper_corner_y + BRICK_HEIGHT, fill=get_color()) canvas.update() paddle = canvas.create_rectangle(0, PADDLE_Y, PADDLE_WIDTH, CANVAS_HEIGHT - 20, fill="blue") ball = canvas.create_oval(CANVAS_WIDTH/2 - BALL_SIZE/2, CANVAS_HEIGHT / 2 - BALL_SIZE/2, CANVAS_WIDTH/2 + BALL_SIZE/2, CANVAS_HEIGHT / 2 + BALL_SIZE/2, fill='red', outline='red') canvas.update() score = 0 foo=[10,12,-10,-12] dx=random.choice(foo) dy = random.choice(foo) print("Score: ") while True: # TODO: 2. 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_top_wall(canvas, ball): dy *= -1 # TODO: 3. check if the ball hits the paddle if hit_paddle(canvas, ball, paddle) : dy *= -1 # TODO: 4. check if the ball hits the wall and delete brick if hit_bottom_wall(canvas,ball): canvas.create_text(CANVAS_WIDTH / 5, CANVAS_HEIGHT -70 , anchor="w", font=('Arial', 40), text='You lost. Score: ' + str(score)) print("you lost "+ str(score)) canvas.mainloop() for y in range(N_ROWS): for x in range(N_COLS): upper_corner_x = start_x + (BRICK_WIDTH + SPACING) * x upper_corner_y = start_y + (BRICK_HEIGHT + SPACING) * y x1 = upper_corner_x y1 = upper_corner_y x2 = upper_corner_x + BRICK_WIDTH y2 = upper_corner_y + BRICK_HEIGHT results = canvas.find_overlapping(x1, y1, x2, y2) if len(results) >1: dy += -1 for object in results: if object != paddle and object != ball: if random_color() == 'red' or random_color() == 'orange': score += 1 canvas.delete(object) print(score) if random_color() == 'red': score += -1 canvas.create_text(upper_corner_x, upper_corner_y, font=('Arial', 30), text='BOOM') canvas.update() if score < 100: print("you win") canvas.update() elif score >= 100: dx += 1 dy += 2 canvas.update() elif score >= 200: dx += 1 dy += 2 canvas.update() elif object == 0: canvas.create_text(CANVAS_WIDTH / 3, CANVAS_HEIGHT / 5, anchor="w", font=('Arial', 60), text='YOU WON! ') else: print("game over") print(score) canvas.create_text(CANVAS_WIDTH/3, CANVAS_HEIGHT / 2, anchor="w", font=('Arial', 40), text='score: ' + str(score)) canvas.create_text(CANVAS_WIDTH/3, CANVAS_HEIGHT / 5, anchor="w", font=('Arial', 60), text='GAME OVER ') canvas.delete(object) canvas.update() canvas.mainloop() # redraw canvaspython canvas.update() # pause time.sleep(1/30.) def get_color(): for col in range(CANVAS_WIDTH): for row in range(CANVAS_HEIGHT): return random_color() def random_color(): colors = ['orange','orange','orange','red','orange', 'orange'] rand_color = random.choice(colors) return rand_color def hit_paddle(canvas, ball, paddle): # TODO: paddle_coords is of type list. Come to lecture Monday! 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_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] ######## DO NOT MODIFY ANY CODE BELOW THIS LINE ########### # This function is provided to you and should not be modified. # It creates a window that contains a drawing canvas that you # will use to make your drawings. 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() Another project is COVID data analysis. I load the COVID data from the USA and Italy and look for daily positive cases and total cases. https://youtu.be/dfPf-H3iYGw # Data comes from Johns Hopkins Univeristy. Thanks to them for making this public! # https://github.com/CSSEGISandData/COVID-19 # You can find data beyond cumulative cases there! ''' Test your code by analysing total confirmed cases over time Each line in the file represents one day. The first value is confirmed cases on Jan 22nd. The number of confirmed cases is "cumulative" meaning that it is the total number of cases up until the current day. It will never go down! ''' import random ITALY_PATH = 'italy.txt' USA_PATH = 'usa.csv' # This directory has files for all countries if you want to explore further DATA_DIR = 'confirmed' FILE_NAME = 'cswords.txt' def main(): covid_cases_it = load_data_it() covid_cases_usa = load_data_usa() def load_data_usa(): covid_list_usa = [] count = 0 #Displaying the plot with open(USA_PATH, 'r') as usa: for line_usa in usa.readlines(): cases_usa = line_usa.strip() covid_list_usa.append(int(cases_usa)) sm_usa = sum(covid_list_usa) print('There have been '+ str(sm_usa) + ' total cases in USA up until May 9th') total_days_usa = len(covid_list_usa) print('The file has ' + str(total_days_usa)+ ' lines! ' ) for cases in covid_list_usa: if cases != 0: count += 1 print('There were ' + str(count)+ ' days with positive cases in USA' ) for i in range(1,len(covid_list_usa)): totals =[] current = covid_list_usa[i] prev = covid_list_usa[i-1] total = current - prev print(str(current) + ' # There have been ' + str(total) + ' new cases in USA') totals.append(total) return totals def load_data_it(): covid_list_it = [] count = 0 with open(ITALY_PATH, 'r') as it: for line_it in it.readlines(): cases_it = line_it.strip() covid_list_it.append(int(cases_it)) sm_it = sum(covid_list_it) print('There have been '+ str(sm_it) + ' total cases in Italy up until May 9th') total_days_it = len(covid_list_it) print('The file has ' + str(total_days_it)+ ' lines! ' ) for cases in covid_list_it: if cases != 0: count += 1 print('There were ' + str(count)+ ' days with positive cases in Italy' ) # new cases on a given day are: # total cases on that day - total cases on the previous day for i in range(1,len(covid_list_it)): totals= [] current = covid_list_it[i] prev = covid_list_it[i-1] total = current - prev print(str(current) + ' # There have been ' + str(total) + ' new cases in Italy') totals.append(total) return totals if __name__ == '__main__': main() I uploaded the code here but for some reason it doesn't work