Пример #1
0
class Blocks(object):
    def __init__(self, blockLayout, textures, hit_sound, power_ups):
        super(Blocks, self).__init__()
        self.blocks = Array()
        # Center horizontally
        offset_x = (WIDTH - ((BLOCK_DIM + 1) * BLOCK_COLS)) / 2
        # Flush top vertically
        offset_y = HEIGHT - ((BLOCK_DIM + 1) * (BLOCK_ROWS + 1)) - 10
        for j in xrange(len(blockLayout)):
            for i in xrange(len(blockLayout[j])):
                cell = blockLayout[j][i]
                if cell != ' ':
                    x = offset_x + i * (BLOCK_DIM + 1)
                    y = offset_y + j * (BLOCK_DIM + 1)
                    power_up = self.get_power_up(power_ups[cell])
                    self.blocks.add(Block(textures[cell], hit_sound,
                                          Rectangle(x, y, BLOCK_DIM, BLOCK_DIM),
                                          power_up))

    def get_power_up(self, power_up):
        return power_up[0] if random.random() < power_up[1] else None

    def draw(self, batch):
        for block in self.blocks:
            block.draw(batch)

    def check_hit(self, ball):
        iterator = self.blocks.iterator()
        while iterator.hasNext():
            block = iterator.next()
            if block.hits(ball):
                block.hit()
                iterator.remove()
                return block
Пример #2
0
    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
Пример #3
0
 def __init__(self, blockLayout, textures, hit_sound, power_ups):
     super(Blocks, self).__init__()
     self.blocks = Array()
     # Center horizontally
     offset_x = (WIDTH - ((BLOCK_DIM + 1) * BLOCK_COLS)) / 2
     # Flush top vertically
     offset_y = HEIGHT - ((BLOCK_DIM + 1) * (BLOCK_ROWS + 1)) - 10
     for j in xrange(len(blockLayout)):
         for i in xrange(len(blockLayout[j])):
             cell = blockLayout[j][i]
             if cell != ' ':
                 x = offset_x + i * (BLOCK_DIM + 1)
                 y = offset_y + j * (BLOCK_DIM + 1)
                 power_up = self.get_power_up(power_ups[cell])
                 self.blocks.add(Block(textures[cell], hit_sound,
                                       Rectangle(x, y, BLOCK_DIM, BLOCK_DIM),
                                       power_up))
Пример #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()
Пример #5
0
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()
Пример #6
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()
Пример #7
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()