Exemple #1
0
def createObstacle(win):
    y1 = random.randint(5, 45)
    y2 = 60 - y1
    pipe_top = PO.Block(win, 41, y1 / 2.0, 5, y1)
    pipe_bot = PO.Block(win, 41, y1 + 12 + y2 / 2.0, 5, y2)

    Pipes = [pipe_top, pipe_bot]

    for pipe in Pipes:
        pipe.Color('gray55')
        pipe.setVelocity([-15, 0])
        pipe.draw()

    return Pipes
Exemple #2
0
def drawMissile(win, ship):
    missile = pho.Block(win, 15, ship.getPosition()[1], 4, 0)
    missile.setFill('grey')
    missile.setOutline('grey')
    missile.setVelocity([10, 0])
    missile.draw()
    play.append(missile)
    return missile
def init_blocks(x, y, size, number, win):
    bricks = []
    
    # each brick is one across and two supporting
    for i in range(number):
        b = pho.Block( win, size, size/8,
                       [x, i*(size/3) + y],
                       [0, 0], [0, 0], gr.color_rgb( 240, 220, 190 ) )
        bricks.append( b )

    # return the list of bricks and boards
    return bricks
Exemple #4
0
def collision_ball_rotating_block(ball, block, dt):

    # transform the ball the same way by subtracting the center of the
    # original block and rotating it to the same alignment (use anchor point?)
    p0 = ball.getPosition()
    bp = block.getPosition()
    b0 = block.getAnchor()

    # we have the 0-angle state of the block so create a faux block so the anchor is at 0, 0
    # ***** assumes a rotating block has width and height fields  (bad)
    fauxBlock = pho.Block(block.win, bp[0] - b0[0], bp[1] - b0[1], block.width,
                          block.height)

    # rotate the ball's velocity
    v0 = ball.getVelocity()

    theta = math.pi * block.getAngle() / 180.
    cth = math.cos(theta)
    sth = math.sin(theta)
    vtx = v0[0] * cth + v0[1] * sth
    vty = -v0[0] * sth + v0[1] * cth

    px = p0[0] - b0[0]
    py = p0[1] - b0[1]
    pxx = px * cth + py * sth
    pyy = -px * sth + py * cth

    ball.setPosition((pxx, pyy))
    ball.setVelocity((vtx, vty))

    # test if there is a collision
    distToImpact, side = collisionTest_ball_block(ball, fauxBlock)

    if distToImpact < 0:  # back up the ball, and there was a collision
        distToImpact = 0.0

    # set the ball back to its original position
    ball.setPosition(p0)
    ball.setVelocity(v0)

    acc = ball.getAcceleration()
    tvx = v0[0] + acc[0] * dt
    tvy = v0[1] + acc[1] * dt
    vmag = length((tvx, tvy))

    if distToImpact > 0.0 and vmag == 0.0 or distToImpact > vmag * dt:
        return False

    # if there was a collision, update the ball to the collision point
    # set the ball back to its original velocity and update
    distToImpact = min(0.0, distToImpact)
    delta = distToImpact / vmag
    ball.update(delta)

    # if there was a collision, re-align the new velocity with the block
    v0 = ball.getVelocity()

    # convert velocities to the collision space
    vtx = v0[0] * cth + v0[1] * sth
    vty = -v0[0] * sth + v0[1] * cth

    # need to give it a boost or a sink if the block is rotating based on the moment arm from the center of rotation
    dx = pxx  # transformed distance between anchor and the ball
    dy = pyy
    dist = length((dx, dy))
    rotvel = math.pi * block.getRotVelocity() / 180.
    linvel = dist * 2. * math.pi * rotvel / (2. * math.pi)

    if side == 0:  # left side of the block, so x velocity has to be negative
        vtx = math.copysign(vtx * ball.getElasticity() * block.getElasticity(),
                            -1)
        hsin = dy / dist  # length of the moment arm hitting the ball
        velmod = -hsin * linvel  # modify the linear velocity
        #print 'left:', hsin, linvel, vtx, velmod, vtx + velmod
        vtx += velmod

    elif side == 1:  # right side of the block
        vtx = math.copysign(vtx * ball.getElasticity() * block.getElasticity(),
                            1)
        hsin = dy / dist  # length of the moment arm hitting the ball
        velmod = -hsin * linvel  # modify the linear velocity
        #print 'right:', hsin, linvel, vtx, velmod, vtx + velmod
        vtx += velmod

    elif side == 2:  # bottom side
        vty = math.copysign(vty * ball.getElasticity() * block.getElasticity(),
                            -1)
        hsin = dx / dist
        velmod = hsin * linvel
        #print 'bottom:', hsin, linvel, vty, velmod, vty + velmod
        vty += velmod

    else:  # top side
        vty = math.copysign(vty * ball.getElasticity() * block.getElasticity(),
                            1)
        hsin = dx / dist
        velmod = hsin * linvel
        #print 'top:', hsin, linvel, vty, velmod, vty + velmod
        vty += velmod

    # rotate the velocity back
    vttx = vtx * cth - vty * sth
    vtty = vtx * sth + vty * cth

    ball.setVelocity((vttx, vtty))

    ball.update((1 - delta) * dt)

    return True
Exemple #5
0
def main():
    # variables
    key = ''
    dt = 0.02
    frame = 0
    gamma = 100
    delta = 0.5
    winWidth = 50
    winHeight = 50
    counter = 0
    # start screen, ie state is 0
    # make a window
    win = gr.GraphWin('Galaxy Impact', 500, 500, False)
    win.setBackground("black")
    quit = False
    state = 0
    if state == 0:
        ship = pho.Ship(win, 8, 40)
        ship1 = pho.Ship(win, 43, 40)
        ship.setRotVelocity(30)
        ship1.setRotVelocity(30)
        title = gr.Text(gr.Point(250, 100), "Galaxy Impact")
        title.setFill('red')
        title.setSize(36)
        # instruction to start text
        instruction = gr.Text(gr.Point(250, 200), "click start to begin")
        instruction.setFill('orangered')
        instruction.setSize(20)
        begin = pho.Block(win, 25, 25, 10, 6, coloring='indianred')
        begins = gr.Text(gr.Point(250, 250), "start")
        begins.setSize(15)
        close = pho.Block(win, 25, 15, 10, 6, coloring='indianred')
        closes = gr.Text(gr.Point(250, 350), "close")
        closes.setSize(15)
        instructions = gr.Text(
            gr.Point(250, 420),
            " INSTRUCTIONS: \n space to shoot \n up to go up \ left to go left, right to go right \n down to go down"
        )
        instructions.setFill('red')
        starts = [closes, instruction, title, begins, instructions]
        boxes = [begin, close, ship, ship1]
        # draw all the boxes
        for box in boxes:
            box.draw()
    # draw all the text
        for word in starts:
            word.draw(win)
#wait for a mouse click on start to start
        while True:
            ship.update(dt)
            ship1.update(dt)
            event = win.checkMouse()
            if event != None and event.x < 300 and event.x > 200 and event.y > 220 and event.y < 280:
                state = 1
                break
            elif event != None and event.x < 300 and event.x > 200 and event.y > 320 and event.y < 380:
                win.close()
### main phase ie state is 1
# make ship, draw it,
    ship = pho.Ship(win, 25, 5)
    bullet = pho.Ball(win, -10, -10)
    ship.setAngle(90)
    old_rotV = ship.getRotVelocity()
    gamma = 100
    delta = 0.5
    winWidth = 50
    winHeight = 50
    bullets = []
    rains = []
    if state == 1:
        # undraw all from start screen
        for word in starts:
            word.undraw()
    # undraw all the boxes
        for box in boxes:
            box.undraw()
        win.setBackground("gray6")
        # draw objects
        ship.draw()
        while state == 1:
            #scores
            score = gr.Text(gr.Point(450, 50), "SCORE : %d" % counter)
            score.setSize(18)
            score.setFill("white")
            key = win.checkKey()
            # raining
            rain = pho.Ball(win,
                            random.randint(0, 50),
                            random.randint(45, 50),
                            radius=1)
            rain.setVelocity([random.randint(-5, 3), random.randint(-10, -5)])
            # draw
            if frame % 50 == 0:
                rain.draw()
                rains.append(rain)
        # check for collisions
            collided = False
            #			for item, go in zip(rains, bullets): #thought this could help double loop
            # if the bullet hits rain, undraw both ,and increment score
            for item in rains:
                if coll.collision(item, bullet, dt) == True:
                    print "S"
                    collided == True
                    item.undraw()
                    bullet.undraw()
# 					counter+=1
# 					score.undraw()
# 				#	win.update()
# 					score.draw(win)
#	if collided == False:
#rain.update(dt)
#bullet.update(dt)
            for item in bullets:
                if coll.collision(item, rain, dt) == True:
                    print "m"
                    collided == True
                    item.undraw()
                    rain.undraw()
    # 				counter+=1
# 					score.undraw()
# 				#	win.update()
# 					score.draw(win)
#if collided == False:
#rain.update(dt)
#bullet.update(dt)
# if rain hits spaceship, then you die
            for item in rains:
                z = item.getPosition()
                # scores if the rain doesnt touch, and goes down, not to the side :(
                if z[1] < 0:
                    counter += 1
                if coll.collision(item, ship, dt) == True:
                    collided == True
                    # make ship red
                    ship.vis[0].setFill("red")
                    ship.setFlickerOn()
                    time.sleep(0.001)
                    state = 2

# update world
            ship.update(dt)
            for x in bullets:
                x.update(dt)
            for x in rains:
                x.update(dt)
# user interface
            a = ship.getAngle()
            theta = a * math.pi / 180
            cth = math.cos(theta)
            sth = math.sin(theta)
            p = ship.getPosition()
            v = ship.getVelocity()
            g = bullet.getPosition()
            # movement
            if key == 'Up':
                p[1] += 1
                ship.setPosition(p)
                ship.setFlickerOn()
            if key == 'Down':
                p[1] -= 1
                ship.setPosition(p)
                ship.setFlickerOn()
            if key == 'Left':
                p[0] -= 1
                ship.setPosition(p)
                ship.setFlickerOn()
            if key == 'Right':
                p[0] += 1
                ship.setPosition(p)
                ship.setFlickerOn()
#	shoot bullets
            if key == 'space':
                b = 4
                bullet = pho.Ball(win, p[0], p[1], radius=0.5)
                # too much flick...though I could end up using it
                bullet.setVelocity([b * cth, b * sth])
                bullet.draw()
                bullets.append(bullet)
    # end game conditions
            if key == "q":
                state = 2
        # wrap the window
            moveit = False
            p = list(ship.getPosition())
            if p[0] < 0:
                p[0] += winWidth
                moveit = True
            elif p[0] > winWidth:
                p[0] -= winWidth
                moveit = True
            if p[1] < 0:
                p[1] += winHeight
                moveit = True
            elif p[1] > winHeight:
                p[1] -= winHeight
                moveit = True
            if moveit:
                ship.setPosition(p)
                moveit = False
        # update visualization
            if frame % 10 == 0:
                win.update()
                time.sleep(dt * 0.5)
            frame += 1

# end screen ie state == 2

    if state == 2:
        win.setBackground("red")
        ship.undraw()
        # undraw all from main screen
        for rain in rains:
            rain.undraw()
    # undraw all the boxes
        for bullet in bullets:
            bullet.undraw()
    # instruction to start text
        restart = gr.Text(gr.Point(250, 300), "press start to restart")
        restart.setFill('black')
        restart.setSize(20)
        # title of game:
        title = gr.Text(gr.Point(250, 100), "Galaxy Impact")
        title.setFill('black')
        title.setSize(36)
        # restart
        begin = pho.Block(win, 25, 25, 10, 6, coloring='brown4')
        begins = gr.Text(gr.Point(250, 250), "restart")
        begins.setSize(15)
        begins.setFill('white')
        begin.draw()
        gameover = gr.Text(gr.Point(250, 450), "GAME OVER")
        gameover.setStyle('bold italic')
        gameover.setSize(30)
        score = gr.Text(gr.Point(250, 350), "SCORE : %d" % counter)
        score.setSize(28)
        score.setStyle('bold')
        # draw everything
        ending = [restart, begins, title, gameover, score]
        for word in ending:
            word.draw(win)


#wait for a mouse click on start to restart
        while True:
            event = win.checkMouse()
            key = win.checkKey()
            if key == 'q':
                win.close()

            if event != None and event.x < 300 and event.x > 200 and event.y > 220 and event.y < 280:
                state = 0
                win.close()
                main()
                break
    # all done
        if quit == True:
            win.close()
Exemple #6
0
def buildGame(win, x0, y0, height, width):

    # obstacles: floors, left, right, blocks
    awkwardBLock = pho.Block(win,
                             x0 + 40,
                             y0 + 14,
                             height + 3,
                             width + 10,
                             coloring='brown')
    floor_left = pho.Block(win,
                           x0 + 0,
                           y0 + 10,
                           height + 35,
                           width + 3,
                           coloring='green')
    floor_right = pho.Block(win,
                            x0 + 40,
                            y0 + 10,
                            height + 20,
                            width + 3,
                            coloring='green')
    left = pho.Wall(win,
                    x0 + 2,
                    y0 + 10,
                    height + 70,
                    width + 3,
                    coloring='DarkBlue')
    right = pho.Wall(win,
                     x0 + 48,
                     y0 + 10,
                     height + 70,
                     width + 3,
                     coloring='DarkBlue')
    block1 = pho.Block(win,
                       x0 + 15,
                       y0 + 25,
                       height + 3,
                       width + 3,
                       coloring='brown')
    block2 = pho.Block(win,
                       x0 + 35,
                       y0 + 25,
                       height + 3,
                       width + 3,
                       coloring='brown')
    block3 = pho.Block(win,
                       x0 + 25,
                       y0 + 25,
                       height + 3,
                       width + 3,
                       coloring='brown')
    top_left = pho.Block(win,
                         x0 + 0,
                         y0 + 58,
                         height + 35,
                         width + 3,
                         coloring='brown')
    top_right = pho.Block(win,
                          x0 + 40,
                          y0 + 58,
                          height + 20,
                          width + 3,
                          coloring='brown')
    ball3 = pho.Ball(win,
                     x0 + 45,
                     y0 + 56,
                     radius=3,
                     coloring='DarkGoldenrod4')
    ball4 = pho.Ball(win,
                     x0 + 5,
                     y0 + 55,
                     radius=3.3,
                     coloring='DarkGoldenrod4')
    obstacles = [
        ball3, ball4, floor_right, block2, floor_left, top_right, top_left,
        block1, block3, right, left, awkwardBLock
    ]
    for obstacle in obstacles:
        obstacle.draw()
        obstacle.setElasticity(1.01)
    return obstacles
Exemple #7
0
def buildGame(win, x0, y0, height, width):
    # obstacles: floors, left, right, blocks
    awkwardBLock = pho.Block(win,
                             x0 + 40,
                             y0 + 14,
                             height + 3,
                             width + 10,
                             coloring='brown')
    top = pho.Floor(win,
                    x0 + 0,
                    y0 + 58,
                    height + 50,
                    width + 3,
                    coloring='dark green')
    left = pho.Wall(win,
                    x0 + 2,
                    y0 + 10,
                    height + 70,
                    width + 3,
                    coloring='DarkBlue')
    right = pho.Wall(win,
                     x0 + 48,
                     y0 + 10,
                     height + 70,
                     width + 3,
                     coloring='DarkBlue')
    block1 = pho.Block(win,
                       x0 + 10,
                       y0 + 35,
                       height + 3,
                       width + 3,
                       coloring='brown')
    block2 = pho.Block(win,
                       x0 + 40,
                       y0 + 35,
                       height + 3,
                       width + 3,
                       coloring='brown')
    floor_left = pho.Block(win,
                           x0 + 0,
                           y0 + 10,
                           height + 35,
                           width + 3,
                           coloring='brown')
    floor_right = pho.Block(win,
                            x0 + 40,
                            y0 + 10,
                            height + 20,
                            width + 3,
                            coloring='brown')
    samosa = pho.Triangle(win,
                          x0 + 25,
                          y0 + 40,
                          height + 4,
                          width + 4,
                          coloring='orange')
    tri = pho.Triangle(win,
                       x0 + 5,
                       y0 + 50,
                       height + 4,
                       width + 4,
                       coloring='orange')
    samosa1 = pho.Triangle(win,
                           x0 + 23,
                           y0 + 36,
                           height + 4,
                           width + 4,
                           coloring='orange')
    block3 = pho.Block(win,
                       x0 + 25,
                       y0 + 35,
                       height + 5,
                       width + 5,
                       coloring='brown')
    samosa2 = pho.Triangle(win,
                           x0 + 27,
                           y0 + 36,
                           height + 4,
                           width + 4,
                           coloring='orange')
    rathole = pho.Hexagon(win,
                          x0 + 18,
                          y0 + 52,
                          height + 5,
                          width + 5,
                          coloring='hotpink')
    bounce1 = pho.Hexagon(win,
                          x0 + 5,
                          y0 + 40,
                          height + 4,
                          width + 4,
                          coloring='hotpink')
    ball1 = pho.Ball(win, x0 + 5, y0 + 14, radius=3, coloring='DarkGoldenrod4')
    ball3 = pho.Ball(win,
                     x0 + 47,
                     y0 + 56,
                     radius=3,
                     coloring='DarkGoldenrod4')
    ball4 = pho.Ball(win,
                     x0 + 5,
                     y0 + 55,
                     radius=3.3,
                     coloring='DarkGoldenrod4')

    obstacles = [
        ball4, tri, ball3, ball1, rathole, bounce1, samosa2, samosa1, samosa,
        block2, top, floor_right, floor_left, block1, right, left, awkwardBLock
    ]
    for obstacle in obstacles:
        obstacle.draw()
        obstacle.setElasticity(1.01)
    return obstacles