Feb 23, 2024, Friday Classwork

Extends Feb 16, 2024, Friday Classwork

After finished up the 2nd video, and nearly finishing the 3rd in the series, I was able to finally!! Make some players, and get them to move. The main code to get them moving is in our new file, called KeyInput.java

In KeyInput.java

package com.tutorial.main;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter{
	
	private Handler handler;
	
	public KeyInput(Handler handler) {
		this.handler = handler;
		
		
	}
	
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		
		for(int i = 0; i < handler.object.size(); i++) {
			GameObject tempObject = handler.object.get(i);
			
			if(tempObject.getId() == ID.Player) {
				//key events for player 1
				
				if(key == KeyEvent.VK_W) tempObject.setVelY(-1);
				if(key == KeyEvent.VK_S) tempObject.setVelY(1);
				if(key == KeyEvent.VK_D) tempObject.setVelX(1);
				if(key == KeyEvent.VK_A) tempObject.setVelX(-1);
			}
			if(tempObject.getId() == ID.Player2) {
				//key events for player 2
				
				if(key == KeyEvent.VK_UP) tempObject.setVelY(-1);
				if(key == KeyEvent.VK_DOWN) tempObject.setVelY(1);
				if(key == KeyEvent.VK_RIGHT) tempObject.setVelX(1);
				if(key == KeyEvent.VK_LEFT) tempObject.setVelX(-1);
			}
			
		}
			
			

	}
	
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		
		for(int i = 0; i < handler.object.size(); i++) {
			GameObject tempObject = handler.object.get(i);
			
			if(tempObject.getId() == ID.Player) {
				//key events for player 1
				
				if(key == KeyEvent.VK_W) tempObject.setVelY(0);
				if(key == KeyEvent.VK_S) tempObject.setVelY(0);
				if(key == KeyEvent.VK_D) tempObject.setVelX(0);
				if(key == KeyEvent.VK_A) tempObject.setVelX(0);
			}
			if(tempObject.getId() == ID.Player2) {
				//key events for player 2
				
				if(key == KeyEvent.VK_UP) tempObject.setVelY(0);
				if(key == KeyEvent.VK_DOWN) tempObject.setVelY(0);
				if(key == KeyEvent.VK_RIGHT) tempObject.setVelX(0);
				if(key == KeyEvent.VK_LEFT) tempObject.setVelX(0);
			}
		}
	}
}

Obviously the space on here is a lot less than on my actual coding program, making the code wrap, but it should transfer properly if copy and pasted.

In KeyInput.java, there is a lot of familiar workings going on here. I am unsure what ‘VK’ stands for, but it is clear to me that they are linked to key inputs. A ‘KeyEvent’ is simply the press or release of a button. Releasing sets the moving velocity, in whichever direction, to zero so the player stops moving. Pressing sets the player moving at the speed allowed by the number we put, and the refresh/frame rate.

While mine in particular is laggy, likely due to computer issues as I am fairly certain I have been exact with the tutorial, it does move and stop as prompted.

But now, we also have our players. The main code for the player is in Player.java, and the 2nd player was added to our ID.java

In ID.Java

package com.tutorial.main;

public enum ID {
	Player(),
	Player2(),
	Enemy();
}

In Player.Java

package com.tutorial.main;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Player extends GameObject{
	Random r = new Random();
	public Player(int x, int y, ID id) {
		super(x, y, id);
	}
	public void tick() {
		x += velX;
		y += velY;
	}
	public void render(Graphics g) {
		if(id == ID.Player) g.setColor(Color.white);
		else if(id == ID.Player2) g.setColor(Color.blue);
		g.fillRect(x, y, 32, 32);
	}
}

Now, we have two characters. They are simple squares, mostly only differing in color and the controls used for them. Based on the player ID, it links them to each player object. Player 1 is assigned to spawn in the middle of the screen, and player 2 is nearby.

These are the basics, but I definitely need to work on fixing the massive amounts of lag preventing me from testing it properly.

One response to “Feb 23, 2024, Friday Classwork”

  1. Mr. Wilmoth Avatar
    Mr. Wilmoth

    Good work!

Leave a Reply

Your email address will not be published. Required fields are marked *