All pastes #81376 Raw Edit

yokken

public python v1 · immutable
·published 2006-07-07 13:22 UTC
rendered paste body
# project!# a simple pong game# with drawn rectangles# and a small square## by dan cranston and justin davisz=1y = 0q = 0import timeimport randomfrom Tkinter import *import tkFontdef zstop():    global z    z=0root = Tk()root.title("leet pong :: by cranston and davis")canvas = Canvas(root,width=600,height=400,bg="black")canvas.grid(row=1,column=1)font = tkFont.Font(family="Verdana",size=48,weight="bold")boundary = canvas.create_line(300,0,300,400,width=10,fill="white")#scoresp1score = 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)s1text = canvas.create_text(150,50,text=y,fill="white",font=font)s2text = canvas.create_text(450,50,text=q,fill="white",font=font)#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",outline="black")        canvas.update()class rightbar:    def __init__(self):        """make the right bar for pong"""        self.id = canvas.create_rectangle(575,150,590,250,fill="white",outline="black")        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",width=0,outline="white")        canvas.update()    def move(self):        global p1score,p2score,y,z,q,s1text,s2text        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) >= 580 ):            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                if(q == 9 or y == 9):                    end = Label(root,text="GAME OVER",font=font)                    end.grid(row=1,column=1)                    ball.canvas.after_cancel(ball)                    canvas.delete(ball)                else:                    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.canvas.delete(s1text)                    s1text = canvas.create_text(150,50,text=y,fill="white",font=font)                    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(-5,5)                    self.dy = random.randint(-5,5)                    while(self.dx == 0):                        self.dx = random.randint(-5,5)                    time.sleep(1)                        elif( (self.x + self.dx) <= 20 ):            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                if(q == 9 or y == 9):                    end = Label(root,text="GAME OVER",font=font)                    end.grid(row=1,column=1)                    ball.canvas.after_cancel(ball)                    canvas.delete(ball)                else:                    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.canvas.delete(s2text)                    s2text = canvas.create_text(450,50,text=q,fill="white",font=font)                    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(-5,5)                    self.dy = random.randint(-5,5)                    while(self.dx == 0):                        self.dx = random.randint(-5,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()        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(-5,5),random.randint(-5,5))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 makeButton(event):    button = Button(root,text="Quit",command=root.quit,relief="groove")    button.grid(row=2,column=1)def buttonInit():    root.bind("<Up>",rbUp)    root.bind("<Down>",rbDown)    root.bind("w",lbUp)    root.bind("s",lbDown)    root.bind("`",makeButton)buttonInit()canvas.update()ball.canvas.after(10,ball.move)try:    mainloop()except:    passroot.destroy()