0% found this document useful (0 votes)
9 views2 pages

Ball Bat Game

Uploaded by

csc sishya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Ball Bat Game

Uploaded by

csc sishya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

from tkinter import*

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()

bat = canvas.create_rectangle(10, 470, 100, 460, fill="dark turquoise")


ball = canvas.create_oval(25, 480, 45, 455, fill="yellow")

#Move bat left and right


(batLeft, batTop, batRight,batBottom) = canvas.coords(bat)
def on_key_press(event):
if event.keysym == "Left":
canvas.move(bat, -20,0)
elif event.keysym == "Right":
canvas.move(bat,20,0)
window.update()

#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()

You might also like