2013-10-24 72 views
1

我使用python包pyglet openGL。我必須使用這種語言和這個包,它是一個任務。我有一個基本的Brickbreaker風格的遊戲,基本上是保持它的遊戲。爲什麼碰撞發生很多次?

我創建了一個球和一個槳。

我分別創建了一個邊界框類,它將用於爲每個對象創建點擊框。

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更新函數(更新,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() 

回答

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] 

我懷疑這就是爲什麼你閃爍。

+0

@HaIR我試圖扭轉碰撞方向的位置。如果我將這行代碼註釋掉,那麼球就會滑過槳,然後反轉方向。但它應該只碰撞一次,它仍然能檢測到13次碰撞。 – BKeschinger

+0

你需要將它從槳中移回來。這將有助於瞭解以前的ballPos [1]是什麼,如果你碰撞過,就回復它。另一種選擇是將球從球拍中移出1個像素。 – HalR