# project! # a simple pong game # with drawn rectangles # and a small square # # by dan cranston and justin davis z=1 y = 0 q = 0 import time import random from Tkinter import * import tkFont def zstop(): global z z=0 root = Tk() root.title("l33t pong :: by cranston and davis") canvas = Canvas(root,width=600,height=400,bg="black") canvas.grid(row=1,column=1) font = tkFont.Font(family="Arial",size=36,weight="bold") #scores p1score = IntVar() p1score.set(0) p2score = IntVar() p2score.set(0) score1 = Label(root,text=("Player 1: "+str(p1score.get())),font=font) score1.grid(row=4,column=1) button = Button(root,text="Quit",command=root.quit,relief="groove") button.grid(row=2,column=1) score2 = Label(root,text=("Player 2: "+str(p2score.get())),font=font) score2.grid(row=5,column=1) class leftbar: """make the left bar for pong""" def __init__(self): self.id = canvas.create_rectangle(10,150,25,250,fill="white") canvas.update() class rightbar: def __init__(self): """make the right bar for pong""" self.id = canvas.create_rectangle(575,150,590,250,fill="white") canvas.update() class Ball: def __init__(self,canvas,x,y,dx,dy): """make the ball for pong""" self.canvas = canvas self.x = x self.y = y self.dx = dx self.dy = dy self.event = 0 self.id = canvas.create_rectangle(self.x-7.5,self.y-7.5,self.x+7.5,self.y+7.5,fill="white") canvas.update() def move(self): global p1score,p2score,y,z,q canvas = self.canvas # if(self.x+self.dx > 595 or self.x+self.dx < 5): # self.dx = -self.dx if(self.y+self.dy > 390 or self.y+self.dy < 10): self.dy = -self.dy if( (self.x + self.dx) >= 585 ): ploc = list(canvas.bbox(rbar.id)) print "Bouncing off 2", ploc if( ploc[1] <= (self.y+self.dy) <= ploc[3] ): # Hit the paddle self.dx = -self.dx if(self.dx>0): self.dx += 1 else: self.dx -= 1 else: # Missed the paddle y += 1 p1score.set(y) self.canvas.delete(self.id) self.x = 300 self.y = 200 time.sleep(.05) score1 = Label(root,text=("Player 1: "+str(p1score.get())),font=font) score1.grid(row=4,column=1) self.id = canvas.create_rectangle(self.x-7.5,self.y-7.5,self.x+7.5,self.y+7.5,fill="white") self.dx = random.randint(3,5) self.dy = random.randint(3,5) time.sleep(1) elif( (self.x + self.dx) <= 15 ): ploc = list(canvas.bbox(lbar.id)) print "Bouncing off 1", ploc if( ploc[1] <= (self.y+self.dy) <= ploc[3] ): # Hit the paddle self.dx = -self.dx if(self.dx>0): self.dx += 1 else: self.dx -= 1 else: # Missed the paddle q += 1 p2score.set(q) self.canvas.delete(self.id) self.x = 300 self.y = 200 time.sleep(.05) score2 = Label(root,text=("Player 2: "+str(p2score.get())),font=font) score2.grid(row=5,column=1) self.id = canvas.create_rectangle(self.x-7.5,self.y-7.5,self.x+7.5,self.y+7.5,fill="white") self.dx = random.randint(3,5) self.dy = random.randint(3,5) time.sleep(1) # balloc = list(self.canvas.bbox(self.id)) # debug error checking, etc # print balloc # if( (10 > balloc[0]) or (balloc[2] > 590) ): # print "Failure", balloc, "x=",self.x, self.dx, "y=", self.y, self.dy lbloc = list(self.canvas.bbox(lbar.id)) rbloc = list(self.canvas.bbox(rbar.id)) self.canvas.move(self.id,self.dx,self.dy) self.canvas.update() # time.sleep(.01) self.x = self.x+self.dx self.y = self.y+self.dy self.event = self.canvas.after(1,self.move) lbar = leftbar() rbar = rightbar() ball = Ball(canvas,300,200,random.randint(3,5),random.randint(3,5)) #ball = Ball(canvas,300,200, 3,0) def lbUp(event): lbloc = list(canvas.bbox(lbar.id)) print "Left bar loc: ",lbloc if(lbloc[1]-10<=0): canvas.move(lbar.id,0,(1-lbloc[1])) else: canvas.move(lbar.id,0,-30) def lbDown(event): lbloc = list(canvas.bbox(lbar.id)) print "Left bar loc: ",lbloc if(lbloc[3]+10>=400): canvas.move(lbar.id,0,(400-lbloc[3])) else: canvas.move(lbar.id,0,30) def rbUp(event): rbloc = list(canvas.bbox(rbar.id)) print "Right bar loc: ",rbloc if(rbloc[1]-10<=0): canvas.move(rbar.id,0,(1-rbloc[1])) else: canvas.move(rbar.id,0,-30) def rbDown(event): rbloc = list(canvas.bbox(rbar.id)) print "Right bar loc: ",rbloc if(rbloc[3]+10>=400): canvas.move(rbar.id,0,(400-rbloc[3])) else: canvas.move(rbar.id,0,30) def buttonInit(): root.bind("",rbUp) root.bind("",rbDown) root.bind("w",lbUp) root.bind("s",lbDown) buttonInit() canvas.update() ball.canvas.after(10,ball.move) try: mainloop() except: pass root.destroy()