player(class)

player class in Package "Game"
mail@pastecode.io avatar
unknown
java
2 years ago
1.6 kB
7
Indexable
Never
package Game;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class player implements KeyListener{
	
	int x = 100;
	int y = 100;
	int sizeX = 75;
	int sizeY = 75;
	boolean KeyUp = false;
	boolean KeyDown = false;
	boolean KeyRight = false;
	boolean KeyLeft = false;
	
	public player() {
		
		KeyUp = false;
		KeyDown = false;
		KeyRight = false;
		KeyLeft = false;
		
	}
	
	public void draw(Graphics2D g2D) {
		g2D.setColor(Color.gray);
		g2D.fillRect(x, y, sizeX, sizeY);
		g2D.setColor(Color.DARK_GRAY);
		g2D.setStroke(new BasicStroke(3));
		g2D.drawRect(x, y, sizeX, sizeY);
		
	}
	@Override
	public void keyTyped(KeyEvent e) {
		
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyChar() == 'w') {
			KeyUp = true;
			System.out.println("sa");
		}
			
		if(e.getKeyChar() == 'a') 
			KeyLeft = true;
		if(e.getKeyChar() == 's') 
			KeyDown = true;
		if(e.getKeyChar() == 'd') 
			KeyRight = true;
		
	}
	@Override
	public void keyReleased(KeyEvent e) {
		//if(e.getKeyChar() == 'w') KeyUp = false;
		//if(e.getKeyChar() == 'a') KeyLeft = false;
		//if(e.getKeyChar() == 's') KeyDown = false;
		//if(e.getKeyChar() == 'd') KeyRight = false;
		
	}
	
	public void setPlayer() {
		if(KeyUp && y != 0) 
			y--;
		if(KeyDown && y != 500 - sizeY) 
			y++;
		if(KeyRight && x != 500 - sizeX) 
			x++;
		if(KeyLeft && x != 0) 
			x--;
		System.out.println(KeyDown + " / " + y);
	}
	
}