Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import random
- WIDTH = 1000
- HEIGHT = 500
- R_W = 10
- R_H = 100
- BALL_RADIUS = 35
- root = Tk()
- c = Canvas(root, width=WIDTH, height=HEIGHT, background="#006400")
- c.pack()
- c.create_line(R_W, 0, R_W, HEIGHT, fill="#006400")
- c.create_line(WIDTH-R_W, 0, WIDTH-R_W, HEIGHT, fill="#006400")
- c.create_line(WIDTH/2, 0, WIDTH/2, HEIGHT, fill="white")
- BALL = c.create_oval(WIDTH/2+BALL_RADIUS/2,
- HEIGHT/2+BALL_RADIUS/2,
- WIDTH/2-BALL_RADIUS/2,
- HEIGHT/2-BALL_RADIUS/2,
- fill="white")
- LEFT_PAD = c.create_line(R_W, 0, R_W, R_H+1, width=R_W, fill="red")
- RIGHT_PAD = c.create_line(WIDTH-R_W/2, 0, WIDTH-R_W/2,
- R_H+10, width=R_W, fill="blue")
- BALL_X_CHANGE = 10
- BALL_Y_CHANGE = 0
- def move_ball():
- c.move(BALL, BALL_X_CHANGE, BALL_Y_CHANGE)
- def main():
- move_ball()
- root.after(30, main)
- main()
- PAD_SPEED = 20
- LEFT_PAD_SPEED = 0
- RIGHT_PAD_SPEED = 0
- def move_pads():
- PADS = {LEFT_PAD: LEFT_PAD_SPEED,
- RIGHT_PAD: RIGHT_PAD_SPEED}
- for pad in PADS:
- c.move(pad, 0, PADS[pad])
- if c.coords(pad)[1] < 0:
- c.move(pad, 0, -c.coords(pad)[1])
- elif c.coords(pad)[3] > HEIGHT:
- c.move(pad, 0, HEIGHT - c.coords(pad)[3])
- def main():
- move_ball()
- move_pads()
- root.after(30, main)
- c.focus_set()
- def movement_handler(event):
- global LEFT_PAD_SPEED, RIGHT_PAD_SPEED
- if event.keysym == "w":
- LEFT_PAD_SPEED = -PAD_SPEED
- elif event.keysym == "s":
- LEFT_PAD_SPEED = PAD_SPEED
- elif event.keysym == "Up":
- RIGHT_PAD_SPEED = -PAD_SPEED
- elif event.keysym == "Down":
- RIGHT_PAD_SPEED = PAD_SPEED
- c.bind("<KeyPress>", movement_handler)
- def stop_pad(event):
- global LEFT_PAD_SPEED, RIGHT_PAD_SPEED
- if event.keysym in "ws":
- LEFT_PAD_SPEED = 0
- elif event.keysym in ("Up", "Down"):
- RIGHT_PAD_SPEED = 0
- c.bind("<KeyRelease>", stop_pad)
- BALL_SPEED_UP = 1.04
- BALL_MAX_SPEED = 20
- BALL_X_SPEED = 10
- BALL_Y_SPEED = 10
- right_line_distance = WIDTH - R_W
- INITIAL_SPEED = 10
- def bounce(action):
- global BALL_X_SPEED, BALL_Y_SPEED
- if action == "strike":
- BALL_Y_SPEED = random.randrange(-10, 10)
- if abs(BALL_X_SPEED) < BALL_MAX_SPEED:
- BALL_X_SPEED *= -BALL_SPEED_UP
- else:
- BALL_X_SPEED = -BALL_X_SPEED
- else:
- BALL_Y_SPEED = -BALL_Y_SPEED
- def run_ball():
- global BALL_X_SPEED
- c.coords(BALL, WIDTH/2-BALL_RADIUS/2,
- HEIGHT/2-BALL_RADIUS/2,
- WIDTH/2+BALL_RADIUS/2,
- HEIGHT/2+BALL_RADIUS/2)
- BALL_X_SPEED = -(BALL_X_SPEED * -INITIAL_SPEED) / abs(BALL_X_SPEED)
- def move_ball():
- ball_left, ball_top, ball_right, ball_bot = c.coords(BALL)
- ball_center = (ball_top + ball_bot) / 2
- if ball_right + BALL_X_SPEED < right_line_distance and \
- ball_left + BALL_X_SPEED > R_W:
- c.move(BALL, BALL_X_SPEED, BALL_Y_SPEED)
- elif ball_right == right_line_distance or ball_left == R_W:
- if ball_right > WIDTH / 2:
- if c.coords(RIGHT_PAD)[1] < ball_center < c.coords(RIGHT_PAD)[3]:
- bounce("strike")
- else:
- run_ball()
- else:
- if c.coords(LEFT_PAD)[1] < ball_center < c.coords(LEFT_PAD)[3]:
- bounce("strike")
- else:
- run_ball()
- else:
- if ball_right > WIDTH / 2:
- c.move(BALL, right_line_distance-ball_right, BALL_Y_SPEED)
- else:
- c.move(BALL, -ball_left+R_W, BALL_Y_SPEED)
- if ball_top + BALL_Y_SPEED < 0 or ball_bot + BALL_Y_SPEED > HEIGHT:
- bounce("ricochet")
- root.mainloop()
Add Comment
Please, Sign In to add comment