# # Copyright (C) 2006 Andre Stemper # Written in 2006 for the Science Club # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. from Tkinter import * #----------------------------------------------------------- def move(): global x1, y1, dx, dy, playing, gwidth, gheight, ballsize, rfb global lplayer, rplayer, nextplayer, counter, rpos, lpos, rs # position the ball on the playground if playing > 0: x1, y1 = x1 +dx, (y1+dy) # calculate the next ball position else: # follow racket if nextplayer==0: # if the next player is the right player give him the ball x1,y1,dx = rfb+10, lpos+(rs-ballsize)/2, abs(dx) else: # otherwise give the left player the ball x1,y1,dx = gwidth-rfb-10, rpos+(rs-ballsize)/2,-abs(dx) # test top/bottom boundary if y1 > (gheight-ballsize): y1, dy = gheight-ballsize, -dy if y1 < 0: y1, dy= 0, -dy # give the left player a point if x1 > (gwidth-ballsize): lplayer=lplayer+1 # increment score for left player x1, y1 = rfb, (gheight/2) # give right player the ball nextplayer = 0 playing = 0 # stop the game # give the right player a point if x1 < 0: rplayer=rplayer+1 # increment score for right player x1, y1 = gwidth-rfb, (gheight/2) # give left player the ball nextplayer = 1 playing = 0 # stop the game # test if the ball hits the left racket if x1 <= rfb: if y1>lpos and y1 < (lpos+rs): x1, dx = rfb+5, -dx # test if the ball hits the right racket if x1 >= (gwidth-rfb-ballsize): if y1 >= rpos and y1 <= (rpos+rs): x1, dx = (gwidth-rfb-ballsize-5), -dx # draw ball position can.coords(oval1, x1, y1, x1+ballsize, y1+ballsize) # draw current score score = str(lplayer) + ":" + str(rplayer) can.itemconfigure(counter, text=score) # draw rackets can.coords(rracket, gwidth-rfb, rpos, gwidth-rfb, rpos+rs) can.coords(lracket, rfb, lpos, rfb, lpos+rs) # trigger next frame win.after(50, move) #----------------------------------------------------------- def lup(event): global lpos, rspeed if lpos > rspeed: lpos=lpos-rspeed def ldown(event): global lpos, gheight, rspeed if lpos < (gheight-rs-rspeed): lpos=lpos+rspeed def rmove(event): global rpos ypos = event.y if ypos > rspeed and ypos < (gheight-rs-rspeed): # if in range rpos = ypos def startit(event): global playing playing = 1 #----------------------------------------------------------- dx, dy = 15, 12 # Start directions of the ball gwidth = 800; gheight = 400 # Size of the playground ballsize = 20; rfb = 20; rs = 60 # Ball size, racket from border, racket size rspeed = 20 # Speed of the racket lplayer=rplayer=0 # Start with no points #----------------------------------------------------------- x1, y1 = rfb, (gheight-ballsize)/2 rpos=lpos=(gheight-rs)/2 nextplayer = 0 # next player: 0=right, 1=left playing = 0 # Do not play when starting the program win = Tk(); # Create new window win.title("Pong") # Name it "Pong" can = Canvas(win, bg='black',height=gheight, width=gwidth) can.pack(side=LEFT) # Create the ball oval1 = can.create_oval(x1, y1, x1+ballsize, y1+ballsize, width=2, fill='white') # Create mid line line = can.create_line(gwidth/2, 0, gwidth/2, gheight, width=4, fill="white", dash=(4, 8)) # Create to rackets lracket = can.create_line(rfb, lpos, rfb, lpos+rs, width=10, fill="white") rracket = can.create_line(gwidth-rfb, rpos, gwidth-rfb, rpos+rs, width=10, fill="white") # Create the score text font=('courier', 20) counter=can.create_text(gwidth/2, 20, text='0:0', font=font, fill="white") # Use the following keys to move the racket win.bind('q', lup) win.bind('p', ldown) win.bind('', rmove) win.bind('', startit) # start game by hitting space move() # draw next sequence win.mainloop() # redraw window, watch for keys...