Code: Alles auswählen
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)
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)
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
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
if x1 < 0:
x1, y1 = gwidth-rfb, (gheight/2)
# test if the ball hits the left racket let it bounce back
if x1 <= rfb:
if y1>lpos and y1 < (lpos+rs):
x1, dx = rfb+5, -dx
# test if the ball hits the right racket let it bounce back
if x1 >= (gwidth-rfb-ballsize):
if y1 >= rpos and y1 <= (rpos+rs):
x1, dx = (gwidth-rfb-ballsize-5), -dx
can.coords(oval1, x1, y1, x1+ballsize, y1+ballsize)
# draw current score
score = str(lplayer) + ":" + str(rplayer)
can.itemconfigure(counter, text=score)
# Change the positon of the two rackets
can.coords(rracket, gwidth-rfb, rpos, gwidth-rfb, rpos+rs)
can.coords(lracket, rfb, lpos, rfb, lpos+rs)
win.after(50, move)
#-------------------------------------------------
def lup(event):
global lpos, rspeed
if lpos > rspeed:
lpos=lpos-rspeed
def ldown(event):
global lpos, gheight, rspeed, rs
if lpos < (gheight-rs-rspeed):
lpos=lpos+rspeed
def rmove(event):
global rpos, gheight, rs
ypos = event.y
if ypos > 0 and ypos < (gheight-rs): # if in range
rpos = ypos
#------------------------------------------------------
def startit(event):
global playing
playing = 1
#---------------------------------------------------------
dx, dy = 15, 15 # 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();
win.title("Pong")
can = Canvas(win, bg='black',height=gheight, width=gwidth)
can.pack(side=LEFT)
oval1 = can.create_oval(x1, y1, x1+ballsize, y1+ballsize, width=2, fill='white')
line = can.create_line(gwidth/2, 0, gwidth/2, gheight, width=4, fill="white", dash=(4, 8))
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('<B1-Motion>', rmove)
win.bind('<space>', startit) # start game by hitting space
move() # draw next sequence
win.mainloop()