Esempio n. 1
0
class TiledMapBench(GdxTest):
    def __init__(self):
        self.map = None
        self.renderer = None
        self.camera = None
        self.cameraController = None
        self.assetManager = None
        self.tiles = None
        self.texture = None
        self.font = None
        self.batch = None

    def create(self):
        w = Gdx.graphics.getWidth()
        h = Gdx.graphics.getHeight()

        self.camera = OrthographicCamera()
        self.camera.setToOrtho(False, (w / h) * 320, 320)
        self.camera.update()

        self.cameraController = OrthoCamController(self.camera)
        Gdx.input.setInputProcessor(self.cameraController)

        self.font = BitmapFont()
        self.batch = SpriteBatch()

        self.tiles = Texture(
            Gdx.files.internal('../../data/maps/tiled/tiles.png'))
        splitTiles = TextureRegion.split(self.tiles, 32, 32)
        self.map = TiledMap()
        layers = self.map.getLayers()
        for l in range(20):
            layer = TiledMapTileLayer(150, 100, 32, 32)
            for x in range(150):
                for y in range(100):
                    ty = int(Math.random() * len(splitTiles))
                    tx = int(Math.random() * len(splitTiles[ty]))
                    cell = Cell()
                    cell.setTile(StaticTiledMapTile(splitTiles[ty][tx]))
                    layer.setCell(x, y, cell)
            layers.add(layer)

        self.renderer = OrthogonalTiledMapRenderer(self.map)

    def render(self):
        Gdx.gl.glClearColor(100.0 / 255.0, 100.0 / 255.0, 250.0 / 255.0, 1.0)
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
        self.camera.update()
        self.renderer.setView(self.camera)
        self.renderer.render()
        self.batch.begin()
        self.font.draw(self.batch,
                       'FPS: ' + str(Gdx.graphics.getFramesPerSecond()), 10,
                       20)
        self.batch.end()
Esempio n. 2
0
class MainGame:
	def __init__(self):
		self.batch = SpriteBatch()
		self.img = Texture("badlogic.jpg")

	def render(self):
		Gdx.gl.glClearColor(1, 0, 0, 1)
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
		self.batch.begin()
		self.batch.draw(self.img, 0, 0)
		self.batch.end()

	def dispose(self):
		self.batch.dispose()
		self.img.dispose()
Esempio n. 3
0
class PyGDX(ApplicationAdapter):
    def __init__(self):
        self.camera = None
        self.batch = None
        self.texture = None
        self.bucketimg = None
        self.dropsound = None
        self.rainmusic = None
        self.bucket = None
        self.raindrops = None

        self.IMGLEN = 64 # width and height of bucket and drop
        self.SPEED = 200

        self.lastdrop = 0
        self.width = 800
        self.height = 480

    def spawndrop(self):
        raindrop = Rectangle()
        raindrop.x = MathUtils.random(0, self.width - self.IMGLEN) # can't be outside screen
        raindrop.y = self.height
        raindrop.width = self.IMGLEN
        raindrop.height = self.IMGLEN
        self.raindrops.add(raindrop)
        self.lastdrop = TimeUtils.nanoTime()

    def create(self):
        self.camera = OrthographicCamera()
        self.camera.setToOrtho(False, self.width, self.height)
        self.batch = SpriteBatch()

        self.dropimg = Texture("assets/droplet.png")
        self.bucketimg = Texture("assets/bucket.png")
        self.dropsound = Gdx.audio.newSound(Gdx.files.internal("assets/drop.wav"))
        self.rainmusic = Gdx.audio.newSound(Gdx.files.internal("assets/rain.mp3"))

        self.bucket = Rectangle()
        self.bucket.x = (self.width / 2) - (self.IMGLEN / 2) # center of screen
        self.bucket.y = 20 # how far up it is
        self.bucket.width = self.IMGLEN
        self.bucket.height = self.IMGLEN

        self.raindrops = Array()
        self.spawndrop()

        self.rainmusic.setLooping(True, True)
        self.rainmusic.play()

    def render(self):
        SECOND = 1000000000
        DROPTIME = SECOND / 6
        Gdx.gl.glClearColor(0, 0, 0.2, 0)
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT)

        self.camera.update()

        self.batch.setProjectionMatrix(self.camera.combined)
        self.batch.begin()
        self.batch.draw(self.bucketimg, self.bucket.x, self.bucket.y)
        for drop in self.raindrops:
            self.batch.draw(self.dropimg, drop.x, drop.y)
        self.batch.end()

        if Gdx.input.isTouched():
            touchpos = Vector3()
            touchpos.set(Gdx.input.getX(), Gdx.input.getY(), 0)
            self.camera.unproject(touchpos)
            self.bucket.x = touchpos.x - (self.IMGLEN / 2)
        if Gdx.input.isKeyPressed(Input.Keys.LEFT):
            self.bucket.x -= self.SPEED * Gdx.graphics.getDeltaTime()
        if Gdx.input.isKeyPressed(Input.Keys.RIGHT):
            self.bucket.x += self.SPEED * Gdx.graphics.getDeltaTime()

        if self.bucket.x < 0: self.bucket.x = 0
        if self.bucket.x > (self.width - self.IMGLEN): self.bucket.x = self.width - self.IMGLEN

        if (TimeUtils.nanoTime() - self.lastdrop) > DROPTIME: self.spawndrop()


        # rip from java, don't hate me ;)
        iterator = self.raindrops.iterator()
        while iterator.hasNext():
            raindrop = iterator.next()
            raindrop.y -= self.SPEED * Gdx.graphics.getDeltaTime();
            if (raindrop.y + self.IMGLEN) < 0: iterator.remove()
            if raindrop.overlaps(self.bucket):
                self.dropsound.play()
                iterator.remove()


    def dispose(self):
        self.batch.dispose()
        self.dropimg.dispose()
        self.bucketimg.dispose()
        self.dropsound.dispose()
        self.rainmusic.dispose()
Esempio n. 4
0
class SmashGame(ApplicationListener):
    def __init__(self):
        super(SmashGame, self).__init__()
        self.camera = None
        self.batch = None
        self.textures = None
        self.paddle = None
        self.drop_sound = None
        self.rain_music = None
        self.blocks = None
        self.background = None
        self.state = None
        self.ball = None
        self.dropimg = None
        self.hud_font = None
        self.input = None
        self.power_ups = None
        self.broken_blocks = 0
        self.delta_acc = 0
        self.play_time = 0

    def screen_width(self):
        return WIDTH

    def screen_height(self):
        return HEIGHT

    def create(self):
        self.input = SmashInput()
        Gdx.input.setInputProcessor(self.input)

        self.camera = OrthographicCamera()
        self.camera.setToOrtho(False, WIDTH, HEIGHT)
        self.batch = SpriteBatch()
        self.state = PLAYING

        self.background = Texture("assets/swahili.png")
        self.ball = Ball(Texture("assets/red_ball_16_16.png"),
                         BALL_SPEED, Circle(), Rectangle())
        self.dropimg = Texture("assets/red_rectangle.png")
        self.textures = {
            "r": Texture("assets/red_rectangle.png"),
            "b": Texture("assets/blue_rectangle.png"),
            "g": Texture("assets/green_rectangle.png"),
        }
        self.hud_font = BitmapFont()
        self.power_ups = {
            "r": (FireBall(2, Texture("assets/fire_ball.png")), 0.9),
            "b": (None, 1),
            "g": (LargeBall(2, Texture("assets/red_ball_32_32.png")), 0.9)
            }

        paddle_width = 100
        paddle_height = 50
        self.paddle = Paddle(Texture("assets/paddle.png"),
                             Rectangle((WIDTH - paddle_width) / 2, 0,
                                       paddle_width, paddle_height), self)
        self.drop_sound = Gdx.audio.newSound(
            Gdx.files.internal("assets/drop.wav"))
        self.rain_music = Gdx.audio.newSound(
            Gdx.files.internal("assets/rain.mp3"))

        with open("assets/checker_board.level") as f:
            blockLayout = f.read().split("\n")
        self.blocks = Blocks(blockLayout=blockLayout,
                             textures=self.textures,
                             hit_sound=self.drop_sound,
                             power_ups=self.power_ups)

        self.broken_blocks = 0
        self.delta_acc = 0
        self.play_time = 0

    def score(self):
        return "Blocks %d, Time %.1f, Rating: %s" % (
            self.broken_blocks, self.play_time,
            self.ball.get_power_ups_string())

    def draw(self):
        """ Do any and all drawing. """
        self.camera.update()
        self.batch.setProjectionMatrix(self.camera.combined)
        self.batch.begin()
        self.batch.draw(self.background, 0, 0, WIDTH, HEIGHT)
        self.blocks.draw(self.batch)
        self.paddle.draw(self.batch)
        self.ball.draw(self.batch)
        self.hud_font.draw(self.batch, self.score(), 20, 20)
        if self.state == LOST:
            self.big_centered_text(self.batch, "You are lose!")
        elif self.state == WON:
            self.big_centered_text(self.batch, "A winner is you!")
        self.batch.end()

    def tick(self, delta, input):
        """ Another 1/60 seconds have passed.  Update state. """
        if self.state == PLAYING:
            self.play_time += delta

            if input.touched:
                # not tested
                self.paddle.set_x(input.touched.x - (64 / 2))
            if input.is_left_pressed():
                self.paddle.move(delta, -1)
            if input.is_right_pressed():
                self.paddle.move(delta)

            if (self.ball.rectangle.y < self.paddle.top()
                and not self.paddle.hits(self.ball)):
                self.state = LOST

            if self.blocks.blocks.size == 0:
                self.state = WON

            self.ball.tick(delta)
            self.ball.update_coordinates(
                delta, WIDTH, HEIGHT,
                check_hits_block=self.check_hits_block,
                check_hits_paddle=self.paddle.hits)

    def render(self):
        Gdx.gl.glClearColor(0, 0, 0, 0)
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT)

        self.delta_acc += Gdx.graphics.getDeltaTime()
        while self.delta_acc > TICK_TIME:
            input = self.input.tick(TICK_TIME, self.camera)
            self.tick(TICK_TIME, input)
            self.delta_acc -= TICK_TIME

        self.draw()

    def big_centered_text(self, batch, text):
        self.hud_font.draw(
            batch, text,
            (WIDTH - self.hud_font.getBounds(text).width) / 2,
            HEIGHT / 3 * 2)

    def check_hits_block(self, ball):
        block = self.blocks.check_hit(ball)
        if block:
            self.broken_blocks += 1
            power_up = block.get_power_up()
            if power_up:
                ball.add_power_up(power_up)
                power_up.reset_remaining()
        return block

    def updatePowerUps(self):
        self.ball.updatePowerUps()

    def resize(self, width, height):
        pass

    def pause(self):
        pass

    def resume(self):
        pass

    def dispose(self):
        """"Handle dispose event"""
        self.batch.dispose()
        for (_, texture) in self.textures.items():
            texture.dispose()
        self.paddle.texture.dispose()
        self.drop_sound.dispose()
        self.rain_music.dispose()
        self.hud_font.dispose()
Esempio n. 5
0
class PyGdx(ApplicationListener):
    def __init__(self):
        self.camera = None
        self.batch = None
        self.texture = None
        self.bucketimg = None
        self.dropsound = None
        self.rainmusic = None
        self.bucket = None
        self.raindrops = None

        self.lastdrop = 0
        self.width = 800
        self.height = 480

    def spawndrop(self):
        raindrop = Rectangle()
        raindrop.x = MathUtils.random(0, self.width - 64)
        raindrop.y = self.height
        raindrop.width = 64
        raindrop.height = 64
        self.raindrops.add(raindrop)
        self.lastdrop = TimeUtils.nanoTime()

    def create(self):        
        self.camera = OrthographicCamera()
        self.camera.setToOrtho(False, self.width, self.height)
        self.batch = SpriteBatch()

        self.dropimg = Texture("../assets/droplet.png")
        self.bucketimg = Texture("../assets/bucket.png")
        self.dropsound = Gdx.audio.newSound(Gdx.files.internal("../assets/drop.wav"))
        self.rainmusic = Gdx.audio.newSound(Gdx.files.internal("../assets/rain.mp3"))

        self.bucket = Rectangle()
        self.bucket.x = (self.width / 2) - (64 / 2)
        self.bucket.y = 20
        self.bucket.width = 64
        self.bucket.height = 64

        self.raindrops = Array()
        self.spawndrop()

        self.rainmusic.setLooping(True, True)
        self.rainmusic.play()

    def render(self):
        Gdx.gl.glClearColor(0,0,0.2,0)
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)

        self.camera.update()

        self.batch.setProjectionMatrix(self.camera.combined)
        self.batch.begin()
        self.batch.draw(self.bucketimg, self.bucket.x, self.bucket.y)
        for drop in self.raindrops:
            self.batch.draw(self.dropimg, drop.x, drop.y)
        self.batch.end()

        if Gdx.input.isTouched():
            touchpos = Vector3()
            touchpos.set(Gdx.input.getX(), Gdx.input.getY(), 0)
            self.camera.unproject(touchpos)
            self.bucket.x = touchpos.x - (64 / 2)
        if Gdx.input.isKeyPressed(Input.Keys.LEFT): self.bucket.x -= 200 * Gdx.graphics.getDeltaTime()
        if Gdx.input.isKeyPressed(Input.Keys.RIGHT): self.bucket.x += 200 * Gdx.graphics.getDeltaTime()

        if self.bucket.x < 0: self.bucket.x = 0
        if self.bucket.x > (self.width - 64): self.bucket.x = self.width - 64

        if (TimeUtils.nanoTime() - self.lastdrop) > 1000000000: self.spawndrop()

        iterator = self.raindrops.iterator()
        while iterator.hasNext():
            raindrop = iterator.next()
            raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
            if (raindrop.y + 64) < 0: iterator.remove()
            if raindrop.overlaps(self.bucket):
                self.dropsound.play()
                iterator.remove()

    def resize(self, width, height):
        pass

    def pause(self):
        pass

    def resume(self):
        pass

    def dispose(self):
        self.batch.dispose()
        self.dropimg.dispose()
        self.bucketimg.dispose()
        self.dropsound.dispose()
        self.rainmusic.dispose()