Explanation on why program must maintain simplicity:I am a peer tutor of a computer programming class and my teacher has asked me to develop a few simple games to teach the class. This is the first year the school has taught Python (thanks to my final project last semester, and now I'm stuck doing the teaching... =/ ). Because of this, my teacher cannot assist me very successfully.
I am aware of PyGame and wxPython but my teacher really wants me to keep things as simple and self contained as possible (he does not want to reimage every computer in the school with an external Python module). I am also aware of canvas.delete(item) but again, he wants me to use the "draw a shape, then draw the same shape in the background colour" method of animation.
Problem:
Sorry for the long preamble, here is my problem. I have a pong game started and it works for the most part (the ball does not move on a diagonal yet, but I have not gotten that far). The left paddle can flawlessly be controlled by the user and the right paddle remains stationary for now (I will make computer control this paddle, again not there yet). I have the ball moving back and forth and bouncing off the paddles, but after a few bounces back and forth the speed of the ball significantly drops a few times and then seems to maintain a constant slow speed (does not slow down to a halt). I'm not sure what would be causing this? but here is my code, maybe someone can spot the problem. Thank you for your time.
from tkinter import *#imports tkinter module
import random#imports random module
def DrawPaddle1():
#draws paddle on left side
global xPos1,yPos1
canvas.create_rectangle(xPos1,yPos1,xPos1+25,yPos1+100,fill="white",width=0)
def ErasePaddle1():
#erases paddle on left side
global xPos1,yPos1
canvas.create_rectangle(xPos1,yPos1,xPos1+25,yPos1+100,fill="black",width=0)
def DrawPaddle2():
#draws paddle on right side
global xPos2,yPos2
canvas.create_rectangle(xPos2,yPos2,xPos2+25,yPos2+100,fill="white",width=0)
def ErasePaddle2():
#erases paddle on right side
global xPos2,yPos2
canvas.create_rectangle(xPos2,yPos2,xPos2+25,yPos2+100,fill="black",width=0)
def DrawBall():
#draws ball
global ballX,ballY
canvas.create_rectangle(ballX,ballY,ballX+20,ballY+20,fill="white",width=0)
def EraseBall():
#erases ball
global ballX,ballY
canvas.create_rectangle(ballX,ballY,ballX+20,ballY+20,fill="black",width=0)
def KeyPress(event):
#callback function for when a key is pressed
global xPos1,yPos1
if event.keysym == "Up":
#moves paddle up if "Up" key is pressed
ErasePaddle1()
yPos1-=10
DrawPaddle1()
if event.keysym == "Down":
#moves paddle down if "Down" key is pressed
ErasePaddle1()
yPos1+=10
DrawPaddle1()
###################################################
root=Tk()#initializes toplevel window
widthVal=1000#width value of canvas
heightVal=700#height value of canvas
canvas = Canvas(width=widthVal, height=heightVal, bg='black')#initializes the canvas
canvas.pack()#prepares canvas
moveAmt=-0.9#variable which determines speed/direction of the ball
xPos1=20#variable for x coordinate of left paddle (user's)
yPos1=yPos2=heightVal//2-50#variables for y coordinates of left (user's) and right (computer's) paddles
xPos2=widthVal-45#variable for x coordinate of right (computer's) paddle
ballX=widthVal//2-10#variable for x coordinate of the ball
ballY=heightVal//2-10#variable for y coordinate of the ball
DrawPaddle1()
DrawPaddle2()
canvas.create_line (widthVal//2,heightVal,widthVal//2,0,fill="white",width=4,dash=(100))#draws middle line
DrawBall()
root.bind_all('<Key>',KeyPress)#binds all keys
quitGame=False
while quitGame==False:
if ballX < xPos1+26 and ballY > yPos1 and ballY+20 < yPos1+100:#checks for collision on left paddle
moveAmt=0.9
elif ballX+20 > xPos2-1 and ballY > yPos2 and ballY+20 < yPos2+100:#checks for collision on right paddle
moveAmt=-0.9
EraseBall()
ballX+=moveAmt#changes ball's x coordinate value by the move amount
DrawBall()
canvas.update()#updates canvas
if ballX > widthVal//2-25 and ballX <widthVal//2+5:#redraws line when ball passes over it
canvas.create_line (widthVal//2,heightVal,widthVal//2,0,fill="white",width=4,dash=(100))
root.mainloop()