fahad005

keyboard input

Apr 19th, 2022
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. package com.mygdx.game;
  2.  
  3. import com.badlogic.gdx.Game;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8. import com.badlogic.gdx.utils.ScreenUtils;
  9.  
  10. public class MyGdxGame extends Game {
  11.     SpriteBatch batch;
  12.     Texture img;
  13.     float x = 0, y = 0;
  14.     public static final float SPEED = 150;
  15.    
  16.     @Override
  17.     public void create () {
  18.         batch = new SpriteBatch();
  19.         img = new Texture("badlogic.jpg");
  20.     }
  21.  
  22.     @Override
  23.     public void render () {
  24.         ScreenUtils.clear(0, 1, 0, 1);
  25.         if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
  26.             y += SPEED * Gdx.graphics.getDeltaTime();
  27.         }
  28.         if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
  29.             y -= SPEED * Gdx.graphics.getDeltaTime();
  30.         }
  31.         if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
  32.             x -= SPEED * Gdx.graphics.getDeltaTime();
  33.         }
  34.         if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
  35.             x += SPEED * Gdx.graphics.getDeltaTime();
  36.         }
  37.         batch.begin();
  38.         batch.draw(img, x, y);
  39.         batch.end();
  40.     }
  41.    
  42.     @Override
  43.     public void dispose () {
  44.         batch.dispose();
  45.         img.dispose();
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment