碰撞为什么会发生多次?

时间:2013-10-24 17:55:32

标签: python opengl pyglet

我正在使用带有Pyglet的openGL,这是一个python包。我必须使用这种语言和这个包,它是一个任务。我有一个基本的破砖风格游戏,基本上是一个保持它的游戏。

我创造了一个球和一个球拍。

我单独创建一个边界框类,用于为每个对象创建命中框。

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

然后我自己创建盒子

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

在运行@ pyglet.clock.schedule_interval(update,1 / 100.0)的更新函数中,我调用checkcollide()函数来检查是否存在冲突:

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

如果命中框中有重叠,则重叠函数返回一个布尔值:

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]是最小x 宽度是最大x pos [1]是最小y 高度是最大y

当我运行游戏并且球击中球拍时,它会闪烁约15次,并在每次检测到击球时增加碰撞计数器。然后碰撞在控制台中打印。为什么会发生这种闪烁?我该如何阻止它?

这是程序的完整代码(你必须安装pyglet来运行它):

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()

1 个答案:

答案 0 :(得分:0)

我认为你不想在这里反转这个位置:

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

你想用这条线做什么?

  ballPos[1] = -ballPos[1]

我怀疑这就是你闪烁的原因。

相关问题