def __init__(self, game):
        """Dont need `create` in Screen instances. Constructor will do
         (even though __init__ isnt really a constructor...)
        """
        self.game = game

        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.newMusic(Gdx.files.internal("assets/rain.mp3"))
        self.rainmusic.setLooping(True)

        self.camera = OrthographicCamera()
        self.camera.setToOrtho(False, WIDTH, HEIGHT)

        self.bucket = Rectangle()
        self.bucket.x = 800 / 2 - 64 / 2 # initial starting point @ center of screen
        self.bucket.y = 20

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

        self.lastdroptime = 0
        self.dropsgathered = 0
Exemple #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()
Exemple #3
0
    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)
Exemple #4
0
 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()
class GameScreen(Screen):

    def __init__(self, game):
        """Dont need `create` in Screen instances. Constructor will do
         (even though __init__ isnt really a constructor...)
        """
        self.game = game

        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.newMusic(Gdx.files.internal("assets/rain.mp3"))
        self.rainmusic.setLooping(True)

        self.camera = OrthographicCamera()
        self.camera.setToOrtho(False, WIDTH, HEIGHT)

        self.bucket = Rectangle()
        self.bucket.x = 800 / 2 - 64 / 2 # initial starting point @ center of screen
        self.bucket.y = 20

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

        self.lastdroptime = 0
        self.dropsgathered = 0


    def spawndrop(self):
        raindrop = Rectangle() # no self! :p
        raindrop.x = MathUtils.random(0, WIDTH - 64) # screen X co-ords
        raindrop.y = HEIGHT # always spawns on top
        raindrop.width = raindrop.height = 64
        self.raindrops.add(raindrop)
        self.lastdroptime = TimeUtils.nanoTime()


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

        self.camera.update()

        self.game.batch.setProjectionMatrix(self.camera.combined)

        SECOND = 1000000000 # nano-seconds
        DROPTIME = SECOND


        self.game.batch.begin() # much less verbose with a context manager....
        self.game.batch.draw(self.bucketimg, self.bucket.x, self.bucket.y)
        self.game.batch.end()
        with rend(self.game.batch) as btch: # don't add an "i"!
            self.game.font.draw(btch, "Drops Collected: {}".format(self.dropsgathered), 0, HEIGHT)
            btch.draw(self.bucketimg, self.bucket.x, self.bucket.y)
            for drop in self.raindrops:
                btch.draw(self.dropimg, drop.x, drop.y)


        if Gdx.input.isTouched():
            touchpos = Vector3()
            touchpos.set(Gdx.input.getX(), Gdx.input.getY(), 0)
            self.camera.unproject(touchpos)
            self.bucket.x = touchpos.x - (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 > (WIDTH - 64): self.bucket.x = WIDTH - 64

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


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


    def resize(self, width, height):
        pass

    def show(self):
        self.rainmusic.play()

    def hide(self):
        pass

    def pause(self):
        pass

    def resume(self):
        pass

    def dispose(self):
        self.dropimg.dispose()
        self.bucketimg.dispose()
        self.dropsound.dispose()
        self.rainmusic.dispose()
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()
Exemple #7
0
	def __init__(self):
		self.batch = SpriteBatch()
		self.img = Texture("badlogic.jpg")
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()