I have been working on this project mainly for the hell of It and I wanted to know what I could do better because I know just how bad I am at coding. Pay no attention to my imports.
/*
- Elude
- By Tylor
/
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.io.PrintStream;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Toolkit;
import java.applet.;
import java.awt.;
import java.awt.image.;
import java.awt.event.;
import java.io.;
import java.net.;
import java.text.;
import java.util.;
import java.util.zip.;
public final class JGame extends JFrame
{
Grid grid;
GameLoop gLoop;
public static boolean p_up = false;
public static boolean p_down = false;
public static boolean p_left = false;
public static boolean p_right = false;
static Player player;
static BufferedImage img;
public JGame()
{
super("Eludƫ 0.1");
this.grid = new Grid();
add(this.grid);
setDefaultCloseOperation(3);
this.gLoop = new GameLoop(this);
this.gLoop.start();
}
public void Menu()
{
}
public static void main(String[] args)
{
int a = 2;
int b = "DSU".hashCode() % 3000;
int c = "WXC".hashCode() % 3000;
for (int i = 0; i <= c; i++)
{
a = (a ^ i) % b;
}
System.out.println(a);
JGame fm = new JGame();
fm.setVisible(true);
fm.setResizable(false);
fm.setExtendedState(fm.MAXIMIZED_BOTH);
fm.pack();
fm.show();
}
}
class Player
implements KeyListener
{
int x;
int buildings;
Grid grid;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int y;
int clevel = 1;
String level = "Level: " + this.clevel;
String version = "Eludƫ 0.1";
String user = "Player.gif";
boolean p_up = false;
boolean p_down = false;
boolean p_left = false;
boolean p_right = false;
int loop = 400;
int startloop = 0;
int backgroundx = 50;
int backgroundy = 50;
int backgroundx2 = 50;
int backgroundy2 = 50;
int backgroundx3 = 50;
int backgroundy3 = 50;
int backgroundx4 = 50;
int backgroundy4 = 50;
BufferedImage image;
static int followx = 400;
static int followy = 400;
static boolean morex = false;
static boolean morey = false;
public Player(int i, int a)
{
this.x = i;
this.y = a;
try
{
image = ImageIO.read(Player.class.getResource(this.user));
}
catch (IOException e)
{
System.out.println("Image Not In Root");
}
}
public void draw(Graphics g)
{
int gamespeed = 5;
boolean boxagro = false;
g.fillRect(followx, followy, 20, 20);
g.drawString("Player", this.x - 2, this.y - 5);
g.drawString(this.level, 0, screenSize.height - 25);
g.drawString(this.version, screenSize.width - 90, screenSize.height - 25);
//while ( startloop < loop)
//{
Random generator = new Random();
backgroundx = generator.nextInt(screenSize.width);
// System.out.println("backgroundX: " + backgroundx);
backgroundy = generator.nextInt(screenSize.height);
backgroundx2 = generator.nextInt(screenSize.width);
//System.out.println("backgroundX2: " + backgroundx2);
backgroundy2 = generator.nextInt(screenSize.height);
backgroundx3 = generator.nextInt(screenSize.width);
//System.out.println("backgroundX3: " + backgroundx3);
backgroundy3 = generator.nextInt(screenSize.height);
backgroundx4 = generator.nextInt(screenSize.width);
backgroundy4 = generator.nextInt(screenSize.height);
//}
g.fillRect(backgroundx,backgroundy,5,5);
g.fillRect(backgroundx2,backgroundy2,5,5);
g.fillRect(backgroundx3,backgroundy3,5,5);
g.fillRect(backgroundx4,backgroundy4,5,5);
g.drawImage(image, this.x, this.y, null);
int ydiff = this.y - followy;
int xdiff = this.x - followx;
//System.out.println("Xdiff" + xdiff);
//System.out.println("Ydiff" + ydiff);
if (ydiff < 300 || (xdiff < 300))
{
boxagro = true;
}
if (ydiff > 300 || (xdiff > 300) || (xdiff < -300) || (ydiff < -300))
{
boxagro = false;
//System.out.println(boxagro);
}
if (boxagro == true)
{
if ( this.y < followy)
{
if (followy > 0 )
{
followy--;followy--;
}
}
if ( this.y > followy )
{
if (followy < screenSize.height - 30)
{
followy++; followy++;
}
}
if ( this.x > followx)
{
if (followx > 0 )
{
followx++;followx++;
}
}
if ( this.x < followx)
{
if (followy < screenSize.width )
{
followx--;followx--;
}
}
}
if ((this.p_up == true) && (this.y > 25))
{
this.y -= gamespeed;
}
if ((this.p_down == true) && (this.y < screenSize.height - 55))
{
this.y += gamespeed;
}
if ((this.p_left == true) && (this.x > 10))
{
this.x -= gamespeed;
}
if ((this.p_right == true) && (this.x < screenSize.width - 35))
{
this.x += gamespeed;
}
}
public void keyPressed(KeyEvent ke)
{
if ((ke.getKeyCode() == 38) &&
(this.y > 15))
{
//System.out.println("Y: " + this.y);
this.p_up = true;
//System.out.println("FollowY" + followy);
}
if ((ke.getKeyCode() == 40) &&
(this.y < screenSize.height - 45))
{
//System.out.println("Y: " + this.y);
this.p_down = true;
//System.out.println("FollowY" + followy);
}
if ((ke.getKeyCode() == 37) &&
(this.x > 5))
{
//System.out.println("X: " + this.x);
this.p_left = true;
//System.out.println("FollowX" + followx);
}
if ((ke.getKeyCode() == 39) &&
(this.x < screenSize.width - 35))
{
//System.out.println("X: " + this.x);
this.p_right = true;
//System.out.println("FollowX" + followx);
}
if (ke.getKeyCode() == 16)
{
this.clevel += 1;
this.level = ("Level: " + this.clevel);
}
}
public void keyReleased(KeyEvent ke)
{
if (ke.getKeyCode() == 38)
{
this.p_up = false;
}
if (ke.getKeyCode() == 40)
{
this.p_down = false;
}
if (ke.getKeyCode() == 37)
{
this.p_left = false;
}
if (ke.getKeyCode() == 39)
{
this.p_right = false;
}
}
public void keyTyped(KeyEvent ke)
{
}
}
class Grid extends JPanel
{
Player player;
public Grid()
{
setFocusable(true);
requestFocus();
setBackground(Color.pink);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,screenSize.width, screenSize.height);
setPreferredSize(new Dimension(screenSize.width, screenSize.height));
this.player = new Player(16, 16);
addKeyListener(this.player);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.player.draw(g);
}
}
class GameLoop extends Thread
{
//int gamespeed = 1;
JGame game;
public GameLoop(JGame g)
{
this.game = g;
}
@Override
public void run()
{
Player player;
Grid grid;
while (true)
{
this.game.repaint();
try
{
Thread.sleep(15L);
}
catch (Exception e)
{
System.out.println("Error @ Thread.Sleep");
}
}
}
}
I was unable to compile/run the code from the link at the command line. I got these error messages:
javac JGame.java
Note: JGame.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
java JGame
1955
Exception in thread āmainā java.lang.IllegalArgumentException: input == null!
ā¦
at Player.JGame.java:122
From within Eclipse:
in main, the line: fm.show() is marked as deprecated.
The line where things crash is on the resource load, which kind of makes sense since the gif didnāt arrive with the download, afaik.
Thereās lots of unused methods as well. Maybe you are withholding parts of the code, and this is a simplification?
++++++++++++++++++++++++++
The main thing that jumps out at me is that it is unusual to see so much work done within a paint() method. Most folks separate the game logic from the drawing. Thus new positions and sizes would all be worked out prior to the draw call.
There are also what seem to be a few redundant calculations here and there. For example, in the calculation of new positions, I assume screen.height & .width donāt vary during the course of the game, yet you are adding constants to them within a loop. Since you get the same result every time, why not just do it once?
There is a lot of discussion here about āthe bestā way to time game loops, and whether to use passive or active rendering. I will leave that to the more experienced!
Getting a working version up as an Applet will go a long way towards eliciting more feedback, as well.
Good luck!
Im not really sure on your error. Mine compiles fine with no problem, Im using blueJ right now.
Could you please put your code in code blocks like so: your code goes here
Also, we are not here/donāt have the time to look through all your code and point out every error and bad coding you might have done. If you have specific questions, we will answer them to the best of our knowledge.