Hey guys, I am making a geometrical game for some fun right now and have been pondering for a while how to make sure my “users” would be able to see the direction they are moving in. Right now I have a line that shows the direction, but I don’t think this will work long term. My idea is to create a particle system that trails behind the playable character that gives an indication of where the player is moving, but I’ve been having a lot of trouble trying to make this work.
If anyone can help me do this, it would be great!
This is all my code right now:
(Also if anyone can help me with the post formatting please do!!)
//variables
final int KEY_LIMIT = 1024;
boolean[] keysPressed = new boolean[KEY_LIMIT];
final float DIAGONAL_FACTOR = (0.5 * sqrt(2));
final int PLAYER_SPEED = 4;
final int PLAYER_DIAM = 50;
final color PLAYER_COLOUR = 0;
final float LAZER_DIRECTION = 0;
final float LAZER_LENGTH = 300;
final float LAZER_WIDTH = 8;
final color LAZER_COLOUR = #FF6200;
final color BULLET_COLOUR = #0700F7;
final int PLAYER_STROKE_WEIGHT = 4;
final int WALL_START_X = 0;
final int WALL_START_Y = 0;
final float ARCADE_HEIGHT = 80;
final float ARCADE_WIDTH = 1400;
final float ARCADE_COLOUR = #434343;
final float ARCADE_X = 0;
final float ARCADE_Y = 0;
final float ARCADE_X_BOTTOM = 0;
final float ARCADE_Y_BOTTOM = 900 - ARCADE_HEIGHT;
final float GAME_NAME_X = 500;
final float GAME_NAME_Y = 60;
final float SCORE_TEXT_X = 50;
final float SCORE_TEXT_Y = 880;
final color SCORE_COLOUR = 255;
final float BULLET_SPEED_MULTIPLIER = 10;
final float BULLET_DIAM = 12;
float lookingDirectionY;
float lookingDirectionX;
int lastFired = millis();
PFont arcadeFont;
ArrayList bullets;
Player player;
Background bg;
ArcadeMachine machine;
void setup() {
size(1400, 900);
player = new Player(width/2, height/2, PLAYER_DIAM);
bg = new Background();
lazer = new Lazer(player.xPos, player.yPos, lookingDirectionX, lookingDirectionY);
machine = new ArcadeMachine();
bullets = new ArrayList();
arcadeFont = createFont(“arcade.TTF”, 60);
bg.setLines();
}
void draw() {
background(0);
bg.display();
displayBullet();
player.display();
player.move();
player.collision();
machine.display();
}
void keyPressed() {
if (keyCode >= KEY_LIMIT) return; //if keycode exceeds limit, exit function (‘return’).
keysPressed[keyCode] = true; // sets its boolean to true
}
void keyReleased() {
if (keyCode >= KEY_LIMIT) return;
keysPressed[keyCode] = false;
}
class Player {
float xPos, yPos, diameter, xVelocity, yVelocity;
Player(float xPos, float yPos, float diameter) {
this.xPos = xPos;
this.yPos = yPos;
this.diameter = diameter;
}
void display() {
fill(PLAYER_COLOUR);
stroke(#9EFF0F);
strokeWeight(PLAYER_STROKE_WEIGHT);
circle(xPos, yPos, diameter);
stroke(255);
}
void move() {
if (keysPressed[UP] && !keysPressed[RIGHT] && !keysPressed[LEFT] && !keysPressed[DOWN]) {
yVelocity = -PLAYER_SPEED;
lookingDirectionY = -1;
lookingDirectionX = 0;
line(xPos, yPos, xPos, yPos - diameter/2);
}
if (keysPressed[DOWN] && !keysPressed[RIGHT] && !keysPressed[LEFT] && !keysPressed[UP]) {
yVelocity = PLAYER_SPEED;
lookingDirectionY = 1;
lookingDirectionX = 0;
line(xPos, yPos, xPos, yPos + diameter/2);
}
if (keysPressed[LEFT] && !keysPressed[UP] && !keysPressed[DOWN] && !keysPressed[RIGHT]) {
xVelocity = -PLAYER_SPEED;
lookingDirectionY = 0;
lookingDirectionX = -1;
line(xPos, yPos, xPos - diameter/2, yPos);
}
if (keysPressed[RIGHT] && !keysPressed[UP] && !keysPressed[DOWN] && !keysPressed[LEFT]) {
xVelocity = PLAYER_SPEED;
lookingDirectionY = 0;
lookingDirectionX = 1;
line(xPos, yPos, xPos + diameter/2, yPos);
}
if (keysPressed[UP] && keysPressed[RIGHT] && !keysPressed[DOWN] && !keysPressed[LEFT]) {
xVelocity = PLAYER_SPEED * DIAGONAL_FACTOR;
yVelocity = -PLAYER_SPEED * DIAGONAL_FACTOR;
lookingDirectionY = -1 * DIAGONAL_FACTOR;
lookingDirectionX = 1 * DIAGONAL_FACTOR;
line(xPos, yPos, xPos + diameter/2 * DIAGONAL_FACTOR, yPos - diameter/2 * DIAGONAL_FACTOR);
}
if (keysPressed[UP] && keysPressed[LEFT] && !keysPressed[DOWN] && !keysPressed[RIGHT]) {
xVelocity = -PLAYER_SPEED * DIAGONAL_FACTOR;
yVelocity = -PLAYER_SPEED * DIAGONAL_FACTOR;
lookingDirectionY = -1 * DIAGONAL_FACTOR;
lookingDirectionX = -1 * DIAGONAL_FACTOR;
line(xPos, yPos, xPos - diameter/2 * DIAGONAL_FACTOR, yPos - diameter/2 * DIAGONAL_FACTOR);
}
if (keysPressed[DOWN] && keysPressed[RIGHT] && !keysPressed[LEFT] && !keysPressed[UP]) {
xVelocity = PLAYER_SPEED * DIAGONAL_FACTOR;
yVelocity = PLAYER_SPEED * DIAGONAL_FACTOR;
lookingDirectionY = 1 * DIAGONAL_FACTOR;
lookingDirectionX = 1 * DIAGONAL_FACTOR;
line(xPos, yPos, xPos + diameter/2 * DIAGONAL_FACTOR, yPos + diameter/2 * DIAGONAL_FACTOR);
}
if (keysPressed[DOWN] && keysPressed[LEFT] && !keysPressed[RIGHT] && !keysPressed[UP]) {
xVelocity = -PLAYER_SPEED * DIAGONAL_FACTOR;
yVelocity = PLAYER_SPEED * DIAGONAL_FACTOR;
lookingDirectionY = 1 * DIAGONAL_FACTOR;
lookingDirectionX = -1 * DIAGONAL_FACTOR;
line(xPos, yPos, xPos - diameter/2 * DIAGONAL_FACTOR, yPos + diameter/2 * DIAGONAL_FACTOR);
}
xPos += xVelocity;
yPos += yVelocity;
// Adds new bullet to ArrayList when "S" or "s" is pressed - ascii table nummbers
if (keysPressed[83] || keysPressed[115]) {
if (millis() > lastFired + 150)
{
bullets.add(new Bullet(xPos, yPos, lookingDirectionX * BULLET_SPEED_MULTIPLIER, lookingDirectionY * BULLET_SPEED_MULTIPLIER));
lastFired = millis();
}
}
//resets the x and the y Velocity
xVelocity = 0;
yVelocity = 0;
//pushMatrix();
//popMatrix();
}
void collision() {
xPos = constrain(xPos, WALL_START_X + diameter/2, width-diameter/2);
yPos = constrain(yPos, WALL_START_Y + ARCADE_HEIGHT + diameter/2, height-diameter/2 - ARCADE_HEIGHT);
}
}
class ArcadeMachine {
ArcadeMachine() {
}
void display() {
fill(ARCADE_COLOUR);
rect(ARCADE_X, ARCADE_Y, ARCADE_WIDTH, ARCADE_HEIGHT);
rect(ARCADE_X_BOTTOM, ARCADE_Y_BOTTOM, ARCADE_WIDTH, ARCADE_HEIGHT);
fill(#FEFF00);
textFont(arcadeFont);
text(“SHAPE SKIRMISH”, GAME_NAME_X, GAME_NAME_Y);
fill(SCORE_COLOUR);
text(“SCORE 0000”, SCORE_TEXT_X, SCORE_TEXT_Y);
text(“HEALTH” , 1000, SCORE_TEXT_Y);
}
}
class Bullet {
float xPos, yPos, speed, diameter, xVelocity, yVelocity, colour;
Bullet(float xPos, float yPos, float xVelocity, float yVelocity) {
this.xPos = xPos;
this.yPos = yPos;
this.xVelocity = xVelocity;
this.yVelocity = yVelocity;
diameter = BULLET_DIAM;
colour = 255;
}
void display() {
fill(colour);
noStroke();
circle(xPos, yPos, diameter);
xPos += xVelocity;
yPos += yVelocity;
}
}
void displayBullet() {
for (int i = 0; i < bullets.size(); i ++) {
Bullet aBullet = bullets.get(i);
aBullet.display();
// When bullets are beyond screen they are removed
if ( ( aBullet.xPos >= width || aBullet.xPos < 0 ) || ( aBullet.yPos >= height || aBullet.yPos < 0 ) ) {
bullets.remove(i);
}
}
}
class Background {
int[] lines;
int[] lines2;
int multiplyRate = 75;
final int AMOUNT_OF_LINES = 30;
Background() {
}
// Sets the value of every printed line
void setLines() {
lines = new int[AMOUNT_OF_LINES];
lines2 = new int[lines.length];
for ( int i=0; i < lines.length; i++) {
lines[i] = height - (i * multiplyRate);
}
for (int i = 0; i < lines2.length; i++) {
lines2[i] = width - (i * multiplyRate);
}
}
// Displays the lines on the background
void display() {
for ( int i = 0; i < lines.length; i++ ) {
// Give each line a random color each time it’s drawn.
stroke(#FE00FF);
strokeWeight(1);
line( i - 10, lines[i], (i) + width, lines[i] );
}
for ( int i = 0; i < lines2.length; i++ ) {
// Give each line a random color each time it’s drawn.
stroke(#1CE9FA);
line(lines2[i], i - 10, lines2[i], (i) + height );
}
}
}