Exemple #1
0
 def test_SDL_Delay(self):
     for wait in range(5, 200, 5):
         start = time.time() * 1000
         timer.SDL_Delay(wait)
         end = time.time() * 1000
         sm = (end - start)
         self.assertTrue(abs(wait - sm) <= 3,
             "%f is not <= 3 for %f and %f" % (abs(wait - sm), wait, sm))
Exemple #2
0
def run():
    sdl2ext.init()
    window = sdl2ext.Window("The Pong Game", size=(800, 600))
    window.show()

    if "-hardware" in sys.argv:
        print("Using hardware acceleration")
        renderer = sdl2ext.RenderContext(window)
        factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
    else:
        print("Using software rendering")
        factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)

    # Create the paddles - we want white ones. To keep it easy enough for us,
    # we create a set of surfaces that can be used for Texture- and
    # Software-based sprites.
    sp_paddle1 = factory.from_color(WHITE, size=(20, 100))
    sp_paddle2 = factory.from_color(WHITE, size=(20, 100))
    sp_ball = factory.from_color(WHITE, size=(20, 20))

    world = World()

    movement = MovementSystem(0, 0, 800, 600)
    collision = CollisionSystem(0, 0, 800, 600)
    aicontroller = TrackingAIController(0, 600)
    if factory.sprite_type == sdl2ext.SOFTWARE:
        spriterenderer = SoftwareRenderer(window)
    else:
        spriterenderer = TextureRenderer(renderer)

    world.add_system(aicontroller)
    world.add_system(movement)
    world.add_system(collision)
    world.add_system(spriterenderer)

    player1 = Player(world, sp_paddle1, 0, 250)
    player2 = Player(world, sp_paddle2, 780, 250, True)
    ball = Ball(world, sp_ball, 390, 290)
    ball.velocity.vx = -BALL_SPEED
    collision.ball = ball
    aicontroller.ball = ball

    running = True
    while running:
        for event in sdl2ext.get_events():
            if event.type == sdlevents.SDL_QUIT:
                running = False
                break
            if event.type == sdlevents.SDL_KEYDOWN:
                if event.key.keysym.sym == sdlkc.SDLK_UP:
                    player1.velocity.vy = -PADDLE_SPEED
                elif event.key.keysym.sym == sdlkc.SDLK_DOWN:
                    player1.velocity.vy = PADDLE_SPEED
            elif event.type == sdlevents.SDL_KEYUP:
                if event.key.keysym.sym in (sdlkc.SDLK_UP, sdlkc.SDLK_DOWN):
                    player1.velocity.vy = 0
        sdltimer.SDL_Delay(10)
        world.process()
Exemple #3
0
    def test_SDL_AddRemoveTimer(self):
        calls = []

        def timerfunc(interval, param):
            calls.append(param)
            return interval

        callback = timer.SDL_TimerCallback(timerfunc)
        timerid = timer.SDL_AddTimer(100, callback, "Test")
        start = timer.SDL_GetTicks()
        end = long(start)
        while (end - start) < 1100:
            # One second wait
            end = timer.SDL_GetTicks()
        # check for <=11, since it can happen that a last call is still
        # executing
        self.assertLessEqual(len(calls), 11)
        timer.SDL_RemoveTimer(timerid)
        self.assertLessEqual(len(calls), 11)
        timer.SDL_RemoveTimer(timerid)
        # Wait a bit, so the last executing handlers can finish
        timer.SDL_Delay(10)
Exemple #4
0
    def loop(self):

        last_tick = timer.SDL_GetTicks()
        avg_time = 0
        while True:
            start = timer.SDL_GetTicks()

            if self.events():
                return

            self.player.tick()
            # self.world.tick() # optimize to be called on every frame
            self.focus_player()
            self.draw()
            self.window.refresh()

            now = timer.SDL_GetTicks()
            if now - last_tick > 1000:
                last_tick = now
                print(int(avg_time))

            avg_time = 0.75 * avg_time + 0.25 * (now - start)

            timer.SDL_Delay(1000 // 60)