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)
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()
def create(self): ''' setup the camera. In Box2D we operate on a meter scale, pixels won't do it. So we use an orthographic camera with a viewport of 48 meters in width and 32 meters in height. we also position the camera so that it looks at (0, 16) (that's where the middle of the screen will be located). ''' self.camera = OrthographicCamera(48, 32) self.camera.position.set(0, 15, 0) # create the debug renderer self.renderer = Box2DDebugRenderer() # create the world self.world = World(Vector2(0, -10), True) # we also need an invisible zero size ground body # to which we can connect the mouse joint bodyDef = BodyDef() self.groundBody = self.world.createBody(bodyDef) # call abstract method to populate the world self.createWorld(self.world) self.batch = SpriteBatch() self.font = BitmapFont(Gdx.files.internal('../data/arial-15.fnt'), False) Gdx.input.setInputProcessor(self)
class DropGame(Game): def __init__(self): self.batch = None self.font = None def create(self): self.batch = SpriteBatch() self.font = BitmapFont() self.setScreen(MainMenuScreen(self)) # def render(self): # super(Game, self).render() # needs more python3! def dispose(self): self.batch.dispose() self.font.dispose()
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 create(self): self.batch = SpriteBatch() self.font = BitmapFont() self.setScreen(MainMenuScreen(self))
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()