Ball Bat Game
Ball Bat Game
import time
canvasWidth = 600
canvasHeight = 500
window = Tk()
canvas = Canvas(window, width=canvasWidth, height=canvasHeight, bg="green")
canvas.pack()
label1=Label(window,text="SCORE",font=("Arial",25),bg="cyan",fg="orange")
label1.pack()
label=Label(window,text="0",font=("Arial",25),bg="grey",fg="red")
label.pack()
#Bouncing Ball
ballMoveX = 4
ballMoveY = -4
setBatTop = canvasHeight-40
setBatBottom = canvasHeight-30
score=0
def move_ball():
GameOver=False
global score
global ballMoveX
global ballMoveY
(ballLeft,ballTop,ballRight,ballBottom) = canvas.coords(ball)
(batLeft, batTop, batRight,batBottom) = canvas.coords(bat)
if ballMoveX > 0 and ballRight > canvasWidth:
ballMoveX = -ballMoveX
if ballMoveX < 0 and ballLeft < 0:
ballMoveX = -ballMoveX
if ballMoveY < 0 and ballTop < 0:
ballMoveY = -ballMoveY
if ballMoveY > 0 and ballBottom > setBatTop and ballBottom < setBatBottom:
(batLeft, batTop, batRight, batBottom) = canvas.coords(bat)
if ballRight > batLeft and ballLeft < batRight:
ballMoveY = -ballMoveY
score=score+1
label.configure(text=str(score))
canvas.move(ball, ballMoveX, ballMoveY)
if ballTop>canvasHeight:
labelGameOver=True
canvas.create_text(300,300,fill="red",text="Game over (:",font=("Arial",30))
canvas.pack()
window.update()
time.sleep(3)
window.destroy()
def main_loop():
while True:
move_ball()
window.update()
time.sleep(0.02)
window.bind("<KeyPress>",on_key_press)
main_loop()