1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #!/usr/bin/python
# ---------------- READ ME --------------------------------------------- # This Script is Created Only For Practise And Educational Purpose Only # This is an Example Of Tkinter Canvas Graphics # This Script Is Created For http://www.bitforestinfo.com # This Script is Written By __author__='''
###################################################### By ######################################################
Suraj Singh surajsinghbisht054@gmail.com http://www.bitforestinfo.com/
###################################################### ''' print __author__ # ----------------------------------------------------------------------
try: import Tkinter except: import tkinter as Tkinter import time BALL_SPEED=5
class GameCanvas(Tkinter.Canvas): def __init__(self, *args, **kwargs): Tkinter.Canvas.__init__(self, *args, **kwargs) self.create_bouncing_ball() self.create_moving_bat() def create_moving_bat(self): self.bat=Tkinter.Canvas.create_rectangle(self,0,570,100,580, fill='lightslateblue') self.bind('<Motion>', self.update_bat_moves) return def update_bat_moves(self, event=None): x=event.x x1,y1,x2,y2=self.coords(self.bat) gap=(x2-x1)/2 center=x1+gap move=x-center self.move(self.bat,move,0) return def create_bouncing_ball(self): self.ball=Tkinter.Canvas.create_oval(self, 0,0,20,20, fill='cornflowerblue') self.x=BALL_SPEED self.y=BALL_SPEED return def update_board(self): width=self.winfo_width() height=self.winfo_height() x1,y1,x2,y2=self.coords(self.ball) hit=len(self.find_overlapping(x1,y1,x2,y2)) if hit>=2: self.y=-BALL_SPEED self.move(self.ball,self.x,self.y) elif x1<0: self.x=BALL_SPEED self.move(self.ball,self.x,self.y) elif x2>width: self.x=-BALL_SPEED self.move(self.ball,self.x,self.y) elif y1<0: self.y=BALL_SPEED self.move(self.ball,self.x,self.y) elif y2>height: x=width/2 y=height/2 self.create_text(x,y, text='Game Over', font=('arial 50 bold'), fill='red') self.y=-BALL_SPEED self.move(self.ball,self.x,self.y) else: self.move(self.ball,self.x,self.y) return
# Gui Handler def main(): root=Tkinter.Tk() root.minsize(800,600) root.maxsize(800,600) board=GameCanvas(root, bg='lavender') board.pack(expand='yes', fill='both') # Program Loop while True: root.update() root.update_idletasks() board.update_board() time.sleep(0.01)
# main Trigger if __name__=='__main__': main()
|