Can anyone help me modify the code so the ball starts in random directions
phamtony33 at gmail.com
phamtony33 at gmail.com
Fri Dec 11 18:19:26 EST 2015
Can anyone direct me in the direction where to start the code for the randomized of the ball to start.
from Tkinter import *
window = Tk()
canvas = Canvas(window, width=500, height=500, background="green")
canvas.pack()
def mouse_paddle_move(event):
mouseY = event.y
current_coords = canvas.coords("paddle")
x1 = current_coords[0]
y1 = current_coords[1]
x2 = current_coords[2]
y2 = current_coords[3]
height = (y2 - y1)
canvas.coords("paddle", x1, mouseY - (height/2), x2, mouseY + (height/2))
def move_ball(speed_x, speed_y, score):
box = canvas.bbox("ball")
x1 = box[0]
y1 = box[1]
x2 = box[2]
y2 = box[3]
if x2 >= 500:
canvas.create_text(250, 250, text="Game Over!")
return
box = canvas.bbox("paddle")
px1 = box[0]
py1 = box[1]
px2 = box[2]
py2 = box[3]
if x2 > px1 and y2 > py1 and y1 < py2:
speed_x = -1.2 * (abs(speed_x))
speed_y = 1.2 * speed_y
score = score + 1
canvas.itemconfig("score_text", text="Score: " + str(score))
if x1 <= 0:
speed_x = -1 * speed_x
if y2 >= 500 or y1 <= 0:
speed_y = -1 * speed_y
canvas.move("ball", speed_x, speed_y)
canvas.after(30, move_ball, speed_x, speed_y, score)
canvas.create_oval(225, 225, 275, 275, fill="blue", tags="ball")
canvas.create_rectangle(450, 200, 455, 300, fill="red", tags="paddle")
canvas.create_text(250, 10, tags = "score_text", text="Score: 0")
move_ball(-10, 7, 0)
canvas.bind('<Motion>', mouse_paddle_move)
mainloop()
More information about the Python-list
mailing list