Just a 2D Beginners code for sharing

Hello! I know I’m new and have only a few posts, but I wanted to be more active and post more, so I think i’ll post some of my beginner/nub code! I’ve been coding in Java(General) for ~2 Months, and only beginning to get into 2D stuff more recently (Past 2 weeks or so). I’ve been following some 2D animating/coding tutorials, but they just don’t make sense until I find some way to simplify and understand them. I like my code for reflecting simplicity. This will probably change though. Without further ado, here it is!

Finding you Character (The most simple, focused AI I know of)


public void findCharacter() {
		int xChar = 0; // The x Position for the Character (Used when comparing changes/data for the zombie to follow)
		int yChar = 0; // The y Position for the character (Used when comparing changes/data for the zombie to follow)

		int xZombie = 0; // The x Position for the Zombie/Enemy (Used when painting the image/rectangle)
		int yZombie = 0; // The y Position for the Zombie/Enemy (Used when painting the image/rectangle)

		int moveSpeed = 1; // How fast to move towards character (Pixels)

		if (xZombie < xChar) {
			xZombie += moveSpeed;
		}
		if (xZombie > xChar) {
			xZombie -= moveSpeed;
		}
		if (yZombie < yChar) {
			yZombie += moveSpeed;
		}
		if (yZombie < yChar) {
			yZombie += moveSpeed;
		}
	}

There’s also my Tile-Setter system :stuck_out_tongue:


public void TileSystem() {

		int Width = 640;
		int Height = 480;
		
		int x = 0;
		int y = 0;

		int tileSize = 32; // How large the tiles are
		int rowTiles = 25; // How many tiles to put into a row (Width / tileSize)
		int columnTiles = 20; // How many tiles/rows to put into a column (Height / tileSize)

		for (x = 0; x < Width; x += tileSize) { // 
			for (y = 0; y < Height; y += tileSize) { //
				x = 0;
			}
		}
		
		System.out.println("X: " + x + "Y: " + y);

	}

//Note: I had to re-write this off the top of my head, since I didn’t feel like going across the room to grab my flash-drive which I keep all of my good Java Tid-Bits on. I think I screwed up somewhere :stuck_out_tongue:

After-Post #1 - Screwed up the tile-rendering thing. It’s an infinity loop. I’ll try to fix it while still working and then pastah back.

Just some beginner’s code! Hope I can develop a better organizational skill and also the ability to actually write this stuff better. 2D Animation is fun until you actually try to make it :stuck_out_tongue:

Ehhh that double for loop is an infinite loop :wink:

Oh, really? No wonder it was screwing with me during test-driving. I knew I had to toss it back to 0 for the next row. Time for experimentation >:D

You reset x back to 0…meaning that loop will go on forever since x will never be greater or equal to Width :slight_smile: