Pygame乒乓球比赛

时间:2017-03-02 06:59:56

标签: python pygame pong

我试图让球对象在屏幕内反弹,我已经测试了几个小时不同的东西,在这里查看其他乒乓球游戏源代码和其他问题,但我似乎无法弄明白。有人可以指点一下从哪里开始?我应该为不同的动作制作单独的功能吗?

    # ----- Game Loop ----- #

# Setting the loop breakers
game_exit = False

# ball positon/velocity
ball_x = DISP_W/2
ball_y = DISP_H/2
velocity = 5

# Game loop
while not game_exit:
    # Gets all events
    for event in pygame.event.get():
        # Close event
        if event.type == pygame.QUIT:
            # Closes game loop
            game_exit = True

    # Background fill
    GAME_DISP.fill(black)

    # Setting positions
    ball.set_pos(ball_x, ball_y)
    # ceil.set_pos(0, 0)
    # floor.set_pos(0, DISP_H - boundary_thickness)

    ball_y += velocity



    # Drawing sprites
    ball_group.draw(GAME_DISP)
    # collision_group.draw(GAME_DISP)

    pygame.display.update()

    # Setting FPS
    clock.tick(FPS)

# ----- Game exit ----- #
pygame.quit()
quit()

完整代码:http://pastebin.com/4XxJaCvf

1 个答案:

答案 0 :(得分:0)

我会看一下pygame sprite类。它有很多方便的方法可以帮助解决碰撞和其他问题:https://www.pygame.org/docs/ref/sprite.html