Skip to content

Instantly share code, notes, and snippets.

@EdwardDowling
Last active March 12, 2016 22:01
Show Gist options
  • Save EdwardDowling/80a27a301e1c39af2614 to your computer and use it in GitHub Desktop.
Save EdwardDowling/80a27a301e1c39af2614 to your computer and use it in GitHub Desktop.
Space invader/Breakout
package com.mygdx.game;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class alien {
public void setCollum(int collum) {
Collum = collum;
}
int Collum;
public Sprite getSprite() {
return sprite;
}
public void setSprite(Sprite sprite) {
this.sprite = sprite;
}
Sprite sprite;
public alien(int collum,Texture texture){
sprite = new Sprite(texture);
sprite.setSize(32,32);
Collum = collum;
}
int getCollum(){
return Collum;
}
}
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.mygdx.game.alien;
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
Sprite Paddle;
Sprite ball;
alien[] Aliens;
int row;
int collum;
int x;
int y;
int targetY;
float direction;
Vector2 ballDirection;
int paddleVel;
boolean locked;
@Override
public void create() {
locked = true;
paddleVel = 0;
ballDirection = new Vector2(0, -3);
direction = 1;
x = 0;
y = 1;
row = 0;
collum = 0;
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
Paddle = new Sprite(img);
Paddle.setSize(128, 8);
Paddle.setPosition(Gdx.graphics.getWidth() / 2, 8);
ball = new Sprite(img);
ball.setSize(16, 16);
ball.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
Aliens = new alien[50];
for (int i = 0; i < 50; i++) {
Aliens[i] = new alien(collum, img);
Aliens[i].getSprite().setPosition(row * Aliens[i].getSprite().getWidth() + 2 * row + 128, Gdx.graphics.getHeight() - y * 32 - collum * 34);
row++;
if (row > 10) {
row = 0;
collum++;
}
}
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
for (alien Alien : Aliens) {
if (Alien.getSprite().getTexture() != null) Alien.getSprite().draw(batch);
}
Paddle.draw(batch);
ball.draw(batch);
batch.end();
updateAliens();
updatePaddle();
updateBall();
}
void paddleCollision() {
if (ball.getBoundingRectangle().overlaps(Paddle.getBoundingRectangle())) {
ballDirection.y *= -1;
ballDirection.x += paddleVel / 4;
}
}
void updateBall() {
if (!locked) {
ball.setPosition(ball.getX() + ballDirection.x, ball.getY() + ballDirection.y);
if (ball.getX() < 0 || ball.getX() > Gdx.graphics.getWidth()) {
ballDirection.x *= -1;
}
if (ball.getY() < 0 || ball.getY() > Gdx.graphics.getHeight()) {
ball.setPosition(Paddle.getX() + Paddle.getWidth() / 2, Paddle.getY() + Paddle.getHeight() + 2);
locked = true;
}
for (alien Alien : Aliens) {
if (Alien.getSprite() != null) {
if (ball.getBoundingRectangle().overlaps(Alien.getSprite().getBoundingRectangle())) {
if (ball.getY() > Alien.getSprite().getY() && ball.getY() < Alien.getSprite().getY() + Alien.getSprite().getHeight()) {
ballDirection.x *= -1;
} else {
ballDirection.y *= -1;
ball.setPosition(ball.getX(), ball.getY() + ballDirection.y);
}
Sprite sprite = new Sprite();
sprite.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight());
Alien.getSprite().set(sprite);
Alien.setCollum(-1000);
}
}
}
} else {
ball.setPosition(Paddle.getX() + Paddle.getWidth() / 2, Paddle.getY() + Paddle.getHeight() + 2);
}
}
void updatePaddle() {
paddleCollision();
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
paddleVel += 1;
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
paddleVel -= 1;
}
if (!Gdx.input.isKeyPressed(Input.Keys.LEFT) && !Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
paddleVel = 0;
}
if (Paddle.getX() + paddleVel > 0 && Paddle.getX() + paddleVel + Paddle.getWidth() < Gdx.graphics.getWidth())
Paddle.setPosition(Paddle.getX() + paddleVel, Paddle.getY());
if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
ballDirection.x += paddleVel;
locked = false;
}
}
void updateAliens() {
for (alien Alien : Aliens) {
if (Alien.getSprite() != null) {
if (Alien.getCollum() % 2 != 0) {
targetY = Gdx.graphics.getHeight() - y * 32 - Alien.getCollum() * 34;
Alien.getSprite().setPosition(Alien.getSprite().getX() + direction / 4, Alien.getSprite().getY() + (targetY - Alien.getSprite().getY()) * 0.1f);
} else {
targetY = Gdx.graphics.getHeight() - y * 32 - Alien.getCollum() * 34;
Alien.getSprite().setPosition(Alien.getSprite().getX() - direction / 4, Alien.getSprite().getY() + (targetY - Alien.getSprite().getY()) * 0.1f);
}
if (Alien.getSprite().getX() > Gdx.graphics.getWidth() || Alien.getSprite().getX() < 0) {
direction *= -1;
y++;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment