Why does the collide happen many times?
Question: I'm using openGL with Pyglet which is a python package. I have to use this
language and this package, it is for an assignment. I have a basic
Brickbreaker style game that is basically a keep it up game.

I create a ball and a paddle.

I separately create a bounding box class that will be used to create the hit
boxes for each object.

    
    
    class BoundBox:
    def __init__ (self, width, height, pos):
        self.width = width
        self.height = height
        self.pos = pos
    

Then I create the boxes themselves

    
    
    paddleBox = BoundBox(200, 20, [0,0])    
    ballBox = BoundBox(40, 40, [236, 236])
    

In the update function which is running @
pyglet.clock.schedule_interval(update,1/100.0) I call the checkcollide()
function which checks if there was a collision:

    
    
    def checkForCollide():
        global collides
        if overlap(ballBox, paddleBox):
            vel = 1.05
            ballVel[0] = ballVel[0]*vel #Ball speeds up when collide happens
            ballVel[1] = ballVel[1]*vel
    
            ballVel[1] = -ballVel[1] #Change direction on collision
            ballPos[1] = -ballPos[1]
    
            collides += 1 #counts how many collides happen
    

The overlap function is returning a boolean if there's an overlap in hit
boxes:

    
    
    def overlap(box1, box2):
        return (box1.pos[0] <= box2.width + box2.pos[0]) and(box1.width + box1.pos[0] >= box2.pos[0]) and(box1.pos[1] <= box2.height + box2.pos[1]) and(box1.height + box1.pos[1] >= box2.pos[1])
    

pos[0] is the minimum x width is the maximum x pos[1] is the minimum y height
is the maximum y

When I run the game and the ball hits the paddle it flickers about 15 times
and increments the collides counter every time it detects a hit. Collides then
prints in the console. Why does this flicker happen? How do I stop it?

Here is the program's full code (you must have pyglet installed to run it):

    
    
    import sys, time, math
    from pyglet.gl import *
    from euclid import *
    from pyglet.window import key
    from pyglet.clock import *
    
    window = pyglet.window.Window(512,512)
    
    quadric = gluNewQuadric()
    ballPos = Vector2(256,256)
    ballVel = Vector2(200,145)
    x1 = 0
    bar = pyglet.graphics.vertex_list(4, ('v2f', [0,0, 0,20, 200,0, 200,20]))
    
    
    startTime = time.clock()
    
    collides = 0 
    
    #pos is minx, miny
    class BoundBox:
        def __init__ (self, width, height, pos):
            self.width = width
            self.height = height
            self.pos = pos
    
    
    def overlap(box1, box2):
        return (box1.pos[0] <= box2.width + box2.pos[0]) and(box1.width + box1.pos[0] >= box2.pos[0]) and(box1.pos[1] <= box2.height + box2.pos[1]) and(box1.height + box1.pos[1] >= box2.pos[1])
    
    
    paddleBox = BoundBox(200, 20, [0,0])    
    ballBox = BoundBox(40, 40, [236, 236])
    
    @window.event
    def on_draw():
        glClear(GL_COLOR_BUFFER_BIT)
        glPushMatrix()
        glPushMatrix()
        glColor3f(1,1,1)
        glTranslatef(x1, 0, 0)
        bar.draw(GL_TRIANGLE_STRIP)
        glPopMatrix()
        glTranslatef(ballPos[0], ballPos[1], 0)
        glColor3f(1,0,0)
        gluDisk(quadric, 0, 20, 32, 1)
        glPopMatrix()
    
    @window.event
    def on_key_press(symbol, modifiers):
        global x1
        dist = 30
        if symbol == key.RIGHT:
            #print "right"
            x1 += dist
        elif symbol == key.LEFT:
            #print "left"
            x1 -= dist
    
    def checkForBounce():
        if ballPos[0] > 512.0:
            ballVel[0] = -ballVel[0]
            ballPos[0] = 512.0 - (ballPos[0] - 512.0)
        elif ballPos[0] < 0.0:
            ballVel[0] = -ballVel[0]
            ballPos[0] = -ballPos[0]
        if ballPos[1] > 512.0:
            ballVel[1] = -ballVel[1]
            ballPos[1] = 512.0 - (ballPos[1] - 512.0)
        elif ballPos[1] < -100.0:
            gameOver()
    
    
    def gameOver():
        global collides
        '''global startTime
        elapsed = (time.time() - startTime)
        score = elapsed * .000000001
        finalscore = '%.1f' % round(score, 1)'''
        gostr = "GAME OVER"
        print gostr
        str = "Your score = "
        print str 
        print collides
        pyglet.app.exit()
    
    def checkForCollide():
        global collides
        if overlap(ballBox, paddleBox):
            vel = 1.05
            ballVel[0] = ballVel[0]*vel #Ball speeds up when collide happens
            ballVel[1] = ballVel[1]*vel
    
            ballVel[1] = -ballVel[1] #Change direction on collision
            ballPos[1] = -ballPos[1]
    
            collides += 1 #counts how many collides happen
            print collides
    
            #glscale(0.5, 1, 1)
    
    def update(dt):
        global ballPos, ballVel, ballBox, x1, paddleBox
        ballBox = BoundBox(40, 40, [ballPos[0], ballPos[1]])
        paddleBox = BoundBox(200, 20, [x1,0])   
        #print paddleBox.pos
        #print ballBox.pos
        ballPos += ballVel * dt
        checkForBounce()
        checkForCollide()
    
    pyglet.clock.schedule_interval(update,1/100.0)
    pyglet.app.run()
    


Answer: I don't think you wanted to invert the position here:

    
    
    def checkForCollide():
        global collides
        if overlap(ballBox, paddleBox):
            vel = 1.05
            ballVel[0] = ballVel[0]*vel #Ball speeds up when collide happens
            ballVel[1] = ballVel[1]*vel
    
            ballVel[1] = -ballVel[1] #Change direction on collision
            ballPos[1] = -ballPos[1]
    
            collides += 1 #counts how many collides happen
    

What were you trying to do with this line?

    
    
      ballPos[1] = -ballPos[1]
    

I suspect that is why you are flickering.

