My code first creates a Point
object. Then it creates a 25 x 25 array of ASCII in a string where _
marks a miss and the point's symbol (in this case "X") marks a hit. Then, the user can move the point with the arrow keys and it rerenders.
This code is probably really badly written and terrible, but it works. I need some constructive feedback, and some tips on optimizing it as it kinda flickers whenever I move the x with the arrow keys.
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class _Newmain extends Canvas {
private static int renderSizeX = 25;
private static int renderSizeY = 25;
private point player = new point(10,10,'X');
public _Newmain() {
setSize(new Dimension(500, 500));
addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent evt) {
moveIt(evt);
}
});
}
public void paint(Graphics g) {
String text = m2.outputScreen(new point[]{player});
int x = 20; int y = 20;
for (String line : text.split("\n"))
g.drawString(line, x, y += g.getFontMetrics().getHeight());
}
public void moveIt(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DOWN:
player.moveY(1);
break;
case KeyEvent.VK_UP:
player.moveY(-1);
break;
case KeyEvent.VK_LEFT:
player.moveX(-1);
break;
case KeyEvent.VK_RIGHT:
player.moveX(1);
break;
}
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame("TEXT BASED GAME WRITTEN BY 10REPLIES");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_Newmain ex = new _Newmain();
frame.getContentPane().add(ex);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
ex.requestFocus();
}
public static int windowY(){
return renderSizeY;
}
public static int windowX(){
return renderSizeX;
}
}
ASCII generating class
public class m2
{
// instance variables - replace the example below with your own
public static void render(point[] input){
System.out.print("\f");//Clears screen
for(int y = 0; y < _Newmain.windowY(); y++){
for(int x = 0; x < _Newmain.windowX(); x++){
char found = ' ';
for(int i = 0; i < input.length;i++){
if( input[i].getY() == y && input[i].getX() == x ){//checks to see if list of points contains a point at current x and y position
found = input[i].getChar();
}
}
System.out.print(found);//prints char if found else prints space
}
System.out.print("\n");
}
}
public static String outputScreen(point[] input){
String output ="";
for(int y = 0; y < _Newmain.windowY(); y++){
for(int x = 0; x < _Newmain.windowX(); x++){
char found = '_';
for(int i = 0; i < input.length;i++){
if( input[i].getY() == y && input[i].getX() == x ){//checks to see if list of points contains a point at current x and y position
found = input[i].getChar();
}
}
output=output+found;
}
output=output+"\n";
}
return output;
}
}
Point
class
public class point
{
int x, y;
char letter;
public point(int inX, int inY, char letterRepresent)
{
x = inX;
y = inY;
letter=letterRepresent;
}
public char getChar(){return letter;}
public int getX(){return x;}
public int getY(){return y;}
public void moveX(int ammount){x+=ammount;}
public void setX(int location){x=location;}
public void moveY(int ammount){y+=ammount;}
public void setY(int location){y=location;}
}