Question: Hello, I ' m trying to code a simple tanks game in java that looks like this. It's basically the tanks game on mathsisfun.com, I

Hello, I'm trying to code a simple tanks game in java that looks like this. It's basically the tanks game on mathsisfun.com, I'm trying to draw and render the terrain by reading it from a txt file where "X" represents the height of the terrain, "T" represents trees, and alphabet letters represents the default positions of the player tanks as shown in the photo.
Here's my current code, how do i read the level1.txt file and then draw the terrain by reading that file? Each level is read from a text file of characters 28x20. The size of the window should be 864x640, meaning each character in the file corresponds to 32x32 pixels. The terrain should comprise of a smooth curve that is formed from computing the moving average of 32 values twice.
Current code:
App.java
package Tanks;
import org.checkerframework.checker.units.qual.A;
import processing.core.PApplet;
import processing.core.PImage;
import processing.data.JSONArray;
import processing.data.JSONObject;
import processing.event.KeyEvent;
import processing.event.MouseEvent;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
public class App extends PApplet {
public static final int CELLSIZE =32; //8;
public static final int CELLHEIGHT =32;
public static final int CELLAVG =32;
public static final int TOPBAR =0;
public static int WIDTH =864; //CELLSIZE*BOARD_WIDTH;
public static int HEIGHT =640; //BOARD_HEIGHT*CELLSIZE+TOPBAR;
public static final int BOARD_WIDTH = WIDTH/CELLSIZE;
public static final int BOARD_HEIGHT =20;
public static final int INITIAL_PARACHUTES =1;
public static final int FPS =30;
public String configPath;
public static Random random = new Random();
// Feel free to add any additional methods or attributes you want. Please put classes in different files.
public App(){
this.configPath = "config.json";
}
/**
* Initialise the setting of the window size.
*/
@Override
public void settings(){
size(WIDTH, HEIGHT);
}
/**
* Load all resources such as images. Initialise the elements such as the player and map elements.
*/
PImage desertImage;
PImage forestImage;
PImage snowImage;
PImage treeOneImage;
PImage treeTwoImage;
@Override
public void setup(){
frameRate(FPS);
desertImage = loadImage(getClass().getResource("/Tanks/desert.png").getPath());
forestImage = loadImage(getClass().getResource("/Tanks/forest.png").getPath());
snowImage = loadImage(getClass().getResource("/Tanks/snow.png").getPath());
treeOneImage = loadImage(getClass().getResource("/Tanks/tree1.png").getPath());
treeTwoImage = loadImage(getClass().getResource("/Tanks/tree2.png").getPath());
//See PApplet javadoc:
//loadJSONObject(configPath)
//loadImage(this.getClass().getResource(filename).getPath().toLowerCase(Locale.ROOT).replace("%20",""));
}
/**
* Receive key pressed signal from the keyboard.
*/
@Override
public void keyPressed(KeyEvent event){
}
/**
* Receive key released signal from the keyboard.
*/
@Override
public void keyReleased(){
}
@Override
public void mousePressed(MouseEvent e){
//TODO - powerups, like repair and extra fuel and teleport
}
@Override
public void mouseReleased(MouseEvent e){
}
float[] terrainHeights;
/**
* Draw all elements in the game by current frame.
*/
@Override
public void draw(){
image(snowImage,0,0, WIDTH, HEIGHT);
//----------------------------------
//display HUD:
//----------------------------------
//TODO
//----------------------------------
//display scoreboard:
//----------------------------------
//TODO
//----------------------------------
//----------------------------------
//TODO: Check user action
}
public static void main(String[] args){
PApplet.main("Tanks.App");
}
}
I attached a photo of the "level1.txt" file that needs to be read and used to draw and render the terrain. Please help.
 Hello, I'm trying to code a simple tanks game in java

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!