1

PythonパッケージであるPygletでopenGLを使用しています。この言語とこのパッケージを使用する必要があります。これは課題用です。基本的にキープ・イット・アップ・ゲームである基本的なブリックブレーカー・スタイルのゲームがあります。

ボールとパドルを作ります。

各オブジェクトのヒット ボックスの作成に使用するバウンディング ボックス クラスを個別に作成します。

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) を実行している update 関数で、衝突があったかどうかをチェックする 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()
4

1 に答える 1

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]

それがあなたがちらつく理由だと思います。

于 2013-10-24T19:09:46.560 に答える