Aqifmahmood.files.wordpress.com



Program implementation and testing – commented code (including the use of external libraries and APIs) and screenshots of key functionality testingimport _tkinterimport timeimport randomfrom Tkinter import*#importing python image libraryfrom PIL import ImageTkimport winsound#importing library for operating systemimport os, sys#setting up the GUIwindow = Tk()#setting up the window titlewindow.wm_title("Project")#obtaining project root folderpath = sys.path[0]canvas= Canvas(window,width=800, height=600, bg='light gray')canvas.pack(expand = YES, fill = BOTH)#play musicwinsound.PlaySound(path+"/resources/Lucky.wav", winsound.SND_ASYNC)#robot velocityvx = 0.0 # x velocityvy = 0.0 # y velocity#blue robot velocityvxb = 0.0 # x velocityvyb = 0.0 # y velocity#generate random speed variables #purple robot speedspeed = random.randint(1, 15) #orange robot speedspeedb = random.randint(1, 15)#add speed slidersslider1 =Scale(window,orient=HORIZONTAL,width=10,length=150.0,from_= 1,to= 15,tickinterval=2,resolution=1,sliderlength=25,label="Purple Robot Speed")slider1.set(speed)slider1.pack(padx=5, pady=0, side=LEFT)slider2 =Scale(window,orient=HORIZONTAL,width=10,length=150.0,from_= 1,to= 15,tickinterval=2,resolution=1,sliderlength=25,label="Orange Robot Speed")slider2.set(speedb)slider2.pack(padx=5, pady=0, side=LEFT)#adding the timer#seconds (right number)sec=0labels = Label(text=sec,font=("Helvetica", 36))labels.pack(padx=0, pady=0, side=RIGHT)#colonlabel = Label(text=":",font=("Helvetica", 36))label.pack(padx=0, pady=0, side=RIGHT)#minutes(left number)min=0labelm = Label(text=min,font=("Helvetica", 36))labelm.pack(padx=0, pady=0, side=RIGHT)global pausepause=False#pause functiondef pause(): global pause if pause == False: pause = True else: pause=Falseglobal movemove=False#press space to startdef Space(self): if move == False: pausebutton = Button(window, text= "Pause" ,command = pause, width = 10).pack() pause() global move move = True global vx vx = 0 #initialises horizontal speed global vy vy = speed global vxb vxb = 0 # vx for the second robot global vyb vyb = speedb #speed for second robotwindow.bind('<space>', Space) # space cant be used for anything else#title imagetitle1 = ImageTk.PhotoImage(file = path+"/resources/start.png")title2 = ImageTk.PhotoImage(file = path+"/resources/startx2.png")#startup animationwhile move==False: titlex=canvas.create_image(0, 0, image = title1, anchor = NW) time.sleep(0.5) canvas.update() titlex=canvas.create_image(0, 0, image = title2, anchor = NW) time.sleep(0.5) canvas.update()#background imagebg = ImageTk.PhotoImage(file = path+"/resources/bg.png")canvas.create_image(0, 0, image = bg, anchor = NW)#create end imageend = ImageTk.PhotoImage(file = path+"/resources/end.png")end2 = ImageTk.PhotoImage(file = path+"/resources/end2.png")#create first robot: #generate random sizesize= random.randint(20, 30)robot = canvas.create_oval(250,300,250+size,300+size,fill='purple')#create second robot: #generate random sizesizeb= random.randint(20, 30)robotb = canvas.create_oval(540,300,540+sizeb,300+sizeb,fill='orange')#create obstaclesob1 = canvas.create_rectangle(0,0,270,170,fill='brown')# coordinates x1,y1,x2,y2ob1x1, ob1y1, ob1x2, ob1y2 = canvas.coords(ob1)ob2 = canvas.create_rectangle(0,430,270,600,fill='brown')ob2x1, ob2y1, ob2x2, ob2y2 = canvas.coords(ob2)ob3 = canvas.create_rectangle(530,430,800,600,fill='brown')ob3x1, ob3y1, ob3x2, ob3y2 = canvas.coords(ob3)ob4 = canvas.create_rectangle(530,170,800,0,fill='brown')ob4x1, ob4y1, ob4x2, ob4y2 = canvas.coords(ob4)#create diagonalsdg1 = canvas.create_line(0,0,270,170,fill='black')dg2 = canvas.create_line(270,0,0,170,fill='black')dg3 = canvas.create_line(0,430,270,600,fill='black')dg4 = canvas.create_line(0,600,270,430,fill='black')dg5 = canvas.create_line(530,430,800,600,fill='black')dg6 = canvas.create_line(530,600,800,430,fill='black')dg7 = canvas.create_line(530,170,800,0,fill='black')dg8 = canvas.create_line(530,0,800,170,fill='black')#create chimneyscanvas.create_rectangle(200,80,240,100, fill="black",outline="grey",width=6)canvas.create_rectangle(560,80,600,100, fill="black",outline="grey",width=6)canvas.create_rectangle(200,500,240,520, fill="black",outline="grey",width=6)canvas.create_rectangle(560,500,600,520, fill="black",outline="grey",width=6)#create treestree1 = ImageTk.PhotoImage(file = path+"/resources/tree.png")canvas.create_image(120, 350, image = tree1, anchor = NW)tree2 = ImageTk.PhotoImage(file = path+"/resources/tree.png")canvas.create_image(600, 350, image = tree2, anchor = NW)tree3 = ImageTk.PhotoImage(file = path+"/resources/tree.png")canvas.create_image(600, 150, image = tree3, anchor = NW)tree4 = ImageTk.PhotoImage(file = path+"/resources/tree.png")canvas.create_image(120, 150, image = tree4, anchor = NW)tree5 = ImageTk.PhotoImage(file = path+"/resources/tree.png")canvas.create_image(270, 40, image = tree5, anchor = NW)tree6 = ImageTk.PhotoImage(file = path+"/resources/tree.png")canvas.create_image(470, 40, image = tree6, anchor = NW)tree7 = ImageTk.PhotoImage(file = path+"/resources/tree.png")canvas.create_image(270, 500, image = tree7, anchor = NW)tree8 = ImageTk.PhotoImage(file = path+"/resources/tree.png")canvas.create_image(470, 500, image = tree8, anchor = NW)# boundariesx_min = 0.0y_min = 0.0x_max = 800.0y_max = 600.0#traffic lights #left #supports1 = canvas.create_rectangle(200,200,220,200+62, fill='black') #lightstlight1 = canvas.create_oval(201,202,200+19,220,fill='grey')tlight2 = canvas.create_oval(201,222,200+19,240,fill='grey')tlight3 = canvas.create_oval(201,242,200+19,260,fill='green')traffic1=random.randint(10,25) #right #supports2 = canvas.create_rectangle(580,200,601,200+62, fill='black') #lightstlight4 = canvas.create_oval(581,202,600,220,fill='grey')tlight5 = canvas.create_oval(581,222,600,240,fill='grey')tlight6 = canvas.create_oval(581,242,600,260,fill='green')traffic2=random.randint(10,25)print traffic1print traffic2#object collision detectiondef objCollision(x1,x2,y1,y2,vx,vy,speed): edge=20 #obj1bottom if x2<ob1x2+edge and y1>ob1y2-edge and y1<ob1y2+edge: vx=speed vy=0 #obj4bottom if x2>ob4x1+edge and y1>ob1y2-edge and y1<ob1y2+edge: vx=0 vy=speed #obj3top if x2>ob3x1+edge and y2>ob3y1-edge and y2<ob3y1+edge: vx=-speed vy=0 #obj2top if x2<ob2x2+edge and y2>ob2y1-edge and y2<ob2y1+edge: vx=0 vy=-speed return(vx,vy)def zebra(x1,y1,x2,y2,ob2y1): if x2<330 and y2<ob2y1 and y1>225: return 1 return 0def zebra2(x1,y1,x2,y2,ob4x1,ob4y2,ob3y1): if x2>470 and y2<375 and y1>ob4y2: return 1 return 0red=0red2=0counter=0counter2=0tick=0#move robotwhile True: #loop for the pause button while pause == False: #this changes the seconds and minutes if tick%10==0 and tick <> 0: sec=sec+1 labels['text'] = sec labels.pack() if sec==60: sec=0 labels['text'] = sec min=min+1 labelm['text'] = min labels.pack() labelm.pack() traffic1=random.randint(10,25) traffic2=random.randint(10,25) print traffic1 print traffic2 #check time #checks if 1:30 has passed if min==1 and sec == 30: while sec==30: end_scr=canvas.create_image(0, 0, image = end, anchor = NW) canvas.update() time.sleep(1.5) end_scr=canvas.create_image(0, 0, image = end2, anchor = NW) canvas.update() time.sleep(0.5) #check for traffic light if counter <> 0: counter=counter-1 if zebra(x1,y1,x2,y2,ob2y1)==1: vx=0 vy=0 if zebra(x1b,y1b,x2b,y2b,ob2y1)==1: vxb=0 vyb=0 if sec==traffic1: counter=5 if red==0: tlight2 = canvas.create_oval(201,222,200+19,240,fill='gold') tlight3 = canvas.create_oval(201,242,200+19,260,fill='grey') else: tlight1 = canvas.create_oval(201,202,200+19,220,fill='red') tlight2 = canvas.create_oval(201,222,200+19,240,fill='grey') tlight3 = canvas.create_oval(201,242,200+19,260,fill='grey') red=1 if counter==0 and red ==1 : tlight1 = canvas.create_oval(201,202,200+19,220,fill='grey') tlight3 = canvas.create_oval(201,242,200+19,260,fill='green') red=0 if vx==0 and vy ==0: vx=save1 vy=save2 if vxb==0 and vyb ==0: vxb=save3 vyb=save4 canvas.update() #check for second traffic light if counter2 <> 0: counter2=counter2-1 if zebra2(x1,y1,x2,y2,ob4x1,ob4y2,ob3y1)==1: vx=0 vy=0 if zebra2(x1b,y1b,x2b,y2b,ob4x1,ob4y2,ob3y1)==1: vxb=0 vyb=0 if sec==traffic2: counter2=5 if red2==0: tlight5 = canvas.create_oval(581,222,600,240,fill='gold') tlight6 = canvas.create_oval(581,242,600,260,fill='grey') else: tlight4 = canvas.create_oval(581,202,600,220,fill='red') tlight5 = canvas.create_oval(581,222,600,240,fill='grey') tlight6 = canvas.create_oval(581,242,600,260,fill='grey') red2=1 if counter2==0 and red2 ==1: tlight4 = canvas.create_oval(581,202,600,220,fill='grey') tlight6 = canvas.create_oval(581,242,600,260,fill='green') red2=0 if vx==0 and vy ==0: vx=save1 vy=save2 if vxb==0 and vyb ==0: vxb=save3 vyb=save4 canvas.update() #update speed from sliders speed=slider1.get() slider1.pack() speedb=slider2.get() slider2.pack() #update from sliders #purple robot if(vx >0): vx=speed elif(vx<0): vx=-speed if(vy >0): vy=speed elif(vy<0): vy=-speed #orange robot if(vxb >0): vxb=speedb elif(vxb<0): vxb=-speedb if(vyb >0): vyb=speedb elif(vyb<0): vyb=-speedb #get robot coordonates x1,y1,x2,y2=canvas.coords(robot) x1b,y1b,x2b,y2b=canvas.coords(robotb) #check for object collison vx,vy = objCollision(x1,x2,y1,y2,vx,vy,speed) vxb, vyb = objCollision(x1b,x2b,y1b,y2b,vxb,vyb,speedb) if vx<>0 or vy <> 0: save1=vx save2=vy if vxb<>0 or vyb <>0: save3=vxb save4=vyb #move robots canvas.coords(robot,x1+vx,y1+vy,x2+vx,y2+vy) canvas.coords(robotb,x1b+vxb,y1b+vyb,x2b+vxb,y2b+vyb) #update canvas canvas.update() #delay animation by 0.1 seconds time.sleep(0.1) tick=tick+1 canvas.update()mainloop()TestingMost of the program testing was focused towards avoiding the obstacles without hitting them, this was often hindered by the randomized sizes of the two robots, as well as the speed which was both randomly generated and user inputted. Our solution for this was adding an edge value (optimized though testing with different speeds and sizes) which prevents the robots from touching the edges and provides a cleaner final aspect. Fig 1.1 Program without the edge value Fig 1.2 Our solutionAnother troublesome aspect was combining code written from Python 3 to Python 2, while most of the time the errors were caused by simple capitalisation changes such as tkinter (Python 3) and Tkinter (Python 2), there were also errors caused by missing features such as colour names and built-in functions.Fig 2.1 Tkinter import errorOne of the most common errors encountered by us during testing were caused by conflicts between the program’s play and movement loop. In the end we discovered that updating the canvas in both of them prevents the movement loop (which is basically an infinite, endless loop without the canvas update)from overloading the CPU and thus the program from crashing. Fig 3.1 Program stops responding Fig 3.2 Our Solution ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download