Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am making a game in java that sort of moves like flappy bird. In this code so

ID: 3774930 • Letter: I

Question

I am making a game in java that sort of moves like flappy bird. In this code so far there is a moving background with a picture of trump in the middle. I would like for you to make another class that will make bird go up if you press w and move down if you press s. This project requires 2 classes that's why.

public class flappy {

   public static void main(String[] args) {

       // Setup EZ graphics system.

       EZ.initialize(1000,500); // PIXEL picture element

      

       EZImage sky1 = EZ.addImage("skyrepeat.jpg", 500,255);

       EZImage sky2 = EZ.addImage("skyrepeat.jpg", 1500, 255);

      

  

       EZImage[] trumpPictures = new EZImage[1];

       for (int i=0; i < 1; i++){

           trumpPictures[i]= EZ.addImage("bird.png", 500, i*100+250);//i starts at 0. use 50 to determine the first vertical position

       } //variable is no longer in scope

      

              

       while (true) {

           for (int i = 0; i < 1; i++){

          

           sky1.moveForward(-1);

           sky2.moveForward(-1);

          

  

          

           if (sky1.getXCenter() < -500) {

                   sky1.moveForward(2000);

           }

           if (sky2.getXCenter() < -500) {

                   sky2.moveForward(2000);

           }

  

      

           EZ.refreshScreen();

       }

   }

   }}

Explanation / Answer

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Flappy
{
public static final int WidthOfBoard = 25;
public static final int HeightOfBoard = 12;
public static final int Gravity = 3;
public static final int Velocity = 3;
public static final int MinHeightOfObstacle = 1;
public static final int MaxHeightOfObstacle = 5;
public static final int Min_Timer = 6;
public static final int Max_Timer = 12;
private GameState gameState = new GameState();
public void play() {
Scanner input = new Scanner(System.in);
while (true) {
printBoard();
if (gameState.isGameOver()) {
System.out.println("Game over!");
break;
}
int move = -1;
while (move < 0 || move > 4) {
System.out.printf("(score %d) 0-4? ", gameState.getScore());
move = input.nextInt();
}
gameState.update(move);
}
}
public void printBoard() {
char[][] board = new char[HeightOfBoard][WidthOfBoard];
Arrays.stream(board).forEach(a -> Arrays.fill(a, '.'));

for (Obstacle obstacle : gameState.getObstacles()) {
for (int i = obstacle.getY(); i < obstacle.getY() + obstacle.getHeight(); i++) {
board[i][obstacle.getX()] = '#';
}
}
Bird bird = gameState.getBird();
board[bird.getY()][bird.getX()] = gameState.isGameOver() ? 'X' : '@';

for (char[] row : board) {
for (char c : row) {
System.out.print(c);
}
System.out.println();
}
}
public static void main(String[] args) {
// Setup EZ graphics system.
EZ.initialize(1000,500); // PIXEL picture element
  
EZImage sky1 = EZ.addImage("skyrepeat.jpg", 500,255);
EZImage sky2 = EZ.addImage("skyrepeat.jpg", 1500, 255);
  
  
EZImage[] trumpPictures = new EZImage[1];
for (int i=0; i < 1; i++){
trumpPictures[i]= EZ.addImage("bird.png", 500, i*100+250);//i starts at 0. use 50 to determine the first vertical position
Flappy game = new Flappy();
game.play();
}
}
class GameState {
private static final int BIRD_X = 1;
private Random random = new Random();
private List<Obstacle> obstacles = new ArrayList<>();
private Bird bird = new Bird(BIRD_X, 5);
private int score = 0;
private boolean gameOver = false;
private int topSpawnTimer;
private int bottomSpawnTimer;
public GameState() {
topSpawnTimer = randomInt(Flappy.Min_Timer, Flappy.Max_Timer);
bottomSpawnTimer = randomInt(Flappy.Min_Timer, Flappy.Max_Timer);
}
private int randomInt(int min, int max) {
return random.nextInt(max - min + 1) + min;
}
private boolean checkCollisions() {
for (Obstacle obstacle : obstacles) {
if (bird.getX() == obstacle.getX() && bird.getY() >= obstacle.getY() && bird.getY() <= obstacle.getY() + obstacle.getHeight() - 1) {
return true;
}
}
return false;
}
public void update(int move) {
if (move == 0) {
bird.setY(bird.getY() + Flappy.Gravity);
} else {
bird.setY(bird.getY() - move);
if (bird.getY() < 0) {
bird.setY(0);
}
}
if ((topSpawnTimer -= Flappy.Velocity) <= 0) {
obstacles.add(new Obstacle(randomInt(Flappy.MinHeightOfObstacle, Flappy.MaxHeightOfObstacle),
Flappy.WidthOfBoard - 1, 0));
topSpawnTimer = randomInt(Flappy.Min_Timer, Flappy.Max_Timer);
}
if ((bottomSpawnTimer -= Flappy.Velocity) <= 0) {
int height = randomInt(Flappy.MinHeightOfObstacle, Flappy.MaxHeightOfObstacle);
obstacles.add(new Obstacle(height, Flappy.WidthOfBoard - 1, Flappy.HeightOfBoard - height));
bottomSpawnTimer = randomInt(Flappy.Min_Timer, Flappy.Max_Timer);
}
for (Iterator<Obstacle> i = obstacles.iterator(); i.hasNext(); ) {
Obstacle obstacle = i.next();
obstacle.setX(obstacle.getX() - Flappy.Velocity);
if (obstacle.getX() <= 0) {
i.remove();
score++;
}
}
if (bird.getY() > Flappy.HeightOfBoard - 1 || checkCollisions()) {
gameOver = true;
bird.setY(Math.min(Flappy.HeightOfBoard - 1, bird.getY()));
}
}
public List<Obstacle> getObstacles() {
return obstacles;
}
public boolean isGameOver() {
return gameOver;
}
public int getScore() {
return score;
}
public Bird getBird() {
return bird;
}
}
class GameObject {
private int x;
private int y;
public GameObject(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
class Bird extends GameObject {
public Bird(int x, int y) {
super(x, y);
}
}
class Obstacle extends GameObject {
private int height;

public Obstacle(int height, int x, int y) {
super(x, y);
this.height = height;
}
public int getHeight() {
return height;
}
}